#include <iostream> #include <vector> #include "kanapka.h" #include "message.h" using namespace std; int main() { long long N = GetN(); //long long N; cin >> N; vector<long long> a = vector<long long>(); a.reserve(N); long long k = 0; long long total = 0; bool dod = true; for (int i = 0; i < N; i++) { long long w = GetTaste(i); //int w; cin >> w; total += w; if ((w >= 0) == dod) { k += w; } else { dod = !dod; a.push_back(-k); k = w; } } if (k) { a.push_back(-k); } if (a.size() == 1) { if (a[0] < 0) { cout << -a[0] << endl; return 0; } else { cout << 0 << endl; return 0; } } int start = 2; if (a[0] < 1) { start = 3; } long long max = a[start - 2]; long long maxCur = a[start - 2]; while (start < a.size()) { long long cur = maxCur + a[start] + a[start - 1]; if (cur < 0) { if (max < maxCur) { max = maxCur; } maxCur = 0; } else { if (maxCur < cur) { maxCur = cur; } } start += 2; } cout << total + max << endl; //int h; //cin >> h; 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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <iostream> #include <vector> #include "kanapka.h" #include "message.h" using namespace std; int main() { long long N = GetN(); //long long N; cin >> N; vector<long long> a = vector<long long>(); a.reserve(N); long long k = 0; long long total = 0; bool dod = true; for (int i = 0; i < N; i++) { long long w = GetTaste(i); //int w; cin >> w; total += w; if ((w >= 0) == dod) { k += w; } else { dod = !dod; a.push_back(-k); k = w; } } if (k) { a.push_back(-k); } if (a.size() == 1) { if (a[0] < 0) { cout << -a[0] << endl; return 0; } else { cout << 0 << endl; return 0; } } int start = 2; if (a[0] < 1) { start = 3; } long long max = a[start - 2]; long long maxCur = a[start - 2]; while (start < a.size()) { long long cur = maxCur + a[start] + a[start - 1]; if (cur < 0) { if (max < maxCur) { max = maxCur; } maxCur = 0; } else { if (maxCur < cur) { maxCur = cur; } } start += 2; } cout << total + max << endl; //int h; //cin >> h; return 0; } |