본문 바로가기
Algorithm/정올

기업투자(정올 1825,백준2662)

by Ujajuck 2021. 7. 1.

 

문제링크

http://jungol.co.kr/bbs/board.php?bo_table=pbank&wr_id=1098&sca=99&sfl=wr_hit&stx=1825 

 

JUNGOL

 

www.jungol.co.kr

https://www.acmicpc.net/problem/2662 

 

2662번: 기업투자

어떤 투자가가 여러 기업들에게 돈을 투자해서 최대의 이익을 얻고자 한다. 단, 투자는 만원 단위로 할 수 있으며 각 기업은 많이 투자할수록 많은 이익을 투자가에게 돌려준다. 돈을 투자하지

www.acmicpc.net

코드

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);
	}
}

 

 

반응형

댓글