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 DBG 1
// chrono::system_clock::now().time_since_epoch().count()
#include<bits/stdc++.h>
//#include<ext/pb_ds/tree_policy.hpp>
//#include<ext/pb_ds/assoc_container.hpp>
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define debug(x) if (DBG) cerr << #x << " = " << x << endl;
using namespace std;
//using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
//template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const int MAXN = (int)2e5 + 5;
vector<int> pos[MAXN];
vector<pii> stars[MAXN];
ll dp[MAXN], ext[MAXN];
ll fenw[MAXN];
int par[MAXN], L[MAXN], R[MAXN], sz[MAXN];
int arr[MAXN];
bool active[MAXN];
ll sum;
int n, m;
void fenwUpd(int p, ll x) {
    for (; p <= n; p |= (p + 1)) {
        fenw[p] += x;
    }
}
void update(int l, int r, ll x) {
    fenwUpd(l, x);
    fenwUpd(r + 1, -x);
}
ll getVal(int p) {
    ll ret = 0;
    
    for (; p > 0; --p) {
        ret += fenw[p];
        p &= (p + 1);
    }
    
    return ret;
}
int getPar(int x) {
    if (x == par[x]) {
        return x;
    }
    
    return par[x] = getPar(par[x]);
}
void uni(int a, int b) {
    a = getPar(a);
    b = getPar(b);
    
    if (a == b) {
        return;
    }
    
    if (sz[a] < sz[b]) {
        swap(a, b);
    }
    
    dp[a] += ext[a];
    ext[a] = 0;
    dp[b] += ext[b];
    ext[b] = 0;
    update(L[a], R[a], dp[b]);
    update(L[b], R[b], dp[a]);
    dp[a] += dp[b];
    sz[a] += sz[b];
    par[b] = a;
    L[a] = min(L[a], L[b]);
    R[a] = max(R[a], R[b]);
}
void solve() {
	scanf("%d", &n);
    
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &arr[i]);
        pos[arr[i]].pb(i);
    }
    
    scanf("%d", &m);
    
    for (int i = 1; i <= m; ++i) {
        int x, y, c;
        scanf("%d %d %d", &x, &y, &c);
        stars[y].pb(mp(x, c));
        sum += c;
    }
    
    for (int i = 1; i <= n; ++i) {
        par[i] = L[i] = R[i] = i;
    }
    
    for (int y = 1; y <= n; ++y) {
        for (auto it : stars[y]) {
            int x, c;
            tie(x, c) = it;
            int p = getPar(x);
            ext[p] = max(ext[p], c + getVal(x) - dp[p]);
        }
        for (int x : pos[y]) {
            active[x] = 1;
        
            if (active[x - 1]) {
                uni(x - 1, x);
            }
            
            if (active[x + 1]) {
                uni(x, x + 1);
            }
        }
    }
    
    ll ans = 0;
    
    for (int i = 1; i <= n; ++i) {
        if (getPar(i) == i) {
            ans = max(ans, dp[i] + ext[i]);
        }
    }
    
    printf("%lld\n", sum - ans);
}
int main() {
	int tt = 1;
	
	while (tt--) {
		solve();
	}
	return 0;
}
Compilation message (stderr)
constellation3.cpp: In function 'void solve()':
constellation3.cpp:91:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d", &n);
  ~~~~~^~~~~~~~~~
constellation3.cpp:94:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &arr[i]);
         ~~~~~^~~~~~~~~~~~~~~
constellation3.cpp:98:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &m);
     ~~~~~^~~~~~~~~~
constellation3.cpp:102:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d %d", &x, &y, &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... |