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 pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>
const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;
const int MAXN = 2e5 + 10;
ll a[MAXN];
int L[MAXN], R[MAXN];
ll dp[2020][2020];
int n;
void solve() {
cin >> n;
set <pair <ll, int> > st;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
st.insert(mp(a[i], i));
L[i] = i - 1;
R[i] = i + 1;
}
if(n <= 2000) {
for(int i = 0; i <= n; ++i) fill(dp[i], dp[i] + n + 1, (ll)-1e18);
dp[0][0] = 0;
dp[1][0] = 0;
dp[1][1] = a[1];
for(int i = 2; i <= n; ++i) {
dp[i][0] = 0;
for(int j = 1; j <= (i + 1) / 2; ++j) dp[i][j] = max(dp[i - 1][j], dp[i - 2][j - 1] + a[i]);
}
for(int i = 1; i <= (n + 1) / 2; ++i) cout << dp[n][i] << '\n';
return;
}
ll ans = 0;
for(int cs = 1; cs <= (n + 1) / 2; ++cs) {
pair <ll, int> mx = *st.rbegin();
st.erase(mx);
ans += mx.f;
int i = mx.s;
int lf = L[i], rg = R[i];
if(lf) st.erase(mp(a[lf], lf));
if(rg <= n) st.erase(mp(a[rg], rg));
if(lf) L[i] = L[lf];
else L[i] = 0;
if(rg <= n) R[i] = R[rg];
else R[i] = n + 1;
if(L[i]) R[L[i]] = i;
if(R[i] <= n) L[R[i]] = i;
a[i] = a[lf] + a[rg] - a[i];
st.insert(mp(a[i], i));
cout << ans << "\n";
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int CNT_TESTS = 1;
///cin >> CNT_TESTS;
for(int NUMCASE = 1; NUMCASE <= CNT_TESTS; ++NUMCASE) {
solve();
if(NUMCASE != CNT_TESTS) cout << '\n';
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |