• + 1 comment

    Can anyone here help me correct this solution??

    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;
    
    public class Solution {
        
        static long largestValue(int[] A) 
        {
            int size=A.length;
            long values;
            long sum;
            int i,j;
            values=A[0]*A[1];
            sum=A[0]+A[1];
            
            long largeValue=values;
            i=2;
            long sum1,sum2,values1,values2;
            j=0;
            while(i<size-1)
            {
                values1=values+sum*A[i];
                sum1=sum+A[i];
                sum2=sum1-A[j];
                values2=values1-A[j]*sum2;
                if(values1>values2)
                {
                    values=values1;
                    sum=sum1;
                }
                else if(values1==values2)
                {
                    if(sum1>sum2)
                    {
                        
                    values=values1;
                    sum=sum1;
                    }
                    else
                        {
                    values=values2;
                    sum=sum2;
                    j++;
                }
                        
                }
                else
                {
                    values=values2;
                    sum=sum2;
                    j++;
                }
                largeValue=largeValue>values?largeValue:values;
                i++;
            }
            return largeValue;
        }
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            int n = in.nextInt();
            int[] A = new int[n];
            for(int A_i = 0; A_i < n; A_i++){
                A[A_i] = in.nextInt();
            }
            long result = largestValue(A);
            System.out.println(result);
            in.close();
        }
    }