#include <iostream> #include <algorithm> using namespace std; int main(int argc, char const *argv[]) { int samples_num, curr, odd_prev = 0, odd = 0, sum = 0; std::cin >> samples_num; for (int i = 0; i < samples_num; ++i){ std::cin >> curr; if (curr % 2 == 0) // value is even sum += curr; else { // value is odd if ((odd + odd_prev) != 0){ if ((odd_prev + odd) % 2 == 1){ // only one value odd = curr; } else { int min_val = std::min(curr, std::min(odd_prev, odd)); sum += (curr + odd + odd_prev) - min_val; odd_prev = min_val; odd = 0; } } else { odd_prev = curr; } } } if ((odd + odd_prev) % 2 == 0) sum += odd + odd_prev; if (sum == 0){ cout << "NIESTETY\n"; } else { cout << sum << "\n"; } return 0; }
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 | #include <iostream> #include <algorithm> using namespace std; int main(int argc, char const *argv[]) { int samples_num, curr, odd_prev = 0, odd = 0, sum = 0; std::cin >> samples_num; for (int i = 0; i < samples_num; ++i){ std::cin >> curr; if (curr % 2 == 0) // value is even sum += curr; else { // value is odd if ((odd + odd_prev) != 0){ if ((odd_prev + odd) % 2 == 1){ // only one value odd = curr; } else { int min_val = std::min(curr, std::min(odd_prev, odd)); sum += (curr + odd + odd_prev) - min_val; odd_prev = min_val; odd = 0; } } else { odd_prev = curr; } } } if ((odd + odd_prev) % 2 == 0) sum += odd + odd_prev; if (sum == 0){ cout << "NIESTETY\n"; } else { cout << sum << "\n"; } return 0; } |