Cod sursa(job #2273372)

Utilizator sandupetrascoPetrasco Sandu sandupetrasco Data 31 octombrie 2018 14:40:41
Problema Subsecventa de suma maxima Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.18 kb
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define MOD 2011
using namespace std;
typedef long long ll;
typedef pair< ll , ll > PII;

int n, a[1001000], sol, idx1, idx2;

ll divide(int st, int dr){
	if (st > dr) return 0;

	if (st == dr){
		idx1 = idx2 = st;
		return a[st];
	}

	int mid = (st + dr) >> 1;

	divide(st, mid); divide(mid + 1, dr);

	ll d1 = LLONG_MIN, d2 = LLONG_MIN;
	ll sum1 = 0, sum2 = 0;
	int pos1 = 2e9, pos2 = 2e9;

	for (int i = mid; i >= st; i--){
		sum1 += a[i];

		if (d1 < sum1) d1 = sum1, pos1 = i;
		else if (d1 == sum1 && pos1 > i) pos1 = i;
	}

	for (int i = mid + 1; i <= dr; i++){
		sum2 += a[i];

		if (d2 < sum2) d2 = sum2, pos2 = i;
		else if (d2 == sum2 && pos2 > i) pos2 = i;
	}

	if (d1 + d2 > sol){
		sol = d1 + d2;
		idx1 = pos1, idx2 = pos2;
	}else if (d1 + d2 == sol){
		if (pos1 < idx1){
			idx1 = pos1, idx2 = pos2;
		}else if (pos1 == idx1 && pos2 < idx2){
			idx1 = pos1, idx2 = pos2;
		}
	}

}

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

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

	cin >> n;
	for (int i = 1; i <= n; i++){
		cin >> a[i];
	}

	divide(1, n);

	cout << sol << " " << idx1 << " " << idx2;
	
	return 0;
}