#P9468. [EGOI 2023] Candy / 糖果

[EGOI 2023] Candy / 糖果

Background

Day 2 Problem B.

Translated from EGOI2023 candy.

CC BY-SA 3.0

Problem Description

In the city of Ikaagu, it is said that there is a palace whose wealth is beyond imagination. Inside it, along a corridor, there are NN boxes of candy from all over the world. Any traveler passing by can take as much candy as they want, as long as they pay gold equal to the weight of the candy.

The boxes are numbered from left to right from 00 to N1N-1. In box ii, there are aia_i units of candy remaining, where aia_i is a non-negative integer.

As the guardian of the palace, you need to move these boxes so that boxes with lots of candy are closer to the entrance.

You are given the array a0,a1,,aN1a_0,a_1,\cdots,a_{N-1}, and integers FF and TT. In one operation, you are allowed to swap any two adjacent elements in a0,a1,,aN1a_0,a_1,\cdots,a_{N-1}. What is the minimum number of operations needed to make the sum of the first FF elements of the array at least TT?

Input Format

The first line contains three integers N,F,TN,F,T.

The second line contains NN integers a0,a1,,aN1a_0,a_1,\cdots,a_{N-1}.

Output Format

If it is impossible to achieve the goal, output NO.

Otherwise, output one integer, the minimum number of operations.

6 2 27
10 4 20 6 3 3
1
6 5 5000000000
1000000000 1000000000 0 1000000000 1000000000 1000000000
3
3 2 100
20 30 60
NO
1 1 100
100
0

Hint

Sample 11 Explanation

In sample 11, the sum of the first two elements must be at least 2727. This can be achieved with one adjacent swap: swap 44 and 2020. After the swap, the array becomes 10 20 4 6 3 3, and the sum of the first two elements is 10+20=302710+20=30\ge 27.


Sample 22 Explanation

In sample 22, the 00 must be moved all the way to the end of the array; this requires 33 swaps.


Sample 33 Explanation

In sample 33, it is impossible to make the sum of the first two elements at least 100100; the best we can do is 60+30=9060+30=90.


Constraints

For all testdata, 1N1001\le N\le 100, 1FN1\le F\le N, 0T10110\le T\le 10^{11}, 0ai1090\le a_i\le 10^9.

  • Subtask 1 (66 points): N2N\le 2, ai100a_i\le 100, T109T\le 10^9.
  • Subtask 2 (1919 points): ai1a_i\le 1.
  • Subtask 3 (1616 points): N20N\le 20.
  • Subtask 4 (3030 points): ai100a_i\le 100.
  • Subtask 5 (2929 points): no additional constraints.

Hint

The answer may not fit in a 32-bit integer. If you use C++, watch out for possible overflow.

Translated by ChatGPT 5