-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRodcutting.cpp
More file actions
47 lines (37 loc) · 776 Bytes
/
Rodcutting.cpp
File metadata and controls
47 lines (37 loc) · 776 Bytes
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
//2012004236_정구열
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i, j, max;
int *p, *r, *s;
scanf("%d",&n);
p=(int*)malloc(sizeof(int)*(n+1));
r=(int*)malloc(sizeof(int)*(n+1));
s=(int*)malloc(sizeof(int)*(n+1));
p[0]=0;
r[0]=0;
s[0]=0;
for(i=1;i<=n;i++)
scanf("%d",&p[i]);
for(i=1;i<=n;i++){
max = -1;
for(j=1;j<=i;j++){
if(max<p[j]+r[i-j]){
max = p[j]+r[i-j];
s[i] = j;
}
}
r[i] = max;
}
printf("%d",r[n]);
printf("\n");
while(n>0){
printf("%d ",s[n]);
n -= s[n];
}
printf("\n");
free(s);
free(r);
free(p);
return 0;
}