Pagini recente » Cod sursa (job #1061396) | Cod sursa (job #2885777) | Cod sursa (job #8895) | Cod sursa (job #908060) | Cod sursa (job #2294646)
/**
* author: Telechi Nicolae
*/
#include <iostream>
#include <fstream>
#include <vector>
#include <numeric>
using namespace std;
//----------------------------------------------------------------------------
int main()
{
ifstream in("ssm.in");
ofstream out("ssm.out");
vector<int> ret;
int n = 0;
int x = 0;
in >> n;
for (int i = 0; i < n; ++i)
{
in >> x;
ret.push_back(x);
}
// reserve +1 for a dummy element 0 witch will make our live easier, this will make
// begin and end to point on the correct positions
vector<int> sums;
sums.reserve(ret.size() + 1);
sums.push_back(0);
std::partial_sum(ret.begin(), ret.end(),
back_inserter(sums));
int smax = -2147483647;
int smaxbegin = 0, smaxend = 0;
for (int beg = 0, end = 1; end < sums.size(); ++end)
{
if (sums[end] - sums[beg] > smax)
{
smax = sums[end] - sums[beg];
smaxbegin = beg;
smaxend = end;
}
// longest or shortest <=
if (sums[end] < sums[beg])
beg = end;
}
out << smax << " " << smaxbegin + 1 << " " << smaxend;
return 0;
}