Pagini recente » Cod sursa (job #382715) | Cod sursa (job #875986) | Cod sursa (job #1869220) | Cod sursa (job #1167723) | Cod sursa (job #1540117)
#include<iostream>
#include<fstream>
#define nmax 200005
using namespace std;
ifstream fin ("buline.in");
ofstream fout ("buline.out");
int smax, smin, stmin, stmax, drmin, drmax, n, S;
int a[nmax];
void Citire()
{
int i, nr, semn;
fin >> n; S = 0;
for (i=1; i<=n; i++)
{
fin >> nr >> semn;
if (semn == 1) a[i] = nr;
else a[i] = nr * (-1);
S += a[i];
}
}
void Ssmax()
{
int i, s, st;
smax = s = a[1];
stmax = drmax = 1;
st = 1;
if (s > 0) {s = 0; st = 2;}
for (i = 2; i <= n; i++)
{
s += a[i];
if (s > smax)
{
smax = s; stmax = st; drmax = i;
}
if (s > 0)
{
s = 0; st = i + 1;
}
}
}
void Ssmin() /// imi ramane in stmin, drmin si smin tot ce imi trebuie
{
int i, s, st;
smin = s = a[1];
stmin = drmin = 1;
st = 1;
if (s > 0) {s = 0; st = 2;}
for (i = 2; i <= n; i++)
{
s += a[i];
if (s < smin)
{
smin = s; stmin = st; drmin = i;
}
if (s > 0)
{
s = 0; st = i + 1;
}
}
}
void Afisare()
{
S = S - smin;
if (S < smax)
fout << smax << " " << stmax << " " << (drmax-stmax+1) << "\n";
else if (S > smax)
{
fout << S << " " << (drmin + 1) << " " << (n - (drmin-stmin+1)) << "\n";
}
else
{
fout << smax << " ";
if (stmax < drmin + 1)
fout << stmax << " " << (drmax-stmax+1) << "\n";
else
fout << (drmin + 1) << " " << (n - (drmin-stmin+1)) << "\n";
}
}
int main ()
{
Citire();
Ssmax();
Ssmin();
Afisare();
fout.close();
fin.close();
return 0;
}