Cod sursa(job #2858015)

Utilizator MR0L3eXMaracine Constantin Razvan MR0L3eX Data 26 februarie 2022 20:20:35
Problema Subsecventa de suma maxima Scor 90
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.92 kb
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include "bits/stdc++.h"

using namespace std;

using ld = long double;
using ll = long long;
using ull = unsigned long long;

#if defined(ONPC)
#include "bits/debug.h"
#endif

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

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int N;
    fin >> N;
    vector<int> v(N);
    for (int &x : v) fin >> x;

    ll ans = 0, best = 0;
    pair<int, int> best_ind = {0, 0}, ans_ind = {0, 0};

    for (int i = 0; i < N; ++i) {
        ll  new_sum = best + v[i];
        if (new_sum > v[i]) {
            best = new_sum;
            best_ind.second = i;
        } else {
            best = v[i];
            best_ind = {i, i};
        }
        if (best > ans) {
            ans = best;
            ans_ind = best_ind;
        }
    }

    fout << ans << ' ' << ans_ind.first + 1 << ' ' << ans_ind.second + 1 << "\n";
}