AmazonOA #185
Get Largest Number
Estimated HardJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
Split the whole array into exactly p non-empty contiguous segments. The cost of a segment is its first element plus its last element; a single-element segment counts that element twice. Output the minimum and the maximum total cost, in that order. The source's second example had them reversed; this site corrects it to 8 12.
Input
The first line contains n p; the second line contains the costs. This site uses 1≤n≤200000, 1≤p≤n, costs 1..10^9.
Output
Output the minimum cost and the maximum cost, separated by a space.
Example 1
Input:
5 3
1 2 3 2 5
Output:
14 18
Example 2
Input:
4 2
1 2 3 4
Output:
8 12
Example 3
Input:
1 1
5
Output:
10 10
Example notes
The answers are 14 18, 8 12 and 10 10.
Editorial
Loading solution…