#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx=2e3+5;
ll n, m, h[nx], dp[nx][nx], x, y, c, sm, l[nx], r[nx], rt, res;
vector<pair<int, int>> d[nx];
stack<int> s;
void solve(int u)
{
if (l[u]) solve(l[u]);
if (r[u]) solve(r[u]);
for (auto [y, c]:d[u]) dp[u][y]=max(dp[u][y], c+dp[l[u]][h[u]]+dp[r[u]][h[u]]);
for (int i=h[u]; i<=n; i++) dp[u][i]=max({dp[u][i], dp[u][i-1], dp[l[u]][i]+dp[r[u]][h[u]], dp[r[u]][i]+dp[l[u]][h[u]]});
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n;
for (int i=1; i<=n; i++) cin>>h[i];
cin>>m;
for (int i=1; i<=m; i++) cin>>x>>y>>c, d[x].push_back({y, c}), sm+=c;
for (int i=1; i<=n; i++)
{
while (!s.empty()&&h[s.top()]<h[i]) l[i]=s.top(), s.pop();
if (!s.empty()) r[s.top()]=i;
else rt=i;
s.push(i);
}
//for (int i=1; i<=n; i++) cout<<"tree "<<i<<' '<<l[i]<<' '<<r[i]<<'\n';
solve(rt);
for (int i=h[rt]; i<=n; i++) res=max(res, dp[rt][i]);
cout<<sm-res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
2104 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
3 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
2 ms |
2044 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
1620 KB |
Output is correct |
18 |
Correct |
2 ms |
1844 KB |
Output is correct |
19 |
Correct |
1 ms |
1788 KB |
Output is correct |
20 |
Correct |
1 ms |
1620 KB |
Output is correct |
21 |
Correct |
2 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
2104 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
3 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
2 ms |
2044 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
1620 KB |
Output is correct |
18 |
Correct |
2 ms |
1844 KB |
Output is correct |
19 |
Correct |
1 ms |
1788 KB |
Output is correct |
20 |
Correct |
1 ms |
1620 KB |
Output is correct |
21 |
Correct |
2 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1620 KB |
Output is correct |
23 |
Correct |
26 ms |
21588 KB |
Output is correct |
24 |
Correct |
24 ms |
21844 KB |
Output is correct |
25 |
Correct |
23 ms |
21696 KB |
Output is correct |
26 |
Correct |
27 ms |
22868 KB |
Output is correct |
27 |
Correct |
25 ms |
22112 KB |
Output is correct |
28 |
Correct |
28 ms |
21580 KB |
Output is correct |
29 |
Correct |
24 ms |
22356 KB |
Output is correct |
30 |
Correct |
26 ms |
23636 KB |
Output is correct |
31 |
Correct |
22 ms |
21600 KB |
Output is correct |
32 |
Correct |
23 ms |
22100 KB |
Output is correct |
33 |
Correct |
24 ms |
23240 KB |
Output is correct |
34 |
Correct |
24 ms |
22612 KB |
Output is correct |
35 |
Correct |
24 ms |
22868 KB |
Output is correct |
36 |
Correct |
26 ms |
24148 KB |
Output is correct |
37 |
Correct |
26 ms |
24444 KB |
Output is correct |
38 |
Correct |
28 ms |
23320 KB |
Output is correct |
39 |
Correct |
7 ms |
8532 KB |
Output is correct |
40 |
Correct |
18 ms |
17764 KB |
Output is correct |
41 |
Correct |
6 ms |
8532 KB |
Output is correct |
42 |
Correct |
7 ms |
8788 KB |
Output is correct |
43 |
Correct |
20 ms |
17748 KB |
Output is correct |
44 |
Correct |
8 ms |
8532 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1876 KB |
Output is correct |
2 |
Correct |
2 ms |
1876 KB |
Output is correct |
3 |
Correct |
2 ms |
2104 KB |
Output is correct |
4 |
Correct |
2 ms |
1876 KB |
Output is correct |
5 |
Correct |
2 ms |
1876 KB |
Output is correct |
6 |
Correct |
2 ms |
1876 KB |
Output is correct |
7 |
Correct |
2 ms |
1876 KB |
Output is correct |
8 |
Correct |
2 ms |
1876 KB |
Output is correct |
9 |
Correct |
2 ms |
1876 KB |
Output is correct |
10 |
Correct |
2 ms |
1876 KB |
Output is correct |
11 |
Correct |
2 ms |
1876 KB |
Output is correct |
12 |
Correct |
3 ms |
1876 KB |
Output is correct |
13 |
Correct |
2 ms |
1876 KB |
Output is correct |
14 |
Correct |
2 ms |
2044 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Correct |
2 ms |
1876 KB |
Output is correct |
17 |
Correct |
2 ms |
1620 KB |
Output is correct |
18 |
Correct |
2 ms |
1844 KB |
Output is correct |
19 |
Correct |
1 ms |
1788 KB |
Output is correct |
20 |
Correct |
1 ms |
1620 KB |
Output is correct |
21 |
Correct |
2 ms |
1876 KB |
Output is correct |
22 |
Correct |
2 ms |
1620 KB |
Output is correct |
23 |
Correct |
26 ms |
21588 KB |
Output is correct |
24 |
Correct |
24 ms |
21844 KB |
Output is correct |
25 |
Correct |
23 ms |
21696 KB |
Output is correct |
26 |
Correct |
27 ms |
22868 KB |
Output is correct |
27 |
Correct |
25 ms |
22112 KB |
Output is correct |
28 |
Correct |
28 ms |
21580 KB |
Output is correct |
29 |
Correct |
24 ms |
22356 KB |
Output is correct |
30 |
Correct |
26 ms |
23636 KB |
Output is correct |
31 |
Correct |
22 ms |
21600 KB |
Output is correct |
32 |
Correct |
23 ms |
22100 KB |
Output is correct |
33 |
Correct |
24 ms |
23240 KB |
Output is correct |
34 |
Correct |
24 ms |
22612 KB |
Output is correct |
35 |
Correct |
24 ms |
22868 KB |
Output is correct |
36 |
Correct |
26 ms |
24148 KB |
Output is correct |
37 |
Correct |
26 ms |
24444 KB |
Output is correct |
38 |
Correct |
28 ms |
23320 KB |
Output is correct |
39 |
Correct |
7 ms |
8532 KB |
Output is correct |
40 |
Correct |
18 ms |
17764 KB |
Output is correct |
41 |
Correct |
6 ms |
8532 KB |
Output is correct |
42 |
Correct |
7 ms |
8788 KB |
Output is correct |
43 |
Correct |
20 ms |
17748 KB |
Output is correct |
44 |
Correct |
8 ms |
8532 KB |
Output is correct |
45 |
Runtime error |
3 ms |
596 KB |
Execution killed with signal 11 |
46 |
Halted |
0 ms |
0 KB |
- |