-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMandragora Forest.cpp
More file actions
50 lines (39 loc) · 1.38 KB
/
Mandragora Forest.cpp
File metadata and controls
50 lines (39 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* Que is at:- HackerRank > HourRank9 > Mandragora Forest */
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
long t;
cin >> t;
while(t--){
long n,num;
long long p = 0,sum = 0;
cin >> n;
vector<long> a;
for(int i=0; i<n; i++){
cin >> num;
a.push_back(num);
}
/* First sort the health point (input array) because according to question Garnet's pet
* can eat one Mandragora, and each time S(his strength point) will increase by 1
* so it is better to eat Mandragora having less health point */
sort(a.begin(),a.end());
/* We will find maximum experience by checking from last;
* Means we first assume that Granet's pet has eaten all the Mandrogora except last one;
* if it will give the max experience then we are done but if not we decrease by one, means
* this time the pet has eaten first n-2 Mandragora, then we find the experience according
* question if this will be max we are done if not we will proceed until S = 0; */
p = a[a.size()-1]*n;
sum = a[a.size()-1];
long S = a.size() - 2;
while(S>=0){
sum += a[S];
p = max(p,sum*(S + 1));
S--;
}
cout << p << endl;
}
return 0;
}