No.2006 Decrease All to Zero
タグ : / 解いたユーザー数 6
作問者 : 👑
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もしくは右上の雲マークをクリックしてアカウントを作成してください。