Pagini recente » Cod sursa (job #1557586) | Cod sursa (job #700676) | Cod sursa (job #205841) | Cod sursa (job #567598) | Cod sursa (job #526410)
Cod sursa(job #526410)
// http://infoarena.ro/problema/ssm
#include <fstream>
using namespace std;
#define INFINITY 0x3f3f3f3f
#define maxLenght 6000001
int lenght,bestSum,leftPosition,rightPosition;
int array[maxLenght],sum[maxLenght],best[maxLenght];
void read();
void getMaxSubsequence();
void findLeftPosition();
void write();
int main() {
read();
getMaxSubsequence();
findLeftPosition();
write();
return (0);
}
void read() {
ifstream in("ssm.in");
in >> lenght;
for(int i=1;i<=lenght;i++)
in >> array[i];
in.close();
}
void getMaxSubsequence() {
bestSum = array[1];
for(int i=1;i<=lenght;++i) {
best[i] = array[i];
if(best[i] < best[i-1] + array[i])
best[i] = best[i-1] + array[i];
if(bestSum < best[i]) {
bestSum = best[i];
rightPosition = i;
}
}
}
void findLeftPosition() {
int tmp = 0;
for(int i=rightPosition;i>=1;i--) {
tmp = tmp + array[i];
if(tmp == bestSum) {
leftPosition = i;
//break;
}
}
}
void write() {
ofstream out("ssm.out");
out << bestSum << " " << leftPosition << " " << rightPosition << "\n";
out.close();
}