Cf-366C-Dima and Salad

Source: Internet
Author: User

Redefine the problem as c = a-B * k; find the maximum value of a when a series of c values are 0. Because c can be positive or negative. So I put the c positive 01 backpack once. Compress the 0-negative c package once. When the two are equal, add the aplus to the maximum value.

#include<stdio.h>  #include<string.h>  #include<algorithm>  #include<iostream>  using namespace std;  #define INF 99999999  int n,k,i,j;  int num[101];  int dp1[100001];  int dp2[100001];  int as[1001];  int bs[1001];  int main()  {      int a,b;      while(~scanf("%d%d",&n,&k))      {          for(i=0;i<=100000;i++)dp1[i]=dp2[i]=-INF;          dp1[0]=dp2[0]=0;          for(i=1;i<=n;i++)scanf("%d",&as[i]);          for(j=1;j<=n;j++)scanf("%d",&bs[j]);          for(i=1;i<=n;i++)          {              a=as[i];              b=bs[i];              num[i]=a-b*k;              if(num[i]<0)              {                  num[i]=-num[i];                  for(j=100000;j>=num[i];j--)                  {                      dp2[j]=max(dp2[j],dp2[j-num[i]]+a);                  }              }              else              {                  for(j=100000;j>=num[i];j--)                  {                      dp1[j]=max(dp1[j],dp1[j-num[i]]+a);                  }              }          }          int maxn;          maxn=-1;          for(i=10000;i>=0;i--)          {              maxn=max(maxn,dp1[i]+dp2[i]);          }          if(maxn==0)maxn=-1;          cout<<maxn<<endl;      }      return 0;  }  

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.