#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#ifdef local
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define pary(a...) danb(#a, a)
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
std::cerr << "\033[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
std::cerr << (f++ ? ", " : "") << *L;
std::cerr << " ]\033[0m\n";
}
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#define pary(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
using namespace std;
using ll = long long;
using ld = long double;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
const int mod = 1000000007;
const int inf = 1e9;
const ll INF = 1e18;
const int maxn = 100025;
void chmax(ll &x, ll v) {
if (x < v) x = v;
}
bool alive[maxn];
int pa[maxn];
int anc(int x) { return x==pa[x] ? x : pa[x]=anc(pa[x]); }
vector<pair<int,ll>> top[maxn];
ll dp[maxn];
void join(int a, int b, int h) {
a = anc(a);
b = anc(b);
assert (a != b);
for (auto [y, val]: top[a]) {
if (y <= h)
chmax(dp[a], val);
}
for (auto [y, val]: top[b]) {
if (y <= h)
chmax(dp[b], val);
}
vector<pair<int,ll>> tmptop;
ll tmpdp = dp[b] + dp[a];
for (auto [y, val]: top[a]) {
if (y > h)
tmptop.emplace_back(y, val + dp[b]);
}
for (auto [y, val]: top[b]) {
if (y > h)
tmptop.emplace_back(y, val + dp[a]);
}
top[b] = tmptop;
dp[b] = tmpdp;
pa[a] = b;
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int n;
cin >> n;
iota(pa, pa+n, 0);
vector<int> A(n);
for (int i = 0; i < n; i++) {
cin >> A[i];
}
int m;
cin >> m;
ll tot = 0, ans = 0;
for (int i = 0; i < m; i++) {
int x, y, c;
cin >> x >> y >> c;
--x;
top[x].emplace_back(y, c);
tot += c;
}
map<int, vector<int>> mp;
for (int i = 0; i < n; i++) mp[A[i]].push_back(i);
for (const auto &[h, indices]: mp) {
for (int i: indices) {
if (i > 0 && alive[i-1]) {
join(i-1, i, h);
}
if (i+1 < n && alive[i+1]) {
join(i, i+1, h);
}
alive[i] = true;
}
}
for (auto [y, val]: top[anc(0)]) chmax(ans, val);
chmax(ans, dp[anc(0)]);
cout << tot - ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2748 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
4 ms |
2684 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2640 KB |
Output is correct |
9 |
Correct |
3 ms |
2684 KB |
Output is correct |
10 |
Correct |
3 ms |
2848 KB |
Output is correct |
11 |
Correct |
3 ms |
2764 KB |
Output is correct |
12 |
Correct |
3 ms |
2816 KB |
Output is correct |
13 |
Correct |
3 ms |
2764 KB |
Output is correct |
14 |
Correct |
3 ms |
2764 KB |
Output is correct |
15 |
Correct |
4 ms |
2764 KB |
Output is correct |
16 |
Correct |
3 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
2636 KB |
Output is correct |
18 |
Correct |
4 ms |
2808 KB |
Output is correct |
19 |
Correct |
3 ms |
2636 KB |
Output is correct |
20 |
Correct |
3 ms |
2636 KB |
Output is correct |
21 |
Correct |
4 ms |
2764 KB |
Output is correct |
22 |
Correct |
3 ms |
2764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2748 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
4 ms |
2684 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2640 KB |
Output is correct |
9 |
Correct |
3 ms |
2684 KB |
Output is correct |
10 |
Correct |
3 ms |
2848 KB |
Output is correct |
11 |
Correct |
3 ms |
2764 KB |
Output is correct |
12 |
Correct |
3 ms |
2816 KB |
Output is correct |
13 |
Correct |
3 ms |
2764 KB |
Output is correct |
14 |
Correct |
3 ms |
2764 KB |
Output is correct |
15 |
Correct |
4 ms |
2764 KB |
Output is correct |
16 |
Correct |
3 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
2636 KB |
Output is correct |
18 |
Correct |
4 ms |
2808 KB |
Output is correct |
19 |
Correct |
3 ms |
2636 KB |
Output is correct |
20 |
Correct |
3 ms |
2636 KB |
Output is correct |
21 |
Correct |
4 ms |
2764 KB |
Output is correct |
22 |
Correct |
3 ms |
2764 KB |
Output is correct |
23 |
Correct |
6 ms |
2892 KB |
Output is correct |
24 |
Correct |
4 ms |
3020 KB |
Output is correct |
25 |
Correct |
4 ms |
2892 KB |
Output is correct |
26 |
Correct |
5 ms |
3020 KB |
Output is correct |
27 |
Correct |
5 ms |
3020 KB |
Output is correct |
28 |
Correct |
5 ms |
2892 KB |
Output is correct |
29 |
Correct |
4 ms |
2892 KB |
Output is correct |
30 |
Correct |
4 ms |
3020 KB |
Output is correct |
31 |
Correct |
4 ms |
2892 KB |
Output is correct |
32 |
Correct |
14 ms |
10060 KB |
Output is correct |
33 |
Correct |
16 ms |
9420 KB |
Output is correct |
34 |
Correct |
13 ms |
8024 KB |
Output is correct |
35 |
Correct |
11 ms |
8008 KB |
Output is correct |
36 |
Correct |
16 ms |
12620 KB |
Output is correct |
37 |
Correct |
13 ms |
12296 KB |
Output is correct |
38 |
Correct |
4 ms |
3020 KB |
Output is correct |
39 |
Correct |
5 ms |
3076 KB |
Output is correct |
40 |
Correct |
16 ms |
8780 KB |
Output is correct |
41 |
Correct |
5 ms |
3404 KB |
Output is correct |
42 |
Correct |
5 ms |
3076 KB |
Output is correct |
43 |
Correct |
17 ms |
9156 KB |
Output is correct |
44 |
Correct |
6 ms |
3276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2636 KB |
Output is correct |
2 |
Correct |
3 ms |
2748 KB |
Output is correct |
3 |
Correct |
3 ms |
2636 KB |
Output is correct |
4 |
Correct |
4 ms |
2684 KB |
Output is correct |
5 |
Correct |
3 ms |
2636 KB |
Output is correct |
6 |
Correct |
3 ms |
2636 KB |
Output is correct |
7 |
Correct |
3 ms |
2680 KB |
Output is correct |
8 |
Correct |
4 ms |
2640 KB |
Output is correct |
9 |
Correct |
3 ms |
2684 KB |
Output is correct |
10 |
Correct |
3 ms |
2848 KB |
Output is correct |
11 |
Correct |
3 ms |
2764 KB |
Output is correct |
12 |
Correct |
3 ms |
2816 KB |
Output is correct |
13 |
Correct |
3 ms |
2764 KB |
Output is correct |
14 |
Correct |
3 ms |
2764 KB |
Output is correct |
15 |
Correct |
4 ms |
2764 KB |
Output is correct |
16 |
Correct |
3 ms |
2636 KB |
Output is correct |
17 |
Correct |
3 ms |
2636 KB |
Output is correct |
18 |
Correct |
4 ms |
2808 KB |
Output is correct |
19 |
Correct |
3 ms |
2636 KB |
Output is correct |
20 |
Correct |
3 ms |
2636 KB |
Output is correct |
21 |
Correct |
4 ms |
2764 KB |
Output is correct |
22 |
Correct |
3 ms |
2764 KB |
Output is correct |
23 |
Correct |
6 ms |
2892 KB |
Output is correct |
24 |
Correct |
4 ms |
3020 KB |
Output is correct |
25 |
Correct |
4 ms |
2892 KB |
Output is correct |
26 |
Correct |
5 ms |
3020 KB |
Output is correct |
27 |
Correct |
5 ms |
3020 KB |
Output is correct |
28 |
Correct |
5 ms |
2892 KB |
Output is correct |
29 |
Correct |
4 ms |
2892 KB |
Output is correct |
30 |
Correct |
4 ms |
3020 KB |
Output is correct |
31 |
Correct |
4 ms |
2892 KB |
Output is correct |
32 |
Correct |
14 ms |
10060 KB |
Output is correct |
33 |
Correct |
16 ms |
9420 KB |
Output is correct |
34 |
Correct |
13 ms |
8024 KB |
Output is correct |
35 |
Correct |
11 ms |
8008 KB |
Output is correct |
36 |
Correct |
16 ms |
12620 KB |
Output is correct |
37 |
Correct |
13 ms |
12296 KB |
Output is correct |
38 |
Correct |
4 ms |
3020 KB |
Output is correct |
39 |
Correct |
5 ms |
3076 KB |
Output is correct |
40 |
Correct |
16 ms |
8780 KB |
Output is correct |
41 |
Correct |
5 ms |
3404 KB |
Output is correct |
42 |
Correct |
5 ms |
3076 KB |
Output is correct |
43 |
Correct |
17 ms |
9156 KB |
Output is correct |
44 |
Correct |
6 ms |
3276 KB |
Output is correct |
45 |
Runtime error |
7 ms |
6220 KB |
Execution killed with signal 11 |
46 |
Halted |
0 ms |
0 KB |
- |