#P4553. World Tour by Air – Minimizing Flight Costs

    ID: 17799 Type: Default 1000ms 256MiB

World Tour by Air – Minimizing Flight Costs

World Tour by Air – Minimizing Flight Costs

A famous movie once showed an 80‐person gang traveling the world in 80 days. In this problem a (possibly smaller) group of M travelers wishes to do just that – but only flying westward. The countries are numbered 1 through N from east to west. Each traveler’s itinerary is a (possibly empty) strictly increasing sequence of country numbers where they disembark (i.e. make a landing). When a traveler lands in a country they are said to have visited that country. Note that if a traveler lands in some country i, then, because they only travel westward, they must have landed in all previous countries in their itinerary. In other words, if a traveler’s landing countries are P1, P2, …, Pk (with 0 ≤ k ≤ N), then it must hold that P1 < P2 < … < Pk.

Each country i has an attractiveness value Vi (a positive integer). This value means that exactly Vi travelers must visit country i. (Note that since every traveler who travels must first visit country 1, it is necessary that V1 = M.) It is not hard to see that in order for an assignment of itineraries to exist, the attractiveness values must be monotone nonincreasing (i.e. V1 ≥ V2 ≥ … ≥ VN).

All travel is by airplane. When a traveler buys a flight ticket from country i (or the starting point 0) to country j (with j > i) the ticket cost is given by the square of the distance, that is, (j - i)2. Each traveler who actually makes a stop pays the cost for that leg of their journey. If a traveler’s itinerary is P1, P2, …, Pk, then their total cost is

\(cost = P_1^2 + (P_2 - P_1)^2 + \cdots + (P_k - P_{k-1})^2\)

Since the square function is convex, it is always cheaper to break a long flight into several shorter ones. Thus if it is possible to choose the itineraries so that every traveler makes as many stops as possible the total cost will be minimized. In fact, under the constraint that exactly Vi travelers visit country i, it turns out that the optimal strategy is to have every traveler make every possible stop. (That is, if a traveler goes as far as country r, then their itinerary will be 1, 2, …, r.)

Under this strategy the number of travelers landing in country i is Vi - Vi+1 (where we define VN+1 = 0). Consequently, the total minimum cost is

[ \text{Total Cost} = 1V_1 + 1V_2 + \cdots + 1*V_N = \sum_{i=1}^{N} V_i. ]

Your task is very simple: Given N, M and the attractiveness values V1, V2, …, VN (which satisfy V1 = M and are nonincreasing), compute the minimum total flight cost.

Note: It can be shown that under the feasibility condition the optimal itineraries are unique – every traveler who goes as far as country r will stop in every country from 1 to r – and the minimum total cost is exactly \(\sum_{i=1}^{N} V_i\).

inputFormat

The input consists of two lines:

  • The first line contains two integers N and M (1 ≤ N ≤ 105, 1 ≤ M ≤ 105), where M is the number of travelers and N the number of countries.
  • The second line contains N positive integers V1, V2, …, VN (each ≤ 109), representing the attractiveness of each country. It is guaranteed that V1 = M and that V1 ≥ V2 ≥ … ≥ VN.

outputFormat

Output a single integer – the minimum possible total flight cost.

sample

3 3
3 2 1
6