Submission #146911

# Submission time Handle Problem Language Result Execution time Memory
146911 2019-08-26T14:59:36 Z bortoz Potatoes and fertilizers (LMIO19_bulves) C++17
0 / 100
2 ms 504 KB
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define fi first
#define se second

int main() {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> V(N);
  set<int> L;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    V[i] = a - b;
    if (V[i] > 0) {
      L.insert(i);
    }
  }
  ll res = 0;
  for (int i = 0; i < N; i++) {
    while (V[i] < 0) {
      auto itR = L.lower_bound(i);
      if (itR == L.end() && itR != L.begin()) {
        itR--;
      }
      auto itL = itR;
      if (itL != L.begin()) {
        itL--;
      }
      assert(itR != L.end() && itL != L.begin());
      if (abs(i - *itL) < abs(i - *itR)) {
        int m = min(-V[i], V[*itL]);
        res += 1ll * m * abs(i - *itL);
        V[i] += m;
        V[*itL] -= m;
        if (V[*itL] == 0) {
          L.erase(itL);
        }
      } else {
        int m = min(-V[i], V[*itR]);
        res += 1ll * m * abs(i - *itR);
        V[i] += m;
        V[*itR] -= m;
        if (V[*itR] == 0) {
          L.erase(itR);
        }
      }
    }
  }
  cout << res << endl;

  return 0;
}
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 2 ms 504 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -