No.2492 Knapsack Problem?
タグ : / 解いたユーザー数 236
作問者 :
Problem Statement
Alice has come to a knapsack shop to buy a knapsack. The shop sells $N$ knapsacks, and the $i\ (1\leq i\leq N)$-th knapsack has capacity $v_i$ and weight $w_i$.
Alice wants a knapsack with as large a capacity as possible that is also not too heavy.
Find the capacity of the knapsack with the maximum capacity among the knapsacks with weight at most $W$.
However, if there is no knapsack with weight at most $W$, output -1.
Input
$N\ W$ $v_1\ w_1$ $v_2\ w_2$ $\vdots$ $v_N\ w_N$
- $1\leq N\leq 1000$
- $1\leq W\leq 1000$
- $1\leq v_i\leq 1000$
- $1\leq w_i\leq 1000$
- All inputs are integers.
Output
If there exist knapsacks with weight at most $W$, output the maximum capacity among them; otherwise, output -1.
Print a newline at the end.
Sample
Sample 1
Input
3 7 6 9 3 3 5 6
Output
5
The knapsacks with weight at most $7$ are the $2,3$-th ones, and the maximum volume among them is $5$, that of the $3$-rd knapsack.
Sample 2
Input
3 14 1 59 2 65 3 58
Output
-1
There is no knapsack with weight at most $14$, so output -1.
Sample 3
Input
10 414 851 708 305 850 438 421 245 191 409 476 764 490 159 386 350 505 531 42 292 822
Output
531
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。