-
Notifications
You must be signed in to change notification settings - Fork 0
/
12019.cpp
49 lines (38 loc) · 899 Bytes
/
12019.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
// 12019.cpp
// BOJ
//
// Created by 최환일 on 2017. 12. 15..
// Copyright © 2017년 최환일. All rights reserved.
//
#include <bits/stdc++.h>
using namespace std;
#define INF 1<<20
int n, m;
int dp[101][11][2001];
int p[101];
int rec(int cur, int c, int dirty){
if(c > m) return INF;
if(cur >= n) return 0;
int &ret = dp[cur][c][dirty];
if(ret != -1) return ret;
ret = min(rec(cur + 1, c, dirty + p[cur]),rec(cur+1, c+1, 0));
return ret += dirty*p[cur];
}
void trace(int cur, int c, int dirty){
if(cur >= n) return;
if(c<=m && rec(cur+1, c+1, 0) <= rec(cur+1, c, dirty + p[cur])){
cout << cur + 1 << " ";
trace(cur+1, c+1, 0);
}
else trace(cur+1, c, dirty + p[cur]);
}
int main(){
ios_base::sync_with_stdio(false);
memset(dp, -1, sizeof(dp));
cin >> n >> m;
for(int i=0 ;i<n;i++) cin >> p[i];
cout << rec(0,0,0) << '\n';
trace(0,0,0);
return 0;
}