Advertisement
Emiliatan

a066

May 21st, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. /* a066             */
  2. /* AC (17ms, 1.8MB) */
  3. #include <cstdio>
  4. #include <set>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. set<int> Set;
  10. set<int>::iterator up, low;
  11. int in;
  12.  
  13. int main()
  14. {
  15.     for(int n, sum; ~scanf("%d", &n) && n; sum, Set.clear())
  16.     {        
  17.         scanf("%d", &in);
  18.         Set.insert(in);
  19.         sum = in;
  20.         for(int i = 1; i < n && scanf("%d", &in); ++i, Set.insert(in))
  21.         {
  22.             low = Set.lower_bound(in);
  23.             up = low;
  24.             if(low != Set.begin()) --low;
  25.             if(up == Set.end()) sum += in - *low;
  26.             else sum += min(abs(*up - in), abs(in - *low));
  27.         }
  28.         printf("%d\n", sum);
  29.     }
  30.     return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement