Cod sursa(job #3282998)

Utilizator AlexPlesescuAlexPlesescu AlexPlesescu Data 7 martie 2025 19:39:30
Problema Subsecventa de suma maxima Scor 70
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.09 kb
#include <bits/stdc++.h>

using namespace std;
#define int long long int 
#define pb push_back
#define len(s) (int) s.size()
const int N = 2e5 + 5, mod = 1e9 + 7, INF = 1e16;

#define cin fin
#define cout fout

ifstream cin("ssm.in");
ofstream cout("ssm.out");

int n;

/// dp[i][j] = numarul maxim care poate fi obtinut daca mai ramane intervalul [i..j]
/// dp[i][j] = max(dp[i][j], dp[i][k] + dp[k + 1][j] / 2);

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	vector<pair<int, int>> dp(n + 5, {-INF, 0});
	int ans = -INF;
	for (int i = 1; i <= n; i++) {
		if (dp[i - 1].first + a[i] > dp[i].first) {
			dp[i].first = dp[i - 1].first + a[i];
			dp[i].second = dp[i - 1].second + 1;
		}
		if (a[i] > dp[i].first) {
			dp[i].first = a[i];
			dp[i].second = 1;
		}
		ans = max(ans, dp[i].first);
	}
	cout << ans << ' ';
	for (int i = 1; i <= n; i++) {
		if (dp[i].first == ans) {
			cout << i - dp[i].second + 1 << ' ' << i;
			return 0;
		}
	}
}