#include <bits/stdc++.h>
using namespace std;
const int BOUND = 505000;
template <typename T> class negarr {
private:
vector<T> data;
int n;
public:
negarr(int n) : n(n) { data.resize(2 * n + 1, 0); }
void reset() { fill(data.begin(), data.end(), -1000000000); }
inline T &operator[](int i) { return data[i + n]; }
};
class pstate {
public:
int x, y;
pstate(int x, int y) : x(x), y(y) {}
friend bool operator<(const pstate &a, const pstate &b) {
return a.x + a.y < b.x + b.y;
}
};
class nstate {
public:
int x, y;
nstate(int x, int y) : x(x), y(y) {}
friend bool operator<(const nstate &a, const nstate &b) {
return a.y - a.x < b.y - b.x;
}
};
int main() {
int m;
long long l;
cin >> m >> l;
negarr<int> a(m), knapsack(BOUND);
if (l > BOUND || l < -BOUND) {
printf("impossible\n");
return 0;
}
knapsack.reset();
knapsack[0] = 0;
for (int i = -m; i <= m; i++) {
cin >> a[i];
if (i == 0) {
for (int id = -BOUND; id <= BOUND; id++) {
knapsack[id] += a[i];
}
} else {
negarr<int> tmp(BOUND);
tmp.reset();
if (i > 0) {
vector<vector<nstate>> pq(i);
for (int id = -BOUND; id <= BOUND; id++) {
int mod = id % i;
if (mod < 0)
mod += i;
pq[mod].reserve(2 * BOUND / i + 5);
}
for (int id = -BOUND; id <= BOUND; id++) {
int mod = id % i;
if (mod < 0)
mod += i;
int xx = (id + BOUND) / i;
while (!pq[mod].empty() && xx - pq[mod].front().x > a[i]) {
pop_heap(pq[mod].begin(), pq[mod].end());
pq[mod].pop_back();
}
while (!pq[mod].empty() &&
pq[mod].front() < nstate(xx, knapsack[id])) {
pop_heap(pq[mod].begin(), pq[mod].end());
pq[mod].pop_back();
}
pq[mod].push_back({xx, knapsack[id]});
push_heap(pq[mod].begin(), pq[mod].end());
if (!pq[mod].empty()) {
nstate t = pq[mod].front();
tmp[id] = max(tmp[id], t.y - t.x + xx);
}
}
} else if (i < 0) {
vector<vector<pstate>> pq(-i);
for (int id = -BOUND; id <= BOUND; id++) {
int mod = id % (-i);
if (mod < 0)
mod += (-i);
pq[mod].reserve(2 * BOUND / (-i) + 5);
}
for (int id = BOUND; id >= -BOUND; id--) {
int mod = id % (-i);
if (mod < 0)
mod += (-i);
int xx = (id + BOUND) / (-i);
while (!pq[mod].empty() && pq[mod].front().x - xx > a[i]) {
pop_heap(pq[mod].begin(), pq[mod].end());
pq[mod].pop_back();
}
while (!pq[mod].empty() &&
pq[mod].front() < pstate(xx, knapsack[id])) {
pop_heap(pq[mod].begin(), pq[mod].end());
pq[mod].pop_back();
}
pq[mod].push_back({xx, knapsack[id]});
push_heap(pq[mod].begin(), pq[mod].end());
if (!pq[mod].empty()) {
pstate t = pq[mod].front();
knapsack[id] = max(knapsack[id], t.y + t.x - xx);
}
}
}
for (int id = -BOUND; id <= BOUND; id++) {
knapsack[id] = max(knapsack[id], tmp[id]);
}
}
}
if (knapsack[l] >= 0)
printf("%d\n", knapsack[l]);
else
printf("impossible\n");
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
8244 KB |
Output is correct |
2 |
Correct |
204 ms |
8208 KB |
Output is correct |
3 |
Correct |
83 ms |
8232 KB |
Output is correct |
4 |
Correct |
734 ms |
8296 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Execution timed out |
5050 ms |
10848 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
8244 KB |
Output is correct |
2 |
Correct |
204 ms |
8208 KB |
Output is correct |
3 |
Correct |
83 ms |
8232 KB |
Output is correct |
4 |
Correct |
734 ms |
8296 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Execution timed out |
5050 ms |
10848 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
744 ms |
8384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4180 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
744 ms |
8384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4180 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
744 ms |
8384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4180 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
8244 KB |
Output is correct |
2 |
Correct |
204 ms |
8208 KB |
Output is correct |
3 |
Correct |
83 ms |
8232 KB |
Output is correct |
4 |
Correct |
734 ms |
8296 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Execution timed out |
5050 ms |
10848 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
744 ms |
8384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4180 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
8244 KB |
Output is correct |
2 |
Correct |
204 ms |
8208 KB |
Output is correct |
3 |
Correct |
83 ms |
8232 KB |
Output is correct |
4 |
Correct |
734 ms |
8296 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Execution timed out |
5050 ms |
10848 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
744 ms |
8384 KB |
Output is correct |
2 |
Incorrect |
2 ms |
4180 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
8244 KB |
Output is correct |
2 |
Correct |
204 ms |
8208 KB |
Output is correct |
3 |
Correct |
83 ms |
8232 KB |
Output is correct |
4 |
Correct |
734 ms |
8296 KB |
Output is correct |
5 |
Correct |
2 ms |
4180 KB |
Output is correct |
6 |
Execution timed out |
5050 ms |
10848 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |