Sicily 1342 (0/1 backpack problems)

Source: Internet
Author: User

// The first contact with a backpack is hard to understand! More exercises are required!

# Include "iostream" # include "cmath" using namespace STD; int total [26] [300010]; int main () {int n, m; while (CIN> N> m) {int * price = new int [M + 1]; int * degree = new int [M + 1]; for (INT I = 0; I <= m; I ++) for (Int J = 0; j <= N; j ++) total [I] [J] = 0; For (INT I = 1; I <= m; I ++) CIN> price [I]> degree [I]; int ans = 0; For (INT I = 1; I <= m; I ++) for (Int J = 1; j <= N; j ++) {If (price [I] <= J) {If (total [I- 1] [J] <total [I-1] [J-price [I] + price [I] * degree [I]) // Dynamic Programming formula! Be thorough and familiar with application! {Total [I] [J] = total [I-1] [J-price [I] + price [I] * degree [I]; ans = max (total [I] [J], ANS);} else {total [I] [J] = total [I-1] [J]; ans = max (total [I] [J], ANS) ;}} else {total [I] [J] = total [I-1] [J]; ans = max (total [I] [J], ANS) ;}} cout <ans <Endl ;}}

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: 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.