問題一覧 > 通常問題

No.2006 Decrease All to Zero

レベル : / AI推定難易度 : 2700 / 実行時間制限 : 1ケース 4.000秒 / メモリ制限 : 512 MB / 標準ジャッジ問題
タグ : (AC するまで非表示) / 解いたユーザー数 6
作問者 : 👑 to-omer / テスター : chineristAC ygussany
お気に入りにしたユーザー ProblemId : 7758 / 自分の提出
問題文最終更新日: 2022-07-05 22:15:04
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 351 (順位表) の他の問題:

Problem Statement

There are buttons numbered from $1$ to $N$ and a sequence $A$ of positive integers of length $N$.
Button $i$ is located at coordinate $X_i$ on a number line, and pressing button $i$ $1$ time decreases $A_i$ by $1$.
However, the same button cannot be pressed consecutively. More precisely, when buttons $B_1,B_2,\dots,B_K$ are pressed in this order, $B_j\neq B_{j+1}$ must hold for every $j$ satisfying $1\le j\le K-1$.
Determine whether it is possible to make all elements of $A$ equal to $0$, and if it is possible, find the minimum distance traveled on the number line from the first button press to the last button press.

Constraints

  • All inputs are integers.
  • $1\le N\le 200$
  • $1\le X_1<X_2<\dots<X_N\le 10^{9}$
  • $1\le A_i\le 5000\ (1\le i\le N)$

Input

$N$
$X_1$ $X_2$ $\dots$ $X_N$
$A_1$ $A_2$ $\dots$ $A_N$

Output

If it is impossible to make all elements of $A$ equal to $0$, print $-1$. If it is possible, print the minimum distance traveled.

Sample

Sample 1
Input
3
1 2 3
1 2 1
Output
3

By pressing the buttons in the order $1\to 2\to 3\to 2$, all elements of $A$ can be made $0$ with a travel distance of $3$.
Note that the buttons cannot be pressed in the order $1\to 2\to 2\to 3$.

Sample 2
Input
2
1 2
1 3
Output
-1
Sample 3
Input
10
2 3 5 7 11 13 17 19 23 29
5000 5000 5000 5000 5000 5000 5000 5000 5000 5000
Output
130001

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