#include <bits/stdc++.h>
using namespace std;
template < typename T >
class SegmentTree {
private:
T *Stree;
T *Snum;
long long n;
T NullValue = 0;
T combine(T a, T b) {
return max(a,b);
}
void build(long long idx, long long s, long long e) {
if (s > e)
return;
if (s == e) {
Stree[idx] = Snum[s];
return ;
}
long long m = ( s + e ) / 2;
build( 2 * idx, s, m);
build( 2 * idx + 1, m + 1, e);
Stree[idx] = combine(Stree[2 * idx], Stree[2 * idx + 1] );
return;
}
T queryUtil(long long idx, long long s, long long e, long long l, long long r) {
if (l > e || r < s)
return NullValue;
if (s >= l && e <= r) {
return Stree[idx];
}
long long m = ( s + e ) / 2;
T left = queryUtil(2 * idx, s, m, l, r);
T right = queryUtil(2 * idx + 1, m + 1, e, l, r);
return combine(left, right );
}
public:
SegmentTree(vector<T> a) {
this->n = a.size();
Stree = new T[4 * n + 5];
Snum = new T[n];
for (long long i = 0; i < n; i++) {
Snum[i] = a[i];
}
build(1, 0, n - 1);
}
T query(long long l, long long r) {
return queryUtil(1, 0, n - 1, l - 1, r - 1);
}
};
long long dp[2000][2000];
long long solve(long long idx,long long last,long long n,SegmentTree<long long> &ST) {
if(idx>=n)return 0;
if(dp[idx][last]!=-1) {
return dp[idx][last];
}
if(last>=n) {
dp[idx][last]=ST.query(idx+1,last+1)*(n-idx);
return ST.query(idx+1,last+1)*(n-idx);
}
long long pos1 = ST.query(idx+1,last+1)*(n-idx) + solve(last+1,last+1,n,ST);
long long pos2 = solve(idx,last+1,n,ST);
dp[idx][last]=min(pos1,pos2);
return min(pos1,pos2);
}
bool is_this_subtask4(vector<long long> a) {
reverse(a.begin(),a.end());
return is_sorted(a.begin(),a.end());
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
long long n;
cin >> n;
vector<long long> a(n);
for(long long i = 0 ; i < n ; i++) {
cin >> a[i];
}
if(is_this_subtask4(a)) {
cout << a[0]*n << '\n';
return 0;
}
SegmentTree<long long> ST(a);
memset(dp,-1,sizeof dp);
long long res = solve(0,0,n,ST);
cout << res << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
19 ms |
31596 KB |
Output is correct |
3 |
Correct |
17 ms |
31724 KB |
Output is correct |
4 |
Correct |
17 ms |
31596 KB |
Output is correct |
5 |
Correct |
17 ms |
31744 KB |
Output is correct |
6 |
Correct |
17 ms |
31596 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
21 ms |
31596 KB |
Output is correct |
9 |
Correct |
18 ms |
31596 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
18 ms |
31596 KB |
Output is correct |
12 |
Correct |
17 ms |
31596 KB |
Output is correct |
13 |
Correct |
17 ms |
31596 KB |
Output is correct |
14 |
Correct |
17 ms |
31596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
31980 KB |
Output is correct |
2 |
Correct |
155 ms |
31976 KB |
Output is correct |
3 |
Correct |
160 ms |
31980 KB |
Output is correct |
4 |
Correct |
149 ms |
31980 KB |
Output is correct |
5 |
Correct |
165 ms |
31980 KB |
Output is correct |
6 |
Correct |
157 ms |
31980 KB |
Output is correct |
7 |
Correct |
162 ms |
31980 KB |
Output is correct |
8 |
Correct |
148 ms |
32000 KB |
Output is correct |
9 |
Correct |
164 ms |
31972 KB |
Output is correct |
10 |
Correct |
148 ms |
32100 KB |
Output is correct |
11 |
Correct |
165 ms |
31980 KB |
Output is correct |
12 |
Correct |
157 ms |
31980 KB |
Output is correct |
13 |
Correct |
153 ms |
31980 KB |
Output is correct |
14 |
Correct |
169 ms |
32100 KB |
Output is correct |
15 |
Correct |
154 ms |
32004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
31980 KB |
Output is correct |
2 |
Correct |
155 ms |
31976 KB |
Output is correct |
3 |
Correct |
160 ms |
31980 KB |
Output is correct |
4 |
Correct |
149 ms |
31980 KB |
Output is correct |
5 |
Correct |
165 ms |
31980 KB |
Output is correct |
6 |
Correct |
157 ms |
31980 KB |
Output is correct |
7 |
Correct |
162 ms |
31980 KB |
Output is correct |
8 |
Correct |
148 ms |
32000 KB |
Output is correct |
9 |
Correct |
164 ms |
31972 KB |
Output is correct |
10 |
Correct |
148 ms |
32100 KB |
Output is correct |
11 |
Correct |
165 ms |
31980 KB |
Output is correct |
12 |
Correct |
157 ms |
31980 KB |
Output is correct |
13 |
Correct |
153 ms |
31980 KB |
Output is correct |
14 |
Correct |
169 ms |
32100 KB |
Output is correct |
15 |
Correct |
154 ms |
32004 KB |
Output is correct |
16 |
Runtime error |
234 ms |
150496 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
17380 KB |
Output is correct |
2 |
Correct |
124 ms |
25700 KB |
Output is correct |
3 |
Correct |
150 ms |
25700 KB |
Output is correct |
4 |
Correct |
122 ms |
25700 KB |
Output is correct |
5 |
Correct |
120 ms |
25700 KB |
Output is correct |
6 |
Correct |
121 ms |
25700 KB |
Output is correct |
7 |
Correct |
121 ms |
25700 KB |
Output is correct |
8 |
Correct |
147 ms |
25712 KB |
Output is correct |
9 |
Correct |
120 ms |
25700 KB |
Output is correct |
10 |
Correct |
125 ms |
25832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
19 ms |
31596 KB |
Output is correct |
3 |
Correct |
17 ms |
31724 KB |
Output is correct |
4 |
Correct |
17 ms |
31596 KB |
Output is correct |
5 |
Correct |
17 ms |
31744 KB |
Output is correct |
6 |
Correct |
17 ms |
31596 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
21 ms |
31596 KB |
Output is correct |
9 |
Correct |
18 ms |
31596 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
18 ms |
31596 KB |
Output is correct |
12 |
Correct |
17 ms |
31596 KB |
Output is correct |
13 |
Correct |
17 ms |
31596 KB |
Output is correct |
14 |
Correct |
17 ms |
31596 KB |
Output is correct |
15 |
Correct |
195 ms |
31980 KB |
Output is correct |
16 |
Correct |
155 ms |
31976 KB |
Output is correct |
17 |
Correct |
160 ms |
31980 KB |
Output is correct |
18 |
Correct |
149 ms |
31980 KB |
Output is correct |
19 |
Correct |
165 ms |
31980 KB |
Output is correct |
20 |
Correct |
157 ms |
31980 KB |
Output is correct |
21 |
Correct |
162 ms |
31980 KB |
Output is correct |
22 |
Correct |
148 ms |
32000 KB |
Output is correct |
23 |
Correct |
164 ms |
31972 KB |
Output is correct |
24 |
Correct |
148 ms |
32100 KB |
Output is correct |
25 |
Correct |
165 ms |
31980 KB |
Output is correct |
26 |
Correct |
157 ms |
31980 KB |
Output is correct |
27 |
Correct |
153 ms |
31980 KB |
Output is correct |
28 |
Correct |
169 ms |
32100 KB |
Output is correct |
29 |
Correct |
154 ms |
32004 KB |
Output is correct |
30 |
Correct |
157 ms |
31980 KB |
Output is correct |
31 |
Correct |
165 ms |
32144 KB |
Output is correct |
32 |
Correct |
150 ms |
31980 KB |
Output is correct |
33 |
Correct |
163 ms |
31980 KB |
Output is correct |
34 |
Correct |
152 ms |
31980 KB |
Output is correct |
35 |
Correct |
165 ms |
32100 KB |
Output is correct |
36 |
Correct |
161 ms |
32100 KB |
Output is correct |
37 |
Correct |
164 ms |
32228 KB |
Output is correct |
38 |
Correct |
160 ms |
32008 KB |
Output is correct |
39 |
Correct |
165 ms |
31980 KB |
Output is correct |
40 |
Correct |
143 ms |
31980 KB |
Output is correct |
41 |
Correct |
191 ms |
32100 KB |
Output is correct |
42 |
Correct |
164 ms |
31980 KB |
Output is correct |
43 |
Correct |
166 ms |
31980 KB |
Output is correct |
44 |
Correct |
151 ms |
31980 KB |
Output is correct |
45 |
Correct |
153 ms |
32100 KB |
Output is correct |
46 |
Correct |
150 ms |
31980 KB |
Output is correct |
47 |
Correct |
165 ms |
31980 KB |
Output is correct |
48 |
Correct |
146 ms |
31972 KB |
Output is correct |
49 |
Correct |
161 ms |
32100 KB |
Output is correct |
50 |
Correct |
161 ms |
31904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
19 ms |
31596 KB |
Output is correct |
3 |
Correct |
17 ms |
31724 KB |
Output is correct |
4 |
Correct |
17 ms |
31596 KB |
Output is correct |
5 |
Correct |
17 ms |
31744 KB |
Output is correct |
6 |
Correct |
17 ms |
31596 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
21 ms |
31596 KB |
Output is correct |
9 |
Correct |
18 ms |
31596 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
18 ms |
31596 KB |
Output is correct |
12 |
Correct |
17 ms |
31596 KB |
Output is correct |
13 |
Correct |
17 ms |
31596 KB |
Output is correct |
14 |
Correct |
17 ms |
31596 KB |
Output is correct |
15 |
Correct |
195 ms |
31980 KB |
Output is correct |
16 |
Correct |
155 ms |
31976 KB |
Output is correct |
17 |
Correct |
160 ms |
31980 KB |
Output is correct |
18 |
Correct |
149 ms |
31980 KB |
Output is correct |
19 |
Correct |
165 ms |
31980 KB |
Output is correct |
20 |
Correct |
157 ms |
31980 KB |
Output is correct |
21 |
Correct |
162 ms |
31980 KB |
Output is correct |
22 |
Correct |
148 ms |
32000 KB |
Output is correct |
23 |
Correct |
164 ms |
31972 KB |
Output is correct |
24 |
Correct |
148 ms |
32100 KB |
Output is correct |
25 |
Correct |
165 ms |
31980 KB |
Output is correct |
26 |
Correct |
157 ms |
31980 KB |
Output is correct |
27 |
Correct |
153 ms |
31980 KB |
Output is correct |
28 |
Correct |
169 ms |
32100 KB |
Output is correct |
29 |
Correct |
154 ms |
32004 KB |
Output is correct |
30 |
Runtime error |
234 ms |
150496 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
31 |
Halted |
0 ms |
0 KB |
- |