usaco

Clean implementations of solutions to USACO problems

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
#include <bits/stdc++.h>
using namespace std;

int main() {
    ifstream cin("restack.in");
    ofstream cout("restack.out");
    int n, c = 0, ans = 0; cin >> n;
    vector<int> v;
    for (int i = 0; i < n; i++) {
        int a, b; cin >> a >> b;
        v.push_back(c += a - b);
    }
    sort(v.begin(), v.end());
    for (int i = 0; i < n; i++) ans += abs(v[i] - v[n / 2]);
    cout << ans << '\n';
}