fork download
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. int solution(int A[],int N,int X,int Y)
  6. {
  7. int min_cost=1e+9;
  8. int count,cost;
  9. for(int i=0;i<N;i++)
  10. {
  11. count=1;
  12. cost=A[i];
  13. int j=i;
  14. if(j+Y>N)
  15. {
  16. break;
  17. }
  18. else {
  19. while(count<X)
  20. {
  21. cost=cost+A[j+Y];
  22. j=j+Y;
  23. count++;
  24. }
  25. min_cost=min(cost,min_cost);
  26. }
  27.  
  28. }
  29. //cout<<min_cost;
  30. return min_cost;
  31. }
  32.  
  33. int main() {
  34. // your code goes here
  35. int n;
  36. cin>>n;
  37. int arr[n];
  38. int i=0;
  39. while(i<n)
  40. {
  41. cin>>arr[i];
  42. i++;
  43. }
  44. int x,y;
  45. cin>>x;
  46. cin>>y;
  47. cout<<solution(arr,n,x,y);
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5276KB
stdin
7
1 2 5 1 8 8 4
2
3
stdout
2