forked from DVampire/LeetCodePro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3457.eat-pizzas.cpp
More file actions
34 lines (32 loc) · 803 Bytes
/
3457.eat-pizzas.cpp
File metadata and controls
34 lines (32 loc) · 803 Bytes
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
#
# @lc app=leetcode id=3457 lang=cpp
#
# [3457] Eat Pizzas!
#
# @lc code=start
class Solution {
public:
long long maxWeight(vector<int>& pizzas) {
sort(pizzas.begin(), pizzas.end());
int n = pizzas.size();
int k = n / 4;
int left = 0;
int right = n - 1;
long long ans = 0;
for(int i = 0; i < k; ++i){
if(i % 2 == 0){ // Odd day
ans += pizzas[right];
--right;
left += 3;
}
else{ // Even day
--right; // Largest becomes Max of this group without adding
ans += pizzas[right]; // Second-largest added
--right;
left += 2;
}
}
return ans;
}
};
# @lc code=end