Cod sursa(job #1566885)

Utilizator horatiuchevalHoratiu Cheval horatiucheval Data 12 ianuarie 2016 18:53:47
Problema Secventa 2 Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.85 kb
#include <iostream>
#include <fstream>
using namespace std;

int main(){
    ifstream fin("secv2.in");
    ofstream fout("secv2.out");

    int sum[50001];
    sum[0] = 0;
    int n, k, x, sc, smax = -2000000000, st, stmax = 1, drmax = 1;
    fin>>n>>k;
    for(int i = 1; i <= n; i++){
        fin>>x;
        sum[i] = sum[i - 1] + x;

        if(i < k)
            continue;
        if(i == k){
            sc = smax = sum[i];
            st = stmax = 1;
            drmax = k;
        }else{
            if(sum[i] - sum[i - k] > sc + x){
                sc = sum[i] - sum[i - k];
                st = i - k + 1;
            }
            else sc += x;
        }

        if(sc > smax){
            smax = sc;
            drmax = i;
            stmax = st;
        }
    }

    fout<<stmax<<" "<<drmax<<" "<<smax;
    fin.close();
    fout.close();
    return 0;
}