#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MOD 1000000007
#define INF 1000000000000000000
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll LASTBIT(ll mask) {return (mask) & (-mask);}
ll pop_cnt(ll mask) {return __builtin_popcountll(mask);}
ll ctz(ll mask) {return __builtin_ctzll(mask);}
ll clz(ll mask) {return __builtin_clzll(mask);}
ll logOf(ll mask){return 63 - clz(mask);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b){a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b){a = b; return true;}
return false;
}
template <class T>
void printArr(vector<T> &a, char separator = ' ', char finish = '\n'){
for(long i = 0; i<a.size(); ++i) cout << a[i] << separator;
cout << finish;
}
long n;
vector<long> a(n+1);
ll sub4(){
return 1LL * a[1] * n;
}
ll sub5(){
vector<ll> dp(n+1, INF);
dp[0] = 0;
for(long i = 1; i<=n; ++i){
ll ma = 0;
for(long j = i; j>=1; --j){
maximize(ma, a[j]);
minimize(dp[i], dp[j-1] + ma * (n - j + 1));
}
}
return dp[n];
}
ll sub3(){
vector<ll> dp(n+1, INF);
dp[0] = 0;
for(long i = 1; i<=n; ++i){
dp[i] = min(dp[0] + 1LL * n * a[i], dp[i-1] + 1LL * (n - i + 1) * a[i]);
long l = 1, r = i;
while(r - l > 300){
long m1 = l + (r - l) / 3;
long m2 = r - (r - l) / 3;
ll val1 = dp[m1 - 1] + 1LL * (n - m1 + 1) * a[i];
ll val2 = dp[m2 - 1] + 1LL * (n - m2 + 1) * a[i];
if (val1 > val2) l = m1;
else r = m2;
}
for(long j = l; j<=r; ++j)
minimize(dp[i], dp[j-1] + 1LL * (n - j + 1) * a[i]);
}
return dp[n];
}
#define MAX 1000001
#define LOGMAX 20
long rmq[MAX][LOGMAX];
long getMax(long l, long r){
long len = r - l + 1;
long j = logOf(len);
return max(rmq[l][j], rmq[r - MASK(j) + 1][j]);
}
ll subbo(){
for(long i = 1; i<=n; ++i) rmq[i][0] = a[i];
for(long j = 1; MASK(j) <= n; ++j)
for(long i = 1; i + MASK(j) - 1 <= n; ++i)
rmq[i][j] = max(rmq[i][j-1], rmq[i + MASK(j-1)][j-1]);
vector<ll> dp(n+1, INF);
dp[0] = 0;
for(long i = 1; i<=n; ++i){
dp[i] = min(dp[0] + 1LL * n * getMax(1, i), dp[i-1] + 1LL * (n - i + 1) * a[i]);
long l = 1, r = i;
while(r - l > 300){
long m1 = l + (r - l) / 3;
long m2 = r - (r - l) / 3;
ll val1 = dp[m1 - 1] + 1LL * (n - m1 + 1) * getMax(m1, i);
ll val2 = dp[m2 - 1] + 1LL * (n - m2 + 1) * getMax(m2, i);
if (val1 > val2) l = m1;
else r = m2;
}
for(long j = l; j<=r; ++j)
minimize(dp[i], dp[j-1] + 1LL * (n - j + 1) * getMax(j, i));
}
return dp[n];
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen("discharg.inp", "r", stdin);
cin >> n;
a.resize(n+1);
for(long i = 1; i<=n; ++i)
cin >> a[i];
bool check1 = true, check2 = true;;
for(long i = 1; i<n; ++i){
if (a[i] < a[i+1]) check1 = false;
if (a[i] > a[i+1]) check2 = false;
}
if (check1) cout << sub4() << "\n";
else if (n <= 10000) cout << sub5() << "\n";
else if (check2) cout << sub3() << "\n";
else cout << subbo() << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
352 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
3 ms |
352 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
3 ms |
340 KB |
Output is correct |
10 |
Correct |
3 ms |
340 KB |
Output is correct |
11 |
Correct |
3 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
340 KB |
Output is correct |
16 |
Correct |
419 ms |
15988 KB |
Output is correct |
17 |
Correct |
428 ms |
16084 KB |
Output is correct |
18 |
Incorrect |
410 ms |
20428 KB |
Output isn't correct |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
8148 KB |
Output is correct |
2 |
Correct |
88 ms |
8148 KB |
Output is correct |
3 |
Correct |
87 ms |
8148 KB |
Output is correct |
4 |
Correct |
91 ms |
8148 KB |
Output is correct |
5 |
Correct |
87 ms |
8148 KB |
Output is correct |
6 |
Correct |
104 ms |
8148 KB |
Output is correct |
7 |
Correct |
89 ms |
8148 KB |
Output is correct |
8 |
Correct |
88 ms |
8148 KB |
Output is correct |
9 |
Correct |
89 ms |
8148 KB |
Output is correct |
10 |
Correct |
88 ms |
8148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
3 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
352 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
2 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
2 ms |
340 KB |
Output is correct |
39 |
Correct |
2 ms |
340 KB |
Output is correct |
40 |
Correct |
2 ms |
340 KB |
Output is correct |
41 |
Correct |
2 ms |
340 KB |
Output is correct |
42 |
Correct |
2 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
340 KB |
Output is correct |
44 |
Correct |
2 ms |
340 KB |
Output is correct |
45 |
Correct |
2 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
2 ms |
340 KB |
Output is correct |
48 |
Correct |
2 ms |
340 KB |
Output is correct |
49 |
Correct |
2 ms |
340 KB |
Output is correct |
50 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
3 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
3 ms |
352 KB |
Output is correct |
22 |
Correct |
3 ms |
340 KB |
Output is correct |
23 |
Correct |
3 ms |
340 KB |
Output is correct |
24 |
Correct |
3 ms |
340 KB |
Output is correct |
25 |
Correct |
3 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
2 ms |
340 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
419 ms |
15988 KB |
Output is correct |
31 |
Correct |
428 ms |
16084 KB |
Output is correct |
32 |
Incorrect |
410 ms |
20428 KB |
Output isn't correct |
33 |
Halted |
0 ms |
0 KB |
- |