This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define replr(i, a, b) for (int i = int(a); i <= int(b); ++i)
#define reprl(i, a, b) for (int i = int(a); i >= int(b); --i)
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define mkp(a, b) make_pair(a, b)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<PII> VPI;
typedef vector<VI> VVI;
typedef vector<VVI> VVVI;
typedef vector<VPI> VVPI;
typedef pair<ll, ll> PLL;
typedef vector<ll> VL;
typedef vector<PLL> VPL;
typedef vector<VL> VVL;
typedef vector<VVL> VVVL;
typedef vector<VPL> VVPL;
template<class T> T setmax(T& a, T b) {if (a < b) return a = b; return a;}
template<class T> T setmin(T& a, T b) {if (a < b) return a; return a = b;}
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
template<class T>
using indset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#include "fish.h"
#define solvefor(a) if (a::check(N, M, X, Y, W)) return a::solve(N, M, X, Y, W);
namespace TEST1 {
bool check(int N, int M, VI X, VI Y, VI W) {
for (int x : X) if (x & 1) return false;
return true;
}
ll solve(int N, int M, VI X, VI Y, VI W) {
ll ans = 0;
for (int x : W) ans += x;
return ans;
}
};
namespace TEST2 {
bool check(int N, int M, VI X, VI Y, VI W) {
for (int x : X) if (x > 1) return false;
return true;
}
struct POINT {
int x, y, w;
};
ll solve(int N, int M, VI X, VI Y, VI W) {
if (N == 2) {
ll sum[2]{};
rep(i, M) sum[X[i]] += W[i];
return max(sum[0], sum[1]);
}
vector<POINT> vec;
rep(i, M) vec.pb({X[i], Y[i], W[i]});
int n = vec.size();
sort(all(vec), [](const POINT& a, const POINT& b){return make_tuple(a.y, -a.x, a.w) < make_tuple(b.y, -b.x, b.w);});
ll sum = 0;
rep(i, n) if (vec[i].x == 1) sum += vec[i].w;
ll ans = sum;
rep(i, n) {
if (vec[i].x == 1) sum -= vec[i].w;
else sum += vec[i].w;
setmax(ans, sum);
}
return ans;
}
};
namespace TEST3 {
bool check(int N, int M, VI X, VI Y, VI W) {
for (int x : Y) if (x != 0) return false;
return true;
}
ll solve(int N, int M, VI X, VI Y, VI W) {
int n = N+3;
VL a(n+2);
rep(i, M) a[X[i]+3] = W[i];
VL dp(n);
replr(i, 3, n-1) {
dp[i] = dp[i-1];
setmax(dp[i], dp[i-3] + a[i-1] + a[i+1]);
setmax(dp[i], dp[i-2] + a[i+1]);
setmax(dp[i], dp[i-1] - a[i] + a[i+1]);
}
return dp[n-1];
}
};
namespace TEST4 {
ll solve(int N, int M, VI X, VI Y, VI W) {
int n = N+3;
int m = min(2, N);
VVL a(n+1, VL(m));
rep(i, M) a[X[i]+3][Y[i]] = W[i];
VVL dpvar(n, VL(m)); // mec, poqr
VVL dpver(n, VL(m)); // poqr, mec
VL dp2(n);
replr(i, 3, n-1) {
replr(k, 0, m-1) {
replr(prevk, 0, k) {
ll sum = 0;
replr(l, 0, k) {
sum += a[i+1][l];
if (l <= prevk) sum -= a[i][l];
else sum += a[i-1][l];
}
setmax(dpver[i][k], dpver[i-1][prevk] + sum);
}
replr(prevk, k, m-1) {
ll sum = 0;
replr(l, 0, k) {
sum += a[i+1][l];
if (l <= k) sum -= a[i][l];
else sum += a[i-1][l];
}
setmax(dpvar[i][k], dpvar[i-1][prevk] + sum);
setmax(dpvar[i][k], dpver[i-1][prevk] + sum);
}
replr(prevk, 0, m-1) {
ll sum = 0;
replr(l, 0, k) {
sum += a[i+1][l];
}
setmax(dpver[i][k], dpvar[i-2][prevk] + sum);
setmax(dpver[i][k], dpver[i-2][prevk] + sum);
}
ll sum = 0;
replr(l, 0, k) {
sum += a[i-1][l];
sum += a[i+1][l];
}
setmax(dpver[i][k], dp2[i-3] + sum);
setmax(dp2[i], dpver[i][k]);
setmax(dp2[i], dpvar[i][k]);
setmax(dp2[i], dp2[i-1]);
}
}
/*cout << "dpver" << endl;*/
/*reprl(j, m-1, 0) {*/
/* replr(i, 3, n-1) cout << dpver[i][j] << " ";*/
/* cout << endl;*/
/*}*/
/*cout << endl;*/
/*cout << "dpvar" << endl;*/
/*reprl(j, m-1, 0) {*/
/* replr(i, 3, n-1) cout << dpvar[i][j] << " ";*/
/* cout << endl;*/
/*}*/
/*cout << endl;*/
return dp2[n-1];
}
};
mt19937 rng;
void stresstest() {
rng = mt19937(chrono::steady_clock().now().time_since_epoch().count());
int t = 20;
while (t--) {
int N, M;
VI X, Y, W;
N = 50;
M = 10;
X = Y = W = VI(M);
for (int& x : X) x = rng()%N;
for (int& x : W) x = rng()%10+1;
int ans1 = TEST3::solve(N, M, X, Y, W);
int ans2 = TEST4::solve(N, M, X, Y, W);
if (ans1 != ans2) {
cout << N << " " << M << endl;
rep(i, M) cout << X[i] << " " << Y[i] << " " << W[i] << endl;
cout << "answers" << endl;
cout << ans1 << " " << ans2 << endl;
abort();
}
}
}
ll max_weights(int N, int M, VI X, VI Y, VI W) {
return TEST4::solve(N, M, X, Y, W);
solvefor(TEST1);
solvefor(TEST2);
solvefor(TEST3);
return -1;
}
# | 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... |