1 条题解
-
3I_am_Chinese_qwq LV 8 MOD @ 2020-02-26 15:38:57
0-1背包模板,不过出题人过于毒瘤,卡常,STL的max过不了
用宏定义max即可
交了十遍才过。
我太弱了,orz出题人
#pragma GCC optimize(10)//请忽略 #include <cstdio> #include <iostream> #include <cmath> #include <cstring> #define max(i,j) (i)>(j)?(i):(j) #define For(i,j,k) for(int i=j;i<=(k);++i) using namespace std; typedef long long ll; struct ios { inline char gc(){ static const int IN_LEN=1<<18|1; static char buf[IN_LEN],*s,*t; return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++; } template <typename _Tp> inline ios & operator >> (_Tp&x){ static char ch,sgn; ch = gc(), sgn = 0; for(;!isdigit(ch);ch=gc()){if(ch==-1)return *this;sgn|=ch=='-';} for(x=0;isdigit(ch);ch=gc())x=x*10+(ch^'0'); sgn&&(x=-x); return *this; } } io; int dp[2580005]; int main(){ register int v,n,w;scanf("%d%d",&v,&n); for(int i=1;i<=n;++i){ io>>w; for(int j=v;j>=w;--j){ dp[j]=max(dp[j],dp[j-w]+w); } } if(dp[v]!=0) printf("%d",dp[v]); else puts("-1"); return 0; }
- 1