# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1112591 | guagua0407 | Potatoes and fertilizers (LMIO19_bulves) | C++17 | 143 ms | 17344 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.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int ll
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
signed main() {_
int n;
cin>>n;
vector<ll> dif(n+1);
for(int i=1;i<=n;i++){
int a,b;
cin>>a>>b;
dif[i]=dif[i-1]+a-b;
}
priority_queue<ll> pq;
pq.push(0);
ll ans=0;
for(int i=1;i<=n;i++){
if(dif[i]<0){
ans-=dif[i];
dif[i]=0;
}
pq.push(dif[i]);
if(pq.top()>dif[i]){
pq.push(dif[i]);
ans+=pq.top()-dif[i];
pq.pop();
}
}
int cnt=1;
while(pq.top()>dif[n]){
int r=pq.top();
pq.pop();
ans+=1ll*cnt*(r-max(dif[n],pq.top()));
cnt++;
}
cout<<ans<<'\n';
return 0;
}
//maybe its multiset not set
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... |