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>
#include "homecoming.h"
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int NN = 4000000 + 10;
const int MOD = 1000000007;
const int LOG = 20;
const ll INF = 1000000000000000010;
const int delta = 11353;
ll dp[NN][2], sm[NN], ps[NN];
int n, k, A[NN], B[NN];
ll solve(int N, int K, int a[], int b[]){
ps[0] = 0;
n = N;
for (int i = 0; i < n; i++) A[i] = a[i], B[i] = b[i];
for (int i = n; i < 2 * n; i++) B[i] = B[i - n];
for (int i = 1; i <= 2 * n; i++){
ps[i] = ps[i - 1] + B[i - 1];
// cout << i << ' ' << ps[i] << '\n';
}
if (K == n){
ll ans = 0;
ll Sum = 0;
for (int i = 0; i < n; i++) Sum += A[i], Sum -= B[i];
return max(ans, Sum);
}
for (int i = 0; i < n; i++)sm[i] = ps[i + 1 + K - 1] - ps[i];
// for (int i = 0; i < n; i++) cout << i << ' ' << sm[i] << '\n';
ll ans = 0;
ll Sum = 0;
for (int i = 0; i < n; i++) Sum += A[i], Sum -= B[i];
ans = max(ans, Sum);
for (int i = 0; i < n; i++) for (int j = 0; j < 2; j++) dp[i][j] = -INF;
dp[n - 1][0] = 0, dp[n - 1][1] = A[n - 1] - B[n - 1];
//cout << dp[n - 1][0] << ' ' << dp[n - 1][1] << '\n';
for (int i = n - 2; i >= 0; i--){
dp[i][0] = max(dp[i + 1][1], dp[i + 1][0]);
dp[i][1] = max(dp[i + 1][0] + A[i] - sm[i], dp[i + 1][1] + A[i] - B[i]);
//cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << '\n';
}
Sum = A[0] - sm[0];
for (int i = 1; i < n; i++){
ans = max(ans, dp[i][0] + Sum);
Sum += sm[i - 1] - B[i - 1];
Sum += A[i] - sm[i];
}
//cout << ans << '\n';
for (int i = 0; i < n; i++) for (int j = 0; j < 2; j++) dp[i][j] = -INF;
dp[n - 1][0] = 0, dp[n - 1][1] = A[n - 1] - sm[n - 1];
for (int i = n - 2; i >= 0; i--){
dp[i][0] = max(dp[i + 1][0], dp[i + 1][1]);
dp[i][1] = max(dp[i + 1][0] + A[i] - sm[i], dp[i + 1][1] + A[i] - B[i]);
//cout << i << ' ' << dp[i][0] << ' ' << dp[i][1] << '\n';
}
ans = max(ans, dp[0][0]);
return ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |