Cod sursa(job #1703093)

Utilizator XandraAlexandra Manciu Xandra Data 16 mai 2016 10:43:39
Problema Subsecventa de suma maxima Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.71 kb
#include <fstream>

using namespace std;
ifstream in ("sum.in");
ofstream out ("sum.out");
int main()
{
    int n, i, a[100], S = 0, poz1,poz , poz1max = 1, poz2max = 1, k = 0;
    in >> n;
    for (i = 1; i <= n; i ++)
        in >> a[i];
    poz1 = 1;
    int Smax = a[1];
    for (i = 1; i <= n; i ++)
    {
        S = S + a[i];
        if(a[i] > Smax) {
            Smax = a[i];
            poz1max = poz2max = i;

        }
        if(S > Smax) {
            Smax = S;
            poz1max = poz1;
            poz2max = i;
        }
        if(S < 0) {
            S = 0;
            poz1 = i + 1;
        }
    }
    out << Smax << " " << poz1max << " " << poz2max;
    return 0;
}