/*
.:*+=%@@@@@@=-.
.:=@#@@@#@@#######%==*.
.-=####@######%*-.....:%##%.
.*@###########%+:--........-%@-
.*@##############@+--.........-:%-
.+##################@==%%%%=+*:----+.
.-@####################%++%@@@@@=+**%@@*
.%###################@%%@@@###@%+:--%@@%.
-@###################@%%%%*::*%++:-----=@+.
-#####################@%=++++++*:-------.-=:
.+####################@%++*::-:::--::*:::***=:
.@#####################%=++*::::-:::++*=##@@#@-
..#####################@%%=++**:::::**+%@#@%%##-..
.%####################@@%=+++*+****::*=@######@.
.=######################@%%==+==++**+=@%@##@###+:...
-#######################@@@%%%===++=@@@%=++===*::--...
-########################@@@@@@@%==%%=++==@@:::::*:--.
..:#########################@@@@@@%%======++++::-..:-.--...
%#############################@###@%%@@%==%=%*----.--.::---.
#############################################*-:*:-:---*---- .
#############################################*--*--:---*---:-.
#############################################+--::--::-*::-::.
###########################################+:*-.---.---.:---*-..
###########################################**:-----------------.
##########################################@::**:--::::::--:::::-
###########################################:--:*:::::::::**::*+*
###########################################=:::***::::::**:::*+*
############################@@@@@@#########@+****::::********+++
############################@%%%%%@@@@@@@###%+***::::::::***+==+
############################@%%%%%%%%%%%@####=+:::-::::-::*+=%%+
#############################@%%%%%%%%%%@#####=::--------:*=%@%+
%###########################@%%%%==%%%%%%@##@#=:------..-:+%@@%=
----------------------------------------------
--------------------------------------------
----------------------------------------------
--------------------------------------------
----------------------------------------------
o###########oo
o##" ""##o
o#" "##
o#" "#o
#" ## ## "##
#" ##
# ################### #
# #
# #
# #
# #
# #
# #
#o #
"#o ##
"#o ##
"#o o#"
"#o ##
"#o o#"
"#ooo ooo#######oo
############### "######o
o###"" "###o # ###
o###o oooo ### oo####"
o###**# #**# ############"
""##""""""""""########### #
# oooooooo#"#** ## #
# # # # ** ## #
#o# #o# *****###ooo#
##
## o###o
## o##***##
o########## #***#**##o
o##" ""### #***##***#
o#######o ### oo#### ##**####*#
o##" ""#############"" ##****###
##" ## ##*##*###
## ### ##### ##
## ### # ## #
## ## #
## ##
## ###
## ###oo
### ""###
###
###
*/
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")
//#pragma GCC target("avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;
int mysqrt(ll x){
int l = 0, r = 1e9 + 1;
while (r - l > 1){
int m = (l + r) / 2;
if (m * (ll)m <= x)
l = m;
else
r = m;
}
return l;
}
mt19937 rnd(1337);
ll AR = 19, BR = 13, CR = 23, XR = 228, YR = 322, MODR = 1e9 + 993;
ll myrand(){
ll ZR = (XR * AR + YR * BR + CR) % MODR;
XR = YR;
YR = ZR;
return ZR;
}
const int Mod = 1e9 + 7;
int bpow(int x, int y){
if (y == 0)
return 1;
if (y == 1)
return x;
int ret = bpow(x, y >> 1);
ret = (ret * (ll)ret) % Mod;
if (y & 1)
ret = (ret * (ll)x) % Mod;
return ret;
}
int bdiv(int x, int y){
return (x * (ll)bpow(y, Mod - 2)) % Mod;
}
void setmin(int &x, int y){
x = min(x, y);
}
void setmax(int &x, int y){
x = max(x, y);
}
const ll llinf = 1e18 + 100;
const int maxn = 1e5 + 100, inf = 1e9 + 100, sq = 300, mod = 1e9 + 7, LG = 17;
int n, k;
int a[maxn];
int sp[LG][maxn];
int splast[maxn];
int getmin(int l, int r){
int lg = splast[r - l + 1];
return max(sp[lg][l], sp[lg][r - (1 << lg) + 1]);
}
int dp[2][maxn];
void go(int l, int r, int opl, int opr){
if (l > r)
return;
int m = (l + r) / 2;
int opm = opr;
int val = inf;
for (int i = opl; i <= min(opr, m - 1); i++)
if (dp[0][i] != inf && dp[0][i] + getmin(i + 1, m) <= val)
val = dp[0][i] + getmin(i + 1, m), opm = i;
for (int i = l; i <= m - 1; i++)
if (dp[0][i] != inf && dp[0][i] + getmin(i + 1, m) <= val)
val = dp[0][i] + getmin(i + 1, m);
dp[1][m] = val;
go(l, m - 1, opm, opr);
go(m + 1, r, opl, opm);
}
int main()
{
#ifdef ONPC
//ifstream cin("a.in");
//ofstream cout("a.out");
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#else
//ifstream cin("a.in");
//ofstream cout("a.out");
//freopen("trap.in", "r", stdin);
//freopen("trap.out", "w", stdout);
#endif // ONPC
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
splast[1] = 0;
for (int i = 2; i <= n; i++){
splast[i] = splast[i - 1];
if ((1 << (splast[i] + 1)) == i)
splast[i]++;
}
for (int i = 0; i < n; i++)
cin >> a[i], sp[0][i] = a[i];
for (int j = 1; j < LG; j++)
for (int i = 0; i + (1 << j) <= n; i++)
sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << (j - 1))]);
dp[1][0] = a[0];
for (int i = 1; i < n; i++)
dp[1][i] = max(dp[1][i - 1], a[i]);
while (--k){
for (int i = 0; i < n; i++)
dp[0][i] = dp[1][i];
go(0, n - 1, 0, n - 1);
/*for (int i = 0; i < n; i++)
cout << dp[1][i] << ' ';
cout << '\n';
for (int i = 0; i < n; i++){
int val = inf;
for (int j = 0; j < i; j++)
val = min(val, dp[0][j] + getmin(j + 1, i));
cout << val << ' ';
//assert(val == dp[1][i]);
}
cout << '\n' << '\n';*/
}
cout << dp[1][n - 1];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
10392 KB |
Output is correct |
2 |
Correct |
0 ms |
10392 KB |
Output is correct |
3 |
Correct |
0 ms |
10392 KB |
Output is correct |
4 |
Correct |
0 ms |
10392 KB |
Output is correct |
5 |
Correct |
0 ms |
10392 KB |
Output is correct |
6 |
Correct |
0 ms |
10392 KB |
Output is correct |
7 |
Correct |
0 ms |
10392 KB |
Output is correct |
8 |
Incorrect |
0 ms |
10392 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
10392 KB |
Output is correct |
2 |
Correct |
0 ms |
10392 KB |
Output is correct |
3 |
Correct |
0 ms |
10392 KB |
Output is correct |
4 |
Correct |
0 ms |
10392 KB |
Output is correct |
5 |
Correct |
0 ms |
10392 KB |
Output is correct |
6 |
Correct |
0 ms |
10392 KB |
Output is correct |
7 |
Correct |
0 ms |
10392 KB |
Output is correct |
8 |
Incorrect |
0 ms |
10392 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
10392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
10392 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |