#include <bits/stdc++.h>
#define int long long
using namespace std;
int32_t main()
{
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
a[i] = n - a[i];
}
int m;
cin >> m;
vector<vector<pair<int, int>>> stars(n + 1);
int sum = 0;
for (int i = 1; i <= m; ++i)
{
int qui, wh, coef;
cin >> qui >> wh >> coef;
wh = n - wh + 1;
sum += coef;
stars[qui].push_back({wh, coef});
}
for (int i = 1; i <= n; ++i)
{
sort(stars[i].begin(), stars[i].end());
}
vector<vector<int>> g(n + 1);
int root = 0;
function<void(int, int, int)> build = [&](int st, int dr, int parent)
{
int mini = INT_MAX;
int pos = 0;
for (int i = st; i <= dr; ++i)
{
if (a[i] < mini)
{
mini = a[i];
pos = i;
}
}
g[parent].push_back(pos);
g[pos].push_back(parent);
if (!root)
{
root = pos;
}
if (pos != st)
{
build(st, pos - 1, pos);
}
if (pos != dr)
{
build(pos + 1, dr, pos);
}
};
build(1, n, 0);
vector<vector<int>> dp(n + 1, vector<int>(n + 2));
function<void(int, int)> dfs = [&](int node, int parent)
{
int children = 0;
vector<int> ind;
for (auto i : g[node])
{
if (i != parent)
{
dfs(i, node);
ind.push_back(i);
children++;
}
}
for (int j = 1; j <= n; ++j)
{
int cost_maxim = 0;
for (auto k : stars[node])
{
if (k.first >= j)
{
cost_maxim = max(cost_maxim, k.second);
}
}
if (j <= a[node])
{
if (children == 0)
{
dp[node][j] = cost_maxim;
}
if (children == 1)
{
dp[node][j] = max(dp[ind[0]][j], dp[ind[0]][a[node] + 1] + cost_maxim);
}
if (children == 2)
{
dp[node][j] = max({dp[ind[0]][j] + dp[ind[1]][a[node] + 1], dp[ind[0]][a[node] + 1] + dp[ind[1]][j], dp[ind[0]][a[node] + 1] + dp[ind[1]][a[node] + 1] + cost_maxim});
}
}
else
{
for (auto k : ind)
{
dp[node][j] += dp[k][j];
}
}
}
};
dfs(root, 0);
cout << sum - dp[root][1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Correct |
2 ms |
960 KB |
Output is correct |
3 |
Correct |
1 ms |
960 KB |
Output is correct |
4 |
Correct |
2 ms |
1008 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
836 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
2 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
984 KB |
Output is correct |
12 |
Correct |
2 ms |
960 KB |
Output is correct |
13 |
Correct |
2 ms |
964 KB |
Output is correct |
14 |
Correct |
2 ms |
916 KB |
Output is correct |
15 |
Correct |
2 ms |
968 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
832 KB |
Output is correct |
21 |
Correct |
2 ms |
964 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Correct |
2 ms |
960 KB |
Output is correct |
3 |
Correct |
1 ms |
960 KB |
Output is correct |
4 |
Correct |
2 ms |
1008 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
836 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
2 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
984 KB |
Output is correct |
12 |
Correct |
2 ms |
960 KB |
Output is correct |
13 |
Correct |
2 ms |
964 KB |
Output is correct |
14 |
Correct |
2 ms |
916 KB |
Output is correct |
15 |
Correct |
2 ms |
968 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
832 KB |
Output is correct |
21 |
Correct |
2 ms |
964 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
38 ms |
28968 KB |
Output is correct |
24 |
Correct |
40 ms |
29220 KB |
Output is correct |
25 |
Correct |
43 ms |
29132 KB |
Output is correct |
26 |
Correct |
43 ms |
31160 KB |
Output is correct |
27 |
Correct |
38 ms |
29548 KB |
Output is correct |
28 |
Correct |
39 ms |
28972 KB |
Output is correct |
29 |
Correct |
42 ms |
30216 KB |
Output is correct |
30 |
Correct |
44 ms |
31760 KB |
Output is correct |
31 |
Correct |
42 ms |
29524 KB |
Output is correct |
32 |
Correct |
41 ms |
30052 KB |
Output is correct |
33 |
Correct |
47 ms |
32136 KB |
Output is correct |
34 |
Correct |
43 ms |
30744 KB |
Output is correct |
35 |
Correct |
41 ms |
31512 KB |
Output is correct |
36 |
Correct |
48 ms |
31812 KB |
Output is correct |
37 |
Correct |
50 ms |
32092 KB |
Output is correct |
38 |
Correct |
43 ms |
32212 KB |
Output is correct |
39 |
Correct |
39 ms |
31216 KB |
Output is correct |
40 |
Correct |
41 ms |
29548 KB |
Output is correct |
41 |
Correct |
41 ms |
30792 KB |
Output is correct |
42 |
Correct |
39 ms |
31480 KB |
Output is correct |
43 |
Correct |
40 ms |
29408 KB |
Output is correct |
44 |
Correct |
40 ms |
30360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
980 KB |
Output is correct |
2 |
Correct |
2 ms |
960 KB |
Output is correct |
3 |
Correct |
1 ms |
960 KB |
Output is correct |
4 |
Correct |
2 ms |
1008 KB |
Output is correct |
5 |
Correct |
2 ms |
852 KB |
Output is correct |
6 |
Correct |
1 ms |
852 KB |
Output is correct |
7 |
Correct |
2 ms |
836 KB |
Output is correct |
8 |
Correct |
1 ms |
980 KB |
Output is correct |
9 |
Correct |
2 ms |
980 KB |
Output is correct |
10 |
Correct |
2 ms |
1108 KB |
Output is correct |
11 |
Correct |
1 ms |
984 KB |
Output is correct |
12 |
Correct |
2 ms |
960 KB |
Output is correct |
13 |
Correct |
2 ms |
964 KB |
Output is correct |
14 |
Correct |
2 ms |
916 KB |
Output is correct |
15 |
Correct |
2 ms |
968 KB |
Output is correct |
16 |
Correct |
2 ms |
1108 KB |
Output is correct |
17 |
Correct |
1 ms |
852 KB |
Output is correct |
18 |
Correct |
1 ms |
980 KB |
Output is correct |
19 |
Correct |
1 ms |
856 KB |
Output is correct |
20 |
Correct |
1 ms |
832 KB |
Output is correct |
21 |
Correct |
2 ms |
964 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
38 ms |
28968 KB |
Output is correct |
24 |
Correct |
40 ms |
29220 KB |
Output is correct |
25 |
Correct |
43 ms |
29132 KB |
Output is correct |
26 |
Correct |
43 ms |
31160 KB |
Output is correct |
27 |
Correct |
38 ms |
29548 KB |
Output is correct |
28 |
Correct |
39 ms |
28972 KB |
Output is correct |
29 |
Correct |
42 ms |
30216 KB |
Output is correct |
30 |
Correct |
44 ms |
31760 KB |
Output is correct |
31 |
Correct |
42 ms |
29524 KB |
Output is correct |
32 |
Correct |
41 ms |
30052 KB |
Output is correct |
33 |
Correct |
47 ms |
32136 KB |
Output is correct |
34 |
Correct |
43 ms |
30744 KB |
Output is correct |
35 |
Correct |
41 ms |
31512 KB |
Output is correct |
36 |
Correct |
48 ms |
31812 KB |
Output is correct |
37 |
Correct |
50 ms |
32092 KB |
Output is correct |
38 |
Correct |
43 ms |
32212 KB |
Output is correct |
39 |
Correct |
39 ms |
31216 KB |
Output is correct |
40 |
Correct |
41 ms |
29548 KB |
Output is correct |
41 |
Correct |
41 ms |
30792 KB |
Output is correct |
42 |
Correct |
39 ms |
31480 KB |
Output is correct |
43 |
Correct |
40 ms |
29408 KB |
Output is correct |
44 |
Correct |
40 ms |
30360 KB |
Output is correct |
45 |
Runtime error |
369 ms |
524288 KB |
Execution killed with signal 9 |
46 |
Halted |
0 ms |
0 KB |
- |