2789: Selling CPUs
时间限制: 1 Sec 内存限制: 64 MB
题目描述
You are very happy, that you got a job at ACME Corporation’s CPU factory. After a hard month of labor, your boss has given you c identical CPUs as payment. Apparently, ACME Corporation is low on cash at the moment.
Since you can’t live on CPUs alone, you want to sell them on the market and buy living essentials from the money you make. Unfortunately, the market is very restrictive in the way you are allowed to conduct business. You are only allowed to enter the market once, you can only trade with each merchant once, and you have to visit the traders in a specific order. The market organizers have marked each merchant with a number from 1 to m (the number of merchants) and you must visit them in this order. Each trader has his own price for every amount of CPUs to buy.
输入The input consists of:
• one line with two integers c and m (1 ≤ c, m ≤ 100), where c is the number of CPUs and m is the number of merchants;
• m lines describing the merchants. Each merchant is described by:
– one line with c integers p 1 , . . . , p c (1 ≤ p i ≤ 10 5 for all 1 ≤ i ≤ c), where p i is the amount of money the merchant will give you for i CPUs.
输出Output the maximum amount of money you can make by selling the CPUs you have.
Note that you don’t have to sell all CPUs.
样例输入5 3
1 4 10 1 1
1 1 8 1 1
1 1 9 1 1
样例输出
13
> #include<bits/stdc++.h>
> using namespace std; const int maxn = 103;
> int c,m,ans=0; i
> nt cost[maxn][maxn];
> int dp[maxn][maxn]; //dp[i][j]储存在第i个商家时,已经销售了j个CPU最大值
> void solve()
{
> for(int i=0;i<=c;i++) //在第一个位置时,每一个都是最大值
>
> {
> dp[1][i] = cost[1][i];
> ans = max(ans,dp[1][i]);
> }
> for(int i=2;i<=m;i++)
> {
> for(int j=1;j<=c;j++) // 求在第i个商家时,销售j个cpu的最大值
> {
> for(int k=0;k<=j;k++) //枚举最大值
> {
> dp[i][j] = max(dp[i][j],dp[i-1][j-k]+cost[i][k]);
> }
> ans=max(ans,dp[i][j]);
> }
> }
}
int main()
{
> scanf("%d%d",&c,&m);
> for(int i=1;i<=m;i++)
> {
> for(int j=1;j<=c;j++)
> scanf("%d",&cost[i][j]);
> }
> solve();
> cout<<ans;
}