#include<bits/stdc++.h> using namespace std; int main() { int ilosc; cin >> ilosc; vector<int> ile (1001); for (int i=0; i<1001; ++i) ile[i] = 0; for (int i=0; i<ilosc; ++i) { int a; cin >> a; ile[a]++; } int result = 0; for (int i=0 ; i<1001; i+=2) result+=i*ile[i]; int ileniep = 0; for (int i=0; i<1001; i+=2) { ileniep+=ile[i]; } if (ileniep==0 && ilosc==1) cout << "NIESTETY" << endl; else { int temp; int left=0; for (int i=999; i>0; i-=2) { if (ile[i]==0) continue; if (left==0) { if (ile[i]%2==0) result+=i*ile[i]; else { result += i*(ile[i]-1); left = i; } } else { ile[i]--; result+=left+i; left=0; if (ile[i]%2==0) result+=i*ile[i]; else { result += i*(ile[i]-1); left = i; } } } cout << result << endl; } }
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | #include<bits/stdc++.h> using namespace std; int main() { int ilosc; cin >> ilosc; vector<int> ile (1001); for (int i=0; i<1001; ++i) ile[i] = 0; for (int i=0; i<ilosc; ++i) { int a; cin >> a; ile[a]++; } int result = 0; for (int i=0 ; i<1001; i+=2) result+=i*ile[i]; int ileniep = 0; for (int i=0; i<1001; i+=2) { ileniep+=ile[i]; } if (ileniep==0 && ilosc==1) cout << "NIESTETY" << endl; else { int temp; int left=0; for (int i=999; i>0; i-=2) { if (ile[i]==0) continue; if (left==0) { if (ile[i]%2==0) result+=i*ile[i]; else { result += i*(ile[i]-1); left = i; } } else { ile[i]--; result+=left+i; left=0; if (ile[i]%2==0) result+=i*ile[i]; else { result += i*(ile[i]-1); left = i; } } } cout << result << endl; } } |