Cod sursa(job #3183479)

Utilizator Tudor.1234Holota Tudor Matei Tudor.1234 Data 11 decembrie 2023 23:10:02
Problema Subsecventa de suma maxima Scor 10
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.1 kb
#include "fstream"
#define ll long long int
#define petrisor std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
std::ifstream cin("ssm.in");
std::ofstream cout("ssm.out");
const int dim = 6000001;
int v[dim];
ll dp[dim];
void secv_max_dim(int v[], int dimensiune) {
    dp[0] = v[0];
    ll maxx = dp[0];
    int start_index = 0, end_index = 0;

    for (int i = 1; i < dimensiune; i++) {
        if (v[i] > v[i] + dp[i - 1]) {
            dp[i] = v[i];
            start_index = i;
        } else {
            dp[i] = v[i] + dp[i - 1];
        }

        if (dp[i] > maxx) {
            maxx = dp[i];
            end_index = i;
        }
    }
    int actual_start = start_index;
    for (int i = start_index - 1; i >= 0; i--) {
        if (dp[i] == v[actual_start] && v[actual_start] == dp[actual_start]) {
            actual_start = i;
            start_index = i;
        }
    }

    cout << maxx << " " << start_index + 1 << " " << end_index+1 << "\n";
}

signed main() {
    petrisor
    ll n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }
    secv_max_dim(v, n);
    return 0;
}