#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,int> > s;
set<int> idx;
for(int i=0;i<n;i++){
s.insert(mp(arr[i],i));
idx.insert(i);
}
ll sum=0;
for(int i=0;i<(n+1)/2;i++){
auto it=(--s.end());
sum+=(it->fi);printf("%lld\n",sum);
int x=(it->se);idx.erase(x);s.erase(it);
auto l=idx.lower_bound(x),r=idx.lower_bound(x+1);
arr[x]=-arr[x];
bool add=1;
if(l==idx.begin())add=0;
else{
--l;
arr[x]+=arr[*l];
s.erase(mp(arr[*l],*l));
idx.erase(l);
}
if(r==idx.end())add=0;
else{
arr[x]+=arr[*r];
s.erase(mp(arr[*r],*r));
idx.erase(r);
}
if(add){s.insert(mp(arr[x],x));idx.insert(x);}
}
}
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]);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
2 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
3 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
640 KB |
Output is correct |
3 |
Correct |
2 ms |
640 KB |
Output is correct |
4 |
Correct |
2 ms |
640 KB |
Output is correct |
5 |
Correct |
2 ms |
640 KB |
Output is correct |
6 |
Correct |
2 ms |
640 KB |
Output is correct |
7 |
Correct |
3 ms |
640 KB |
Output is correct |
8 |
Correct |
3 ms |
640 KB |
Output is correct |
9 |
Correct |
2 ms |
640 KB |
Output is correct |
10 |
Correct |
2 ms |
640 KB |
Output is correct |
11 |
Correct |
3 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
2 ms |
640 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
2 ms |
640 KB |
Output is correct |
16 |
Correct |
2 ms |
640 KB |
Output is correct |
17 |
Correct |
3 ms |
640 KB |
Output is correct |
18 |
Correct |
2 ms |
640 KB |
Output is correct |
19 |
Correct |
2 ms |
640 KB |
Output is correct |
20 |
Correct |
2 ms |
640 KB |
Output is correct |
21 |
Correct |
569 ms |
27272 KB |
Output is correct |
22 |
Correct |
581 ms |
27256 KB |
Output is correct |
23 |
Correct |
648 ms |
27256 KB |
Output is correct |
24 |
Correct |
251 ms |
27128 KB |
Output is correct |
25 |
Correct |
257 ms |
27260 KB |
Output is correct |
26 |
Correct |
250 ms |
27240 KB |
Output is correct |
27 |
Correct |
265 ms |
27256 KB |
Output is correct |
28 |
Correct |
263 ms |
27256 KB |
Output is correct |
29 |
Correct |
264 ms |
27256 KB |
Output is correct |
30 |
Correct |
275 ms |
27384 KB |
Output is correct |
31 |
Correct |
291 ms |
27268 KB |
Output is correct |
32 |
Correct |
274 ms |
27252 KB |
Output is correct |
33 |
Correct |
383 ms |
27000 KB |
Output is correct |
34 |
Correct |
385 ms |
27016 KB |
Output is correct |
35 |
Correct |
394 ms |
27128 KB |
Output is correct |
36 |
Correct |
561 ms |
27300 KB |
Output is correct |
37 |
Correct |
585 ms |
27312 KB |
Output is correct |
38 |
Correct |
569 ms |
27320 KB |
Output is correct |
39 |
Correct |
250 ms |
27128 KB |
Output is correct |
40 |
Correct |
255 ms |
27128 KB |
Output is correct |
41 |
Correct |
255 ms |
27128 KB |
Output is correct |
42 |
Correct |
293 ms |
27240 KB |
Output is correct |
43 |
Correct |
275 ms |
27260 KB |
Output is correct |
44 |
Correct |
260 ms |
27256 KB |
Output is correct |
45 |
Correct |
274 ms |
27256 KB |
Output is correct |
46 |
Correct |
276 ms |
27256 KB |
Output is correct |
47 |
Correct |
273 ms |
27384 KB |
Output is correct |
48 |
Correct |
380 ms |
27000 KB |
Output is correct |
49 |
Correct |
383 ms |
27000 KB |
Output is correct |
50 |
Correct |
388 ms |
27000 KB |
Output is correct |