#include<stdio.h>
#include<stdlib.h>
void findbestpath(int s[] , int n ,int &s1 ,int &s2);
int main(void)
{
int a[3][5] = {-50 , -47 , 36 , -30 , -23 ,
17 , -19 , -34 , -43 , -8 ,
-42 , -3 , -43 , 34 , -45 } ;
int s[5] = {-100 , -100 , -100 , -100 , -100};
for(int j = 0 ; j < 5 ; j++)
{
for(int i = 0 ; i < 3 ;i++)
{
if(s[j] < a[i][j])
{
s[j] = a[i][j];
}
}
}
int s1 = 0 , s2 = 0;
findbestpath( s , 5 , s1 ,s2 );
printf("the best path is :");
for(int i = s1 ; i <= s2 ; i++)
{
printf("%d\t",s[i]);
}
printf("\n");
return 0;
}
void findbestpath(int s[] , int n , int &s1 ,int &s2 )
{
int value = 0 , sum = 0 ;
for(int i = 0 ; i < n ; i++)
{
sum = sum + s[i];
if(sum > value)
{
value = sum ;
s2 = i;
}
if(sum < 0)
{
sum = 0 ;
}
}
int temp = value ;
s1 = s2;
while(temp)
{
temp = temp - s[s1];
s1--;
}
s1++;
}