CODFURY - Megatron and his rage

Infuriated by the Decepticons' defeat after a long epic battle with the Autobots, Megatron, in his rage, has decided to destroy all the planets on his way back to Cybertron from Earth. There are multiple planets between Earth and Cybertron, and each planet has some number of Autobots to guard it from him. Since Megatron is low on ammo, he wants to fight as few Autobots as possible on his way back. He can defeat no more than "M" Autobots in total.

You need to find the maximum number of planets he can possibly destroy in his journey.

NOTE: Megatron can start his "destruction spree" from any planet, and can only move to the next planet from the planet he's currently on.


You will receive one integer "T" denoting the number of test cases. (T <= 20)

Then, the next line will contain two non-negative space-separated integers "P" and "M", where P is the number of planets on his way back (P <= 50000) and M is the maximum number of Autobots that Megatron can see (M <= 1000,000).

After that, one line containing P integers separated by a single space will denote the number of Autobots present in each planet. (For each planet there will be no more than 1000 Autobots).


Your output should consist of "T" pairs of space-separated integers, one pair per line, denoting the number of Autobots Megatron will fight and the number of planets he will destroy respectively.


4 50
20 5 23 45

48 3


Megatron starts at planet 1 (with 20 Autobots) and goes to planet 2, then the 3rd planet, at this point, he has seen 48 Autobots, if he decides to go to planet 4 he will see 93 Autobots... so he stops his journey at the 3rd planet.

Megatron, however, could have started at planet 2 with 5 Autobots, then continue up to the 4th planet, then, he would have seen 73 Autobots, but, as he wants to see the minimum Autobots possible and this number of Autobots exceeds what he wants to see, he decides to choose the way from the 1st to the 3rd planet.

hide comments
boominggem: 2023-01-03 19:03:39

10 10
1 1 1 1 30 2 2 2 2 1
9 10
1 1 1 1 30 2 2 2 2
4 29
20 5 23 45
9 5
4 4
25 2

drkspark: 2022-07-21 10:44:02

-_- got so many WA just because of default value of max autobot's

cpchef7: 2021-07-26 17:00:52

Getting WA in 6th test case, please help

smso: 2019-11-26 10:06:00

Maximize the number of planets and minimize the number of autobots.

Jumpy: 2019-04-21 19:42:34

Strage thing happened. Got AC in nlog(n) approach with 0.13 sec. Whereas, when it used fast I/O optimization adding these two lines, I am getting WA. Requesting author to check why it is so.


mag1x_: 2018-06-23 20:49:27

when corner cases hunt you :3

babur: 2017-06-24 09:12:06


karan_batra: 2016-09-11 18:45:49

Same as ALIEN -_-

krototype: 2016-07-24 09:13:40

Exactly same problem as ALIENS.....O(n) approach

dwij28: 2016-06-27 11:21:24

Just one thing to take care of: In case there are more than one such solution where maximum number of planets is same, choose the one the with less total number of autobots. Classical Two Pointer Problem. :)

Added by:CSI
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64