# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
829264 | tolbi | Prisoner Challenge (IOI22_prison) | C++17 | 0 ms | 0 KiB |
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;
typedef long long ll;
#include "fish.h"
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
vector<vector<ll>> grid(N,vector<ll>(N,0));
for (int i = 0; i < M; ++i)
{
grid[X[i]][Y[i]]=W[i];
}
vector<vector<vector<ll>>> dp(N,vector<vector<ll>>(N,vector<ll>(3,-1)));
//0 decreasing
//1 increasing
//2 increasing, \
but forbidden to profit from back
function<ll(int,int,int)> f;
f = [&](int x, int y, int flag)->int{
if (dp[x][y][flag]!=-1) return dp[x][y][flag];
if (flag==1){
dp[x][y][flag]=0;
if (y+1<N){
ll crr = 0;
if (x>0) crr = grid[x-1][y+1];
dp[x][y][flag]=crr+f(x,y+1,1);
}
if (x+1<N){
dp[x][y][flag]=max(dp[x][y][flag],f(x+1,y,1));
dp[x][y][flag]=max(dp[x][y][flag],f(x+1,y,0));
}
}
else if (flag==0){
dp[x][y][flag]=0;
if (y-1>=0){
dp[x][y][flag]=grid[x][y]+f(x,y-1,0);
}
if (x+1<N){
dp[x][y][flag]=max(dp[x][y][flag],f(x+1,y,0));
if (y==0){
dp[x][y][flag]=max(dp[x][y][flag],f(x+1,y,2));
}
}
}
else {
dp[x][y][flag]=0;
if (y+1<N){
dp[x][y][flag]=f(x,y+1,2);
}
if (x+1<N){
dp[x][y][flag]=max(dp[x][y][flag],f(x+1,y,1));
dp[x][y][flag]=max(dp[x][y][flag],f(x+1,y,0));
}
}
return dp[x][y][flag];
};
return f(0,0,2);
}