fork download
  1. /* package whatever; // 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 Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. Scanner sc=new Scanner(System.in);
  14. int n=sc.nextInt();
  15. int k=sc.nextInt();
  16. int a[]=new int[n+1];
  17. for(int i=1;i<=n;i++)
  18. a[i]=sc.nextInt();
  19.  
  20. int pre[]=new int[n+1];
  21. for(int i=1;i<=n;i++)
  22. pre[i]=a[i]+pre[i-1];
  23.  
  24. int suf[]=new int[n+1];
  25. suf[n]=a[n];
  26.  
  27. for(int i=n-1;i>=1;i--)
  28. suf[i]=a[i]+suf[i+1];
  29.  
  30. int ans=0;
  31. for(int i=1;i<=n-k;i++)
  32. ans=Math.max(ans, pre[i]+suf[n-k+(i+1)]);
  33. System.out.println(ans);
  34. }
  35. }
Success #stdin #stdout 0.13s 54448KB
stdin
5
3
5 -2 3 1 2
stdout
8