#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
typedef pair<ll,ll> pll;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ALL(x) x.begin(),x.end()
#define SZ(x) (int)x.size()
const int INF=2012345678;
const ll LLINF=4012345678012345678LL;
const ll MOD=1000000007; //998244353; //
const ld PI=3.1415926535898;
const ld EPS=1e-9;
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(b==0)return a;return gcd(b,a%b);}
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll expo(ll b,ll p,ll m){ll res=1; while(p){if(p&1)res=(res*b)%m; b=(b*b)%m; p>>=1;} return res;}
inline ll modinv(ll a,ll m){return expo(a,m-2,m);}
int main(){
int n;
scanf("%d",&n);
ll arr[n];
for(int i=0;i<n;i++)scanf("%lld",&arr[i]);
set<pair<ll,ii> > s;
set<pair<ii,ll> > st;
set<pair<ii,ll> > en;
for(int i=0;i<n;i++){
s.insert(mp(arr[i],mp(i,i)));
st.insert(mp(mp(i,i),arr[i]));
en.insert(mp(mp(i,i),arr[i]));
}
ll sum=0;
for(int i=0;i<(n+1)/2;i++){
auto it=s.end();--it;
sum+=(it->fi);
printf("%lld\n",sum);
int l=(it->se).fi,r=(it->se).se;
ll nsum=-(it->fi);int nl=-1,nr=-1;
s.erase(it);
auto it1=st.lower_bound(mp(mp(r+1,-1),-1));
if(it1==st.end()){
nr=n-1;
}else{
nr=(it1->fi).se,nsum+=(it1->se);
s.erase(mp(it1->se,it1->fi));
en.erase(mp(mp((it1->fi).se,(it1->fi).fi),it1->se));
st.erase(it1);
}
it1=en.lower_bound(mp(mp(l,-1),-1));
if(it1==en.begin())nl=0;
else{
--it1;
nl=(it1->fi).se,nsum+=(it1->se);
s.erase(mp(it1->se,mp((it1->fi).se,(it1->fi).fi)));
st.erase(mp(mp((it1->fi).se,(it1->fi).fi),it1->se));
en.erase(it1);
}
//printf("%lld %d %d\n",nsum,nl,nr);
s.insert(mp(nsum,mp(nl,nr)));
st.insert(mp(mp(nl,nr),nsum));
en.insert(mp(mp(nr,nl),nsum));
}
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:26:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
candies.cpp:28:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=0;i<n;i++)scanf("%lld",&arr[i]);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |