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;
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#define getchar_unlocked getchar
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define psf push_front
#define ppb pop_back
#define ppf pop_front
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(),x.end()
#define lb(x,v) (lower_bound(all(x),v)-x.begin())
#define ub(x,v) (upper_bound(all(x),v)-x.begin())
#define disc(x) sort(all(x));x.resize(unique(all(x))-x.begin());
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef tuple<int,int,int> iii;
typedef tuple<int,int,int,int> iiii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
mt19937 rng(time(0));
#define int long long
#define mod 1000000007
inline int add(int a,int b){
int r=a+b;
while(r>=mod)r-=mod;
while(r<0)r+=mod;
return r;
}
inline int mult(int a,int b){
return (int)(((ll)(a*b))%mod);
}
inline int rd(){
int x=0;
char ch=getchar_unlocked();
while(!(ch&16))ch=getchar_unlocked();//keep reading while current character is whitespace
while(ch&16){//this will break when ‘\n’ or ‘ ‘ is encountered
x=(x<<3)+(x<<1)+(ch&15);
ch=getchar_unlocked();
}
return x;
}
#define maxn 500005
struct node{
int s,e,m,v,lz,mn;
node *l,*r;
node(int _s,int _e){
s=_s,e=_e,m=(s+e)>>1,mn=INF,v=0,lz=0;
if(s!=e)l=new node(s,m),r=new node(m+1,e);
}
void ppo(){
v+=lz;mn+=lz;
if(s!=e&&lz)l->lz+=lz,r->lz+=lz;
lz=0;
}
void up(int x,int y,int nv){
if(s==x&&e==y){lz+=nv;return;}
if(y<=m)l->up(x,y,nv);
else if(x>m)r->up(x,y,nv);
else l->up(x,m,nv),r->up(m+1,y,nv);
l->ppo(),r->ppo();
mn=min(l->mn,r->mn);
}
void set(int x){
if(s==x&&e==x){mn=v;return;}
if(x<=m)l->set(x);
else r->set(x);
l->ppo(),r->ppo();
mn=min(l->mn,r->mn);
}
int qmn(int x,int y){
ppo();
if(s==x&&e==y)return mn;
if(y<=m)return l->qmn(x,y);
if(x>m)return r->qmn(x,y);
return min(l->qmn(x,m),r->qmn(m+1,y));
}
int qry(int x){
ppo();
if(s==x&&e==x)return v;
if(x<=m)return l->qry(x);
else return r->qry(x);
}
}*rt;
int n,a[maxn],d[maxn],mx[maxn],bad[maxn],pv[maxn];
set<ii> s;
ll ans;
vi play,shop;
main(){
sf("%lld",&n);
rt=new node(0,n-1);
for(int i=0;i<n;++i){
sf("%lld",&a[i]);
if(a[i]==-1){
play.pb(i);
rt->up(i,i,INF);
}
else{
shop.pb(i);
ans+=a[i];
rt->up(i,i,i);
}
}
int ptr=-1;
memset(mx,-1,sizeof mx);
for(int i=0;i<sz(shop);++i){
while(ptr+1<sz(play)&&play[ptr+1]<shop[i])++ptr;
int x=shop[i];
pv[x]=ptr;
if(a[x]==0)continue;
d[x]=INF;
if(ptr!=-1)d[x]=min(d[x],shop[i]-play[ptr]);
if(ptr+1<sz(play))d[x]=min(d[x],play[ptr+1]-shop[i]);
if(mx[ptr+1]==-1||d[x]>d[mx[ptr+1]])mx[ptr+1]=x;
s.insert({d[x],x});
}
for(int i=-1;i<sz(play)-1;++i){
if(mx[i+1]==-1)continue;
int x=mx[i+1];
if(a[x]==0)continue;
rt->set(x);
--ans;--a[x];
bad[x]=1;
//pf("take %d\n",x);
if(a[x]==0)s.erase(s.find({d[x],x}));
}
while(!s.empty()){
auto[_,i]=*s.begin();
s.erase(s.begin());
//printf("now: %d\n",i);
int cur=rt->qry(i);
if(cur<0)continue;
//pf("pv %d, mx %d, %d\n",pv[i],mx[pv[i]+1],pv[i]==play.size()-1||i<mx[pv[i]+1]);
int num=min(a[i],cur/(2*d[i])+(pv[i]==play.size()-1||i<mx[pv[i]+1]));
int mn=rt->qmn(i,n-1);
if(mn<0)num=0;
else num=min(num,mn/(2*d[i]));
//printf("d=%d cur=%d, num=%d\n",d[i],cur,num);
ans-=num;
rt->up(i,n-1,-num*d[i]*2);
rt->set(i);
//for(int i=0;i<n;++i)printf("%d ",rt->qry(i));
//printf("\n");
}
pf("%lld\n",ans);
}
Compilation message (stderr)
tortoise.cpp:113:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
113 | main(){
| ^~~~
tortoise.cpp: In function 'int main()':
tortoise.cpp:136:45: error: no matching function for call to 'min(long long int&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type)'
136 | if(ptr!=-1)d[x]=min(d[x],shop[i]-play[ptr]);
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from tortoise.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:230:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
230 | min(const _Tp& __a, const _Tp& __b)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:230:5: note: template argument deduction/substitution failed:
tortoise.cpp:136:45: note: deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
136 | if(ptr!=-1)d[x]=min(d[x],shop[i]-play[ptr]);
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from tortoise.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:278:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
278 | min(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:278:5: note: template argument deduction/substitution failed:
tortoise.cpp:136:45: note: deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
136 | if(ptr!=-1)d[x]=min(d[x],shop[i]-play[ptr]);
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from tortoise.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3468:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(std::initializer_list<_Tp>)'
3468 | min(initializer_list<_Tp> __l)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3468:5: note: template argument deduction/substitution failed:
tortoise.cpp:136:45: note: mismatched types 'std::initializer_list<_Tp>' and 'long long int'
136 | if(ptr!=-1)d[x]=min(d[x],shop[i]-play[ptr]);
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from tortoise.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3474:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(std::initializer_list<_Tp>, _Compare)'
3474 | min(initializer_list<_Tp> __l, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3474:5: note: template argument deduction/substitution failed:
tortoise.cpp:136:45: note: mismatched types 'std::initializer_list<_Tp>' and 'long long int'
136 | if(ptr!=-1)d[x]=min(d[x],shop[i]-play[ptr]);
| ^
tortoise.cpp:137:54: error: no matching function for call to 'min(long long int&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type)'
137 | if(ptr+1<sz(play))d[x]=min(d[x],play[ptr+1]-shop[i]);
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from tortoise.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:230:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
230 | min(const _Tp& __a, const _Tp& __b)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:230:5: note: template argument deduction/substitution failed:
tortoise.cpp:137:54: note: deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
137 | if(ptr+1<sz(play))d[x]=min(d[x],play[ptr+1]-shop[i]);
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from tortoise.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:278:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
278 | min(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:278:5: note: template argument deduction/substitution failed:
tortoise.cpp:137:54: note: deduced conflicting types for parameter 'const _Tp' ('long long int' and '__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type' {aka 'int'})
137 | if(ptr+1<sz(play))d[x]=min(d[x],play[ptr+1]-shop[i]);
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from tortoise.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3468:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(std::initializer_list<_Tp>)'
3468 | min(initializer_list<_Tp> __l)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3468:5: note: template argument deduction/substitution failed:
tortoise.cpp:137:54: note: mismatched types 'std::initializer_list<_Tp>' and 'long long int'
137 | if(ptr+1<sz(play))d[x]=min(d[x],play[ptr+1]-shop[i]);
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from tortoise.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3474:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(std::initializer_list<_Tp>, _Compare)'
3474 | min(initializer_list<_Tp> __l, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3474:5: note: template argument deduction/substitution failed:
tortoise.cpp:137:54: note: mismatched types 'std::initializer_list<_Tp>' and 'long long int'
137 | if(ptr+1<sz(play))d[x]=min(d[x],play[ptr+1]-shop[i]);
| ^
tortoise.cpp:158:39: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
158 | int num=min(a[i],cur/(2*d[i])+(pv[i]==play.size()-1||i<mx[pv[i]+1]));
| ~~~~~^~~~~~~~~~~~~~~
tortoise.cpp:114:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
114 | sf("%lld",&n);
| ^
tortoise.cpp:117:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
117 | sf("%lld",&a[i]);
| ^