Cod sursa(job #2724506)

Utilizator mariusn01Marius Nicoli mariusn01 Data 17 martie 2021 10:49:18
Problema Subsecventa de suma maxima Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.78 kb
#include <fstream>

using namespace std;
int v[6000001];
int n, imaxim, jmaxim, suma, i, j, k, maxim = -(1<<31);
int main (){
    ifstream fin ("ssm.in");
    ofstream fout("ssm.out");

    fin>>n;
    for (i=1;i<=n;i++)
        fin>>v[i];

    /// solutia 1
    /// punem in evidenta toate secventele posibile si apoi calculam la fiecare
    /// dintre ele suma

    for (i=1;i<=n;i++)
        for (j=i;j<=n;j++) {
            /// ma uit la secventa dintre indicii i si j
            suma = 0;
            for (k=i;k<=j;k++)
                suma += v[k];
            if (suma > maxim) {
                maxim = suma;
                imaxim = i;
                jmaxim = j;
            }
        }
    fout<<maxim<<" "<<imaxim<<" "<<jmaxim<<"\n";
    return 0;
}