Card Pairs

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

요약
같은 종류의 카드 두 장을 다른 종류의 카드 한 장으로 바꾸는 거래를 반복할 때, 주어진 초기 카드 수에서 가능한 최대 거래 횟수를 구한다.
난이도

어려움10점 중 8점

유형
그리디, 수학, 조합론, 구현
정답자
아직 제출이 없습니다

문제

You have a hand of cards, where each card has one of nn types. For each ii from 11 to nn, you have a_ia\_i cards with type ii, and the bank has infinite cards with type ii.

You can perform the following trade with the bank any number of times:

Choose any two cards with the same type from your hand, and exchange them for a single card from the bank with any type except the type of the cards you just exchanged. Note that the bank only has cards with types 11 through nn, so you cannot trade for cards with any other types.

For example, here is a valid sequence of trades on the first sample case:

What is the maximum number of trades you can perform?

입력

The first line of the input contains a single integer tt (1≤t≤1051 \le t \le 10^5) --- the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤2⋅1052 \le n \le 2\cdot 10^5) --- the number of card types.

The second line of each test case contains nn integers a_1,a_2⋯a_na\_1, a\_2 \cdots a\_n (0≤a_i≤1090 \le a\_i \le 10^9), where a_ia\_i is the number of cards of type ii that you currently have.

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

출력

For each test case, print a single integer --- the maximum number of trades you can perform.

힌트

The diagram above describes an optimal sequence of trades in the first test case.

In the fourth test case, it is impossible to perform any trades, since you don't start with any pair of cards with the same type, so the answer is 00.

예제1

  1. 예제 1

    입력
    9
    4
    2 0 0 1
    2
    5 2
    6
    1 5 0 6 7 1
    4
    1 1 0 1
    2
    4 2
    7
    1 2 4 8 4 2 1
    2
    1 1
    4
    0 0 0 0
    3
    1000000000 1000000000 1000000000
    
    예상 출력
    2
    5
    19
    0
    5
    21
    0
    0
    2999999999