Pagini recente » Cod sursa (job #1597971) | Cod sursa (job #1269256) | Cod sursa (job #1821635) | Cod sursa (job #2692244) | Cod sursa (job #1515997)
#include <cstdio>
#include <algorithm>
using namespace std;
const int NMAX = 100001;
int N, ANS;
int v[NMAX], d[NMAX];
int add (int a, int x) {
if (x == -1) {
if (a - 1 == 0) {
return N;
}
return a - 1;
}
if (x == 1) {
if (a + 1 == N + 1) {
return 1;
}
return a + 1;
}
if (x == -2) {
a -= 2;
a %= N;
if (a == 0) {
return N;
}
if (a == N + 1) {
return 1;
}
return a;
}
}
inline void complete (int start, int stop) {
for (int i = start; i <= stop; i++) {
d[i] = max (d[i - 1], d[i - 3] + v[i - 1] + v[i]);
}
}
int main () {
freopen ("oo.in", "r", stdin);
freopen ("oo.out", "w", stdout);
scanf ("%d", &N);
for (int i = 1; i <= N; i++) {
scanf ("%d", &v[i]);
}
if (N == 2) {
printf ("%d\n", v[1] + v[2]);
return 0;
}
if (N == 3) {
printf ("%d\n", max (max (v[1] + v[2], v[2] + v[3]), v[1] + v[3]));
return 0;
}
if (N == 4) {
printf ("%d\n", max (max (v[1] + v[2], v[2] + v[3]), max (v[3] + v[4], v[4] + v[1])));
return 0;
}
d[2] = d[3] = d[4] = v[1] + v[2];
complete (5, N - 1);
ANS = d[N - 1];
d[1] = d[2] = d[3] = v[1] + v[N];
complete (4, N - 2);
ANS = max (ANS, d[N - 2]);
d[0] = d[1] = d[2] = 0;
complete (3, N);
ANS = max (ANS, d[N]);
printf ("%d\n", ANS);
return 0;
}