fork download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. int t,j;
  7. cin>>t;
  8.  
  9. for(j=0;j<t;j++)
  10. {
  11. int N,P;
  12. cin>>N>>P;
  13.  
  14. int A[N],i,tot=0;
  15. for(i=0;i<N;++i)
  16. {
  17. cin>>A[i];
  18. }
  19.  
  20. //sort(A,A+N);
  21.  
  22. for(i=0;i<P;++i)
  23. {
  24. tot+=A[P-1]-A[i];
  25. }
  26.  
  27. int tot1=0;
  28. for(i=P;i<N;++i)
  29. {
  30. tot1=tot+(P-1)*(A[i]-A[i-1]);
  31. tot1-=(A[i-1]-A[i-(P)]);
  32. if(tot1<tot&&tot1>=0)
  33. {
  34. tot=tot1;
  35. }
  36. }
  37.  
  38. cout<<"Case #"<<j+1<<": "<<tot<<endl;
  39. }
  40. }
Success #stdin #stdout 0s 4364KB
stdin
3
4 3
3 1 9 100
6 2
5 5 1 2 3 4
5 5
7 7 1 7 7
stdout
Case #1: 14
Case #2: 0
Case #3: 6