Minimum Sum of Maximums
Time limit2sMemory limit1024 MB
Given a line of tiles with some fixed positions, the task is to permute the free tiles to minimize the sum of pairwise maximums of adjacent tiles.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Dynamic programming
- Solved
- No attempts yet
Problem
Bessie has () tiles in a line with ugliness values in that order (). () of the tiles are stuck in place. They are the tiles at indices ().
Bessie wants to minimize the total ugliness of the tiles, which is the sum of the larger ugliness value over every consecutive pair of tiles: . She can swap any two tiles that are both not stuck in place, and she can perform this operation any number of times.
Determine the minimum total ugliness Bessie can achieve by performing the operations optimally.
Input
The first line contains and .
The second line contains .
The third line contains the indices .
Output
Output the minimum possible total ugliness.