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 "fish.h"
#include <bits/stdc++.h>
using namespace std;
long long max_weights(int n,int m,vector<int>x,vector<int>y,vector<int>w){
int mx = *max_element(y.begin(),y.end());
if(mx <= 8){
long long dp[n+1][mx+2][mx+2]{};
int sum[n+1][mx+2]{};
for(int i = 0; i < m; i++) x[i] += 1;
for(int i = 0; i < m; i++) y[i] += 1;
for(int i = 0; i < m; i++) sum[x[i]][y[i]] += w[i];
long long ans = 0;
for(int i = 0; i < 1; i++){
for(int j = 0; j < mx+2; j++){
for(int k = 0; k < mx+2; k++){
dp[i][j][k] = -1e18;
}
}
}
dp[0][0][0] = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j < mx+2; j++){
for(int k = 0; k < mx+2; k++){
for(int v = 0; v < mx+2; v++){
long long now = dp[i-1][k][j];
int cur = v;
while(cur > max(j,k)) now += sum[i-1][cur--];
cur = k;
while(cur > v) now += sum[i][cur--];
dp[i][v][k] = max(dp[i][v][k],now);
ans = max(ans,now);
}
}
}
}
return ans;
}
mx = *max_element(x.begin(),x.end());
if(mx <= 1){
long long ans = 0;
long long ans2 = 0;
int cost[n][2]{};
for(int i = 0; i < m; i++){
if(x[i] == 1){cost[y[i]][1] = w[i]; ans += w[i];}
if(x[i] == 0){ans2 += w[i];cost[y[i]][0]=w[i];}
}
if(n == 2 || n == 1) return max(ans,ans2);
ans2 = max(ans,ans2);
for(int i = 1; i < n; i++){
ans -= cost[i-1][1];
ans += cost[i-1][0];
ans2 = max(ans2,ans);
}
return ans2;
}else{
bool z = 1;
for(int i = 0; i < n; i++){
z = (z && (x[i] % 2 == 0));
}
if(z){
long long sum = 0;
for(int i = 0; i < m; i++) sum += w[i];
return sum;
}
else{
long long dp[n+1][4][4]{};
vector<pair<int,int>>to[n+1];
for(int i = 0; i < m; i++){to[x[i]+1].push_back({y[i]+1,w[i]});}
for(int i = 0; i <= n; i++){
to[i].push_back({0,0});
to[i].push_back({n,0});
sort(to[i].begin(), to[i].end());
}
for(int i = 0; i < to[0].size(); i++){
for(int j = 0; j < to[0].size(); j++){
dp[0][i][j] = -1e18;
}
}
dp[0][0][0] = 0;
long long ans = 0;
for(int i = 1; i <= n; i++){
for(int j = 0; j < to[max(i-2,0)].size(); j++){
for(int k = 0; k < to[i-1].size(); k++){
for(int v = 0; v < to[i].size(); v++){
long long now = dp[i-1][k][j];
int cur = to[i][v].first;
for(auto nodes : to[i-1]){
if(cur > max(to[i-2][j].first,to[i-1][k].first)){
if(nodes.first > max(to[i-2][j].first,to[i-1][k].first)){
now += nodes.second;
}
}
}
cur = to[i-1][k].first;
for(auto nodes : to[i]){
if(cur > to[i][v].first){
if(nodes.first > to[i][v].first){
now += nodes.second;
}
}
}
dp[i][v][k] = max(dp[i][v][k],now);
ans = max(ans,now);
}
}
}
}
return ans;
}
}
}
// int main(){
// cout << max_weights(5, 4, {0, 1, 4, 3}, {2, 1, 4, 3}, {5, 2, 1, 3}) << '\n';
// }
Compilation message (stderr)
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:82:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
82 | for(int i = 0; i < to[0].size(); i++){
| ~~^~~~~~~~~~~~~~
fish.cpp:83:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for(int j = 0; j < to[0].size(); j++){
| ~~^~~~~~~~~~~~~~
fish.cpp:90:34: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
90 | for(int j = 0; j < to[max(i-2,0)].size(); j++){
| ~~^~~~~~~~~~~~~~~~~~~~~~~
fish.cpp:91:38: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for(int k = 0; k < to[i-1].size(); k++){
| ~~^~~~~~~~~~~~~~~~
fish.cpp:92:42: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for(int v = 0; v < to[i].size(); v++){
| ~~^~~~~~~~~~~~~~
# | 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... |