Submission #430753

#TimeUsernameProblemLanguageResultExecution timeMemory
430753Kevin_Zhang_TWConstellation 3 (JOI20_constellation3)C++17
35 / 100
166 ms38980 KiB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
// My bug list :
// integer overflow
// 0based, 1based forgotten
// index out of bound
// n, m, i, j typo
// some cases are missing

const int MAX_N = 200010, MAX_M = 2005;

int n, h[MAX_N], m;
//           y, c
vector<pair<int,int>> star[MAX_N];

//ll dp[MAX_M][MAX_M];

map<int,ll> dp[MAX_N];

ll tg[MAX_N];

// merge to b
void merge_dp(int a, int b) {
	int H = h[b];

	ll alow = 0, blow = 0;
	while (dp[a].size() && dp[a].begin()->first <= H) {
		chmax(alow, dp[a].begin()->second + tg[a]);
		dp[a].erase(dp[a].begin());
	}
	while (dp[b].size() && dp[b].begin()->first <= H) {
		chmax(blow, dp[b].begin()->second + tg[b]);
		dp[b].erase(dp[b].begin());
	}

	tg[a] += blow, tg[b] += alow;

	if (dp[a].size() > dp[b].size()) {
		swap(dp[a], dp[b]), swap(tg[a], tg[b]);
	}
	for (auto [k, w] : dp[a]) {
		chmax(dp[b][k], w + tg[a] - tg[b]);
	}
//	for (auto &[k, w] : dp[a]) w += blow;
//	for (auto &[k, w] : dp[b]) w += alow;
//
//	for (auto [k, w] : dp[a])
//		chmax(dp[b][k], w);
//		//chmax(dp[b][k], w + tg[a] - tg[b]);
	
	dp[b][0] = alow + blow - tg[b];
}

int dfs(int l, int r) {
	if (l > r) return n + 1;
	if (l == r) {
		for (auto [y, c] : star[l])
			dp[l][y] = c;
		return l;
	}
	int id = l;
	for (int i = l;i <= r;++i)
		if (h[i] > h[id]) id = i;

	int ld = dfs(l, id-1), rd = dfs(id+1, r);

	for (auto [y, c] : star[id])
		dp[id][y] = c;

	merge_dp(ld, id);
	merge_dp(rd, id);

	return id;
}

ll solve() {
	int rt = dfs(1, n);

	ll sum = 0;
	for (int i = 1;i <= n;++i)
		for (auto [y, c] : star[i])
			sum += c;

	ll mxg = 0;
	for (auto [_, w] : dp[rt])
		chmax(mxg, w + tg[rt]);

	return sum - mxg;
}
int32_t main() {
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1;i <= n;++i)
		cin >> h[i];
	cin >> m;
	for (int x, y, c, i = 0;i < m;++i) {
		cin >> x >> y >> c;
		star[x].pb(y, c);
	}

	assert(n < MAX_M);

	cout << solve() << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...