#include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <fstream>
#include <iterator>
#include <set>
#include <map>
#include <unordered_map>
#include <iomanip>
#include <cctype>
#include <string>
#include <cassert>
#include <set>
#include <bitset>
#include <unordered_set>
#include <numeric>
#define all(a) a.begin(), a.end()
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define pb push_back
#define ppi pair<int,pair<int,int>>
#define int int64_t
using namespace std;
// /\_/\
// (= ._.)
// / > \>
// encouraging cat
const int INF = 10000000000000000;
//const int mod = 1000000007;
const int mod = 998244353;
const int MAXN = 200005;
//ifstream fin('xor.in');
//ofstream fout('xor.out');
const int K = 25;
int n;
vector<set<vector<int>>> stars;
vector<int> a,val, dp, diff, taken;
int res = 0;
signed main()
{
cin >> n;
a.resize(n);
dp.resize(n + 1,0);
vector<pair<int,int>> b(n);
vector<pair<int,int>> lr(n, {-1,-1});
vector<int> pref(n,0), suf(n,0);
for (int i = 0;i < n;i++)
{
cin >> a[i];
b[i].first = a[i];
b[i].second = i;
}
sort(all(b));
//a.push_back(INF);
int M;
cin >> M;
stars.resize(n);
val.resize(M,0);
int sum = 0;
for (int i = 0;i < M;i++)
{
int x,y,c;
cin >> x >> y >> c;
x--;
stars[x].insert({y,c, i});
val[i] = c;
sum += c;
}
//solve(-1, n, INF);
vector<int> par(n + 1,0), had(n + 1,0);
iota(all(par), 0);
for (int i = 0;i < n;i++)
{
int pos = b[i].second;
int M = b[i].first;
int l = n;
int r = n;
int lb = pos - 1,rb = pos + 1;
if (pos > 0 && had[pos - 1])
{
lb -= had[pos - 1];
l = par[pos - 1];
if (stars[l].size() > stars[pos].size())
{
swap(stars[l], stars[pos]);
}
stars[pos].insert(all(stars[l]));
}
if (pos < n && had[pos + 1])
{
rb += had[pos + 1];
r = par[pos + 1];
if (stars[r].size() > stars[pos].size())
{
swap(stars[r], stars[pos]);
}
stars[pos].insert(all(stars[r]));
}
int limit = INF;
if (lb != -1)
{
if (a[lb] < limit)
{
limit = a[lb];
}
}
if (rb < n)
{
if (a[rb] < limit)
{
limit = a[rb];
}
}
had[lb + 1] = rb - lb - 1;
had[rb - 1] = rb - lb - 1;
par[lb + 1] = par[rb - 1] = pos;
int Lans = 0;
int Rans = 0;
Lans = dp[l];
Rans = dp[r];
int ad = 0;
int s = 0;
for (auto &x : stars[pos])
{
if (x[0] > limit)
{
break;
}
s += val[x[2]];
ad = max(ad, val[x[2]]);
}
int high = 0;
for (auto &x: stars[pos])
{
if (x[0] <= limit)
{
continue;
}
high = max(high, val[x[2]]);
val[x[2]] -= ad;
}
res = max(res, max(Lans + Rans + high, Lans + Rans + ad));
dp[pos] = Lans + Rans + ad;
auto it = stars[pos].lower_bound({limit, INF, INF});
stars[pos].erase(stars[pos].begin(), it);
}
cout << sum - res << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |