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;
#define OPTM ios_base::sync_with_stdio(0); cin.tie(0);
#define INF int(1e9+7)
#define ln '\n'
#define ll long long
#define ull unsigned long long
#define ui unsigned int
#define us unsigned short
#define FOR(i,s,n) for (int i = s; i < n; i++)
#define FORR(i,n,s) for (int i = n; i > s; i--)
#define FORX(u, arr) for (auto u : arr)
#define PB push_back
#define in(v,x) (v.find(x) != v.end())
#define F first
#define S second
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define UM unordered_map
#define US unordered_set
#define PQ priority_queue
#define ALL(v) v.begin(), v.end()
const ll LLINF = 1e18+1;
#define int long long
const int MAXN = 3e5+10;
int n,m;
UM<int,int> p[MAXN];
UM<int,int> dp[2], dp1[2], dp2, dp3;
int dp4, dp5;
vector<PII> v[MAXN], v2[MAXN];
long long max_weights(int32_t N, int32_t M, std::vector<int32_t> X, std::vector<int32_t> Y, std::vector<int32_t> W) {
n = N; m = M;
FOR(i,0,m) {
v[X[i]+1].PB({Y[i]+1,W[i]});
}
FOR(i,1,n+1) v[i].PB({0LL,0LL});
FOR(i,1,n+2) {
FORX(u,v[i]) v2[i].PB(u);
FORX(u,v[i+1]) v2[i].PB({u.F,0LL});
FORX(u,v[i-1]) v2[i].PB({u.F,0LL});
}
FOR(i,0,n+1) {
sort(ALL(v[i]));
sort(ALL(v2[i]));
}
FOR(i,1,n+2) {
int cur = 0LL;
FORX(u,v2[i]) {
p[i][u.F] = cur+u.S;
cur += u.S;
}
}
FORX(u,v2[1]) dp2[u.F] = -LLINF;
int cur3 = -LLINF, v2n = v2[1].size();
FORR(x,v2n-1,-1) {
int j = v2[1][x].F;
dp3[j] = max(cur3, p[2][j]);
cur3 = dp3[j];
}
FOR(i,2,n+1) {
FORX(u,v2[i]) {
int j = u.F;
dp[1][j] = dp2[j]+p[i-1][j];
dp1[1][j] = dp3[j]-p[i][j];
int zero = max(dp4, dp5+p[i-1][j]);
dp[1][j] = max(dp[1][j], zero);
dp1[1][j] = max(dp1[1][j], zero);
}
dp2.clear(); dp3.clear();
int cur2 = -LLINF;
dp4 = dp5 = 0LL;
FORX(u,v2[i]) {
int j = u.F;
dp2[j] = max(cur2, dp[1][j]-p[i][j]);
dp4 = max(dp4, max(dp[0][j],dp1[0][j])+p[i][j]);
dp5 = max(dp5, max(dp[0][j],dp1[0][j]));
cur2 = dp2[j];
}
int vn = v2[i].size();
int cur3 = -LLINF;
FORR(x,vn-1,-1) {
int j = v2[i][x].F;
dp3[j] = max(cur3, max(dp[1][j], dp1[1][j])+p[i+1][j]);
cur3 = dp3[j];
}
dp[0].clear(); dp1[0].clear();
FORX(u,dp[1]) dp[0].insert(u);
FORX(u,dp1[1]) dp1[0].insert(u);
dp[1].clear(); dp1[1].clear();
}
int res = -LLINF;
FORX(u,v2[n]) {
int j = u.F;
res = max(res, max(dp[0][j], dp1[0][j]));
}
return res;
}
# | 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... |