Watch the movie
Time Limit: 3000/1000 MS (Java/others) memory limit: 65535/65535 K (Java/Others)
Total submission (s): 4458 accepted submission (s): 1407
Problem descriptionnew semester is coming, and duoduo has to go to school tomorrow. she decides to have fun tonight and will be very busy after tonight. she like watch cartoon very much. so she wants her uncle to buy some movies and watch
Her tonight. Her grandfather gave them l minutes to watch the cartoon. After that they have to go to sleep.
Duoduo list n piece of movies from 1 to n. all of them are her favorite, and she wants her uncle buy for her. she give a value VI (VI> 0) of the N piece of movies. the higher value a movie gets shows that duoduo likes it more. each movie has a time TI to play
Over. If a movie duoduo choice to watch she won't stop until it goes to end.
But there is a strange problem, the shop just should M piece of movies (not less or more then), it is difficult for her uncle to make the demo. how to Select M piece of movies from N piece of DVDs that duoduo want to get the highest value and the time they
Cost not more then L.
How clever you are! Please help duoduo's uncle.
Inputthe first line of the input file contains a single integer T (1 ≤ T ≤ 10), the number of test cases, followed by input data for each test case:
The first line is: n (n <= 100), m (M <= N), L (L <= 1000)
N: The number of DVDs that duoduo want buy.
M: The number of DVDs that the shop can sale.
L: the longest time that her grandfather allowed to watch.
The second line to n + 1 line, each line contain two numbers. The first number is the time of the ith DVD, and the second number is the value of ith DVD that duoduo rated.
Outputcontain one number. (It is less then 2 ^ 31 .)
The total value that duoduo can get tonight.
If duoduo can't watch all of the movies that her uncle had bought for her, please output 0.
Sample Input
13 2 1011 1001 29 1
Sample output
3
Question: given n movies, each of which is sometimes long and valuable, I want to watch M movies, and the time is limited to less than L. It turns into a backpack problem, let's find exactly m items in N items and compress them into a package with a capacity of L for maximum value.
Import Java. io. *; import Java. util. *; public class main {int N, M, L, M = 1005; int [] [] DP; node [] node; public static void main (string ARGs []) {New Main (). work ();} void work () {vertex SC = new vertex (New bufferedinputstream (system. in); int T = SC. nextint (); While (t --! = 0) {n = SC. nextint (); M = SC. nextint (); L = SC. nextint (); Init (); For (INT I = 0; I <n; I ++) {int A = SC. nextint (); int B = SC. nextint (); node [I] = new node (a, B);} // two-dimensional cost backpack for (INT I = 1; I <= N; I ++) {for (Int J = m; j> 0; j --) {for (int K = L; k> = 0; k --) {int Pa = node [I-1]. a; int PBS = node [I-1]. b; If (k> = PA & DP [J-1] [k-PA]! =-1) {DP [J] [k] = math. max (DP [J] [K], DP [J-1] [k-PA] + Pb) ;}}} if (DP [m] [l]! =-1) system. out. println (DP [m] [l]); elsesystem. out. println (0) ;}} void Init () {node = new node [N]; dp = new int [m] [m]; for (INT I = 0; I <= L; I ++) {arrays. fill (DP [I],-1);} arrays. fill (DP [0], 0);} class node {int A; int B; node (int A, int B) {This. A = A; this. B = B ;}}}