I'm trying to solve this problem: https://open.kattis.com/problems/alicedigital
Basically my solution is to take inputs as a prefix sum array instead of storing the inputs themselves while also keeping track of the index of every minimum element in the array. Then, I can get each sum that contains the minimum value once and print the maximum of these sums
This is my code so far:
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
while (num-- > 0)
{
int n = sc.nextInt(), m = sc.nextInt();
int[] pfs = new int[n];
ArrayList<Integer> min = new ArrayList<>();
//min.add(-1);
for (int i = 0; i < n; i++)
{
int input = sc.nextInt();
if (input == m)
min.add(i);
if (i == 0)
pfs[0] = input;
else
pfs[i] = pfs[i-1] + input;
}
min.add(n);
ArrayList<Integer> sums = new ArrayList<>();
for (int i = 0; i < min.size()-1; i++)
{
if (i == 0)
sums.add(pfs[min.get(i+1)-1]);
else
sums.add(pfs[min.get(i+1)-1] - pfs[min.get(i-1)]);
}
//System.out.println(sums);
System.out.println(Collections.max(sums));
}
}
}
I'm not sure how, but this gives the wrong answer on one of the test cases. I think it's in issue with how I calculated the prefix sums since I not familiar with them at all