아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

House Moving

시간 제한1초메모리 제한256 MB

요약
크기가 주어진 M개의 가족을 N개의 집에 서로 다르게 배치해 모든 사람 쌍의 거리 합을 최대로 만든다.
난이도

보통10점 중 7점

유형
그리디, 정렬, 수학
정답자
아직 제출이 없습니다

문제

There are NN houses numbered 1 through NN. The distance between the house ii and the house jj is ∣i−j∣|i - j|.

You want to assign MM families to these houses. There are P_iP\_i people in the ii-th family. No two families can be assigned to the same house.

Your objective is to maximize the distance of residents. For each (unordered) pair of two people among the MM families, compute the distance between their houses. The distance of residents is defined as the sum of these values for all pairs.

Compute the maximum possible value of the distance of residents.

입력

NN MM
P_1P\_1
P_2P\_2
⋮\vdots
P_MP\_M

출력

Print the answer in a single line.

제한

  • 2≤N≤1062 \leq N \leq 10^6
  • 2≤M≤min⁡(N,1000)2 \leq M \leq \min(N, 1000)
  • 1≤P_i≤1001 \leq P\_i \leq 100

힌트

In the Sample 1, let A be the member of the first family, B be the member of the second family, and C, D be the members of the third family.

In the optimal assignment, the first family shuold go to the house 11, the second family should go to the house 22, and the third family shuold go to the house 44.

  • The distance between A and B: 11
  • The distance between A and C: 33
  • The distance between A and D: 33
  • The distance between B and C: 22
  • The distance between B and D: 22
  • The distance between C and D: 00

The distance of residents is 1111.

예제3

  1. 예제 1

    입력
    4 3
    1
    1
    2
    
    예상 출력
    11
    
  2. 예제 2

    입력
    10 10
    3
    1
    4
    1
    5
    9
    2
    6
    5
    3
    
    예상 출력
    2998
    
  3. 예제 3

    입력
    20 10
    2
    7
    1
    8
    2
    8
    1
    8
    2
    8
    
    예상 출력
    9852