2706.cpp (461B)
1 class Solution { 2 public: 3 int buyChoco(const vector<int> &prices, int money) const { 4 int a = prices[0], b = prices[1]; 5 if (a > b) swap(a, b); 6 for (int i = 2; i < prices.size(); i++) { 7 if (a > prices[i]) { 8 b = a; 9 a = prices[i]; 10 } else if (b > prices[i]) { 11 b = prices[i]; 12 } 13 } 14 return a + b <= money ? money - (a + b) : money; 15 } 16 };