fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int trips(vector<int> &weights){
  5.  
  6. int ans = 0;
  7. map<int, int> freq;
  8.  
  9. for(auto val : weights){
  10. freq[val]++;
  11. }
  12.  
  13. for(auto obj : freq){
  14. int val = obj.second;
  15.  
  16. if(val == 1){
  17. return -1;
  18. }
  19.  
  20. ans += val/3;
  21. if(val%3 != 0){
  22. ans += 1;
  23. }
  24. }
  25.  
  26. return ans;
  27. }
  28.  
  29. int main(){
  30. vector<int> weights = {2, 4, 6, 6, 4, 2, 4};
  31. cout << trips(weights);
  32. return 0;
  33. }
Success #stdin #stdout 0.01s 5408KB
stdin
Standard input is empty
stdout
3