#include <stdio.h>
#include <map>
#include <set>
long long A[200005];
std::map<int, long long> V;
std::set<std::pair<long long, int> > PQ;
int cnt[200005];
// bool operator<(std::pair<long long, int> a, std::pair<long long, int> b)
// {
// return (a.first < b.first || (a.first == b.first && a.second < b.second));
// }
int main()
{
int N;
scanf("%d", &N);
int i;
for (i = 0; i < N; i++)
{
scanf("%lld", &A[i]);
V.insert({i, A[i]});
PQ.insert({-A[i], i});
cnt[i] = 1;
}
long long sum = 0, r, diff;
std::map<int, long long>::iterator p, q;
std::set<std::pair<long long, int> >::iterator it;
int s, inc, sumcnt = 0;
for (i = 1; i <= (N + 1)/2;)
{
// for (p = V.begin(); p != V.end(); p++)
// printf("V %d %lld %d\n", p->first, p->second, cnt[p->first]);
// for (it = PQ.begin(); it != PQ.end(); it++)
// printf("PQ %lld %d %d\n", (*it).first, (*it).second, cnt[it->second]);
r = -PQ.begin()->first;
s = PQ.begin()->second;
if (cnt[s] == 0)
r -= 1e18;
sum += r;
sumcnt += cnt[s];
inc = -cnt[s];
diff = 0;
PQ.erase(PQ.begin());
p = V.find(s);
if (p != V.begin())
{
p--;
diff += p->second;
inc += cnt[p->first];
s = p->first;
PQ.erase({-p->second, p->first});
p = V.erase(p);
}
diff -= p->second;
p = V.erase(p);
if (p != V.end())
{
diff += p->second;
inc += cnt[p->first];
PQ.erase({-p->second, p->first});
p = V.erase(p);
}
cnt[s] = inc;
if (inc == 0)
{
V.insert({s, diff});
PQ.insert({1e18 - diff, s});
}
else
{
V.insert({s, diff});
PQ.insert({-diff, s});
}
// printf("sumcnt %d\n", sumcnt);
if (sumcnt == i)
{
printf("%lld\n", sum);
i++;
}
}
}
Compilation message
candies.cpp: In function 'int main()':
candies.cpp:15:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
candies.cpp:19:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &A[i]);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
612 KB |
Output is correct |
2 |
Correct |
4 ms |
776 KB |
Output is correct |
3 |
Correct |
4 ms |
908 KB |
Output is correct |
4 |
Correct |
5 ms |
908 KB |
Output is correct |
5 |
Correct |
4 ms |
908 KB |
Output is correct |
6 |
Correct |
4 ms |
1056 KB |
Output is correct |
7 |
Correct |
4 ms |
1120 KB |
Output is correct |
8 |
Correct |
4 ms |
1120 KB |
Output is correct |
9 |
Correct |
5 ms |
1120 KB |
Output is correct |
10 |
Correct |
4 ms |
1120 KB |
Output is correct |
11 |
Correct |
4 ms |
1204 KB |
Output is correct |
12 |
Correct |
4 ms |
1204 KB |
Output is correct |
13 |
Correct |
4 ms |
1204 KB |
Output is correct |
14 |
Correct |
4 ms |
1204 KB |
Output is correct |
15 |
Correct |
4 ms |
1232 KB |
Output is correct |
16 |
Correct |
4 ms |
1252 KB |
Output is correct |
17 |
Correct |
4 ms |
1272 KB |
Output is correct |
18 |
Correct |
7 ms |
1288 KB |
Output is correct |
19 |
Correct |
4 ms |
1312 KB |
Output is correct |
20 |
Correct |
5 ms |
1380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
612 KB |
Output is correct |
2 |
Correct |
4 ms |
776 KB |
Output is correct |
3 |
Correct |
4 ms |
908 KB |
Output is correct |
4 |
Correct |
5 ms |
908 KB |
Output is correct |
5 |
Correct |
4 ms |
908 KB |
Output is correct |
6 |
Correct |
4 ms |
1056 KB |
Output is correct |
7 |
Correct |
4 ms |
1120 KB |
Output is correct |
8 |
Correct |
4 ms |
1120 KB |
Output is correct |
9 |
Correct |
5 ms |
1120 KB |
Output is correct |
10 |
Correct |
4 ms |
1120 KB |
Output is correct |
11 |
Correct |
4 ms |
1204 KB |
Output is correct |
12 |
Correct |
4 ms |
1204 KB |
Output is correct |
13 |
Correct |
4 ms |
1204 KB |
Output is correct |
14 |
Correct |
4 ms |
1204 KB |
Output is correct |
15 |
Correct |
4 ms |
1232 KB |
Output is correct |
16 |
Correct |
4 ms |
1252 KB |
Output is correct |
17 |
Correct |
4 ms |
1272 KB |
Output is correct |
18 |
Correct |
7 ms |
1288 KB |
Output is correct |
19 |
Correct |
4 ms |
1312 KB |
Output is correct |
20 |
Correct |
5 ms |
1380 KB |
Output is correct |
21 |
Correct |
734 ms |
31928 KB |
Output is correct |
22 |
Correct |
733 ms |
33792 KB |
Output is correct |
23 |
Correct |
726 ms |
35976 KB |
Output is correct |
24 |
Correct |
314 ms |
37528 KB |
Output is correct |
25 |
Correct |
334 ms |
39428 KB |
Output is correct |
26 |
Correct |
321 ms |
41052 KB |
Output is correct |
27 |
Correct |
326 ms |
43044 KB |
Output is correct |
28 |
Correct |
376 ms |
45036 KB |
Output is correct |
29 |
Correct |
339 ms |
46956 KB |
Output is correct |
30 |
Correct |
351 ms |
49136 KB |
Output is correct |
31 |
Correct |
353 ms |
50900 KB |
Output is correct |
32 |
Correct |
355 ms |
52960 KB |
Output is correct |
33 |
Correct |
502 ms |
54556 KB |
Output is correct |
34 |
Correct |
497 ms |
56296 KB |
Output is correct |
35 |
Correct |
487 ms |
58256 KB |
Output is correct |
36 |
Correct |
728 ms |
60128 KB |
Output is correct |
37 |
Correct |
711 ms |
62176 KB |
Output is correct |
38 |
Correct |
708 ms |
64108 KB |
Output is correct |
39 |
Correct |
344 ms |
65680 KB |
Output is correct |
40 |
Correct |
326 ms |
67572 KB |
Output is correct |
41 |
Correct |
327 ms |
69300 KB |
Output is correct |
42 |
Correct |
339 ms |
71352 KB |
Output is correct |
43 |
Correct |
331 ms |
73192 KB |
Output is correct |
44 |
Correct |
342 ms |
75264 KB |
Output is correct |
45 |
Correct |
362 ms |
77104 KB |
Output is correct |
46 |
Correct |
363 ms |
79204 KB |
Output is correct |
47 |
Correct |
434 ms |
81032 KB |
Output is correct |
48 |
Correct |
497 ms |
82796 KB |
Output is correct |
49 |
Correct |
503 ms |
84580 KB |
Output is correct |
50 |
Correct |
479 ms |
86316 KB |
Output is correct |