1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <set> using namespace std; typedef long long ll;
int n;
ll solve(vector<int> vec) { int sz = vec.size(); set<int> st; ll ans = 0; for(int i = sz-1; i >= 0; i--) { set<int>::iterator it = st.lower_bound(vec[i]); if(it == st.begin()) { ans += vec[i]; } else { it--; ans += vec[i] - *it; } st.insert(vec[i]); } return ans; }
int main() { while(scanf("%d", &n) != EOF) { vector<int> vec1, vec2; int x, y; for(int i = 1; i <= n; i++) { scanf("%d%d", &x, &y); vec1.push_back(x); vec2.push_back(y); } cout << solve(vec1) + solve(vec2) << endl; }
return 0; }
|