Cod sursa(job #477637)

Utilizator crushackPopescu Silviu crushack Data 15 august 2010 18:11:31
Problema Operatii Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <stdio.h>
#include <fstream>
#define NMax 1000000

typedef unsigned long long ull;
const char IN[]   ="operatii.in";
const char OUT[]  ="operatii.out";

int N;
int s[NMax+1];

ull citireRez()
{
	int i,x;
	ull c=0;
	freopen(IN,"r",stdin);
	scanf("%d",&N);
	
	for (i=0;i<N;i++)
	{
		scanf("%d",&x);
		
		if (s[0]>0 && s[ s[0] ]>x)
		{
			while (s[0]>1 && s[ s[0] ]>x)
			{
				c+= (x>s[s[0]-1]) ? s[s[0]]-x : s[s[0]]-s[s[0]-1];
				s[s[0]]=0;
				s[0]--;
			}
			if (s[ s[0] ]>x)
				c+=s[s[0]]-x;
		}
		if (x!=s[s[0]] || !s[0])
			s[++s[0]]=x;
	}
	while (s[0]>1)
	{
		c+= s[s[0]]-s[s[0]-1];
		s[0]--;
	}
	if (s[0])
		c+=s[s[0]];
	return c;
}

void scriere(ull x)
{
	using namespace std;
	ofstream fout("operatii.out");
	fout<<x<<"\n";
	fout.close();
}

int main()
{
	scriere(citireRez());
	return 0;
}