FOODIES - ChickenLove

no tags 

A new grand chicken stall is about to open in Chennai. Buddy and Pre being close friends joined hands and planned to go for it.People have to serve themselves here in this stall.

There are N-counters available and each counter has a specified number of chicken nuggets.  
The cost of each nugget being bought at any counter is same as the number of nuggets that are still remaining at the counter at that point of time( Inclusive of the nugget being bought ).

Pre wants to have M nuggets. As usual Buddy accepts to sponsor Pre with all his pocket money. He is so innocent that he obeys each and every order of Pre with no second thought. Pre being cunning wants Buddy to spend her too much money. So she directs buddy to buy nuggets at the counters which she says, so that he spends maximum money for her.

Ofcourse you can't help Buddy :( But try to find him how much he would spend for Pre, if he obeys her like an IDIOT.


The first line of the input contains the number of test cases,T
T test cases follow:
The first line of each test case contains an integer N, denoting the number of counters.
The next line consists of N integers: A1,A2,....,An denoting the number of nuggets available at each counter.
The next line consists of a single integer M,number of nuggets Pre wishes to have.


For each test case output a single integer in a single line denoting the money that buddy would spend.








3 5 4
3 Output: 13


Pre would ask buddy to get two nuggets from the second counter(5+4=9), and then one from the third counter(9+4=13)

hide comments
anirudnits: 2018-12-22 20:02:55

constraints are not correct, there are cases where M > A1+A2+..+An so take care of that and use long long int.

viratian_070: 2017-06-30 10:52:22

n->int wa...n->long long int know why....did it in linear time

vengatesh15: 2017-01-19 19:12:20

did in a linear time....

Afrizal: 2016-06-13 09:03:07

i really don't know why i getting WA, any tricky case?

same algo, but i change my code to LUA and got AC :/

Last edit: 2016-06-13 09:41:59
Abhishek jaiswal: 2016-04-19 19:04:06

It was a good question. ;)
Try test case:
9 9 9 9

thedictator: 2016-02-19 17:48:10

Getting TLE on case 10 :(

Piyush Kumar: 2015-12-01 13:25:24

My O( N + MlogN) is giving timeout. Do I need to find a better algorithm or fast IO will work ?

Bhargav Golla: 2015-10-28 19:39:25

Could you tell me what is wrong with the submission 15493468? I have checked with the overflow and base cases and it works fine

Abhay Jain: 2015-10-22 09:52:17

AC :)

Last edit: 2015-10-25 08:47:14
shantanu tripathi: 2015-10-21 23:05:17

silly mistakes :P.. nice1

Added by:Mohan K
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 GOSU JS-MONKEY
Resource:Own Problem