Cod sursa(job #2905724)

Utilizator _Tudor_Tudor C _Tudor_ Data 23 mai 2022 12:24:11
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>
#include <limits>

int main()
{
	std::ifstream fin("ssm.in");
	std::ofstream fout("ssm.out");
	int n, nr;
	int s = -1, ind;
	int s_max = std::numeric_limits<int>::min();
	int ind_max = 0;
	int end_max = std::numeric_limits<int>::max();

	fin >> n;

	for (int i = 1; i <= n; i++)
	{
		fin >> nr;
		if (s > 0)
			s += nr;
		else
		{
			s = nr;
			ind = i;
		}

		if (s > s_max)
		{
			s_max = s;
			ind_max = ind;
			end_max = i;
		}
		if (s == s_max && (ind < ind_max || i - ind < end_max - ind_max))// && i - ind < end_max - ind_max)
		{
			s_max = s;
			ind_max = ind;
			end_max = i;
		}
	}
	fout << s_max << ' ' << ind_max << ' ' << end_max << '\n';
}