【luogu】 洛谷普及练习场 动态规划的背包问题[动态规划]

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define rg register
 4 #define ll long long
 5 const int N=30,C=30000+5;
 6 int n,m,ans=0,c[N],a[N],f[C];
 7 template<class t>void rd(t &x)
 8 {
 9     x=0;int w=0;char ch=0;
10     while(!isdigit(ch)) w|=ch=='-',ch=getchar();
11     while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
12     x=w?-x:x;
13 }
14 
15 int main()
16 {
17     rd(n),rd(m);
18     memset(f,0,sizeof(f));
19     for(rg int i=1;i<=m;++i) rd(c[i]),rd(a[i]);
20     for(rg int i=1;i<=m;++i)
21     for(rg int j=n;j>=c[i];--j)
22     f[j]=max(f[j],f[j-c[i]]+c[i]*a[i]);
23     for(rg int i=0;i<=n;++i) ans=max(ans,f[i]);
24     printf("%d",ans);
25     return 0;
26 }
27 
28 P1060 开心的金明
P1060 开心的金明 01背包

相关文章:

  • 2021-05-20
  • 2021-12-30
  • 2022-12-23
猜你喜欢
  • 2021-12-18
  • 2022-02-07
  • 2022-02-07
  • 2022-02-07
  • 2022-02-07
  • 2022-02-07
相关资源
相似解决方案