Submission #399553

#TimeUsernameProblemLanguageResultExecution timeMemory
399553ronnithPotatoes and fertilizers (LMIO19_bulves)C++14
24 / 100
145 ms15228 KiB
#include <bits/stdc++.h> using namespace std; struct Line { long long m, c; Line() {} Line(long long mm, long long cc) : m(mm), c(cc) {} inline long long getY(long long x) { return m*x+c; } }; const int N = (int)5e5; int n, a[N], b[N]; long long d[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0;i < n;i ++) { cin >> a[i] >> b[i]; d[i] = a[i] - b[i]; if (i != 0) d[i] += d[i - 1]; } Line line; priority_queue<int> pq; if (d[0] > 0) { pq.push(d[0]); line = Line(-1, d[0]); } else { line = Line(0, -d[0]); } for (int i = 1;i < n - 1;i ++) { if (d[i] >= 0) { pq.push(d[i]); pq.push(d[i]); pq.pop(); line.m --; line.c += d[i]; } else { if (line.m + 1 > 0) { line.m = 0; line.c += -d[i]; while ((int)pq.size()) pq.pop(); } else { line.m ++; line.c += -d[i]; pq.pop(); } } } while ((int)pq.size() && pq.top() < d[n - 1]) { long long y = line.getY(pq.top()); line.m ++; line.c = y - line.m * pq.top(); pq.pop(); } cout << line.getY(d[n - 1]) << '\n'; return 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...