#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF 1000000000000000000
#define MOD 998244353
vector<int> adj[200000];
int mx[5000][5000], memo[5000][5000];
int dp(int x, int y) {
if(x > y) return 0;
if(memo[x][y] != -1) return memo[x][y];
memo[x][y] = 0;
if(x <= mx[x][y]-1) memo[x][y] = max(memo[x][y], dp(x, mx[x][y]-1)+mx[x][y]-mx[x][mx[x][y]-1]);
if(mx[x][y]+1 <= y) memo[x][y] = max(memo[x][y], dp(mx[x][y]+1, y)+mx[mx[x][y]+1][y]-mx[x][y]);
return memo[x][y];
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, x, y;
cin >> n;
int p[n];
for(int i = 0; i < n; i++) cin >> p[i];
for(int i = 0; i < n-1; i++) {
cin >> x >> y;
x--; y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
for(int i = 0; i < n; i++) {
for(int j = i; j < n; j++) {
if(i == j) mx[i][j] = i;
else if(p[mx[i][j-1]] > p[j]) mx[i][j] = mx[i][j-1];
else mx[i][j] = j;
}
}
memset(memo, -1, sizeof(memo));
cout << dp(0, n-1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
203468 KB |
Output is correct |
2 |
Correct |
48 ms |
203624 KB |
Output is correct |
3 |
Correct |
52 ms |
203856 KB |
Output is correct |
4 |
Correct |
48 ms |
203600 KB |
Output is correct |
5 |
Correct |
48 ms |
203600 KB |
Output is correct |
6 |
Correct |
48 ms |
203504 KB |
Output is correct |
7 |
Correct |
49 ms |
203604 KB |
Output is correct |
8 |
Correct |
48 ms |
203600 KB |
Output is correct |
9 |
Correct |
48 ms |
203600 KB |
Output is correct |
10 |
Correct |
48 ms |
203604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
203468 KB |
Output is correct |
2 |
Correct |
48 ms |
203624 KB |
Output is correct |
3 |
Correct |
52 ms |
203856 KB |
Output is correct |
4 |
Correct |
48 ms |
203600 KB |
Output is correct |
5 |
Correct |
48 ms |
203600 KB |
Output is correct |
6 |
Correct |
48 ms |
203504 KB |
Output is correct |
7 |
Correct |
49 ms |
203604 KB |
Output is correct |
8 |
Correct |
48 ms |
203600 KB |
Output is correct |
9 |
Correct |
48 ms |
203600 KB |
Output is correct |
10 |
Correct |
48 ms |
203604 KB |
Output is correct |
11 |
Correct |
51 ms |
213840 KB |
Output is correct |
12 |
Correct |
54 ms |
213844 KB |
Output is correct |
13 |
Correct |
51 ms |
213840 KB |
Output is correct |
14 |
Correct |
51 ms |
213856 KB |
Output is correct |
15 |
Correct |
51 ms |
213776 KB |
Output is correct |
16 |
Correct |
57 ms |
213844 KB |
Output is correct |
17 |
Correct |
52 ms |
214044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
203468 KB |
Output is correct |
2 |
Correct |
48 ms |
203624 KB |
Output is correct |
3 |
Correct |
52 ms |
203856 KB |
Output is correct |
4 |
Correct |
48 ms |
203600 KB |
Output is correct |
5 |
Correct |
48 ms |
203600 KB |
Output is correct |
6 |
Correct |
48 ms |
203504 KB |
Output is correct |
7 |
Correct |
49 ms |
203604 KB |
Output is correct |
8 |
Correct |
48 ms |
203600 KB |
Output is correct |
9 |
Correct |
48 ms |
203600 KB |
Output is correct |
10 |
Correct |
48 ms |
203604 KB |
Output is correct |
11 |
Correct |
51 ms |
213840 KB |
Output is correct |
12 |
Correct |
54 ms |
213844 KB |
Output is correct |
13 |
Correct |
51 ms |
213840 KB |
Output is correct |
14 |
Correct |
51 ms |
213856 KB |
Output is correct |
15 |
Correct |
51 ms |
213776 KB |
Output is correct |
16 |
Correct |
57 ms |
213844 KB |
Output is correct |
17 |
Correct |
52 ms |
214044 KB |
Output is correct |
18 |
Correct |
195 ms |
343080 KB |
Output is correct |
19 |
Correct |
129 ms |
342944 KB |
Output is correct |
20 |
Correct |
131 ms |
344064 KB |
Output is correct |
21 |
Correct |
130 ms |
342656 KB |
Output is correct |
22 |
Correct |
129 ms |
342784 KB |
Output is correct |
23 |
Correct |
128 ms |
342724 KB |
Output is correct |
24 |
Correct |
131 ms |
342680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
203468 KB |
Output is correct |
2 |
Correct |
48 ms |
203624 KB |
Output is correct |
3 |
Correct |
52 ms |
203856 KB |
Output is correct |
4 |
Correct |
48 ms |
203600 KB |
Output is correct |
5 |
Correct |
48 ms |
203600 KB |
Output is correct |
6 |
Correct |
48 ms |
203504 KB |
Output is correct |
7 |
Correct |
49 ms |
203604 KB |
Output is correct |
8 |
Correct |
48 ms |
203600 KB |
Output is correct |
9 |
Correct |
48 ms |
203600 KB |
Output is correct |
10 |
Correct |
48 ms |
203604 KB |
Output is correct |
11 |
Correct |
51 ms |
213840 KB |
Output is correct |
12 |
Correct |
54 ms |
213844 KB |
Output is correct |
13 |
Correct |
51 ms |
213840 KB |
Output is correct |
14 |
Correct |
51 ms |
213856 KB |
Output is correct |
15 |
Correct |
51 ms |
213776 KB |
Output is correct |
16 |
Correct |
57 ms |
213844 KB |
Output is correct |
17 |
Correct |
52 ms |
214044 KB |
Output is correct |
18 |
Correct |
195 ms |
343080 KB |
Output is correct |
19 |
Correct |
129 ms |
342944 KB |
Output is correct |
20 |
Correct |
131 ms |
344064 KB |
Output is correct |
21 |
Correct |
130 ms |
342656 KB |
Output is correct |
22 |
Correct |
129 ms |
342784 KB |
Output is correct |
23 |
Correct |
128 ms |
342724 KB |
Output is correct |
24 |
Correct |
131 ms |
342680 KB |
Output is correct |
25 |
Correct |
48 ms |
203516 KB |
Output is correct |
26 |
Incorrect |
133 ms |
343792 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
203468 KB |
Output is correct |
2 |
Correct |
48 ms |
203624 KB |
Output is correct |
3 |
Correct |
52 ms |
203856 KB |
Output is correct |
4 |
Correct |
48 ms |
203600 KB |
Output is correct |
5 |
Correct |
48 ms |
203600 KB |
Output is correct |
6 |
Correct |
48 ms |
203504 KB |
Output is correct |
7 |
Correct |
49 ms |
203604 KB |
Output is correct |
8 |
Correct |
48 ms |
203600 KB |
Output is correct |
9 |
Correct |
48 ms |
203600 KB |
Output is correct |
10 |
Correct |
48 ms |
203604 KB |
Output is correct |
11 |
Correct |
51 ms |
213840 KB |
Output is correct |
12 |
Correct |
54 ms |
213844 KB |
Output is correct |
13 |
Correct |
51 ms |
213840 KB |
Output is correct |
14 |
Correct |
51 ms |
213856 KB |
Output is correct |
15 |
Correct |
51 ms |
213776 KB |
Output is correct |
16 |
Correct |
57 ms |
213844 KB |
Output is correct |
17 |
Correct |
52 ms |
214044 KB |
Output is correct |
18 |
Correct |
195 ms |
343080 KB |
Output is correct |
19 |
Correct |
129 ms |
342944 KB |
Output is correct |
20 |
Correct |
131 ms |
344064 KB |
Output is correct |
21 |
Correct |
130 ms |
342656 KB |
Output is correct |
22 |
Correct |
129 ms |
342784 KB |
Output is correct |
23 |
Correct |
128 ms |
342724 KB |
Output is correct |
24 |
Correct |
131 ms |
342680 KB |
Output is correct |
25 |
Execution timed out |
2029 ms |
174988 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
203592 KB |
Output is correct |
2 |
Incorrect |
53 ms |
211792 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
203468 KB |
Output is correct |
2 |
Correct |
48 ms |
203624 KB |
Output is correct |
3 |
Correct |
52 ms |
203856 KB |
Output is correct |
4 |
Correct |
48 ms |
203600 KB |
Output is correct |
5 |
Correct |
48 ms |
203600 KB |
Output is correct |
6 |
Correct |
48 ms |
203504 KB |
Output is correct |
7 |
Correct |
49 ms |
203604 KB |
Output is correct |
8 |
Correct |
48 ms |
203600 KB |
Output is correct |
9 |
Correct |
48 ms |
203600 KB |
Output is correct |
10 |
Correct |
48 ms |
203604 KB |
Output is correct |
11 |
Correct |
51 ms |
213840 KB |
Output is correct |
12 |
Correct |
54 ms |
213844 KB |
Output is correct |
13 |
Correct |
51 ms |
213840 KB |
Output is correct |
14 |
Correct |
51 ms |
213856 KB |
Output is correct |
15 |
Correct |
51 ms |
213776 KB |
Output is correct |
16 |
Correct |
57 ms |
213844 KB |
Output is correct |
17 |
Correct |
52 ms |
214044 KB |
Output is correct |
18 |
Correct |
195 ms |
343080 KB |
Output is correct |
19 |
Correct |
129 ms |
342944 KB |
Output is correct |
20 |
Correct |
131 ms |
344064 KB |
Output is correct |
21 |
Correct |
130 ms |
342656 KB |
Output is correct |
22 |
Correct |
129 ms |
342784 KB |
Output is correct |
23 |
Correct |
128 ms |
342724 KB |
Output is correct |
24 |
Correct |
131 ms |
342680 KB |
Output is correct |
25 |
Correct |
48 ms |
203516 KB |
Output is correct |
26 |
Incorrect |
133 ms |
343792 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |