Ninja and Candies Problem

Ninja, a boy from Ninjaland, receives 1 coin every morning from his mother. He wants to purchase exactly 'N' candies. Each candy usually costs 2 coins, but it is available for 1 coin if on sale. Ninja aims to acquire exactly K[i] candies of the i-th type by purchasing them in the evening.

Ninja can buy any number of candies, including zero, of any type on any day, provided he has sufficient money. If candies are on sale, he can buy them at the discounted price of 1 coin; otherwise, the regular price is 2 coins.

In the candy shop, there are 'M' special sale offers. The j-th offer (D[j], C[j]) indicates that candies of the C[j]-th type are on sale on the D[j]-th day.

Ninja's goal is to purchase all candies as quickly as possible. Your task is to calculate the earliest day on which Ninja can buy all the candies.

Input:

The first line of input contains an integer ‘T’, which represents the number of test cases. Then, the test cases follow. Each test case begins with a line containing two space-separated integers, ‘N’ and ‘M’, which denote the total number of candy types and the number of special offers, respectively. The following line in each test case contains ‘N’ space-separated integers K[i], indicating the number of candies of each type that Ninja wants to buy. The next M lines contain two space-separated integers (D[i], C[i]), denoting that candies of the C[i]-th type are on sale on the D[i]-th day.

Output:

For each test case, print the minimum day by which Ninja can purchase all candies. Each result should appear on a new line.

Example:

Consider the following test case:

Input:
T = 1
N = 3, M = 3
K = [2, 3, 5]
Special Offers = [(1, 2), (3, 1), (5, 3)]
Output:
9

Constraints:

  • 1 <= T <= 10
  • 1 <= N, M <= 10^4
  • 0 <= K[i] <= 10^4
  • 1 <= C[i] <= N
  • 1 <= D[i] <= 10^4
  • The sum of all K[i] within a test case is less than 5000.

Note:

You don’t need to print anything explicitly; the task involves implementing the function to compute the answer.
AnswerBot
4mo

Calculate the earliest day on which Ninja can buy all candies given the number of candy types, special offers, and Ninja's candy preferences.

  • Iterate through each day and check if any special offers ar...read more

Help your peers!
Select
Add answer anonymously...

Apple Software Developer Intern interview questions & answers

A Software Developer Intern was asked Q. Kevin and his Fruits Problem Statement Kevin has 'N' buckets, each consisting of...read more
A Software Developer Intern was asked Q. Max GCD Pair Problem Statement Given an array of positive integers, determine th...read more
A Software Developer Intern was asked Q. Lazy Santa Problem Statement It's Christmas and Santa has 'K' gifts to distribut...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked Q1. Kevin and his Fruits Problem Statement Kevin has 'N' buckets, each consisting of...read more
A Software Developer Intern was asked Q2. Max GCD Pair Problem Statement Given an array of positive integers, determine th...read more
A Software Developer Intern was asked Q3. Lazy Santa Problem Statement It's Christmas and Santa has 'K' gifts to distribut...read more
Apple Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits