Cod sursa(job #3244052)

Utilizator Alex21Ungureanu Alexandru Alex21 Data 23 septembrie 2024 10:36:40
Problema Subsecventa de suma maxima Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.79 kb
#include <climits>
#include <fstream>
using namespace std;

ifstream fin("ssm.in");
ofstream fout("ssm.out");

#define N 25

int sir[N] = {0};
int sp[N] = {0};

int main() {
    int n;
    fin >> n;

    for (int i = 1; i <= n; i++) {
        fin >> sir[i];
        sp[i] = sp[i - 1] + sir[i];
    }

    int smax = INT_MIN;
    int st = 1;
    int dr = 1;
    int j = 1;

    for (int i = 0; i <= n; i++) {
        if (i + 1 > j) {
            j++;
        }

        if (sp[i] < sp[st - 1]) {
            st = i + 1;

            while (j < n && sp[j] <= sp[dr]) {
                j++;

                if (sp[j] - sp[st - 1] > smax) {
                    dr = j;
                    smax = sp[dr] - sp[st - 1];
                }
            }
        }
    }

    fout << smax << ' ' << st << ' ' << dr;
}