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>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")
#define fio ios_base::sync_with_stdio(0);cin.tie(0);
#define en cin.close();return 0;
#define ll long long
#define pb push_back
#define fi first
#define se second
using namespace std;
const int N = 3e5+5;
int a[N];
bool st[N];
int main()
{
fio
// ifstream cin("in.in");
int n;
ll cash;
cin >> n >> cash;
ll c1 = cash;
priority_queue<pair<pair<ll,ll>,int>,vector<pair<pair<ll,ll>,int> >,greater<pair<pair<ll,ll>,int> > > q;
for(int i = 1,p;i<=n;i++)
{
cin >> a[i] >> p;
if(p==0)
{
st[i]=1;
q.push({{0,0},i});
}
}
st[n+1]=1;
while(!q.empty()&&q.top().fi.fi<=cash)
{
cash+=q.top().fi.se;
int pos = q.top().se;
q.pop();
if(pos>n)
continue;
ll cur = 0, mi = 0;
for(int i = pos;i==pos||!st[i];i++)
{
cur+=a[i];
mi=min(mi,cur);
if(cur>0)
{
int nxt = (st[i+1]?n+1:i+1);
q.push({{-mi,cur},nxt});
break;
}
}
}
cout << cash-c1;
return 0;
}
# | 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... |