#pragma GCC optimize("Ofast,O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
typedef long long ll;
#define all(x) (x).begin(), (x).end()
#define MP make_pair
const int N = 100500;
long long stress(int m, ll L, vector<ll> a){
vector<int> b;
for (int i = 0; i <= m; i++){
while (a[i]--) b.push_back(i);
}
int LIM = accumulate(all(b), 0) + 1;
if (L >= LIM) return -1;
vector<int> dp(LIM, -1e9);
dp[0] = 0;
for (int x : b) {
for (int i = LIM - 1; i >= x; i--) {
dp[i] = max(dp[i], dp[i - x] + 1);
}
}
return dp[L] < 0 ? -1 : dp[L];
}
long long solve(int m, ll L, vector<ll> a){
if (L < 0) return -1;
if (L == 0) return a[0];
ll ans = -1;
ll tot = 0, cnt = a[0];
vector<int> dp(1, 0);
for (int v = 1; v <= m; v++){
assert(tot <= L);
ll x = min(a[v], (L - tot) / v);
tot += x * v;
cnt += x;
if (tot == L){
ans = max(ans, cnt);
} else if (a[v] > x){
int add = 0;
for (ll del = tot + v - L; del < dp.size(); del += v){
add++;
if (add <= a[v] - x) ans = max(ans, cnt - dp[del] + add);
}
}
for (int it = 0; it < min((ll) 10 * m / v, x); it++){
dp.resize(dp.size() + v, 1e9);
for (int i = dp.size() - 1; i >= v; i--){
dp[i] = min(dp[i], dp[i - v] + 1);
}
}
}
return ans;
}
void brute(){
int iterations = 10000;
while (iterations--){
int m = rng() % 5 + 1;
vector<ll> a(m + 1);
int sum = 0;
for (int i = 0; i <= m; i++){
a[i] = rng() % 10 + 1;
sum += i * a[i];
}
for (int L = 0; L <= min(sum, 50); L++) {
long long correct = stress(m, L, a);
long long result = solve(m, L, a);
if (result != correct){
cout << correct << ' ' << result << endl << endl;
cout << m << ' ' << L << endl;
for (int i = 0; i <= m; i++) cout << a[i] << ' ';
cout << endl;
exit(0);
}
}
}
cout << "Accepted" << endl;
exit(0);
}
int main() {
ios_base::sync_with_stdio(false);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
// brute();
int m; ll L;
cin >> m >> L;
vector<ll> a(m + 1);
for (int i = -m; i <= m; i++){
cin >> a[abs(i)];
}
long long result = solve(m, L, a);
if (result != -1){
cout << result << endl;
} else {
cout << "impossible" << endl;
}
return 0;
}
Compilation message
vault.cpp: In function 'long long int solve(int, ll, std::vector<long long int>)':
vault.cpp:58:44: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (ll del = tot + v - L; del < dp.size(); del += v){
| ~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
272 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
272 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
272 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
272 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
272 KB |
Output is correct |
3 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |