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>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
struct Data
{
	int x, y; ll c;
	int l, r;
};
int N, A[MAXN+10], M, S;
Data B[MAXN+10];
struct Node
{
	int y, l, r, p;
	vector<pll> V;
};
Node nodes[MAXN+10];
vector<int> adj[MAXN+10];
struct BIT
{
	int tree[MAXN+10];
	void update(int i, int v) { for(; i<=N; i+=(i&-i)) tree[i]=max(tree[i], v); }
	int query(int i) { int ret=0; for(; i>0; i-=(i&-i)) ret=max(ret, tree[i]); return ret; }
}bit;
struct SEG
{
	ll tree[MAXN*4+10];
	void update(int node, int tl, int tr, int l, int r, ll v)
	{
		if(r<tl || tr<l) return;
		if(l<=tl && tr<=r)
		{
			tree[node]+=v;
			return;
		}
		int mid=tl+tr>>1;
		update(node*2, tl, mid, l, r, v);
		update(node*2+1, mid+1, tr, l, r, v);
	}
	ll query(int node, int tl, int tr, int p)
	{
		if(tl==tr) return tree[node];
		int mid=tl+tr>>1;
		if(p<=mid) return tree[node]+query(node*2, tl, mid, p);
		else return tree[node]+query(node*2+1, mid+1, tr, p);
	}
}seg;
ll dp[MAXN+10];
void dfs(int now)
{
	int i, j;
	ll val=0;
	vector<int> L, R;
	for(int nxt : adj[now])
	{
		dfs(nxt);
		val+=dp[nxt];
		L.push_back(nodes[nxt].l);
		R.push_back(nodes[nxt].r);
	}
	dp[now]=val;
	for(i=0; i<nodes[now].V.size(); i++)
	{
		ll t=0;
		int x=nodes[now].V[i].first;
		int p=lower_bound(R.begin(), R.end(), x)-R.begin();
		if(p<adj[now].size() && L[p]<=x && x<=R[p]) t=val-dp[adj[now][p]]+seg.query(1, 1, N, x);
		else t=val;
		dp[now]=max(dp[now], t+nodes[now].V[i].second);
	}
	for(i=nodes[now].l, j=0; i<=nodes[now].r; i++)
	{
		for(; j<adj[now].size() && L[j]==i; j++) i=R[j]+1;
		if(i>nodes[now].r) continue;
		seg.update(1, 1, N, i, i, val);
	}
	for(int nxt : adj[now]) seg.update(1, 1, N, nodes[nxt].l, nodes[nxt].r, val-dp[nxt]);
}
int main()
{
	int i, j;
	ll sum=0;
	scanf("%d", &N);
	for(i=1; i<=N; i++) scanf("%d", &A[i]);
	scanf("%d", &M);
	for(i=1; i<=M; i++) scanf("%d%d%lld", &B[i].x, &B[i].y, &B[i].c), sum+=B[i].c;
	sort(B+1, B+M+1, [&](const Data &p, const Data &q) { return p.x<q.x; });
	A[0]=N+1; A[N+1]=N+1;
	vector<int> ST;
	ST.push_back(0);
	for(i=1, j=1; i<=N; i++)
	{
		while(!ST.empty() && A[ST.back()]<=A[i]) ST.pop_back();
		ST.push_back(i);
		for(; j<=M && B[j].x==i; j++)
		{
			int lo=0, hi=ST.size();
			while(lo+1<hi)
			{
				int mid=lo+hi>>1;
				if(A[ST[mid]]>=B[j].y) lo=mid;
				else hi=mid;
			}
			B[j].l=ST[lo]+1;
		}
	}
	ST.clear();
	ST.push_back(N+1);
	for(i=N, j=M; i>=1; i--)
	{
		while(!ST.empty() && A[ST.back()]<=A[i]) ST.pop_back();
		ST.push_back(i);
		for(; j>=1 && B[j].x==i; j--)
		{
			int lo=0, hi=ST.size();
			while(lo+1<hi)
			{
				int mid=lo+hi>>1;
				if(A[ST[mid]]>=B[j].y) lo=mid;
				else hi=mid;
			}
			B[j].r=ST[lo]-1;
		}
	}
	ST.clear();
	sort(B+1, B+M+1, [&](const Data &p, const Data &q)
	{
		if(p.y!=q.y) return p.y>q.y;
		return p.l<q.l;
	});
	S++;
	nodes[S].y=N+1;
	nodes[S].l=1;
	nodes[S].r=N;
	nodes[S].p=S;
	for(i=1; i<=M; i++)
	{
		if(!(nodes[S].y==B[i].y && nodes[S].l==B[i].l && nodes[S].r==B[i].r))
		{
			S++;
			nodes[S].y=B[i].y;
			nodes[S].l=B[i].l;
			nodes[S].r=B[i].r;
			nodes[S].p=S;
		}
		nodes[S].V.push_back({B[i].x, B[i].c});
	}
	sort(nodes+1, nodes+S+1, [&](const Node &p, const Node &q)
	{
		if(p.l!=q.l) return p.l<q.l;
		if(p.r!=q.r) return p.r>q.r;
		return p.p<q.p;
	});
	for(i=1; i<=S; i++)
	{
		int par=bit.query(N-nodes[i].r+1);
		adj[par].push_back(nodes[i].p);
		bit.update(N-nodes[i].r+1, nodes[i].p);
	}
	sort(nodes+1, nodes+S+1, [&](const Node &p, const Node &q) { return p.p<q.p; });
	for(i=1; i<=S; i++)
	{
		sort(adj[i].begin(), adj[i].end(), [&](const int &p, const int &q) { return nodes[p].l<nodes[q].l; });
		sort(nodes[i].V.begin(), nodes[i].V.end());
	}
	dfs(1);
	printf("%lld\n", sum-dp[1]);
/*
	for(i=1; i<=S; i++)
	{
		printf("%d =========\n", i);
		printf("VAL : %d %d %d\n", nodes[i].y, nodes[i].l, nodes[i].r);
		printf("V : "); for(auto it : nodes[i].V) printf("%lld %lld / ", it.first, it.second); printf("\n");
		printf("ADJ : "); for(auto it : adj[i]) printf("%d ", it); printf("\n");
	}
*/
	//for(i=1; i<=M; i++) printf("%d %d %lld : %d %d\n", B[i].x, B[i].y, B[i].c, B[i].l, B[i].r);
}
Compilation message (stderr)
constellation3.cpp: In member function 'void SEG::update(int, int, int, int, int, ll)':
constellation3.cpp:47:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int mid=tl+tr>>1;
           ~~^~~
