Can you help me please this code is show time limit exceeds "Expected Time Limit < 1.732sec"

import java.util.;
import java.lang.
;
import java.io.*;
public class GFG
{
public static void main (String[] args){
Scanner s=new Scanner(System.in);
int testcase=s.nextInt();
while(testcase–>0){
int n=s.nextInt();
int[] arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=s.nextInt();
}
int[] lm=new int[n];
int lmax=arr[0];
lm[0]=arr[0];
for(int i=1;i<n;i++){
if(lmax<arr[i]){
lm[i]=arr[i];
lmax=arr[i];
}
lm[i]=lmax;
}
int[] rm=new int[n];
int rmax=arr[n-1];
rm[n-1]=arr[n-1];
for(int i=n-2;i>=0;i–){
if(rmax<arr[i]){
rm[i]=arr[i];
rmax=arr[i];
}
rm[i]=rmax;
}
int sum=0;
for(int i=0;i<n;i++){
int ans=Math.min(lm[i],rm[i])-arr[i];
sum=sum+ans; }
System.out.println(sum);
}
}
}