Cod sursa(job #2740622)

Utilizator vzzev edmond vzze Data 13 aprilie 2021 17:28:38
Problema Subsecventa de suma maxima Scor 65
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.62 kb
#include <fstream>
#include <vector>
#include <string>

std::ifstream f("ssm.in");
std::ofstream o("ssm.out");

int main() {
    int smax = 0, p1max = 0, p2max = 0;
    int s = 0, p1 = 0, p2 = 0;

    int i = 0;
    int n;

    f>>n;

    int x;
    
    f>>x;

    s = x;
    smax = x;

    while(f>>x) {
        i++;
        if(s + x <= 0) {
            s = x;
            p1 = p2 = i;
        } else {
            s += x;
            p2 = i;
            if(s > smax) {
                p1max = p1;
                p2max = p2;
                smax = s;
            } 
        }
    }

    o<<smax<<" "<<p1max + 1<<" "<<p2max + 1;
}