Submission #5552775
Source Code Expand
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 4e18;
const int MAXN = 100010;
int N, K;
struct Seg {
Seg* ch[2];
int sz;
ll add = 0;
ll ma = -INF;
Seg(int _sz) : sz(_sz) {
if (sz > 1) {
ch[0] = new Seg(sz / 2);
ch[1] = new Seg(sz - sz / 2);
} else {
assert(sz == 1);
}
}
void setmax(int p, ll v) {
if (sz == 1) {
// add are useless on leaf vertices
ma = max(ma, v);
} else {
assert(sz > 1);
if (p < sz / 2) {
ch[0]->setmax(p, v - add);
} else {
ch[1]->setmax(p - sz / 2, v - add);
}
ma = add + max(ch[0]->ma, ch[1]->ma);
}
}
void incr(int a, int b, ll d) {
if (b <= 0 || sz <= a) {
// do nothing
} else if (a <= 0 && sz <= b) {
ma += d;
add += d;
} else {
ch[0]->incr(a, b, d);
ch[1]->incr(a - sz / 2, b - sz / 2, d);
ma = add + max(ch[0]->ma, ch[1]->ma);
}
}
ll getmax(int a, int b) {
if (b <= 0 || sz <= a) {
return -INF;
} else if (a <= 0 && sz <= b) {
return ma;
} else {
return add + max(ch[0]->getmax(a, b), ch[1]->getmax(a - sz / 2, b - sz / 2));
}
}
};
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> N >> K;
Seg* tr = new Seg(N + 1);
tr->setmax(0, 0);
for (int i = 0; i < N; i++) {
ll X; cin >> X;
int bnd = min(i + K + 1, N + 1);
ll ma = tr->getmax(i, bnd);
tr->incr(i + 1, bnd, X);
tr->setmax(i + 1, ma);
if (i + K <= N) {
tr->setmax(i + K, ma + X);
}
}
cout << tr->getmax(N, N + 1) << endl;
return 0;
}
Submission Info
Judge Result
Set Name |
Sample |
All |
Score / Max Score |
0 / 0 |
0 / 400 |
Status |
|
|
Set Name |
Test Cases |
Sample |
0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt |
All |
0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt |
Case Name |
Status |
Exec Time |
Memory |
0_00.txt |
AC |
1 ms |
256 KB |
0_01.txt |
WA |
1 ms |
256 KB |
0_02.txt |
AC |
1 ms |
256 KB |
0_03.txt |
AC |
1 ms |
256 KB |
1_00.txt |
AC |
1 ms |
256 KB |
1_01.txt |
AC |
1 ms |
256 KB |
1_02.txt |
AC |
1 ms |
256 KB |
1_03.txt |
AC |
63 ms |
9600 KB |
1_04.txt |
AC |
68 ms |
9600 KB |
1_05.txt |
AC |
68 ms |
9600 KB |
1_06.txt |
AC |
68 ms |
9600 KB |
1_07.txt |
AC |
68 ms |
9472 KB |
1_08.txt |
AC |
55 ms |
9472 KB |
1_09.txt |
AC |
57 ms |
9600 KB |
1_10.txt |
WA |
57 ms |
9472 KB |
1_11.txt |
WA |
66 ms |
9472 KB |
1_12.txt |
WA |
68 ms |
9344 KB |
1_13.txt |
WA |
69 ms |
9600 KB |
1_14.txt |
WA |
55 ms |
9472 KB |
1_15.txt |
WA |
74 ms |
9600 KB |
1_16.txt |
WA |
80 ms |
9600 KB |
1_17.txt |
WA |
82 ms |
9600 KB |
1_18.txt |
WA |
68 ms |
9472 KB |
1_19.txt |
WA |
62 ms |
9472 KB |
1_20.txt |
WA |
67 ms |
9472 KB |
1_21.txt |
WA |
62 ms |
9600 KB |