fork download
  1. #include <stdio.h>
  2.  
  3. #define MAX_N 200000
  4.  
  5. int n;
  6. long long d;
  7. int a[MAX_N];
  8.  
  9. long long min_moves() {
  10. long long moves = 0;
  11. long long ducks = d; // Start with d ducks at position 1
  12.  
  13. for (int i = 1; i <= n; i++) {
  14. if (ducks >= a[i - 1]) {
  15. // We have enough ducks, so press the button
  16. ducks += a[i - 1]; // Ducks remain in the system
  17. } else {
  18. // Not enough ducks, so bring in extra ducks
  19. long long needed = a[i - 1] - ducks;
  20. moves += needed * (i - 1); // Move needed ducks to this position
  21. ducks = a[i - 1]; // Now we have exactly enough ducks
  22. }
  23. }
  24.  
  25. return moves;
  26. }
  27.  
  28. int main() {
  29. scanf("%d %lld", &n, &d);
  30. for (int i = 1; i <= n; i++) {
  31. scanf("%d", &a[i - 1]);
  32. }
  33.  
  34. printf("%lld\n", min_moves());
  35. return 0;
  36. }
Success #stdin #stdout 0s 5276KB
stdin
2 199
175 42
stdout
0