fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Codechef
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. // your code goes here
  10. int t;
  11. Scanner sc = new Scanner(System.in);
  12. t = sc.nextInt();
  13. while(t-->0){
  14. int n = sc.nextInt();
  15. long[] a =new long[n];
  16. for(int i=0;i<n;i++){
  17. a[i] = sc.nextInt();
  18. }
  19. long sum=0;
  20. for(int i=0;i<n-1;i++){
  21. if(a[i]>a[i+1]){
  22. sum+=a[i];
  23. }else{
  24. sum+=a[i+1];
  25. }
  26. }
  27. sum+=a[n-1];
  28. System.out.println(sum);
  29. }
  30.  
  31. }
  32. }
  33.  
Success #stdin #stdout 0.15s 54492KB
stdin
3
4
1 2 3 4
3
3 2 1
4
2 7 3 5
stdout
13
6
24