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>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include "fish.h"
#define ll long long
#define ar array
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
 
const ll INF = 1e18;
template<typename T> bool ckmin(T &a, const T &b ) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b ) { return a < b ? a = b, 1 : 0; }
long long max_weights(int n, int m, std::vector<int> x, std::vector<int> y, std::vector<int> w) {
	vector<vector<ar<ll, 2>>> each(n);
	for (int i = 0; i < m; i++) {
		each[x[i]].push_back({y[i], w[i]});
		each[x[i]].push_back({y[i]-1, 0});
	}
	for (int i = 0; i < n; i++) each[i].push_back({n-1, 0}), each[i].push_back({-1, 0});
	for (int i = 0; i < n; i++) {
		sort(all(each[i]));
		vector<ar<ll, 2>> nw;
		ll s = 0;
		for (int j = 0; j < sz(each[i]); j++) {
			int r = j;
			while (r+1 < sz(each[i]) && each[i][r+1][0] == each[i][r][0]) r++;
			for (int k = j; k <= r; k++) s += each[i][k][1];
			nw.push_back({each[i][j][0], s});
			j = r;
		}
		swap(each[i], nw);
	}
	vector<ar<ll, 2>> dp;
	auto query = [&](int pos, int x) {
		return (*prev(upper_bound(all(each[pos]), ar<ll, 2>{x, LLONG_MAX})))[1];
	};
	vector<vector<ll>> l(n), r(n);
	for (int i = 0; i < n; i++) {
		l[i].resize(sz(each[i]));
		r[i].resize(sz(each[i]));
		for (int k = 0; k < sz(each[i]); k++) {
			if (i) l[i][k] = query(i - 1, each[i][k][0]);
			if (i+1 < n) r[i][k] = query(i + 1, each[i][k][0]);
		}
	}
	for (int i = 0; i < n; i++) {
		vector<ar<ll, 2>> dp2;
		const int N = sz(each[i]);
		dp2.resize(N+1, {-INF, -INF});
		if (!i) {
			dp2[0][0] = 0, dp2[N][1] = 0;
			swap(dp, dp2);
		}
		else {
			vector<ll> dp3(sz(dp)), dp4(sz(dp));
			for (int j = 0; j < sz(dp); j++) { 
				dp3[j] = max(dp[j][0] - (j ? each[i-1][j-1][1] : 0), j ? dp3[j-1] : -INF);
			}
			for (int j = sz(dp)-1; j >= 0; j--) {
				dp4[j] = max(j+1 < sz(dp) ? dp4[j+1] : -INF, dp[j][1] + (j ? r[i-1][j-1] : 0));
			}
			for (int j = 1; j <= N; j++) {
				int ans = 0;
				{
					int l = 0, r = sz(dp)-1;
					while (l <= r) {
						int mid = (l+r)/2;
						if (each[i][j-1][0] >= (mid ? each[i-1][mid-1][0] : -1)) ans = mid, l = mid+1;
						else r = mid-1;
					}
				}
				ckmax(dp2[j][j==N], max(dp3[ans] + l[i][j-1], dp[1][1]));
			}
			ckmax(dp2[0][0], dp[1][1]);
			for (int j = 1; j <= N; j++) {
				if (each[i][j-1][0] <= each[i-1][sz(dp)-2][0]) {
					int ans = sz(dp)-1;
					{
						int l = 0, r = sz(dp)-1;
						while (l <= r) {
							int mid = (l+r)/2;
							if (each[i][j-1][0] <= each[i-1][mid-1][0]) ans = mid, r = mid-1;
							else l = mid+1;
						}
					}
					ckmax(dp2[j][1], dp4[ans] - each[i][j-1][1]);
				}
			}
			swap(dp, dp2);
		}
	}
	ll ans = -INF;
	for (auto [x, y] : dp) ckmax(ans, x), ckmax(ans, y);
	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... |