

Time Limit: 2.8 sec / Memory Limit: 512 MB
配点 : 点
問題文
長さ の数列 が与えられます。
あなたは、数列 に対して次の 種類の操作を好きな順番で好きな回数行うことができます。
- を好きな位置で分割し、分割された列を自由に並べ替える。分割した位置 つにつきコストが かかる。 厳密には、 のコストをかけて長さ の列 と の順列 を自由にとり、 を の昇順に連結したものを新しい とする。
- 整数 と の好きな要素を つ選び、選んだ要素の値に を加える。コストが かかる。
操作をすべて終えたときに と が等しくなるように操作を行うとき、必要なコストの合計の最小値を求めてください。
制約
- 入力はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
出力
答えを出力せよ。
入力例 1Copy
5 1 3 1 4 1 5 9 2 6 5 3
出力例 1Copy
12
例えば、次のように操作をすることで と を等しくすることができます。
- に を加える。コストが かかり、 となる。
- に を加える。コストが かかり、 となる。
- に を加える。コストが かかり、 となる。
- を と に分割し、順番を入れ替える。コストが かかり、 となる。
- に を加える。コストが かかり、 となる。
- に を加える。コストが かかり、 となる。
- に を加える。コストが かかり、 となる。
かかるコストの合計は となります。
コストの合計を 以下にして と を等しくすることはできないため、 と出力してください。
入力例 2Copy
5 1000000000 3 1 4 1 5 9 2 6 5 3
出力例 2Copy
15
例えば、次のように操作をすることで と を等しくすることができます。
- に を加える。コストが かかり、 となる。
- に を加える。コストが かかり、 となる。
- に を加える。コストが かかり、 となる。
- に を加える。コストが かかり、 となる。
- に を加える。コストが かかり、 となる。
かかるコストの合計は となります。
コストの合計を 以下にして と を等しくすることはできないため、 と出力してください。
入力例 3Copy
22 467772225675200 814424018890229 837987908732596 281175505732576 405797525366223 319378664987871 305374284356649 519144936694626 316916938328237 590332737480143 506785561790072 945769796193819 365498597798550 5386616044591 672368930784037 478017750715806 340276460237787 176509793332130 2734777402752 677509027289850 250325127275409 260270543315523 103584313625431 720386673780641 77160494100361 540947273460639 255177791002759 969333325196025 477751866935037 369600749728569 466236682780196 343161112138696 541310338013515 42740499599240 165778332156355 618106559852784 16582487395877 591851763813728 221861304303645 982850624742022 728669467505250 337968530842725 746724490610504 61587851254728 451153536869240
出力例 3Copy
4370668608634071
入力や答えが 整数に収まらない場合があります。
Score : points
Problem Statement
You are given two sequences of length : and .
You can perform the following two types of operations on the sequence any number of times in any order.
- Split at any positions and freely rearrange the split sequences. Each split position incurs a cost of . More formally, at a cost of , take a sequence of length , , and a permutation of , and replace with the concatenation of in ascending order of .
- Choose an integer and any element of , and add to the value of the chosen element, for a cost of .
Find the minimum total cost required to make and equal by performing the operations.
Constraints
- All input values are integers.
Input
The input is given from Standard Input in the following format:
Output
Print the answer.
Sample Input 1Copy
5 1 3 1 4 1 5 9 2 6 5 3
Sample Output 1Copy
12
For example, you can make and equal by performing the following operations.
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Split into and , and swap their order. It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
The total cost incurred is .
It is impossible to make and equal with a total cost of or less, so print .
Sample Input 2Copy
5 1000000000 3 1 4 1 5 9 2 6 5 3
Sample Output 2Copy
15
For example, you can make and equal by performing the following operations.
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
- Add to . It costs , and becomes .
The total cost incurred is .
It is impossible to make and equal with a total cost of or less, so print .
Sample Input 3Copy
22 467772225675200 814424018890229 837987908732596 281175505732576 405797525366223 319378664987871 305374284356649 519144936694626 316916938328237 590332737480143 506785561790072 945769796193819 365498597798550 5386616044591 672368930784037 478017750715806 340276460237787 176509793332130 2734777402752 677509027289850 250325127275409 260270543315523 103584313625431 720386673780641 77160494100361 540947273460639 255177791002759 969333325196025 477751866935037 369600749728569 466236682780196 343161112138696 541310338013515 42740499599240 165778332156355 618106559852784 16582487395877 591851763813728 221861304303645 982850624742022 728669467505250 337968530842725 746724490610504 61587851254728 451153536869240
Sample Output 3Copy
4370668608634071
Note that the input and the answer may not fit into a integer.