PinterestOA #4
Assign Pins to the Shortest Column
Estimated EasyJudge-ready
Topics (AI-labelled)Stacks & HeapsSimulation
There are k columns, each with initial height 0. Process pins[i] in the original order: place each pin on the column whose current total height is lowest; on a tie in height, choose the column with the smallest index. Return the column index for each pin and the final heights of all columns.
Input
The first line contains n k; the second line contains n positive integers pins[i]. Source constraints: 1≤k≤n≤200000, 1≤pins[i]≤10⁹.
Output
The first line contains the n assigned indices, and the second line contains the final heights of the k columns. Column indices start from 0; total heights use 64-bit integers.
Example 1
Input:
5 2
1 2 3 4 5
Output:
0 1 0 1 0
9 6
Example 2
Input:
3 3
10 20 30
Output:
0 1 2
10 20 30
Example 3
Input:
6 2
5 5 5 5 5 5
Output:
0 1 0 1 0 1
15 15
Editorial
Loading solution…