Can someone please point out the mistake in my code for IPL2025

import java.util.;
import java.io.
;
public class Main {
public static void main (String args[]) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
int f=0;
long max[]=new long[n];
for(int i=0;i<n;i++)
{
if(i==0)
{
max[i]=a[i];
f=1;
continue;
}
if(i==1)
{
max[i]=max[i-1]+a[i];
f=2;
continue;
}
if(f==0 || f==1)
{
max[i]=max[i-1]+a[i];
f++;
}
else
{
long temp1=max[i-2]+a[i];
long temp2=0;
if(i>3)
temp2=max[i-3]+a[i]+a[i-1];
else
temp2=a[i]+a[i-1];
if(temp1>=temp2 && temp1>max[i-1])
{
max[i]=temp1;
f=1;
}
else if(temp2>temp1 && temp2>max[i-1])
{
max[i]=temp2;
f=2;
}
else
{
max[i]=max[i-1];
f=0;
}
}
}
System.out.println(max[n-1]);
}
}

/*
i think that is the first two lines, try using:

import java.util.*;
import java.io.*;

/*

it wasn’t that, can you explain your idea for the code?

Indented code

import java.util.*;
import java.io.*;
public class Main {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a[] = new int[n];
        for (int i = 0; i < n; i++)
            a[i] = sc.nextInt();
        int f = 0;
        long max[] = new long[n];
        for (int i = 0; i < n; i++) {
            if (i == 0) {
                max[i] = a[i];
                f = 1;
                continue;
            }
            if (i == 1) {
                max[i] = max[i - 1] + a[i];
                f = 2;
                continue;
            }
            if (f == 0 || f == 1) {
                max[i] = max[i - 1] + a[i];
                f++;
            } else {
                long temp1 = max[i - 2] + a[i];
                long temp2 = 0;
                if (i > 3)
                    temp2 = max[i - 3] + a[i] + a[i - 1];
                else
                    temp2 = a[i] + a[i - 1];
                if (temp1 >= temp2 && temp1 > max[i - 1]) {
                    max[i] = temp1;
                    f = 1;
                } else if (temp2 > temp1 && temp2 > max[i - 1]) {
                    max[i] = temp2;
                    f = 2;
                } else {
                    max[i] = max[i - 1];
                    f = 0;
                }
            }
        }
        System.out.println(max[n - 1]);
    }
}

for every third consecutive number that is encountered, I check the max of:

  1. Sum excluding the current element(i.e. third consecutive number is not added)
  2. Sum including the current and previous element and excluding the (i-2)th element.
  3. Sum including the current and (i-2)th element and excluding the (i-1)th element.

The f variable is to indicate how many consecutive elements are included yet(0,1 or 2). The max array stores the maximum sum upto that particular index.

P.S. Please tell me how to put proper spaces while uploading my code on discuss while asking a question like you formatted my code. Like is there some kind of shortcut to do it?

i don’t know if there is a way to put spaces, but you can use this: Online Java Formatter

i think that is the problem, i don’t know how are u using the f variable, also is better use Math.max(a,b), instead of some if’s, it’s less complicated

the f variable is simply telling me how many consecutive numbers I’ve taken. So when the value of f reaches 3 (which is not allowed) I check the possibilities of including it or excluding it on the basis of which combination yields the maximum sum.

dude, your solution is good, the problem is that in line 29 you put (i > 3), and it had to be (i >= 3)