constellation3.cpp: In member function 'll SEG::query(int, int, int, int)':
constellation3.cpp:55:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   int mid=tl+tr>>1;
           ~~^~~
constellation3.cpp: In function 'void dfs(int)':
constellation3.cpp:78:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(i=0; i<nodes[now].V.size(); i++)
           ~^~~~~~~~~~~~~~~~~~~~
constellation3.cpp:84:7: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(p<adj[now].size() && L[p]<=x && x<=R[p]) t=val-dp[adj[now][p]]+seg.query(1, 1, N, x);
      ~^~~~~~~~~~~~~~~~
constellation3.cpp:91:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for(; j<adj[now].size() && L[j]==i; j++) i=R[j]+1;
         ~^~~~~~~~~~~~~~~~
constellation3.cpp: In function 'int main()':
constellation3.cpp:124:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid=lo+hi>>1;
             ~~^~~
constellation3.cpp:143:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
     int mid=lo+hi>>1;
             ~~^~~
constellation3.cpp:105:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &N);
  ~~~~~^~~~~~~~~~
constellation3.cpp:106:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=N; i++) scanf("%d", &A[i]);
                      ~~~~~^~~~~~~~~~~~~
constellation3.cpp:107:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &M);
  ~~~~~^~~~~~~~~~
constellation3.cpp:108:66: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(i=1; i<=M; i++) scanf("%d%d%lld", &B[i].x, &B[i].y, &B[i].c), sum+=B[i].c;
                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |