Minimum Sum of Maximums
시간 제한2초메모리 제한1024 MB
고정되지 않은 타일을 자유롭게 교환해 인접한 모든 쌍의 최댓값 합이 최소가 되도록 배열하되, 최대 여섯 개 타일은 위치가 고정되어 있다.
문제
Bessie has () tiles in a line with ugliness values in that order (). () of the tiles are stuck in place; specifically, those at indices ().
Bessie wants to minimize the total ugliness of the tiles, which is defined as the sum of the maximum ugliness over every consecutive pair of tiles; that is, . She is allowed to perform the following operation any number of times: choose two tiles, neither of which are stuck in place, and swap them.
Determine the minimum possible total ugliness Bessie can achieve if she performs operations optimally.
입력
The first line contains and .
The next line contains .
The next line contains the indices .
출력
Output the minimum possible total ugliness.