#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
using ll = long long;
template<class T>
struct RMQ {
vector<vector<T>> jmp;
RMQ(const vector<T>& V) {
int N = sz(V), on = 1, depth = 1;
while (on < sz(V)) on *= 2, depth++;
jmp.assign(depth, V);
rep(i,0,depth-1) rep(j,0,N)
jmp[i+1][j] = max(jmp[i][j],
jmp[i][min(N - 1, j + (1 << i))]);
}
T query(int a, int b) {
assert(a < b); // or return inf if a == b
int dep = 31 - __builtin_clz(b - a);
return max(jmp[dep][a], jmp[dep][b - (1 << dep)]);
}
};
RMQ<pair<int,int> > rmq({});
vector<pair<int,int> > a;
vector<ll> base_cost;
vector<multiset<pair<int, ll> > > cost;
vector<ll> no_star_cost;
int merge(int x, int y){
if(cost[x].size() < cost[y].size()) swap(x, y);
for(pair<int, ll> p : cost[y]){
cost[x].insert({p.first, p.second + no_star_cost[x] - no_star_cost[y]});
}
base_cost[x] += no_star_cost[y] + base_cost[y];
return x;
}
int solve(int l, int r, int cap){
int cur;
if(l + 1 == r){
cur = l;
} else {
pair<int,int> v = rmq.query(l, r);
int best = v.second;
int val = v.first;
cur = solve(best, best + 1, val);
if(l < best){
cur = merge(cur, solve(l, best, val));
}
if(best + 1 < r){
cur = merge(cur, solve(best + 1, r, val));
}
}
while(!cost[cur].empty() && (*cost[cur].begin()).first <= cap){
no_star_cost[cur] = max(no_star_cost[cur], (*cost[cur].begin()).second);
cost[cur].erase(cost[cur].begin());
}
return cur;
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n;
cin >> n;
a.resize(n);
base_cost.resize(n);
no_star_cost.resize(n);
cost.resize(n);
for(int i = 0; i < n; i++){
cin >> a[i].first;
a[i].second = i;
}
rmq = RMQ<pair<int,int> >(a);
int m;
cin >> m;
ll tot_cost = 0;
for(int i = 0; i < m; i++){
ll x, y, c;
cin >> x >> y >> c;
x--;
cost[x].insert({y, c});
tot_cost += c;
}
int z = solve(0, n, n+1);
cout << tot_cost - (no_star_cost[z] + base_cost[z]) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
768 KB |
Output is correct |
24 |
Correct |
6 ms |
768 KB |
Output is correct |
25 |
Correct |
6 ms |
768 KB |
Output is correct |
26 |
Correct |
6 ms |
768 KB |
Output is correct |
27 |
Correct |
6 ms |
768 KB |
Output is correct |
28 |
Correct |
6 ms |
768 KB |
Output is correct |
29 |
Correct |
6 ms |
768 KB |
Output is correct |
30 |
Correct |
6 ms |
768 KB |
Output is correct |
31 |
Correct |
6 ms |
768 KB |
Output is correct |
32 |
Correct |
6 ms |
896 KB |
Output is correct |
33 |
Correct |
7 ms |
896 KB |
Output is correct |
34 |
Correct |
6 ms |
896 KB |
Output is correct |
35 |
Correct |
6 ms |
896 KB |
Output is correct |
36 |
Correct |
6 ms |
1024 KB |
Output is correct |
37 |
Correct |
7 ms |
896 KB |
Output is correct |
38 |
Correct |
6 ms |
896 KB |
Output is correct |
39 |
Correct |
10 ms |
896 KB |
Output is correct |
40 |
Correct |
6 ms |
896 KB |
Output is correct |
41 |
Correct |
6 ms |
896 KB |
Output is correct |
42 |
Correct |
6 ms |
896 KB |
Output is correct |
43 |
Correct |
8 ms |
1152 KB |
Output is correct |
44 |
Correct |
6 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
10 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
6 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
6 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
768 KB |
Output is correct |
24 |
Correct |
6 ms |
768 KB |
Output is correct |
25 |
Correct |
6 ms |
768 KB |
Output is correct |
26 |
Correct |
6 ms |
768 KB |
Output is correct |
27 |
Correct |
6 ms |
768 KB |
Output is correct |
28 |
Correct |
6 ms |
768 KB |
Output is correct |
29 |
Correct |
6 ms |
768 KB |
Output is correct |
30 |
Correct |
6 ms |
768 KB |
Output is correct |
31 |
Correct |
6 ms |
768 KB |
Output is correct |
32 |
Correct |
6 ms |
896 KB |
Output is correct |
33 |
Correct |
7 ms |
896 KB |
Output is correct |
34 |
Correct |
6 ms |
896 KB |
Output is correct |
35 |
Correct |
6 ms |
896 KB |
Output is correct |
36 |
Correct |
6 ms |
1024 KB |
Output is correct |
37 |
Correct |
7 ms |
896 KB |
Output is correct |
38 |
Correct |
6 ms |
896 KB |
Output is correct |
39 |
Correct |
10 ms |
896 KB |
Output is correct |
40 |
Correct |
6 ms |
896 KB |
Output is correct |
41 |
Correct |
6 ms |
896 KB |
Output is correct |
42 |
Correct |
6 ms |
896 KB |
Output is correct |
43 |
Correct |
8 ms |
1152 KB |
Output is correct |
44 |
Correct |
6 ms |
896 KB |
Output is correct |
45 |
Correct |
220 ms |
56184 KB |
Output is correct |
46 |
Correct |
231 ms |
55416 KB |
Output is correct |
47 |
Correct |
227 ms |
55160 KB |
Output is correct |
48 |
Correct |
217 ms |
56056 KB |
Output is correct |
49 |
Correct |
223 ms |
54648 KB |
Output is correct |
50 |
Correct |
217 ms |
54460 KB |
Output is correct |
51 |
Correct |
213 ms |
54776 KB |
Output is correct |
52 |
Correct |
218 ms |
55800 KB |
Output is correct |
53 |
Correct |
219 ms |
55544 KB |
Output is correct |
54 |
Correct |
289 ms |
72312 KB |
Output is correct |
55 |
Correct |
314 ms |
69496 KB |
Output is correct |
56 |
Correct |
324 ms |
68344 KB |
Output is correct |
57 |
Correct |
318 ms |
67704 KB |
Output is correct |
58 |
Correct |
303 ms |
72312 KB |
Output is correct |
59 |
Correct |
297 ms |
72056 KB |
Output is correct |
60 |
Correct |
158 ms |
72520 KB |
Output is correct |
61 |
Correct |
319 ms |
82552 KB |
Output is correct |
62 |
Correct |
312 ms |
72056 KB |
Output is correct |
63 |
Correct |
306 ms |
77432 KB |
Output is correct |
64 |
Correct |
308 ms |
79096 KB |
Output is correct |
65 |
Correct |
316 ms |
72568 KB |
Output is correct |
66 |
Correct |
316 ms |
79992 KB |
Output is correct |