#include "fish.h"
// #include "grader.cpp"
#include<bits/stdc++.h>
#define int long long
#define vi vector<int>
#define pb push_back
#define f0r(i,n) for(int i = 0; i<n; i++)
#define FOR(i, k, n) for(int i = k; i<n; i++)
#define vout(v) for(auto u : v)cout<<u<<' '; cout<<'\n';
using namespace std;
vector<vector<int>>s;
int sum(int col, int l, int r){
if(l > r)return 0;
if(l == 0)return s[col][r];
else return s[col][r] - s[col][l-1];
}
int max_weights(signed n, signed m, std::vector<signed> x, std::vector<signed> y,
std::vector<signed> w) {
vector<vi>grid(n, vi(n));
f0r(i,m){
grid[x[i]][y[i]] = w[i];
}
vector<vi>s(n, vi(n));
f0r(i, n){
s[i][0] = grid[i][0];
FOR(j, 1, n)s[i][j] = s[i][j-1] + grid[i][j];
}
::s = s;
vector<vector<vi>>dp(n, vector<vi>(n+1, vi(2))); // index, prev, 0 = inc, 1 = dec, 0 goes in inc
// if currently increasing, prev must be increasing. if currently decreasing, prev can be anything.
vi zerotake(n); //for this dp[i][0][0], how many were already taken in the current column?
FOR(i, 1, n){
f0r(j,n+1){
f0r(k, 2){
if(k == 0){
if(j == 0){
//current one is empty
f0r(l, n+1){
f0r(o, 2)dp[i][j][k] = max(dp[i][j][k], dp[i-1][l][o] + sum(i, 0, l-1));
}
zerotake[i] = dp[i][j][k];
}
else{
//current one is increasing
f0r(l, j){
if(l != 0)dp[i][j][k] = max(dp[i][j][k], dp[i-1][l][0] + sum(i-1, l, j-1));
else dp[i][j][k] = max(dp[i][j][k], dp[i-1][l][0] + max(0LL, sum(i-1, l, j-1) - zerotake[i-1]));
}
}
}
else{
//current one is decreasing, previous length must be higher than current
FOR(l, j+1, n+1){
f0r(o,2)dp[i][j][k] = max(dp[i][j][k], dp[i-1][l][o] + sum(i, j, l-1));
}
}
}
}
}
/*
f0r(i,n+1){
f0r(j, 2){
cout<<dp[1][i][j]<<' ';
}
cout<<'\n';
}
*/
int ans = 0;
f0r(i,n+1)f0r(j,2)ans = max(ans, dp[n-1][i][j]);
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... |
# | 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... |