You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most out of tape space and have as short unused space as possible.

Assumptions:

  • number of tracks on the CD. does not exceed 20
  • no track is longer than N minutes
  • tracks do not repeat
  • length of each track is expressed as an integer number
  • N is also integer

Program should find the set of tracks which fills the tape best and print it in the same sequence as the tracks are stored on the CD

Input

Any number of lines. Each one contains value N, (after space) number of tracks and durations of the tracks. For example from first line in sample data: N=5, number of tracks=3, first track lasts for 1 minute, second one 3 minutes, next one 4 minutes

Output

Set of tracks (and durations) which are the correct solutions and string ``sum:" and sum of duration times.

Sample Input

5 3 1 3 4

10 4 9 8 4 2

20 4 10 5 7 4

90 8 10 23 1 2 3 4 5 7

45 8 4 10 44 43 12 9 8 2

Sample Output

1 4 sum:5
8 2 sum:10
10 5 4 sum:19
10 23 1 2 3 4 5 7 sum:55
4 10 12 9 8 2 sum:45
题目大意:
给定n,m,然后给m个数字,组成尽可能接近n的和,以及组成部分具体数字
解题思路:
01背包模板,需要进行记录路径.
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
int dp[N],val[];
int vis[][N];///记录路径
int main()
{
int n,m;
while(cin>>n>>m)
{
memset(dp,,sizeof dp);
memset(vis,,sizeof vis);
for(int i=;i<m;i++)
cin>>val[i];
for(int i=;i<m;i++)
{
for(int j=n;j>=val[i];j--)
{
if(dp[j]<=dp[j-val[i]]+val[i])
dp[j]=dp[j-val[i]]+val[i],vis[i][j]=;
}
}
int j=n;
for(int i=m-;i>=;i--)///从后往前的线路是唯一的
{
if(vis[i][j])
cout<<val[i]<<' ',j-=val[i];
}
cout<<"sum:"<<dp[n]<<'\n';
}
}

最新文章

  1. [补充工程统计case]科技活动经费sql2014
  2. Hadoop HDFS 架构设计
  3. NGINX小技巧--将所有目录和目录下所有文件分别给与不同的权限
  4. 用DIV+CSS做网页里要设置body和*规定内容
  5. python pandas stack和unstack函数
  6. 使用ajax实现html页面产品详情页文字具体内容
  7. 获取环境变量,0x000000cb 操作系统找不到已输入的环境选项
  8. 6 Prefer and Would rather
  9. [Android P] Android P版本 新功能介绍和兼容性处理(一)
  10. WebStorm微信小程序单位rpx出现空格问题
  11. 一线工程师带你深入学习和使用Kubernetes
  12. Android 为 TextView 添加超链接 (网址,邮件,电话)
  13. 偏远小渔村选手的noip2017游记
  14. 【Java】java.util.Objects 源码学习
  15. leetcode题解:Valid Parentheses(栈的应用-括号匹配)
  16. rkhunter和chkrootkit
  17. UVa-101-木块问题
  18. Docker - Image创建
  19. python链接mysql数据库
  20. [读书笔记] R语言实战 (六) 基本图形方法

热门文章

  1. yii2 checkbox 的使用实例
  2. 18.3.1获得Class对象
  3. 今天发现一个汉字转换成拼音的模块,记录一下,直接pip install xpinyin即可
  4. Spring------自动化装配Bean(二)
  5. log4j:WARN Please initialize the log4j system properly. 异常解决
  6. 3个解析url的php函数
  7. canvas绘制基础
  8. QQ面板拖拽(慕课网DOM事件探秘)(下)
  9. XML验证
  10. 【数据分析 R语言实战】学习笔记 第七章 假设检验及R实现