I have a wrong answer on test case 12, but I have no idea what is wrong. Here is my code:
import java.util.Arrays; import java.util.Scanner;
public class D2416C {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
s.nextLine();
String lin = s.nextLine();
int[] arr = new int[n];
int[] st = new int[5001];
int[] nd = new int[5001];
String[] linarr = lin.split(" ");
Arrays.fill(st, -1);
Arrays.fill(nd, -2);
for(int i = 0; i < n; i++){
arr[i] = Integer.parseInt(linarr[i]);
if(st[arr[i]]==-1)
st[arr[i]] = i;
}
for(int j = n-1;j >= 0; j--)
if(nd[arr[j]]==-2)
nd[arr[j]]=j;
for(int i = 1; i < arr.length; i++)
arr[i]= arr[i]^arr[i-1];
int[] max = new int[n];
max[0] = 0;
for(int i = 0; i < nd.length; i++)
if(nd[i] == 0)
max[0] = i;
for(int i = 1; i < n; i++){
int k = 0;
for(int j = 0; j < nd.length; j++){
if(nd[j]==i){
k = j;
break;
}
}
if(k != 0){
if(st[k] == 0)
max[i] = Math.max(max[i-1],arr[st[k]]);
else
if(st[k] == nd[k])
max[i] = max[i-1]+k;
else{
if(st[k]== 1)
max[i] = Math.max(max[i-1],(arr[nd[k]-1]^arr[st[k]-1]));
else{
int qw = arr[nd[k]-1]^arr[st[k]-1];
max[i] = Math.max(max[i-1],qw+max[st[k]-2]);
}
}
}
else
max[i] = max[i-1];
}
System.out.println(max[n-1]);
}
}
You have mistake:
for(int i = 1; i < arr.length; i++) arr[i]= arr[i]^arr[i-1];
Comfort of a train trip with people on segment from position l to position r is equal to XOR of all distinct codes of cities for people on the segment from position l to position r. For example comfort of {5, 5} is 5, but you will make it equal to 0.