#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define gcd __gcd
#define sz(v) (int) v.size()
#define pb push_back
#define pi pair<int,int>
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define dbg(x) "[" #x " = " << (x) << "]"
///#define int long long
using ll = long long;
using ld = long double;
using ull = unsigned long long;
template<typename T> bool ckmx(T& a, const T& b){if(a < b) return a = b, true; return false;}
template<typename T> bool ckmn(T& a, const T& b){if(a > b) return a = b, true; return false;}
const int N = 1e5+5;
const ll MOD = 1e9+7;
const ll INF = 1e18;
int n, k, a[N];
namespace Subtask1{
bool ckSub(){
return n <= 1e2;
}
const int N = 105, K = 105;
int dp[K][N];
void process()
{
FOR(i, 0, k){
FOR(j, 0, n){
dp[i][j] = 1e9 + 1;
}
}
dp[0][0] = 0;
FOR(i, 1, k){
FOR(r, 1, n){
int Mx = a[r];
for(int l = r; l >= 1; l--){
ckmx(Mx, a[l]);
ckmn(dp[i][r], dp[i-1][l-1] + Mx);
}
}
}
cout << dp[k][n] <<"\n";
return;
}
}
namespace Subtask2{
bool ckSub(){
return n <= N;
}
const int K = 105;
ll dp[K][N];
void process()
{
FOR(i, 0, k){
FOR(j, 0, n){
dp[i][j] = INF;
}
}
dp[0][0] = 0;
a[0] = 1e9;
FOR(curK, 1, k){
stack<int> st; st.push(0);
FOR(i, 1, n){
ll best = dp[curK-1][i-1] + a[i], best_pos = i;
while(!st.empty() && a[i] >= a[st.top()]){
if(ckmn(best, dp[curK-1][st.top()-1] + a[i])){
best_pos = st.top();
}
st.pop();
}
if(st.top() > 0){
best = min(best, dp[curK][st.top()]);
best = min(best, dp[curK-1][st.top()] + a[i]);
}
dp[curK][i] = min(dp[curK][i], best);
st.push(best_pos), st.push(i);
}
}
cout << dp[k][n] <<"\n";
return;
}
}
void solve()
{
cin >> n >> k;
FOR(i, 1, n) cin >> a[i];
//Subtask1::process();
Subtask2::process();
return;
if(Subtask1::ckSub()){
return Subtask1::process(), void();
}
else{
return Subtask2::process(), void();
}
return;
}
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP","r",stdin);
freopen(name".OUT","w",stdout);
}
int t = 1; //cin >> t;
while(t--) solve();
return 0;
}
Compilation message
blocks.cpp: In function 'int main()':
blocks.cpp:136:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | freopen(name".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
blocks.cpp:137:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | freopen(name".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
596 KB |
Output is correct |
52 |
Correct |
1 ms |
760 KB |
Output is correct |
53 |
Correct |
1 ms |
1108 KB |
Output is correct |
54 |
Correct |
1 ms |
1108 KB |
Output is correct |
55 |
Correct |
1 ms |
596 KB |
Output is correct |
56 |
Correct |
1 ms |
1108 KB |
Output is correct |
57 |
Correct |
1 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
1 ms |
596 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
596 KB |
Output is correct |
52 |
Correct |
1 ms |
760 KB |
Output is correct |
53 |
Correct |
1 ms |
1108 KB |
Output is correct |
54 |
Correct |
1 ms |
1108 KB |
Output is correct |
55 |
Correct |
1 ms |
596 KB |
Output is correct |
56 |
Correct |
1 ms |
1108 KB |
Output is correct |
57 |
Correct |
1 ms |
1108 KB |
Output is correct |
58 |
Correct |
6 ms |
2764 KB |
Output is correct |
59 |
Correct |
5 ms |
1620 KB |
Output is correct |
60 |
Correct |
12 ms |
3668 KB |
Output is correct |
61 |
Correct |
83 ms |
27988 KB |
Output is correct |
62 |
Correct |
232 ms |
80540 KB |
Output is correct |
63 |
Correct |
19 ms |
4436 KB |
Output is correct |
64 |
Correct |
112 ms |
37452 KB |
Output is correct |
65 |
Correct |
1 ms |
596 KB |
Output is correct |
66 |
Correct |
1 ms |
596 KB |
Output is correct |
67 |
Correct |
1 ms |
596 KB |
Output is correct |
68 |
Correct |
1 ms |
596 KB |
Output is correct |
69 |
Correct |
1 ms |
340 KB |
Output is correct |
70 |
Correct |
1 ms |
596 KB |
Output is correct |
71 |
Correct |
29 ms |
8924 KB |
Output is correct |
72 |
Correct |
2 ms |
596 KB |
Output is correct |
73 |
Correct |
7 ms |
2620 KB |
Output is correct |
74 |
Correct |
5 ms |
1780 KB |
Output is correct |
75 |
Correct |
16 ms |
3552 KB |
Output is correct |
76 |
Correct |
93 ms |
27988 KB |
Output is correct |
77 |
Correct |
271 ms |
80460 KB |
Output is correct |
78 |
Correct |
16 ms |
4444 KB |
Output is correct |
79 |
Correct |
123 ms |
37468 KB |
Output is correct |
80 |
Correct |
11 ms |
2900 KB |
Output is correct |
81 |
Correct |
34 ms |
10164 KB |
Output is correct |
82 |
Correct |
291 ms |
80424 KB |
Output is correct |
83 |
Correct |
1 ms |
352 KB |
Output is correct |
84 |
Correct |
1 ms |
596 KB |
Output is correct |
85 |
Correct |
21 ms |
9072 KB |
Output is correct |
86 |
Correct |
3 ms |
852 KB |
Output is correct |
87 |
Correct |
6 ms |
2644 KB |
Output is correct |
88 |
Correct |
5 ms |
1784 KB |
Output is correct |
89 |
Correct |
14 ms |
3672 KB |
Output is correct |
90 |
Correct |
76 ms |
28244 KB |
Output is correct |
91 |
Correct |
223 ms |
81084 KB |
Output is correct |
92 |
Correct |
14 ms |
4948 KB |
Output is correct |
93 |
Correct |
119 ms |
37828 KB |
Output is correct |
94 |
Correct |
2 ms |
760 KB |
Output is correct |
95 |
Correct |
28 ms |
9036 KB |
Output is correct |
96 |
Correct |
1 ms |
596 KB |
Output is correct |
97 |
Correct |
1 ms |
340 KB |
Output is correct |
98 |
Correct |
1 ms |
592 KB |
Output is correct |
99 |
Correct |
1 ms |
468 KB |
Output is correct |