Minimum Sum of Maximums

시간 제한2초메모리 제한1024 MB

요약
고정되지 않은 타일을 자유롭게 교환해 인접한 모든 쌍의 최댓값 합이 최소가 되도록 배열하되, 최대 여섯 개 타일은 위치가 고정되어 있다.
난이도

보통10점 중 7점

유형
동적 계획법, 정렬, 그리디
정답자
아직 제출이 없습니다

문제

Bessie has NN (2≤N≤3002\le N\le 300) tiles in a line with ugliness values a_1,a_2,…,a_Na\_1, a\_2, \dots, a\_N in that order (1≤a_i≤1061\le a\_i\le 10^6). KK (0≤K≤min⁡(N,6)0\le K\le \min(N,6)) of the tiles are stuck in place; specifically, those at indices x_1,…,x_Kx\_1,\dots, x\_K (1≤x_1<x_2<⋯<x_K≤N1\le x\_1 < x\_2<\dots< x\_K\le N).

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, ∑_i=1N−1max⁡(a_i,a_i+1)\sum\_{i=1}^{N-1}\max(a\_i,a\_{i+1}). 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 NN and KK.

The next line contains a_1,…,a_Na\_1,\dots,a\_N.

The next line contains the KK indices x_1,…,x_Kx\_1,\dots,x\_K.

출력

Output the minimum possible total ugliness.

예제4

  1. 예제 1

    입력
    3 0
    1 100 10
    
    
    예상 출력
    110
    
  2. 예제 2

    입력
    3 1
    1 100 10
    3
    
    예상 출력
    110
    
  3. 예제 3

    입력
    3 1
    1 100 10
    2
    
    예상 출력
    200
    
  4. 예제 4

    입력
    4 2
    1 3 2 4
    2 3
    
    예상 출력
    9