Codeforces-846B: Math Show(dfs+贪心)

本文介绍了一种在有限时间内参加数学竞赛并最大化得分的方法。通过优先解决耗时较少的问题或完全解决一组问题来提高总分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Math Show
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarp takes part in a math show. He is given n tasks, each consists of k subtasks, numbered 1 through k. It takes him tj minutes to solve the j-th subtask of any task. Thus, time required to solve a subtask depends only on its index, but not on the task itself. Polycarp can solve subtasks in any order.

By solving subtask of arbitrary problem he earns one point. Thus, the number of points for task is equal to the number of solved subtasks in it. Moreover, if Polycarp completely solves the task (solves all k of its subtasks), he recieves one extra point. Thus, total number of points he recieves for the complete solution of the task is k + 1.

Polycarp has M minutes of time. What is the maximum number of points he can earn?

Input

The first line contains three integer numbers nk and M (1 ≤ n ≤ 451 ≤ k ≤ 450 ≤ M ≤ 2·109).

The second line contains k integer numbers, values tj (1 ≤ tj ≤ 1000000), where tj is the time in minutes required to solve j-th subtask of any task.

Output

Print the maximum amount of points Polycarp can earn in M minutes.

Examples
input
3 4 11
1 2 3 4
output
6
input
5 5 10
1 2 4 8 16
output
7
Note

In the first example Polycarp can complete the first task and spend 1 + 2 + 3 + 4 = 10 minutes. He also has the time to solve one subtask of the second task in one minute.

In the second example Polycarp can solve the first subtask of all five tasks and spend 5·1 = 5 minutes. Also he can solve the second subtasks of two tasks and spend 2·2 = 4 minutes. Thus, he earns 5 + 2 = 7 points in total.


思路:从样例来看,要么优先做完k个任务,再去做花费时间少的任务,要么优先做花费时间少的任务。这样最优。dfs一遍即可。

#include<bits/stdc++.h>
using namespace std;
int cost[50],n,k,m;
int dfs(int d,int sum)
{
    if(d>n)return 0;//n个任务都遍历完了
    int ans=0;
    if(sum>=cost[0])ans=max(ans,k+1+dfs(d+1,sum-cost[0]));//做完k个任务
    int score=0;
    for(int i=1;i<=k;i++) //做花费少的任务
    {
        if(sum>=(n-d+1)*cost[i])
        {
            score+=n-d+1;
            if(i==k)score+=n-d+1;
            sum-=(n-d+1)*cost[i];
        }
        else
        {
            score+=sum/cost[i];
            if(i==k)score+=sum/cost[i];
            sum-=(sum/cost[i])*cost[i];
            break;
        }
    }
    return max(ans,score);//取较大值
}
int main()
{
    scanf("%d%d%d",&n,&k,&m);
    cost[0]=0;
    for(int i=1;i<=k;i++)scanf("%d",&cost[i]),cost[0]+=cost[i];
    sort(cost+1,cost+k+1);//先排序
    cout<<dfs(1,m)<<endl;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值