# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1160367 | InvMOD | Holiday (IOI14_holiday) | C++20 | 0 ms | 0 KiB |
#include<bits/stdc++.h>
#define name "InvMOD"
#ifndef name
#include "holiday.h"
#endif // name
using namespace std;
#define all(v) (v).begin(), (v).end()
using ll = long long;
template<typename T> bool ckmx(T& a, const T& b){
if(a < b)
return a = b, true;
return false;
}
const int N = 1e5 + 5;
const ll INF = 1e15;
ll findMaxAttraction(int n, int start, int d, int attraction[]){
start = start + 1;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++){
a[i] = attraction[i - 1];
}
ll answer = 0;
auto calc_dp = [&]() -> void{
vector<vector<vector<ll>>> dp_Up(2, vector<vector<ll>>(d + 1, vector<ll>(n + 1, -INF)));
vector<vector<vector<ll>>> pdp_Up(2, vector<vector<ll>>(d + 1, vector<ll>(n + 1, -INF)));
dp_Up[0][0][start] = pdp_Up[0][0][start] = 0;
for(int day = 1; day <= d; day++){
for(int i = start; i <= n; i++){
if(dp_Up[0][(day - 1) & 1][i] != -INF)
dp_Up[1][(day & 1)][i] = dp_Up[0][(day - 1) & 1][i] + a[i];
if(i > 1) ckmx(dp_Up[0][(day & 1)][i], dp_Up[0][(day - 1) & 1][i - 1]);
if(i > 1) ckmx(dp_Up[0][(day & 1)][i], dp_Up[1][(day - 1) & 1][i - 1]);
}
pdp_Up[0][(day & 1)] = dp_Up[0][(day & 1)];
pdp_Up[1][(day & 1)] = dp_Up[1][(day & 1)];
for(int i = n - 1; i >= start; i--){
ckmx(pdp_Up[0][(day & 1)][i], pdp_Up[0][(day - 1) & 1][i + 1]);
ckmx(pdp_Up[1][(day & 1)][i], pdp_Up[1][(day - 1) & 1][i + 1]);
}
for(int i = start - 1; i >= 1; i--){
if(pdp_Up[0][(day - 1) & 1][i] != -INF)
pdp_Up[1][(day & 1)][i] = pdp_Up[0][(day - 1) & 1][i] + a[i];
if(i < n) ckmx(pdp_Up[0][(day & 1)][i], pdp_Up[0][(day - 1) & 1][i + 1]);
if(i < n) ckmx(pdp_Up[0][(day & 1)][i], pdp_Up[1][(day - 1) & 1][i + 1]);
}
for(int i = 1; i <= n; i++){
ckmx(answer, pdp_Up[0][(day & 1)][i]);
ckmx(answer, pdp_Up[1][(day & 1)][i]);
}
}
};
calc_dp();
reverse(1 + all(a));
start = n - start + 1;
calc_dp();
return answer;
}
#ifdef name
int32_t main(){
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int n,start,d; cin >> n >> start >> d;
int attraction[n];
for(int i = 0; i < n; i++){
cin >> attraction[i];
}
cout << findMaxAttraction(n, start, d, attraction) << "\n";
return 0;
}
#endif // name