#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define pb push_back
#define freo(task) if(fopen(task".inp", "r")){ freopen(task".inp", "r", stdin); freopen(task".out", "w", stdout); }
#define FOR(i, a, b) for(int i = (a), _b = (b); i <= _b; i ++)
#define FORD(i, a, b) for(int i = (a), _b = (b); i >= _b; i --)
#define bit(x, i) ((x >> i) & 1)
#define oo 1e18
#define all(v) v.begin(), v.end()
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
// mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
// ll rand(ll l, ll r) { assert(l <= r); return uniform_int_distribution<ll>(l, r)(rd); }
const int N = 3e5 + 5;
const int mod = 1e9 + 7;
const int base = 31;
int n, k, a[N], dp[N], s[N];
/*
sub5: dp O(n ^ 3)
dp1[i][j] là tổng lớn nhất khi chia i món cho j thằng
dp1[i][j] = max(dp1[i - 1][j], max(dp1[k - 1][j]) + (a[k] -> i)
*/
int dp1[305][305];
void sub5(){
FOR(i, 1, n) cin>>a[i], a[i] += a[i - 1];
FOR(i, 1, n)
FOR(j, 1, k){
dp1[i][j] = dp1[i - 1][j];
FOR(t, 1, i - 1)
dp1[i][j] = max(dp1[i][j], dp1[t - 1][j - 1] + a[i] - a[t - 1]);
}
// FOR(i, 1, n) cout<<dp1[i][k]<<" ";
cout<<dp1[n][k];
}
int dp2[2005][2005], g[2005][2005];
/*
sub6: O(n ^ 2)
dp[i][j] là tổng lớn nhất khi chia i món cho j thằng vua
g[i][j] là giá tri tối ưu khi chia i món với số lượng vua bất kì từ 1..j
vì có thằng vua có thể không có
*/
void sub6(){
FOR(i, 1, n) cin>>a[i];
FOR(i, 1, n)
FOR(j, 1, k){
dp2[i][j] = max(dp2[i - 1][j] + a[i], g[i - 1][j - 1] + a[i]);
g[i][j] = max(g[i - 1][j], dp2[i][j]);
}
int ans = 0;
FOR(j, 1, k) ans = max(ans, g[n][j]);
cout<<ans;
}
/*
sub7: O(nlog)
tham lam
xoa cac phan tu am o dau va cuoi cua mang
gop cac gia tri am duong -> mang a am duong xen ke
*/
int res[N], pre[N], nxt[N];
bool check[N];
vector<int> v;
priority_queue<tuple<int, int, int>> pq;
void sub7(){
FOR(i, 1, n) cin>>a[i];
int l = 1, r = n;
while(l <= n && a[l] <= 0) l ++;
while(r >= 1 && a[r] <= 0) r--;
if(l > r){
cout<<0;
return;
}
n = 0;
FOR(i, l, r)
a[++ n] = a[i];
// FOR(i, 1, n) cout<<a[i]<<" ";
FOR(i, 1, n) s[i] = s[i - 1] + a[i];
int last = 0, cnt = 0;
FOR(i, 1, n)
if(i > 1 && (a[i] > 0) != (a[i - 1] > 0)){
if(a[i - 1] > 0) cnt ++;
res[i - 1] = -abs(s[i - 1] - s[last]);
pre[i - 1] = last;
nxt[last] = i - 1;
pq.push({res[i - 1], last, i - 1});
last = i - 1;
}
cnt ++;
res[n] = -abs(s[n] - s[last]);
pre[n] = last;
nxt[last] = n;
pq.push({res[n], last, n});
int ans = 0;
while(!pq.empty() && cnt > k){
int val = get<0>(pq.top()),
l = get<1>(pq.top()),
r = get<2>(pq.top());
pq.pop();
if(check[l] || check[r] || val != res[r]) continue;
cnt --;
check[l] = check[r] = 1;
ans += res[r];
int i = pre[l], j = nxt[r];
if(i < j && i >= 0 && j <= n){
pre[j] = i, nxt[i] = j;
res[j] = res[j] + res[l] - res[r];
res[r] = -oo;
}
if(i < j && i >= 0 && j <= n && !check[j]) pq.push({res[j], i, j});
}
FOR(i, 1, n) if(a[i] > 0) ans += a[i];
cout<<ans;
}
void solve(){
cin>>n>>k;
sub7();
}
int32_t main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
// freo("");
int nTest = 1;
// cin>>nTest;
while(nTest --) solve();
// cerr << "\nTime: " << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
/*
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
5160 KB |
Output is correct |
2 |
Correct |
27 ms |
7736 KB |
Output is correct |
3 |
Correct |
38 ms |
6472 KB |
Output is correct |
4 |
Correct |
28 ms |
6548 KB |
Output is correct |
5 |
Correct |
27 ms |
5968 KB |
Output is correct |
6 |
Correct |
28 ms |
7012 KB |
Output is correct |
7 |
Correct |
26 ms |
6992 KB |
Output is correct |
8 |
Correct |
26 ms |
5536 KB |
Output is correct |
9 |
Correct |
26 ms |
7740 KB |
Output is correct |
10 |
Correct |
32 ms |
6768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
8740 KB |
Output is correct |
2 |
Correct |
22 ms |
9040 KB |
Output is correct |
3 |
Correct |
22 ms |
8288 KB |
Output is correct |
4 |
Correct |
20 ms |
9068 KB |
Output is correct |
5 |
Correct |
26 ms |
5600 KB |
Output is correct |
6 |
Correct |
20 ms |
8016 KB |
Output is correct |
7 |
Correct |
19 ms |
9040 KB |
Output is correct |
8 |
Correct |
27 ms |
5960 KB |
Output is correct |
9 |
Correct |
36 ms |
5836 KB |
Output is correct |
10 |
Correct |
21 ms |
8520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
17592 KB |
Output is correct |
2 |
Correct |
72 ms |
17592 KB |
Output is correct |
3 |
Correct |
73 ms |
18172 KB |
Output is correct |
4 |
Correct |
80 ms |
17768 KB |
Output is correct |
5 |
Correct |
81 ms |
18008 KB |
Output is correct |
6 |
Correct |
81 ms |
19948 KB |
Output is correct |
7 |
Correct |
81 ms |
18048 KB |
Output is correct |
8 |
Correct |
79 ms |
19136 KB |
Output is correct |
9 |
Correct |
82 ms |
18096 KB |
Output is correct |
10 |
Correct |
86 ms |
17848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
504 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
504 KB |
Output is correct |
14 |
Correct |
1 ms |
504 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
1 ms |
592 KB |
Output is correct |
23 |
Correct |
1 ms |
480 KB |
Output is correct |
24 |
Correct |
1 ms |
592 KB |
Output is correct |
25 |
Correct |
1 ms |
592 KB |
Output is correct |
26 |
Correct |
1 ms |
592 KB |
Output is correct |
27 |
Correct |
1 ms |
592 KB |
Output is correct |
28 |
Correct |
1 ms |
592 KB |
Output is correct |
29 |
Correct |
1 ms |
592 KB |
Output is correct |
30 |
Correct |
1 ms |
592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
5160 KB |
Output is correct |
2 |
Correct |
27 ms |
7736 KB |
Output is correct |
3 |
Correct |
38 ms |
6472 KB |
Output is correct |
4 |
Correct |
28 ms |
6548 KB |
Output is correct |
5 |
Correct |
27 ms |
5968 KB |
Output is correct |
6 |
Correct |
28 ms |
7012 KB |
Output is correct |
7 |
Correct |
26 ms |
6992 KB |
Output is correct |
8 |
Correct |
26 ms |
5536 KB |
Output is correct |
9 |
Correct |
26 ms |
7740 KB |
Output is correct |
10 |
Correct |
32 ms |
6768 KB |
Output is correct |
11 |
Correct |
19 ms |
8740 KB |
Output is correct |
12 |
Correct |
22 ms |
9040 KB |
Output is correct |
13 |
Correct |
22 ms |
8288 KB |
Output is correct |
14 |
Correct |
20 ms |
9068 KB |
Output is correct |
15 |
Correct |
26 ms |
5600 KB |
Output is correct |
16 |
Correct |
20 ms |
8016 KB |
Output is correct |
17 |
Correct |
19 ms |
9040 KB |
Output is correct |
18 |
Correct |
27 ms |
5960 KB |
Output is correct |
19 |
Correct |
36 ms |
5836 KB |
Output is correct |
20 |
Correct |
21 ms |
8520 KB |
Output is correct |
21 |
Correct |
77 ms |
17592 KB |
Output is correct |
22 |
Correct |
72 ms |
17592 KB |
Output is correct |
23 |
Correct |
73 ms |
18172 KB |
Output is correct |
24 |
Correct |
80 ms |
17768 KB |
Output is correct |
25 |
Correct |
81 ms |
18008 KB |
Output is correct |
26 |
Correct |
81 ms |
19948 KB |
Output is correct |
27 |
Correct |
81 ms |
18048 KB |
Output is correct |
28 |
Correct |
79 ms |
19136 KB |
Output is correct |
29 |
Correct |
82 ms |
18096 KB |
Output is correct |
30 |
Correct |
86 ms |
17848 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
336 KB |
Output is correct |
33 |
Correct |
1 ms |
504 KB |
Output is correct |
34 |
Correct |
1 ms |
336 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
1 ms |
336 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
1 ms |
504 KB |
Output is correct |
40 |
Correct |
1 ms |
336 KB |
Output is correct |
41 |
Correct |
1 ms |
336 KB |
Output is correct |
42 |
Correct |
1 ms |
336 KB |
Output is correct |
43 |
Correct |
1 ms |
504 KB |
Output is correct |
44 |
Correct |
1 ms |
504 KB |
Output is correct |
45 |
Correct |
1 ms |
336 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
1 ms |
336 KB |
Output is correct |
50 |
Correct |
1 ms |
336 KB |
Output is correct |
51 |
Correct |
2 ms |
592 KB |
Output is correct |
52 |
Correct |
1 ms |
592 KB |
Output is correct |
53 |
Correct |
1 ms |
480 KB |
Output is correct |
54 |
Correct |
1 ms |
592 KB |
Output is correct |
55 |
Correct |
1 ms |
592 KB |
Output is correct |
56 |
Correct |
1 ms |
592 KB |
Output is correct |
57 |
Correct |
1 ms |
592 KB |
Output is correct |
58 |
Correct |
1 ms |
592 KB |
Output is correct |
59 |
Correct |
1 ms |
592 KB |
Output is correct |
60 |
Correct |
1 ms |
592 KB |
Output is correct |
61 |
Correct |
44 ms |
20408 KB |
Output is correct |
62 |
Correct |
40 ms |
20664 KB |
Output is correct |
63 |
Correct |
64 ms |
20420 KB |
Output is correct |
64 |
Correct |
44 ms |
20432 KB |
Output is correct |
65 |
Correct |
50 ms |
20524 KB |
Output is correct |
66 |
Correct |
51 ms |
20612 KB |
Output is correct |
67 |
Correct |
49 ms |
20408 KB |
Output is correct |
68 |
Correct |
69 ms |
20512 KB |
Output is correct |
69 |
Correct |
68 ms |
20428 KB |
Output is correct |
70 |
Correct |
67 ms |
20248 KB |
Output is correct |