#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 1000006;
int n, k;
int a[N];
struct ban
{
int x;
ll s;
ban(){}
ban(int x, ll s)
{
this->x = x;
this->s = s;
}
};
bool operator<(const ban& a, const ban& b)
{
if (abs(a.s) < abs(b.s))
return true;
if (abs(a.s) > abs(b.s))
return false;
return a.x < b.x;
}
int dq;
set<ban> t;
int p[N];
int l[N], r[N];
ll s[N];
int fi(int x)
{
if (x == p[x])
return x;
return p[x] = fi(p[x]);
}
void kpc(int x, int y)
{
x = fi(x);
y = fi(y);
t.erase(ban(x, s[x]));
t.erase(ban(y, s[y]));
if (s[x] > 0)
--dq;
if (s[y] > 0)
--dq;
p[x] = y;
l[y] = min(l[x], l[y]);
r[y] = max(r[x], r[y]);
s[y] += s[x];
if (s[y] > 0)
++dq;
t.insert(ban(y, s[y]));
}
void solv()
{
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; ++i)
scanf("%d", &a[i]);
for (int i = 1; i <= n; ++i)
{
p[i] = i;
l[i] = r[i] = i;
s[i] = a[i];
t.insert(ban(i, s[i]));
if (s[i] > 0)
++dq;
}
for (int i = 1; i <= n; ++i)
{
int x = fi(i);
if (s[x] > 0 && r[x] < n && s[fi(r[x] + 1)] > 0)
{
kpc(x, r[x] + 1);
}
if (s[x] < 0 && r[x] < n && s[fi(r[x] + 1)] < 0)
{
kpc(x, r[x] + 1);
}
}
while (dq > k)
{
int x = t.begin()->x;
if (s[x] < 0)
{
if (l[x] == 1 || r[x] == n)
{
t.erase(ban(x, s[x]));
continue;
}
}
if (l[x] > 1)
{
kpc(x, l[x] - 1);
x = fi(x);
}
if (r[x] < n)
{
kpc(x, r[x] + 1);
x = fi(x);
}
}
ll ans = 0;
for (auto it = t.begin(); it != t.end(); ++it)
{
if (it->s > 0)
ans += it->s;
}
printf("%lld\n", ans);
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
solv();
return 0;
}
//while ((double)clock() / CLOCKS_PER_SEC <= 0.9){}
Compilation message
feast.cpp: In function 'void solv()':
feast.cpp:69:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~
feast.cpp:71:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i]);
~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
28228 KB |
Output is correct |
2 |
Correct |
444 ms |
28792 KB |
Output is correct |
3 |
Correct |
444 ms |
29080 KB |
Output is correct |
4 |
Correct |
493 ms |
28920 KB |
Output is correct |
5 |
Correct |
429 ms |
28792 KB |
Output is correct |
6 |
Correct |
421 ms |
28240 KB |
Output is correct |
7 |
Correct |
464 ms |
28280 KB |
Output is correct |
8 |
Correct |
503 ms |
28796 KB |
Output is correct |
9 |
Correct |
431 ms |
28408 KB |
Output is correct |
10 |
Correct |
426 ms |
28536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
382 ms |
26872 KB |
Output is correct |
2 |
Correct |
386 ms |
27300 KB |
Output is correct |
3 |
Correct |
366 ms |
26872 KB |
Output is correct |
4 |
Correct |
373 ms |
26872 KB |
Output is correct |
5 |
Correct |
433 ms |
31992 KB |
Output is correct |
6 |
Correct |
363 ms |
26744 KB |
Output is correct |
7 |
Correct |
382 ms |
27308 KB |
Output is correct |
8 |
Correct |
440 ms |
32248 KB |
Output is correct |
9 |
Correct |
425 ms |
29700 KB |
Output is correct |
10 |
Correct |
387 ms |
27128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
785 ms |
29004 KB |
Output is correct |
2 |
Correct |
760 ms |
28664 KB |
Output is correct |
3 |
Correct |
813 ms |
28792 KB |
Output is correct |
4 |
Correct |
767 ms |
28536 KB |
Output is correct |
5 |
Correct |
834 ms |
28792 KB |
Output is correct |
6 |
Correct |
751 ms |
29048 KB |
Output is correct |
7 |
Correct |
742 ms |
29176 KB |
Output is correct |
8 |
Correct |
713 ms |
28796 KB |
Output is correct |
9 |
Correct |
729 ms |
29176 KB |
Output is correct |
10 |
Correct |
720 ms |
29048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
512 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
2 ms |
512 KB |
Output is correct |
27 |
Correct |
2 ms |
512 KB |
Output is correct |
28 |
Correct |
2 ms |
512 KB |
Output is correct |
29 |
Correct |
2 ms |
512 KB |
Output is correct |
30 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
425 ms |
28228 KB |
Output is correct |
2 |
Correct |
444 ms |
28792 KB |
Output is correct |
3 |
Correct |
444 ms |
29080 KB |
Output is correct |
4 |
Correct |
493 ms |
28920 KB |
Output is correct |
5 |
Correct |
429 ms |
28792 KB |
Output is correct |
6 |
Correct |
421 ms |
28240 KB |
Output is correct |
7 |
Correct |
464 ms |
28280 KB |
Output is correct |
8 |
Correct |
503 ms |
28796 KB |
Output is correct |
9 |
Correct |
431 ms |
28408 KB |
Output is correct |
10 |
Correct |
426 ms |
28536 KB |
Output is correct |
11 |
Correct |
382 ms |
26872 KB |
Output is correct |
12 |
Correct |
386 ms |
27300 KB |
Output is correct |
13 |
Correct |
366 ms |
26872 KB |
Output is correct |
14 |
Correct |
373 ms |
26872 KB |
Output is correct |
15 |
Correct |
433 ms |
31992 KB |
Output is correct |
16 |
Correct |
363 ms |
26744 KB |
Output is correct |
17 |
Correct |
382 ms |
27308 KB |
Output is correct |
18 |
Correct |
440 ms |
32248 KB |
Output is correct |
19 |
Correct |
425 ms |
29700 KB |
Output is correct |
20 |
Correct |
387 ms |
27128 KB |
Output is correct |
21 |
Correct |
785 ms |
29004 KB |
Output is correct |
22 |
Correct |
760 ms |
28664 KB |
Output is correct |
23 |
Correct |
813 ms |
28792 KB |
Output is correct |
24 |
Correct |
767 ms |
28536 KB |
Output is correct |
25 |
Correct |
834 ms |
28792 KB |
Output is correct |
26 |
Correct |
751 ms |
29048 KB |
Output is correct |
27 |
Correct |
742 ms |
29176 KB |
Output is correct |
28 |
Correct |
713 ms |
28796 KB |
Output is correct |
29 |
Correct |
729 ms |
29176 KB |
Output is correct |
30 |
Correct |
720 ms |
29048 KB |
Output is correct |
31 |
Correct |
0 ms |
384 KB |
Output is correct |
32 |
Correct |
0 ms |
384 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
0 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
0 ms |
384 KB |
Output is correct |
37 |
Correct |
0 ms |
384 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
0 ms |
384 KB |
Output is correct |
40 |
Correct |
1 ms |
384 KB |
Output is correct |
41 |
Correct |
1 ms |
384 KB |
Output is correct |
42 |
Correct |
1 ms |
384 KB |
Output is correct |
43 |
Correct |
1 ms |
384 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
1 ms |
384 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
51 |
Correct |
2 ms |
512 KB |
Output is correct |
52 |
Correct |
2 ms |
512 KB |
Output is correct |
53 |
Correct |
2 ms |
512 KB |
Output is correct |
54 |
Correct |
2 ms |
512 KB |
Output is correct |
55 |
Correct |
2 ms |
512 KB |
Output is correct |
56 |
Correct |
2 ms |
512 KB |
Output is correct |
57 |
Correct |
2 ms |
512 KB |
Output is correct |
58 |
Correct |
2 ms |
512 KB |
Output is correct |
59 |
Correct |
2 ms |
512 KB |
Output is correct |
60 |
Correct |
2 ms |
512 KB |
Output is correct |
61 |
Correct |
612 ms |
28536 KB |
Output is correct |
62 |
Correct |
540 ms |
29176 KB |
Output is correct |
63 |
Correct |
711 ms |
28536 KB |
Output is correct |
64 |
Correct |
608 ms |
29176 KB |
Output is correct |
65 |
Correct |
648 ms |
29176 KB |
Output is correct |
66 |
Correct |
661 ms |
29048 KB |
Output is correct |
67 |
Correct |
646 ms |
28536 KB |
Output is correct |
68 |
Correct |
721 ms |
29176 KB |
Output is correct |
69 |
Correct |
713 ms |
28664 KB |
Output is correct |
70 |
Correct |
799 ms |
28392 KB |
Output is correct |