Pagini recente » Cod sursa (job #3206938) | Cod sursa (job #2961152) | Cod sursa (job #2561379) | Cod sursa (job #153551) | Cod sursa (job #176222)
Cod sursa(job #176222)
#include<fstream.h>
int v[100000],x[100000],ok,max;
long s,n,i,poz;
int main ()
{
ifstream f("oo.in");
ofstream g("oo.out");
f>>n;
for (i=0;i<n;i++) f>>v[i];
for (i=0;i<n-1;i++)
x[i]=v[i]+v[i+1];
x[n-1]=v[0]+v[n-1];
ok=1;s=0;
while (ok)
{max=-1;
poz=-1;
for (i=0;i<n;i++)
if (x[i]>max && i>0 && i<n-1) {if (x[i-1]!=-1 && x[i+1]!=-1) {max=x[i];poz=i;}}
else if (x[i]>max && i==0) {if (x[1]!=-1 && x[n-1]!=-1) {max=x[i];poz=i;}}
else if (x[i]>max && i==(n-1)) {if (x[n-2]!=-1 && x[0]!=-1) {max=x[i];poz=i;}}
if (max==-1) {ok=0;break;}
s+=max;
if (poz==0) x[poz]=x[n-1]=x[poz+1]=-1;
else if (poz==(n-1)) x[poz]=x[poz-1]=x[0]=-1;
else x[poz]=x[poz-1]=x[poz+1]=-1;
}
g<<s;
return 0;
}