# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
973139 | hihihah | Potatoes and fertilizers (LMIO19_bulves) | C++14 | 145 ms | 20928 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(), x.end()
#define pii pair<int, int>
int n;
int a[500009];
int b[500009];
int64_t pre[500009];
void hihihah()
{
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> a[i] >> b[i];
for(int i = 1; i <= n; i ++)
{
pre[i] = pre[i - 1] + a[i] - b[i];
}
priority_queue<int64_t> p;
int64_t ans = 0;
p.push(0);
for(int i = 1; i <= n; i ++)
{
if(pre[i] < 0)
{
ans += -pre[i];
pre[i] = 0;
}
if(p.top() > pre[i]) ans += p.top() - pre[i];
p.push(pre[i]);
p.push(pre[i]);
p.pop();
}
while(p.top() > pre[n])
{
ans += p.top() - pre[n];
p.pop();
}
cout << ans;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
#define orz "mo"
if(fopen(orz".inp", "r"))
{
freopen(orz".inp", "r", stdin);
// freopen(orz".out", "w", stdout);
}
int t = 1;
// cin >> t;
while(t --)
{
hihihah();
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |