New update is available. Click here to update.

# Ninja And Candies

Last Updated: 23 Feb, 2023
Medium
0/80
Avg time to solve 15 mins
Success Rate 85 %
Share

## Problem Statement

Suggest Edit

#### Ninja wants to buy all candies as soon as possible. Your task is to calculate the minimum day when he can buy all the candies.

Detailed explanation ( Input/output format, Notes, Images )
##### Constraints:
``````1 <= T <= 10
1 <= N, M <= 10^4
0 <= K[i] <= 10^4
1 <= C[i] <= N
1 <= D[i] <= 10^4

Here N denotes the total number of candies and M denotes the total number of special offers.
Here K[i] denotes the number of candies of the i-th type Ninjas has to order.
Here C[i]  and D[i] denote that the candy if the C[i]-th type will be on sale on the D[i]-th day.
Sum of K[i]’s in each test case will be less than 5000.
``````
##### Sample Input 1 :
``````2
4 4
1 0 2 0
1 1
1 3
2 1
2 3
3 3
1 1 1
1 1
1 2
1 3
``````
##### Sample Output 1:
``````4
5
``````
##### Explanation of Input 1:
``````For test case 1, Ninja can buy candies in the following manner-

• Buy candy of type 1 on day 1 for 1 coin.
• Buy candy of type 3 on day 2 for 1 coin.
• Buy candy of type 3 on day 4 for 2 coins.

For test case 2, Ninja can buy candies in the following manner-
• Buy candy of type 1 on day 1 for 1 coin.
• Buy candy of type 2 on day 3 for 2 coins.
• Buy candy of type 3 on day 5 for 2 coins.
``````
##### Sample Input 2 :
``````1
5 6
1 2 0 2 0
2 4
3 3
1 5
1 2
1 5
2 3
``````
##### Sample Output 2:
``````8
``````
Auto
Console