fork download
  1. /* package codechef; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class codechef
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. int T,N,K,max=0;;
  14. Scanner sc = new Scanner(System.in);
  15. T = sc.nextInt();
  16. while(T-->0)
  17. {
  18. N = sc.nextInt();
  19. K = sc.nextInt();
  20.  
  21. int[] a = new int[N];
  22.  
  23. for(int i=0;i<N;i++)
  24. a[i] = sc.nextInt();
  25.  
  26. for(int i = 0;i<=N-K;i++)
  27. {
  28. System.out.println("i = " + i + "k = " + K);
  29. int sum = 0;
  30. for(int j=i;j<i+K;j++)
  31. sum = sum + a[j];
  32.  
  33. if(sum>max)
  34. max = sum;
  35. }
  36. System.out.print("sum = " + max+ " ");
  37. }
  38.  
  39.  
  40. }
  41. }
  42.  
Success #stdin #stdout 0.1s 29432KB
stdin
1
7 2
2 4 8 1 2 1 8
stdout
i = 0k = 2
i = 1k = 2
i = 2k = 2
i = 3k = 2
i = 4k = 2
i = 5k = 2
sum = 12