Question #34152

One day Bob is playing Zombie World video game. In Zombie World game each round will contain N zombie's and each zombie's energy is Zi (where 1<=i<=N). Bob will start the round with B energy. In order to move to the next level Bob need to kill all the N zombie's but Bob can select any one among N Zombie's. If energy of Bob (B) is less than Zombie energy (Zi) then Bob will die and lose the round else Bob will won, during the fighting with zombie, Bob will lose his energy by (Zi%2)+(Zi/2). At any point of game Bob will play optimally. Now your task is to find out whether Bob can reach to the next level or not.

Input Format:

First line will contains B and N, separated by space, where B is the energy of Bob and N is the number of Zombie. Next line will contain N spaced integers each will represent the energy of zombie.

Line 1

B N, where B is the energy of Bob and N is the number of Zombie

Line 2

Zi, where Zi is a list containing energy of zombies separated by space

Constraints:

1<=N<=10^4

1<=B<=10^9

1<

Input Format:

First line will contains B and N, separated by space, where B is the energy of Bob and N is the number of Zombie. Next line will contain N spaced integers each will represent the energy of zombie.

Line 1

B N, where B is the energy of Bob and N is the number of Zombie

Line 2

Zi, where Zi is a list containing energy of zombies separated by space

Constraints:

1<=N<=10^4

1<=B<=10^9

1<

Expert's answer

#include<iostream>

#include<vector>

#include<algorithm>

using namespace std;

int main()

{

int B;

cin >> B;

int N;

cin >> N;

vector<int> Zi;

int a;

for (int i = 0; i < N; i++) {

cin >> a;

Zi.push_back(a);

}

sort(Zi.begin(), Zi.end()); // sort array of zombie's energy in ascending

reverse(Zi.begin(), Zi.end()); // reverse array.

for (int i = 0; i < N; i++) { // get a sorted array in descending and begin to kill the strongest zombie while bob has energy or number of zombie more than zero

if (B < Zi[i]) {

cout << "Bob will not reach the next level";

system("pause");

exit(0);

}

B -= ((Zi[i] % 2) + (Zi[i] / 2));

}

cout << "Bob will reach the next level!";

system("pause");

return 0;

}

#include<vector>

#include<algorithm>

using namespace std;

int main()

{

int B;

cin >> B;

int N;

cin >> N;

vector<int> Zi;

int a;

for (int i = 0; i < N; i++) {

cin >> a;

Zi.push_back(a);

}

sort(Zi.begin(), Zi.end()); // sort array of zombie's energy in ascending

reverse(Zi.begin(), Zi.end()); // reverse array.

for (int i = 0; i < N; i++) { // get a sorted array in descending and begin to kill the strongest zombie while bob has energy or number of zombie more than zero

if (B < Zi[i]) {

cout << "Bob will not reach the next level";

system("pause");

exit(0);

}

B -= ((Zi[i] % 2) + (Zi[i] / 2));

}

cout << "Bob will reach the next level!";

system("pause");

return 0;

}

## Comments

## Leave a comment