#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
const ll inf = 1e15;
vector<ll> mxconv(vector<ll> &a, vector<ll> &b) {
int n = a.size(), m = b.size();
vector<ll> c(n+m-1);
c[0] = 0;
int i = 1, j = 1;
while (i < n || j < m) {
if (i == n) ++j;
else if (j == m) ++i;
else {
if (a[i] - a[i-1] > b[j] - b[j-1]) ++i;
else ++j;
}
c[i+j-2] = a[i-1] + b[j-1];
}
return c;
}
vector<vector<vector<ll>>> dq(vector<ll> &a) { // dp[i][b1][b2] = uzmes ih i b1 = da li uzmes poslednji b2 = da li uzmes prvi
int n = a.size();
if (n == 0) return {{{{0}, {0}}}, {{{0}, {0}}}};
if (n == 1) return {{{{0, -inf}, {0, -inf}}}, {{{0, -inf}, {0, a[0]}}}};
vector<ll> L, R;
for (int i = 0; i < n/2; ++i) L.pb(a[i]);
for (int i = n/2; i < n; ++i) R.pb(a[i]);
auto A = dq(L), B = dq(R);
vector<vector<vector<ll>>> dp(2, vector<vector<ll>>(2, vector<ll>(1 + (n+1)/2, -inf)));
for (int X = 0; X < 2; ++X) {
for (int Y = 0; Y < 2; ++Y) {
auto t1 = mxconv(A[X][0], B[0][Y]);
auto t2 = mxconv(A[X][1], B[0][Y]);
auto t3 = mxconv(A[X][0], B[1][Y]);
for (int i = 0; i <= (n+1)/2; ++i) {
dp[X][Y][i] = max({
(i < t1.size() ? t1[i] : -inf),
(i < t2.size() ? t2[i] : -inf),
(i < t3.size() ? t3[i] : -inf)});
}
}
}
return dp;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vector<ll> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
auto ans = dq(a);
for (int i = 1; i <= (n+1)/2; ++i) {
cout << max({ans[0][0][i], ans[0][1][i], ans[1][1][i], ans[1][0][i]}) << '\n';
}
}
Compilation message
candies.cpp: In function 'std::vector<std::vector<std::vector<long long int> > > dq(std::vector<long long int>&)':
candies.cpp:40:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | (i < t1.size() ? t1[i] : -inf),
| ~~^~~~~~~~~~~
candies.cpp:41:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | (i < t2.size() ? t2[i] : -inf),
| ~~^~~~~~~~~~~
candies.cpp:42:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | (i < t3.size() ? t3[i] : -inf)});
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
3 ms |
600 KB |
Output is correct |
3 |
Correct |
3 ms |
540 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
600 KB |
Output is correct |
8 |
Correct |
3 ms |
600 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
604 KB |
Output is correct |
18 |
Correct |
3 ms |
548 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
3 ms |
600 KB |
Output is correct |
3 |
Correct |
3 ms |
540 KB |
Output is correct |
4 |
Correct |
3 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
468 KB |
Output is correct |
6 |
Correct |
3 ms |
604 KB |
Output is correct |
7 |
Correct |
3 ms |
600 KB |
Output is correct |
8 |
Correct |
3 ms |
600 KB |
Output is correct |
9 |
Correct |
3 ms |
604 KB |
Output is correct |
10 |
Correct |
3 ms |
600 KB |
Output is correct |
11 |
Correct |
5 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
604 KB |
Output is correct |
13 |
Correct |
3 ms |
604 KB |
Output is correct |
14 |
Correct |
3 ms |
604 KB |
Output is correct |
15 |
Correct |
3 ms |
604 KB |
Output is correct |
16 |
Correct |
3 ms |
604 KB |
Output is correct |
17 |
Correct |
3 ms |
604 KB |
Output is correct |
18 |
Correct |
3 ms |
548 KB |
Output is correct |
19 |
Correct |
3 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
264 ms |
16628 KB |
Output is correct |
22 |
Correct |
270 ms |
16460 KB |
Output is correct |
23 |
Correct |
267 ms |
16464 KB |
Output is correct |
24 |
Correct |
263 ms |
16464 KB |
Output is correct |
25 |
Correct |
229 ms |
16464 KB |
Output is correct |
26 |
Correct |
237 ms |
16464 KB |
Output is correct |
27 |
Correct |
261 ms |
16520 KB |
Output is correct |
28 |
Correct |
242 ms |
16504 KB |
Output is correct |
29 |
Correct |
247 ms |
16896 KB |
Output is correct |
30 |
Correct |
240 ms |
16720 KB |
Output is correct |
31 |
Correct |
238 ms |
16508 KB |
Output is correct |
32 |
Correct |
253 ms |
16512 KB |
Output is correct |
33 |
Correct |
244 ms |
16464 KB |
Output is correct |
34 |
Correct |
256 ms |
16276 KB |
Output is correct |
35 |
Correct |
245 ms |
16480 KB |
Output is correct |
36 |
Correct |
275 ms |
16204 KB |
Output is correct |
37 |
Correct |
265 ms |
16136 KB |
Output is correct |
38 |
Correct |
262 ms |
16204 KB |
Output is correct |
39 |
Correct |
239 ms |
15952 KB |
Output is correct |
40 |
Correct |
230 ms |
16212 KB |
Output is correct |
41 |
Correct |
250 ms |
15948 KB |
Output is correct |
42 |
Correct |
234 ms |
16212 KB |
Output is correct |
43 |
Correct |
244 ms |
16208 KB |
Output is correct |
44 |
Correct |
237 ms |
16208 KB |
Output is correct |
45 |
Correct |
242 ms |
16224 KB |
Output is correct |
46 |
Correct |
245 ms |
16212 KB |
Output is correct |
47 |
Correct |
243 ms |
16260 KB |
Output is correct |
48 |
Correct |
253 ms |
15952 KB |
Output is correct |
49 |
Correct |
241 ms |
16084 KB |
Output is correct |
50 |
Correct |
257 ms |
15952 KB |
Output is correct |