This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define dbg(x) cerr << #x << ": " << x << endl;
const long long MINUS_INF = -1e15;
const int MAX_N = 2e5;
long long ans[MAX_N / 2];
struct State {
long long value;
int cnt;
int index;
State(long long _value = 0, int _cnt = 0, int _index = 0)
: value(_value), cnt(_cnt), index(_index) {}
bool operator<(const State& other) const {
return make_tuple(value, cnt, index) < make_tuple(other.value, other.cnt, other.index);
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<long long> a(n);
for (auto& x : a) cin >> x;
// for (int i = 1; i < n; ++i) {
// dp[i][0] = 0;
// for (int j = 1; j <= (i + 2) / 2; ++j) {
// dp[i][j] = max(dp[i - 1][j], a[i] + (i >= 2 ? dp[i - 2][j - 1] : 0));
// }
// }
priority_queue<State> pq;
pq.emplace(0, 0, 0);
pq.emplace(a[1], 1, 1);
pq.emplace(a[0], 1, 0);
while (!pq.empty()) {
auto [value, cnt, prev_index] = pq.top();
pq.pop();
if (prev_index + 2 < n) {
pq.emplace(value + a[prev_index + 2], cnt + 1, prev_index + 2);
}
if (prev_index + 1 < n) {
pq.emplace(value, cnt, prev_index + 1);
}
ans[cnt] = max(ans[cnt], value);
}
for (int j = 1; j <= (n + 1) / 2; ++j) {
cout << ans[j] << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |