#include <bits/stdc++.h>
using namespace std;
vector<long long> SolveDP(long long N, long long LIM, vector<long long> List, vector<long long> Init) {
vector<long long> dp(LIM + 1, -(1LL << 60));
if (Init.size() == 0) dp[0] = 0;
else {
for (int i = 0; i < (int)Init.size(); i++) dp[i] = Init[i];
}
// DP Start
for (int i = 0; i < N; i++) {
vector<long long> ndp(LIM + 1, -(1LL << 60));
long long kazu = i + 1;
for (int j = 0; j < kazu; j++) {
vector<pair<long long, long long>> maxi;
int cur = 0;
for (int k = j; k <= LIM; k += kazu) {
long long val = dp[k] - (k / kazu);
while (maxi.size() >= cur + 1) {
if (maxi[maxi.size() - 1].second <= val) maxi.pop_back();
else break;
}
maxi.push_back(make_pair(1LL * k, val));
if (maxi[cur].second >= -(1LL << 59)) ndp[k] = maxi[cur].second + (k / kazu);
while (cur < (int)maxi.size() && maxi[cur].first <= k - 1LL * kazu * List[i]) cur += 1;
}
}
dp = ndp;
}
// Return
return dp;
}
void Solve_Subtask3(long long M, long long L, vector<long long> A) {
long long Baseln = (M + 1) * M * M / 2LL + 3LL * M * M;
vector<long long> L1(M + 1, 0);
vector<long long> L2(M + 1, 0);
vector<long long> L3(M, 0);
vector<long long> L4(M, 0);
vector<long long> M1(M + 1, 0);
vector<long long> M2(M + 1, 0);
vector<pair<long long, long long>> Cand1; Cand1.push_back(make_pair(0, 0));
vector<pair<long long, long long>> Cand2; Cand2.push_back(make_pair(0, 0));
// Step 1. Set Up
for (int i = 0; i <= M - 1; i++) {
int idx = M - i;
long long Threshold = M;
L1[idx - 0] = A[i] - min(A[i], Threshold);
L3[idx - 1] = min(A[i], Threshold);
}
for (int i = M + 1; i <= 2 * M; i++) {
int idx = i - M;
long long Threshold = M;
L2[idx - 0] = A[i] - min(A[i], Threshold);
L4[idx - 1] = min(A[i], Threshold);
}
// Step 2. Base DP
vector<long long> dp1 = SolveDP(M, Baseln, L3, vector<long long>{});
vector<long long> dp2 = vector<long long>(Baseln + 1, -(1LL << 60));
for (int i = 0; i <= Baseln; i++) dp2[Baseln - i] = dp1[i];
vector<long long> dp3 = SolveDP(M, Baseln * 2, L4, dp2);
// Step 3. Get Theoritical Value
long long a1 = 0;
long long a2 = 0;
for (int i = 1; i <= M; i++) a1 += 1LL * i * L1[i];
for (int i = 1; i <= M; i++) a2 += 1LL * i * L2[i];
if (a2 - a1 > L) a2 = a1 + L;
else a1 = a2 - L;
// Step 4. Get Candiates for L1
if (a1 >= 0) {
long long rem1 = a1;
long long cnt1 = 0;
for (int i = 1; i <= M; i++) {
M1[i] = min(L1[i], rem1 / i);
rem1 -= 1LL * i * M1[i];
cnt1 += M1[i];
}
Cand1.push_back(make_pair(a1 - rem1, cnt1));
}
// Step 5. Get Candidates for L2
if (a2 >= 0) {
long long rem2 = a2;
long long cnt2 = 0;
for (int i = 1; i <= M; i++) {
M2[i] = min(L2[i], rem2 / i);
rem2 -= 1LL * i * M2[i];
cnt2 += M2[i];
}
Cand2.push_back(make_pair(a2 - rem2, cnt2));
}
// Step 6. Brute Force
long long Answer = -(1LL << 60);
for (pair<long long, long long> v1 : Cand1) {
for (pair<long long, long long> v2 : Cand2) {
long long diff = L - (v2.first - v1.first);
if (diff < -Baseln || diff > Baseln) continue;
if (dp3[diff + Baseln] == -(1LL << 60)) continue;
Answer = max(Answer, v1.second + v2.second + dp3[diff + Baseln]);
}
}
// Step 7. Output
if (Answer == -(1LL << 60)) cout << "impossible" << endl;
else cout << Answer + A[M] << endl;
}
int main() {
// Input
long long M, L;
cin >> M >> L;
vector<long long> A(2 * M + 1, 0);
for (int i = 0; i <= 2 * M; i++) cin >> A[i];
// Execution
Solve_Subtask3(M, L, A);
return 0;
}
Compilation message
vault.cpp: In function 'std::vector<long long int> SolveDP(long long int, long long int, std::vector<long long int>, std::vector<long long int>)':
vault.cpp:20:36: warning: comparison of integer expressions of different signedness: 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
20 | while (maxi.size() >= cur + 1) {
| ~~~~~~~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
124 ms |
11592 KB |
Output is correct |
6 |
Correct |
124 ms |
11564 KB |
Output is correct |
7 |
Correct |
131 ms |
11312 KB |
Output is correct |
8 |
Correct |
126 ms |
10828 KB |
Output is correct |
9 |
Correct |
125 ms |
11564 KB |
Output is correct |
10 |
Correct |
133 ms |
10800 KB |
Output is correct |
11 |
Correct |
133 ms |
10816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
124 ms |
11592 KB |
Output is correct |
6 |
Correct |
124 ms |
11564 KB |
Output is correct |
7 |
Correct |
131 ms |
11312 KB |
Output is correct |
8 |
Correct |
126 ms |
10828 KB |
Output is correct |
9 |
Correct |
125 ms |
11564 KB |
Output is correct |
10 |
Correct |
133 ms |
10800 KB |
Output is correct |
11 |
Correct |
133 ms |
10816 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
125 ms |
11312 KB |
Output is correct |
17 |
Correct |
127 ms |
10544 KB |
Output is correct |
18 |
Correct |
131 ms |
12332 KB |
Output is correct |
19 |
Correct |
126 ms |
11572 KB |
Output is correct |
20 |
Correct |
125 ms |
10560 KB |
Output is correct |
21 |
Correct |
135 ms |
11568 KB |
Output is correct |
22 |
Correct |
133 ms |
12072 KB |
Output is correct |
23 |
Correct |
1922 ms |
80144 KB |
Output is correct |
24 |
Correct |
2033 ms |
80328 KB |
Output is correct |
25 |
Correct |
1982 ms |
79936 KB |
Output is correct |
26 |
Correct |
1990 ms |
80196 KB |
Output is correct |
27 |
Correct |
1897 ms |
80388 KB |
Output is correct |
28 |
Correct |
2072 ms |
80892 KB |
Output is correct |
29 |
Correct |
2060 ms |
80456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
18 ms |
2904 KB |
Output is correct |
3 |
Correct |
21 ms |
2884 KB |
Output is correct |
4 |
Incorrect |
19 ms |
2884 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
18 ms |
2904 KB |
Output is correct |
3 |
Correct |
21 ms |
2884 KB |
Output is correct |
4 |
Incorrect |
19 ms |
2884 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
18 ms |
2904 KB |
Output is correct |
3 |
Correct |
21 ms |
2884 KB |
Output is correct |
4 |
Incorrect |
19 ms |
2884 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
124 ms |
11592 KB |
Output is correct |
6 |
Correct |
124 ms |
11564 KB |
Output is correct |
7 |
Correct |
131 ms |
11312 KB |
Output is correct |
8 |
Correct |
126 ms |
10828 KB |
Output is correct |
9 |
Correct |
125 ms |
11564 KB |
Output is correct |
10 |
Correct |
133 ms |
10800 KB |
Output is correct |
11 |
Correct |
133 ms |
10816 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
18 ms |
2904 KB |
Output is correct |
14 |
Correct |
21 ms |
2884 KB |
Output is correct |
15 |
Incorrect |
19 ms |
2884 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
18 ms |
2904 KB |
Output is correct |
3 |
Correct |
21 ms |
2884 KB |
Output is correct |
4 |
Incorrect |
19 ms |
2884 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
124 ms |
11592 KB |
Output is correct |
6 |
Correct |
124 ms |
11564 KB |
Output is correct |
7 |
Correct |
131 ms |
11312 KB |
Output is correct |
8 |
Correct |
126 ms |
10828 KB |
Output is correct |
9 |
Correct |
125 ms |
11564 KB |
Output is correct |
10 |
Correct |
133 ms |
10800 KB |
Output is correct |
11 |
Correct |
133 ms |
10816 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
125 ms |
11312 KB |
Output is correct |
17 |
Correct |
127 ms |
10544 KB |
Output is correct |
18 |
Correct |
131 ms |
12332 KB |
Output is correct |
19 |
Correct |
126 ms |
11572 KB |
Output is correct |
20 |
Correct |
125 ms |
10560 KB |
Output is correct |
21 |
Correct |
135 ms |
11568 KB |
Output is correct |
22 |
Correct |
133 ms |
12072 KB |
Output is correct |
23 |
Correct |
1922 ms |
80144 KB |
Output is correct |
24 |
Correct |
2033 ms |
80328 KB |
Output is correct |
25 |
Correct |
1982 ms |
79936 KB |
Output is correct |
26 |
Correct |
1990 ms |
80196 KB |
Output is correct |
27 |
Correct |
1897 ms |
80388 KB |
Output is correct |
28 |
Correct |
2072 ms |
80892 KB |
Output is correct |
29 |
Correct |
2060 ms |
80456 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
18 ms |
2904 KB |
Output is correct |
32 |
Correct |
21 ms |
2884 KB |
Output is correct |
33 |
Incorrect |
19 ms |
2884 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
18 ms |
2904 KB |
Output is correct |
3 |
Correct |
21 ms |
2884 KB |
Output is correct |
4 |
Incorrect |
19 ms |
2884 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
124 ms |
11592 KB |
Output is correct |
6 |
Correct |
124 ms |
11564 KB |
Output is correct |
7 |
Correct |
131 ms |
11312 KB |
Output is correct |
8 |
Correct |
126 ms |
10828 KB |
Output is correct |
9 |
Correct |
125 ms |
11564 KB |
Output is correct |
10 |
Correct |
133 ms |
10800 KB |
Output is correct |
11 |
Correct |
133 ms |
10816 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
125 ms |
11312 KB |
Output is correct |
17 |
Correct |
127 ms |
10544 KB |
Output is correct |
18 |
Correct |
131 ms |
12332 KB |
Output is correct |
19 |
Correct |
126 ms |
11572 KB |
Output is correct |
20 |
Correct |
125 ms |
10560 KB |
Output is correct |
21 |
Correct |
135 ms |
11568 KB |
Output is correct |
22 |
Correct |
133 ms |
12072 KB |
Output is correct |
23 |
Correct |
1922 ms |
80144 KB |
Output is correct |
24 |
Correct |
2033 ms |
80328 KB |
Output is correct |
25 |
Correct |
1982 ms |
79936 KB |
Output is correct |
26 |
Correct |
1990 ms |
80196 KB |
Output is correct |
27 |
Correct |
1897 ms |
80388 KB |
Output is correct |
28 |
Correct |
2072 ms |
80892 KB |
Output is correct |
29 |
Correct |
2060 ms |
80456 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
18 ms |
2904 KB |
Output is correct |
32 |
Correct |
21 ms |
2884 KB |
Output is correct |
33 |
Incorrect |
19 ms |
2884 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |