Nap Sort

면접 대비

시간 제한2초메모리 제한1024 MB

요약
최솟값을 반복해서 찾는 정렬과 a_i초 뒤에 깨어나는 도우미 소로 수를 나누어, 정렬이 끝나는 최소 시간을 구한다.
난이도

어려움10점 중 8점

유형
그리디, 정렬, 이분 탐색
정답자
아직 제출이 없습니다

문제

Bessie is trying to sort an array of integers using her own sorting algorithm. She has a pile of NN (1≤N≤2⋅105)(1 \leq N \leq 2\cdot 10^5) integers a_1,a_2,…,a_Na\_1,a\_2,\dots,a\_N (1≤a_i≤1011)(1 \leq a\_i \leq 10^{11}) that she will put in a separate array in sorted order. She repeatedly finds the minimum integer in her pile, removes it, and adds it to the end of the array. It takes Bessie pp seconds to find the minimum integer in a pile of pp integers.

Farmer John instructed some of the other cows in the farm to help Bessie with her task, but they are quite lazy, so Bessie uses that to her advantage. She divides the integers into two piles: Bessie pile and Helper pile. For every integer in Bessie's pile, she performs her algorithm as normal. For every integer in the helper pile, she assigns it to a different helper cow. Farmer John has a large farm, so Bessie can get as many helper cows as she wants. If a helper receives the integer a_ia\_i, Bessie instructs that cow to nap for a_ia\_i seconds, and add their integer to the end of the array immediately when they wake up. If Bessie and a helper add an integer to the array at the same time, Bessie's integer will get added first since she is the leader. If more than one helper gets assigned the same integer, they will add copies of that integer to the array at the same time.

Help Bessie divide her integers so that the final array is sorted and the time it takes to sort the array is minimized.

입력

The first line contains TT, the number of independent test cases (1≤T≤101\le T\le 10).

Each test case is formatted as follows:

The first line of each test case contains the number of integers NN in Bessie's array.

The next line of each test case contains a_1,a_2,…,a_Na\_1, a\_2, \dots, a\_N, the integers that Bessie is sorting. The same integer may appear multiple times.

It is guaranteed that the sum of NN over all tests does not exceed 2⋅1052\cdot 10^5.

출력

For each test case, output the minimum time to sort the array on a new line, if Bessie divides her integers optimally.

예제1

  1. 예제 1

    입력
    4
    5
    1 2 4 5 100000000000
    5
    17 53 4 33 44
    4
    3 5 5 5
    6
    2 5 100 1 4 5
    
    예상 출력
    6
    15
    5
    6