/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 18;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
if(b<0) return 0;
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
ll n, m, sum, ans, tim, dp[N], s[N], a[N], tin[N], tout[N], fen[N];
ll mx[lg][N], h[N];
vector<int> adj[N];
vector<pll> vec[N];
void update(ll i, ll val) {
while(i < N+10) {
fen[i] += val; i += (i&(-i));
}
}
ll query(ll i) {
ll res = 0;
while(i > 0) {
res += fen[i]; i -= (i&(-i));
}
return res;
}
int rot=0;
vector<int> st;
void init(int v) {
tin[v] = ++tim;
h[v] = h[mx[0][v]] + 1;
for(int i=1; i<lg; i++) mx[i][v] = mx[i-1][mx[i-1][v]];
for(auto u : adj[v]) {
init(u);
}
tout[v] = tim+1;
}
ll smc[N];
void dfs(int v) {
for(int u : adj[v]) {
dfs(u);
smc[v] += dp[u];
}
for(int i=0; size(adj[v])==2&&i<2; i++) {
update(tin[adj[v][i]], dp[adj[v][1-i]]);
update(tout[adj[v][i]], -dp[adj[v][1-i]]);
}
dp[v] = smc[v];
for(auto it : vec[v]) {
dp[v] = max(dp[v], smc[it.F] + query(tin[it.F]) + it.S);
}
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>n;
for(int i=1; i<=n; i++) {
cin>>a[i];
int lst = 0;
while(size(st) > 0 && a[st.back()] < a[i]) {
lst = st.back();
st.pop_back();
}
if(size(st) > 0) mx[0][i] = st.back();
else mx[0][i] = i, rot = i;
if(lst > 0) mx[0][lst] = i;
st.pb(i);
}
for(int i=1; i<=n; i++) {
if(mx[0][i] != i) adj[mx[0][i]].pb(i);
}
init(rot);
cin>>m;
for(ll c,x,y,i=1; i<=m; i++) {
cin>>x>>y>>c; sum+=c;
int id = x;
for(int j=lg-1; j>=0; j--) {
if(a[mx[j][id]] < y) id = mx[j][id];
}
vec[id].pb({x, c});
}
dfs(rot);
cout<<sum-dp[rot]<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
59992 KB |
Output is correct |
2 |
Correct |
9 ms |
62040 KB |
Output is correct |
3 |
Correct |
10 ms |
59928 KB |
Output is correct |
4 |
Correct |
8 ms |
59996 KB |
Output is correct |
5 |
Correct |
8 ms |
59996 KB |
Output is correct |
6 |
Correct |
8 ms |
59996 KB |
Output is correct |
7 |
Correct |
8 ms |
59996 KB |
Output is correct |
8 |
Correct |
8 ms |
59932 KB |
Output is correct |
9 |
Correct |
8 ms |
59996 KB |
Output is correct |
10 |
Correct |
8 ms |
59992 KB |
Output is correct |
11 |
Correct |
8 ms |
59996 KB |
Output is correct |
12 |
Correct |
8 ms |
59832 KB |
Output is correct |
13 |
Correct |
9 ms |
59996 KB |
Output is correct |
14 |
Correct |
8 ms |
59892 KB |
Output is correct |
15 |
Correct |
8 ms |
59996 KB |
Output is correct |
16 |
Correct |
10 ms |
59996 KB |
Output is correct |
17 |
Correct |
8 ms |
59996 KB |
Output is correct |
18 |
Correct |
8 ms |
59884 KB |
Output is correct |
19 |
Correct |
8 ms |
59996 KB |
Output is correct |
20 |
Correct |
8 ms |
59996 KB |
Output is correct |
21 |
Correct |
9 ms |
62044 KB |
Output is correct |
22 |
Correct |
8 ms |
62044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
59992 KB |
Output is correct |
2 |
Correct |
9 ms |
62040 KB |
Output is correct |
3 |
Correct |
10 ms |
59928 KB |
Output is correct |
4 |
Correct |
8 ms |
59996 KB |
Output is correct |
5 |
Correct |
8 ms |
59996 KB |
Output is correct |
6 |
Correct |
8 ms |
59996 KB |
Output is correct |
7 |
Correct |
8 ms |
59996 KB |
Output is correct |
8 |
Correct |
8 ms |
59932 KB |
Output is correct |
9 |
Correct |
8 ms |
59996 KB |
Output is correct |
10 |
Correct |
8 ms |
59992 KB |
Output is correct |
11 |
Correct |
8 ms |
59996 KB |
Output is correct |
12 |
Correct |
8 ms |
59832 KB |
Output is correct |
13 |
Correct |
9 ms |
59996 KB |
Output is correct |
14 |
Correct |
8 ms |
59892 KB |
Output is correct |
15 |
Correct |
8 ms |
59996 KB |
Output is correct |
16 |
Correct |
10 ms |
59996 KB |
Output is correct |
17 |
Correct |
8 ms |
59996 KB |
Output is correct |
18 |
Correct |
8 ms |
59884 KB |
Output is correct |
19 |
Correct |
8 ms |
59996 KB |
Output is correct |
20 |
Correct |
8 ms |
59996 KB |
Output is correct |
21 |
Correct |
9 ms |
62044 KB |
Output is correct |
22 |
Correct |
8 ms |
62044 KB |
Output is correct |
23 |
Correct |
9 ms |
59996 KB |
Output is correct |
24 |
Correct |
9 ms |
62096 KB |
Output is correct |
25 |
Correct |
12 ms |
60096 KB |
Output is correct |
26 |
Correct |
9 ms |
59996 KB |
Output is correct |
27 |
Correct |
9 ms |
62044 KB |
Output is correct |
28 |
Correct |
9 ms |
62044 KB |
Output is correct |
29 |
Correct |
9 ms |
59996 KB |
Output is correct |
30 |
Correct |
11 ms |
59996 KB |
Output is correct |
31 |
Correct |
10 ms |
62044 KB |
Output is correct |
32 |
Correct |
9 ms |
62296 KB |
Output is correct |
33 |
Correct |
9 ms |
59996 KB |
Output is correct |
34 |
Correct |
9 ms |
60224 KB |
Output is correct |
35 |
Correct |
9 ms |
60080 KB |
Output is correct |
36 |
Correct |
9 ms |
60140 KB |
Output is correct |
37 |
Correct |
9 ms |
60252 KB |
Output is correct |
38 |
Correct |
9 ms |
60252 KB |
Output is correct |
39 |
Correct |
11 ms |
62040 KB |
Output is correct |
40 |
Correct |
9 ms |
60252 KB |
Output is correct |
41 |
Correct |
11 ms |
59992 KB |
Output is correct |
42 |
Correct |
10 ms |
62044 KB |
Output is correct |
43 |
Correct |
9 ms |
60252 KB |
Output is correct |
44 |
Correct |
9 ms |
62044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
59992 KB |
Output is correct |
2 |
Correct |
9 ms |
62040 KB |
Output is correct |
3 |
Correct |
10 ms |
59928 KB |
Output is correct |
4 |
Correct |
8 ms |
59996 KB |
Output is correct |
5 |
Correct |
8 ms |
59996 KB |
Output is correct |
6 |
Correct |
8 ms |
59996 KB |
Output is correct |
7 |
Correct |
8 ms |
59996 KB |
Output is correct |
8 |
Correct |
8 ms |
59932 KB |
Output is correct |
9 |
Correct |
8 ms |
59996 KB |
Output is correct |
10 |
Correct |
8 ms |
59992 KB |
Output is correct |
11 |
Correct |
8 ms |
59996 KB |
Output is correct |
12 |
Correct |
8 ms |
59832 KB |
Output is correct |
13 |
Correct |
9 ms |
59996 KB |
Output is correct |
14 |
Correct |
8 ms |
59892 KB |
Output is correct |
15 |
Correct |
8 ms |
59996 KB |
Output is correct |
16 |
Correct |
10 ms |
59996 KB |
Output is correct |
17 |
Correct |
8 ms |
59996 KB |
Output is correct |
18 |
Correct |
8 ms |
59884 KB |
Output is correct |
19 |
Correct |
8 ms |
59996 KB |
Output is correct |
20 |
Correct |
8 ms |
59996 KB |
Output is correct |
21 |
Correct |
9 ms |
62044 KB |
Output is correct |
22 |
Correct |
8 ms |
62044 KB |
Output is correct |
23 |
Correct |
9 ms |
59996 KB |
Output is correct |
24 |
Correct |
9 ms |
62096 KB |
Output is correct |
25 |
Correct |
12 ms |
60096 KB |
Output is correct |
26 |
Correct |
9 ms |
59996 KB |
Output is correct |
27 |
Correct |
9 ms |
62044 KB |
Output is correct |
28 |
Correct |
9 ms |
62044 KB |
Output is correct |
29 |
Correct |
9 ms |
59996 KB |
Output is correct |
30 |
Correct |
11 ms |
59996 KB |
Output is correct |
31 |
Correct |
10 ms |
62044 KB |
Output is correct |
32 |
Correct |
9 ms |
62296 KB |
Output is correct |
33 |
Correct |
9 ms |
59996 KB |
Output is correct |
34 |
Correct |
9 ms |
60224 KB |
Output is correct |
35 |
Correct |
9 ms |
60080 KB |
Output is correct |
36 |
Correct |
9 ms |
60140 KB |
Output is correct |
37 |
Correct |
9 ms |
60252 KB |
Output is correct |
38 |
Correct |
9 ms |
60252 KB |
Output is correct |
39 |
Correct |
11 ms |
62040 KB |
Output is correct |
40 |
Correct |
9 ms |
60252 KB |
Output is correct |
41 |
Correct |
11 ms |
59992 KB |
Output is correct |
42 |
Correct |
10 ms |
62044 KB |
Output is correct |
43 |
Correct |
9 ms |
60252 KB |
Output is correct |
44 |
Correct |
9 ms |
62044 KB |
Output is correct |
45 |
Correct |
191 ms |
85924 KB |
Output is correct |
46 |
Correct |
184 ms |
85896 KB |
Output is correct |
47 |
Correct |
180 ms |
86100 KB |
Output is correct |
48 |
Correct |
186 ms |
85708 KB |
Output is correct |
49 |
Correct |
176 ms |
86352 KB |
Output is correct |
50 |
Correct |
171 ms |
86352 KB |
Output is correct |
51 |
Correct |
199 ms |
85608 KB |
Output is correct |
52 |
Correct |
199 ms |
86864 KB |
Output is correct |
53 |
Correct |
198 ms |
85904 KB |
Output is correct |
54 |
Correct |
347 ms |
96644 KB |
Output is correct |
55 |
Correct |
332 ms |
94336 KB |
Output is correct |
56 |
Correct |
334 ms |
91616 KB |
Output is correct |
57 |
Correct |
367 ms |
91292 KB |
Output is correct |
58 |
Correct |
220 ms |
95732 KB |
Output is correct |
59 |
Correct |
193 ms |
95432 KB |
Output is correct |
60 |
Correct |
101 ms |
102468 KB |
Output is correct |
61 |
Correct |
178 ms |
85276 KB |
Output is correct |
62 |
Correct |
380 ms |
100052 KB |
Output is correct |
63 |
Correct |
166 ms |
84952 KB |
Output is correct |
64 |
Correct |
165 ms |
85696 KB |
Output is correct |
65 |
Correct |
311 ms |
99536 KB |
Output is correct |
66 |
Correct |
160 ms |
84816 KB |
Output is correct |