aboutsummaryrefslogtreecommitdiff
path: root/11.5/mar/gold/restack.cpp
blob: 49b325d0a19788487ead3a62bc30b4fccc2a0ebd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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';
}