Pagini recente » Cod sursa (job #837244) | Cod sursa (job #1002348) | Cod sursa (job #331877) | Cod sursa (job #584093) | Cod sursa (job #2297473)
#include <fstream>
using namespace std;
ifstream fin ("oo.in");
ofstream fout ("oo.out");
int n, w[100005], v[100005], maxim1, maxim2, maxim3;
int main()
{
fin>>n;
for(int i = 1; i <= n; i++){
fin>>v[i];
}
w[1] = 0;
w[2] = 0;
w[3] = v[2] + v[3];
for(int i = 4; i <= n; i++){
if(w[i - 1] < v[i] + v[i - 1] + w[i - 3]){
w[i] = v[i] + v[i - 1] + w[i - 3];
}
else{
w[i] = w[i - 1];
}
}
maxim1 = w[n];
w[1] = v[1] + v[n];
w[2] = max(v[n]+v[1],v[1]+v[2]);
w[3] = max(w[2], v[3]+v[4]);
for(int i = 4; i < n; i++){
if(w[i - 1] < v[i] + v[i - 1] + w[i - 3]){
w[i] = v[i] + v[i - 1] + w[i - 3];
}
else{
w[i] = w[i - 1];
}
}
maxim2 = w[n - 2];
w[1] = 0;
w[2] = v[1] + v[2];
w[3] = max(v[1]+v[2],v[2]+v[3]);
for(int i = 4; i <= n; i++){
if(w[i - 1] < v[i] + v[i - 1] + w[i - 3]){
w[i] = v[i] + v[i - 1] + w[i - 3];
}
else{
w[i] = w[i - 1];
}
}
maxim3 = w[n - 1];
fout<<max(maxim1, max(maxim2, maxim3));
return 0;
}