Shoemaker
Time: 1 s · Memory: 256 MB
- Sorting
Statement
A shoemaker has orders. Order takes days. Until work on an order starts, a fine of is paid for every day of delay: if work on order starts after days (that is, days were spent on other orders before it), the fine is .
The shoemaker works on one order at a time and does the orders one after another without breaks. Find an order of jobs with the minimum total fine. If there are several, print the lexicographically smallest sequence of order numbers.
Input format
The first line contains (). The next lines contain and (). Orders are numbered from to .
Output format
The first line contains the minimum total fine. The second line contains numbers — the order of the jobs.
Examples
Input
4 3 4 1 1000 2 2 5 5
Output
42 2 1 3 4
Note
Swapping two neighbouring orders and changes only their fines: with first the extra fine is , with first it is . So goes before if (an exchange argument). Do not compare the ratios as fractions — comparing the products is exact. On a tie the smaller number goes first.
In the example the order is 2 1 3 4 with fine . The total fine reaches — a 64-bit type is needed.
Source: UVa 10026.
