Algorithm training Happy Jinming

Source: Internet
Author: User

Problem description Kim is very happy today, the home purchase of the new house on the key, the new house has a very spacious room of his own dedicated. To his delight, the mother said to him yesterday: "Your room needs to buy what items, how to decorate, you decide, as long as not more than n yuan money on the line." Early this morning, he started budgeting, but he wanted to buy too many things, will certainly exceed the mother limit of N yuan. As a result, he set an important degree of each article, divided into 5, such as: The whole number of the 5th, and so the most important. He also found the price of each item (all integers) from the Internet. He hoped that the sum of the product of the price and the importance of each item would be the largest if not more than n yuan (which could equal N).
The price of the article J is v[j], the importance of w[j], a total of K items selected, the number is J1,J2,......,JK, then the sum is:
v[j1]*w[j1]+v[j2]*w[j2]+. +v[jk]*w[jk]. (where * is multiplication sign)
Please help Jinming to design a shopping list that satisfies the requirements. Enter the format for the 1th line of the input file, two positive integers, separated by a space:
Nm
(where n (<30000) represents the total amount of money, M (<25) is the number of items you wish to purchase. )
From line 2nd to line m+1, line J gives basic data for items numbered j-1, with 2 non-negative integers per line
V P
(where v indicates the price of the item (v<=10000), p indicates that the item's importance ()) The output format output file has only a positive integer, which is the maximum value (<100000000) of the sum of the price and the importance of the item that does not exceed the total amount of money. Sample Input 1000 5
800 2
400 5
300 5
400 3
200 2 Sample output 3900 data size and conventions typical 01 knapsack problem
1 ImportJava.math.BigInteger;2 Importjava.util.Arrays;3 ImportJava.util.Scanner;4 5 6  Public classMain {7     Static intN;8     Static intm;9     Static int[] A;Ten     Static int[] A1; One     Static int[] b; A      Public Static voidMain (string[] args) { -Scanner input =NewScanner (system.in); -n =input.nextint (); them =input.nextint (); -A =New int[M+1]; -A1 =New int[M+1]; -b =New int[M+1] [N+1]; +          for(inti=1;i<=m;i++){ -A[i] =input.nextint (); +A1[i] =input.nextint (); A         } at          for(inti=1;i<=m;i++){ -              for(intj=1;j<=n;j++){ -                 if(a[i]>j) { -B[I][J] = b[i-1][j]; -}Else{ -B[I][J] = Math.max (B[i-1][j], b[i-1][j-a[i]]+a[i]*a1[i]); in                 } -             } to         } + System.out.println (B[m][n]); -          the     } *  $}

Algorithm training Happy Jinming

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.