Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long LL;
- const int maxn = 100000 + 100;
- int n;
- LL ans;
- int num[maxn];
- int main() {
- #ifdef ExRoc
- freopen("test.txt", "r", stdin);
- #endif
- ios::sync_with_stdio(false);
- cin >> n;
- for (int i = 1; i <= n; ++i) {
- cin >> num[i];
- }
- for (int i = 1; i <= n / 2; ++i) {
- int d = num[n - i + 1] - num[i];
- ans += abs(d);
- num[i] += d;
- if (d > 0) {
- num[i + 1] += min(max(num[n - i] - num[i + 1], 0), d);
- }
- if (d < 0) {
- num[i + 1] += max(min(num[n - i] - num[i + 1], 0), d);
- }
- }
- cout << ans << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement