Cod sursa(job #2978743)

Utilizator speedy_gonzalesDuca Ovidiu speedy_gonzales Data 14 februarie 2023 10:35:02
Problema Subsecventa de suma maxima Scor 85
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.8 kb
#include <iostream>
#include <vector>
#include <map>
#include <cstring>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
#include <stack>
#include <iomanip>

using namespace std;

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


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

    long long sum = 0, maxSum = 0;
    long long startIndex = -1, endIndex = 0;
    for (int i = 0; i < n; ++i) {
        int number;
        fin >> number;
        if (sum < 0) {
            sum = number;
            startIndex = i;
        } else {
            sum += number;
        }
        if (maxSum < sum) {
            maxSum = sum;
            endIndex = i;
        }
    }

    fout << maxSum << " " << startIndex + 1 << " " << endIndex + 1;

    return 0;
}