Submission #513012

#TimeUsernameProblemLanguageResultExecution timeMemory
513012CrouchingDragonPotatoes and fertilizers (LMIO19_bulves)C++17
100 / 100
160 ms15340 KiB
#include <bits/stdc++.h>
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int N;
  std::cin >> N;
  std::vector<int64_t> a(N);
  for (int i = 0; i < N; ++i) {
    int64_t x, y;
    std::cin >> x >> y;
    a[i] = x - y;
  }
  std::priority_queue<int64_t> pq;
  int64_t sum = 0, cost = 0;
  for (int i = 0; i < N - 1; ++i) {
    sum += a[i];
    int64_t x = sum < 0 ? (cost += -sum, 0) : sum;
    pq.push(x);
    if (!pq.empty() && x < pq.top()) {
      cost += pq.top() - x;
      pq.pop();
      pq.push(x);
    }
  }
  sum += a[N - 1];
  while (!pq.empty() && pq.top() > sum) {
    cost += pq.top() - sum;
    pq.pop();
  }
  std::cout << cost << '\n';
  exit(0);
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...