fork download
  1. #include <bits/stdc++.h>
  2. #define int long long
  3. using namespace std;
  4.  
  5. signed main() {
  6. ios::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8.  
  9. int n;
  10. cin >> n;
  11. vector<int> a (n);
  12. vector<int> b (n);
  13. for (auto& x: a) cin >> x;
  14. for (auto& x: b) cin >> x;
  15.  
  16. vector<int> delta (n);
  17. for (int i = 0; i < n; i++) delta[i] = b[i] - a[i];
  18. vector<int> flow (n);
  19. flow[0] = 0;
  20. for (int i = 1; i < n; i++) flow[i] = flow[i - 1] + delta[i - 1];
  21. sort(flow.begin(), flow.end());
  22. int rem = flow[n / 2];
  23. int ans = 0;
  24. for (auto& x: flow) ans += abs(x - rem);
  25. cout << ans << '\n';
  26. }
Runtime error #stdin #stdout #stderr 0.01s 5420KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc