# Cod sursa(job #260988)

Utilizator Data 17 februarie 2009 19:58:56 Subsecventa de suma maxima 85 cpp done Arhiva educationala 1.2 kb
``````#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

const char iname[] = "ssm.in";
const char oname[] = "ssm.out";

const int MAXN = 6000005;

#define FOR(i, a, b)  for (int i = (a); i <= (b); ++ i)
#define Max(a, b)  ((a) > (b) ? (a) : (b))

int S[MAXN], n, bestSum = -int(2e9), beg, end;

void F(int lo, int hi) {
if (hi == lo) {
if (bestSum < S[hi])
bestSum = S[hi], beg = end = hi;
return ;
}
int mid = (hi + lo) / 2;
F(lo, mid);
F(mid + 1, hi);

int suf = 0, pre = 0, left, right;
int maxSuf = -int(1e9), maxPre = -int(1e9);

for (int i = mid; i >= lo; -- i) {
suf += S[i];
if (maxSuf <= suf)  maxSuf = suf, left = i;
}
for (int i = mid + 1; i <= hi; ++ i) {
pre += S[i];
if (maxPre < pre)  maxPre = pre, right = i;
}
if (maxPre + maxSuf > bestSum)
bestSum = maxPre + maxSuf, beg = left, end = right;
}

int main(void) {
ifstream in(iname);
in >> n;
FOR (i, 1, n)  in >> S[i];

ofstream out(oname);
F(1, n);
out << bestSum << " " << beg << " " << end;

in.close(), out.close();
return 0;
}
``````