Cod sursa(job #2273383)

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

ll n, a[6001000], sol;
int idx1, idx2;

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

	if (st == dr){
		if (a[st] > sol){
			sol = a[st];
			idx1 = idx2 = st;
		}else if (a[st] == sol){
			if (st < idx1){
				idx1 = st, idx2 = st;
			}else if (st == idx1 && st < idx2){
				idx1 = st, idx2 = st;
			}
		}

		return ;
	}

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

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

	ll d1 = a[mid], d2 = a[mid + 1];
	ll sum1 = d1, sum2 = d2;
	int pos1 = 2e9, pos2 = 2e9;

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

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

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

		if (d2 < sum2) d2 = sum2, pos2 = i;
		else if (d2 == sum2 && i < pos2) 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;
}