Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* a066 */
- /* AC (17ms, 1.8MB) */
- #include <cstdio>
- #include <set>
- #include <algorithm>
- using namespace std;
- set<int> Set;
- set<int>::iterator up, low;
- int in;
- int main()
- {
- for(int n, sum; ~scanf("%d", &n) && n; sum, Set.clear())
- {
- scanf("%d", &in);
- Set.insert(in);
- sum = in;
- for(int i = 1; i < n && scanf("%d", &in); ++i, Set.insert(in))
- {
- low = Set.lower_bound(in);
- up = low;
- if(low != Set.begin()) --low;
- if(up == Set.end()) sum += in - *low;
- else sum += min(abs(*up - in), abs(in - *low));
- }
- printf("%d\n", sum);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement