-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBouquet_Easy_Version.cpp
More file actions
39 lines (33 loc) · 1.26 KB
/
Bouquet_Easy_Version.cpp
File metadata and controls
39 lines (33 loc) · 1.26 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
// there are n flowers and a flower with k petals costs k coins
// we can't buy any 2 flowers who have difference of more than 1 in number of flower petals
// we have m coins , find max number of petals
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
ll testcases;
cin >> testcases;
for (ll testcase = 0; testcase < testcases; testcase++){
ll n, m;
cin >> n >> m;
vector<ll> a(n);
for (ll i = 0; i < n; i++)
cin >> a[i];
sort(a.rbegin(), a.rend()); // Sort petals in ascending order
ll total_cost = 0, petals_bought = 0;
for (ll i = 0; i < n; i++) {
// If this is not the first flower and the difference between consecutive petals > 1, break
if(i > 0 && a[i] - a[i - 1] > 1) {
break;
}
// Check if we can afford the current flower
if(total_cost + a[i] <= m) {
total_cost += a[i]; // Buy the flower
petals_bought += a[i]; // Add its petals to the total
} else {
break; // Stop if we can't afford this flower
}
}
cout << petals_bought << endl; // Output the maximum number of petals we can buy
}
}