問題一覧 > 通常問題

No.288 Piggy Bank's Job

レベル : / AI推定難易度 : 2050 / 実行時間制限 : 1ケース 2.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 37
作問者 : hos.lyric
お気に入りにしたユーザー ProblemId : 333 / 自分の提出
問題文最終更新日: 2026-06-25 10:05:33
This statement was translated by AI (claude-opus-5-5, effort medium) and may contain errors or differ in nuance from the original. The original statement takes precedence. Back to the original
yukicoder contest 122 (順位表) の他の問題:

Problem Statement

Chokinbako lives in Country A. In this country, the unit of currency is called the yen, and $N$ kinds of coins are used: $A_1$-yen coins, $A_2$-yen coins, …, $A_N$-yen coins.

Chokinbako has saved up coins and has $10^{100}$ of every coin. One day, Yukiusagi appeared and said that she wanted to deposit $M$ yen. Yukiusagi currently has $K_1$ $A_1$-yen coins, $K_2$ $A_2$-yen coins, …, $K_N$ $A_N$-yen coins. Chokinbako wants to minimize the number of coins Yukiusagi will have after the deposit. To this end, Chokinbako decided to have Yukiusagi deposit more than needed at first and then return the change. Precisely, a non-negative integer $x$ is chosen, Yukiusagi first pays Chokinbako exactly $M + x$ yen in coins, and then Chokinbako pays Yukiusagi exactly $x$ yen in coins.

Write a program to find the minimum number of coins Yukiusagi will have after the deposit. However, if it is impossible to deposit exactly $M$ yen, point this out.

Input

$N$ $M$
$A_1$ $A_2$ $\cdots$ $A_N$
$K_1$ $K_2$ $\cdots$ $K_N$
  • All input values are integers.
  • $1 \le N \le 500$.
  • $1 \le M \le 10^{18}$.
  • $1 \le A_1 \lt A_2 \lt \cdots \lt A_N \le 500$.
  • $0 \le K_i \le 10^{12}$ ($i = 1, 2, \ldots, N$).

Output

Output on $1$ line the minimum number of coins Yukiusagi will have after the deposit. However, if it is impossible to deposit exactly $M$ yen, output $-1$ instead.

Sample

Sample 1
Input
6 31
1 5 10 50 100 500
1 1 0 1 1 1
Output
5

For example, rather than paying $1$ $50$-yen coin and receiving $19$ yen in change, paying $1$ $1$-yen coin and $1$ $50$-yen coin and receiving $2$ $10$-yen coins in change leaves fewer coins.

Sample 2
Input
2 1
47 53
1 1
Output
-1

In this example, it is impossible to deposit exactly $1$ yen.

Sample 3
Input
3 6
1 3 4
2 2 1
Output
2

For example, you may pay $6$ yen with $2$ $1$-yen coins and $1$ $4$-yen coin, making the change $0$ yen.

提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。