No.3417 Tired Santa
Problem Statement
There are $N$ houses lined up on a number line, each waiting for its own dedicated present.
You are Santa. Initially, you are at position $S$ on the number line, carrying $N$ presents.
The $i$-th house is located at $X_i$ on the number line; these positions are pairwise distinct (and also different from $S$).
The $i$-th house is waiting for the $i$-th present, whose weight is $W_i$.
You must deliver the correct present to every house, but each time you move $1$ along the number line, you gain fatigue equal to the total weight of the presents you are carrying at that time.
You can hand over each present upon arriving at its corresponding house, after which you no longer need to carry that present.
When you act so as to minimize the accumulated fatigue at the moment you hand over the last present, output that minimum value.
Input
$N\ S$ $X_1 X_2 \cdots X_N$ $W_1 W_2 \cdots W_N$
- All inputs are integers.
- $1 \leq N \leq 10^3$
- $1 \leq S \leq 10^6$
- $1 \leq X_i,W_i \leq 10^6$
- $X_1 < X_2 < \cdots < X_N$ and $X_i \neq S$
Output
Output an integer.
Sample
Sample 1
Input
2 5 2 7 10 20
Output
110
The following procedure is optimal.
If you visit house $1$ first, the accumulated fatigue is $30 \times 3 + 20 \times 5 = 190$, so the procedure above is optimal.
Sample 2
Input
4 4 1 3 5 7 1 100 1000 10
Output
1383
It is better to deliver the heavy presents first, even if it means going back and forth repeatedly.
Sample 3
Input
7 5 1 2 3 4 6 7 8 3 1 4 1 5 2 9
Output
114
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。
hibit_at
shinchan