Codeforces 366C C. Dima and Salad (DP) __codeforces

Source: Internet
Author: User
Topic Link:


Codeforces 366C topic to the effect:



Give n items, there are two attributes, ask the last property is the sum of the second attribute of K times, the first property is the maximum number. Topic Analysis: We will make a deformation, weight for a[i]-b[i]*k, the benefit is a, then we only need to do the weight of the backpack, the quality of negative to take absolute value to do a backpack, and then the weight of the same backpack benefits and is the current weight of the maximum income.
AC Code:


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#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);
    }
} 
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.