This page is still under construction.

Parts of this page are still being built. What you see may change.

Gifts from Santa

Time limit1sMemory limit512 MB

Summary
Partition a prefix of the gift sequence into K consecutive nonempty blocks, each child getting one block, minimizing the sum over blocks of (block sum minus the smallest block sum).
Level

Hard8 of 10

Topics
Dynamic programming, Prefix sum, Binary search, Implementation
Solved
No attempts yet

Problem

Christmas comes again in 2021!

Santa, wondering how to hand out gifts, numbered the children and numbered the gifts, then decided to give consecutive gifts as a bundle. A child who does not receive a gift is disappointed, so every child must receive at least one gift.

When handing out gifts, Santa must start with gift 1, bundle several consecutive gifts, then bundle several starting from the gift immediately after, and repeat this. Also, each child can receive only one bundle. However, not all gifts have to be handed out.

For example, with 2 children and 5 gifts, some ways to hand out gifts are [1, 2-3], [1-2, 3-4], [1-3, 4-5], [1-4, 5].

Ways that are not allowed include the following.

  • [1-2, 4-5]: It violates the rule that after giving gifts, Santa must continue from the immediately next gift.
  • [2-4, 5]: It violates the rule that Santa must start from gift 1.

Each gift has a happiness value that is obtained when it is received. Let kik_i be the sum of the happiness values of the gifts received by the ii-th child.

Santa wants to minimize ∑i=1K(ki−min⁡j=1K(kj))\sum_{i=1}^{K} {(k_i - \min_{j=1}^{K}(k_j))}. Here, min⁡j=1K(kj)\min_{j=1}^{K}(k_j) is the smallest of the sums of happiness values of the gifts received by all children.

Since there are too many gifts to compute this by hand, Santa asks you to write a program that computes this value. Find this value for Santa!

Input

The first line gives an integer KK, the number of children, and an integer NN, the number of gifts.

The second line gives NN integers AiA_i (the happiness value of gift ii), separated by spaces and in order.

  • 1<K≤101 < K \leq 10
  • K≤N≤202,000K \leq N \leq 202,000
  • 1≤Ai<500,0001 \leq A_i < 500,000 (1≤i≤N1 \leq i \leq N)
  • ∑i=1NAi≤500,000\sum_{i=1}^{N}A_i \leq 500,000

Output

Print the minimum value of ∑i=1K(ki−min⁡j=1K(kj))\sum_{i=1}^{K} {(k_i - \min_{j=1}^{K}(k_j))}.

Examples3

  1. Example 1

    Input
    3 5
    1 2 3 4 5
    
    Expected output
    1
    
  2. Example 2

    Input
    3 5
    3 1 4 2 2
    
    Expected output
    0
    
  3. Example 3

    Input
    3 6
    21604 95462 60009 79876 82038 95514
    
    Expected output
    76924