TopCoder

User's AC Ratio

NaN% (0/0)

Submission's AC Ratio

NaN% (0/0)

Tags

Description

Waterme1on works in a financial institute as an intern. His job is to help making market prediction. The first exercise from his mentor is to learn when to buy and sell a stock to maximize the profit.

Consider the historical stock prices of n-consecutive days of a stock, where the days are numbered 1, 2, ... , n. Let p[i] be the price of each share of the stock on i-th day . Given an integer k, Waterme1on wants to find exactly k pairs of days (a1, b1), (a2, b2), ... , (ak, bk) where 1 ≤ a1 < b1 < a2 < b2 < ... < ak < bk ≤ n.

For each pair (ai, bi) denotes that on day ai Waterme1on would buy one stock and sell it on day bi. Because of some magic reasons, Waterme1on would gain profit (p[ai] - p[bi])2 dollars. Thus, the profit would be simply .

Given the historical stock prices and k, could you help Waterme1on to find exactly k pairs of days to maximize the profit?

西瓜在一個金融公司當實習生,他的工作是利用市場預測來賺大錢。如今,他面臨了他人生第一個大挑戰:透過買賣商品來獲利。

現在西瓜獲得了一個商品在連續 n 天的價格趨勢圖,我們以 p[i] 表示此商品第 i 天的價格。現在給定 k,西瓜想要找恰好 k 個數對 (a1, b1), (a2, b2), ... , (ak, bk),並且,這 k 個數對滿足 1 ≤ a1 < b1 < a2 < b2 < ... < ak < bk ≤ n

一個數對 (ai, bi) 表示西瓜會在 ai 天買入此商品,並且在 bi 天賣出,因為一些神秘原因,西瓜將會在此獲利 (p[ai] - p[bi])2 元,因此我們可以將總獲利簡單的表示成

現在給定價格趨勢圖與 k,請你幫助西瓜找出他所能獲得的最大獲利。

Input Format

The first line contains two integers n, k.

The second line contains n integers p1, p2, ... , pn, where pi is the price of the stock on i - th day.

第一行有兩個整數 n, k

第二行有 n 個整數 p1, p2, ... , pn,其中 pi 表示商品在第 i 天的價格。

  • 2 ≤ n ≤ 3000
  • 1 ≤ k ≤ n/2
  • 1 ≤ pi ≤ 105

Output Format

Output one integer in a single line, which represent the maximum profit.

輸出一個數字於一行,表示西瓜所能獲得的最大獲利。

Sample Input 1

6 2
2 5 4 5 1 3

4 2
5 3 1 4

4 1
5 3 1 4

Sample Output 1

25

13

16

Hints

Problem Source

Migrated from old NTUJ.

Subtasks

No. Testdata Range Score

Testdata and Limits

No. Time Limit (ms) Memory Limit (VSS, KiB) Output Limit (KiB) Subtasks
0 5000 262144 131072
1 5000 262144 131072
2 5000 262144 131072
3 5000 262144 131072
4 5000 262144 131072
5 5000 262144 131072
6 5000 262144 131072
7 5000 262144 131072
8 5000 262144 131072
9 5000 262144 131072
10 5000 262144 131072
11 5000 262144 131072
12 5000 262144 131072
13 5000 262144 131072
14 5000 262144 131072
15 5000 262144 131072
16 5000 262144 131072
17 5000 262144 131072
18 5000 262144 131072
19 5000 262144 131072
20 5000 262144 131072
21 5000 262144 131072
22 5000 262144 131072
23 5000 262144 131072
24 5000 262144 131072
25 5000 262144 131072
26 5000 262144 131072
27 5000 262144 131072
28 5000 262144 131072
29 5000 262144 131072
30 5000 262144 131072
31 5000 262144 131072
32 5000 262144 131072
33 5000 262144 131072
34 5000 262144 131072
35 5000 262144 131072
36 5000 262144 131072
37 5000 262144 131072
38 5000 262144 131072
39 5000 262144 131072
40 5000 262144 131072
41 5000 262144 131072
42 5000 262144 131072
43 5000 262144 131072
44 5000 262144 131072
45 5000 262144 131072
46 5000 262144 131072
47 5000 262144 131072
48 5000 262144 131072
49 5000 262144 131072
50 5000 262144 131072
51 5000 262144 131072
52 5000 262144 131072
53 5000 262144 131072
54 5000 262144 131072
55 5000 262144 131072
56 5000 262144 131072
57 5000 262144 131072
58 5000 262144 131072
59 5000 262144 131072
60 5000 262144 131072
61 5000 262144 131072
62 5000 262144 131072
63 5000 262144 131072
64 5000 262144 131072
65 5000 262144 131072
66 5000 262144 131072
67 5000 262144 131072
68 5000 262144 131072
69 5000 262144 131072
70 5000 262144 131072
71 5000 262144 131072
72 5000 262144 131072
73 5000 262144 131072
74 5000 262144 131072
75 5000 262144 131072
76 5000 262144 131072
77 5000 262144 131072
78 5000 262144 131072
79 5000 262144 131072