codeforces 366C
给出n个物品,有两个属性,问最后第一个属性的总和是第二个属性的k倍的时候,第一个属性最大是多少。
#include
#include
#include
#include
#define MAX 107
using namespace std;
int dp[MAX*MAX],dd[MAX*MAX];
int n,k,a[MAX],b[MAX],c[MAX];
int main ( )
{
while ( ~scanf ( "%d%d" , &n , &k ) )
{
for ( int i = 0 ; i < n ; i++ )
scanf ( "%d" , &a[i] );
for ( int i = 0 ; i < n ; i++ )
scanf ( "%d" , &b[i] );
for ( int i = 0 ; i < n ; i++ )
c[i] = a[i]-b[i]*k;
memset ( dp , -0x3f , sizeof ( dp ) );
memset ( dd , -0x3f , sizeof ( dd ) );
dp[0] = dd[0] = 0;
for ( int i = 0 ; i < n ; i++ )
if ( c[i] >= 0 )
for ( int j = 10000 ; j >= c[i] ; j-- )
dp[j] = max ( dp[j-c[i]]+a[i] , dp[j] );
for ( int i = 0 ; i < n ; i++ )
if ( c[i] < 0 )
{
c[i] = -c[i];
for ( int j = 10000 ; j >= c[i] ; j-- )
dd[j] = max ( dd[j-c[i]]+a[i] , dd[j] );
}
int ans = -1;
for ( int i = 0; i <= 10000 ; i++ )
{
if ( dd[i] == 0 && dp[i] == 0 )
continue;
ans = max ( ans , dp[i]+dd[i] );
}
printf ( "%d\n" , ans );
}
}