문제링크
http://jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=1098&sca=99&sfl=wr_hit&stx=1825
https://www.acmicpc.net/problem/2662
코드
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st=new StringTokenizer(br.readLine());
int m=Integer.parseInt(st.nextToken());
int n=Integer.parseInt(st.nextToken());
int[][] dp=new int[n+1][m+1];
int[][] pay=new int[n+1][m+1];
int[][] path=new int[n+1][m+1];
for(int i=1;i<=m;i++) {
st=new StringTokenizer(br.readLine());
int idx=Integer.parseInt(st.nextToken());
for(int j=1;j<=n;j++) {
pay[j][idx]=Integer.parseInt(st.nextToken());
}
}
for(int i=1;i<=n;i++) {
for(int cost=1;cost<=m;cost++) {
for(int now=0;now<=cost;now++) {
int temp=pay[i][now]+dp[i-1][cost-now];
if(temp>dp[i][cost]) {
dp[i][cost]=temp;
path[i][cost]=now;
}
}
}
}
//----------------입력
System.out.println(dp[n][m]);
int y=n;
int x=m;
String ans="";
while(y>0) {
ans=path[y][x]+" "+ans;
x-=path[y][x];
y--;
}
System.out.println(ans);
}
}
반응형
'Algorithm > 정올' 카테고리의 다른 글
양팔 저울 (정올 1352,백준 2629) (0) | 2021.07.01 |
---|---|
전기줄 (정올 1257, 백준 2568) (0) | 2021.07.01 |
영역구하기 (정올 1457, 백준 2583) (0) | 2021.06.27 |
세 줄로 타일 깔기 (정올 2112) (0) | 2021.06.27 |
해밀턴순회회로2 (정올 1545 ) (0) | 2021.06.26 |
댓글