fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<int> a;
  26. void consistency(int n) {
  27. unordered_map<int,int> mp;
  28. for(int i=0; i<n; i++){
  29. mp[a[i]]++;
  30. }
  31.  
  32. int ans = 0;
  33. for(auto& it : mp){
  34. int ct = it.second;
  35. if(ct == 1){
  36. cout << - 1<< endl;
  37. return;
  38. }
  39. ans += (ct+2)/3;
  40. }
  41. cout << ans << endl;
  42. }
  43.  
  44. void solve() {
  45.  
  46. int n;
  47. cin >> n;
  48. a.resize(n);
  49. for(int i=0; i<n; i++) cin >> a[i];
  50. consistency(n) ;
  51.  
  52. }
  53.  
  54.  
  55.  
  56.  
  57.  
  58. int32_t main() {
  59. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  60.  
  61. int t = 1;
  62. while (t--) {
  63. solve();
  64. }
  65.  
  66. return 0;
  67. }
Success #stdin #stdout 0s 5320KB
stdin
7
2 4 6 6 4 2 4
stdout
3