fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. int main() {
  7. int N;
  8. cin >> N;
  9.  
  10. vector<int> heights(N);
  11. for (int i = 0; i < N; ++i) {
  12. cin >> heights[i];
  13. }
  14.  
  15. int minSprays = 0; // Số lần phun ít nhất
  16. int currentHeight = heights[0]; // Chiều cao hiện tại
  17.  
  18. // Duyệt qua từng cây, nếu chiều cao của cây hiện tại nhỏ hơn cây trước đó, cần phun thuốc
  19. for (int i = 1; i < N; ++i) {
  20. if (heights[i] < currentHeight) {
  21. minSprays += currentHeight - heights[i]; // Tính số lần phun cần thiết
  22. } else {
  23. currentHeight = heights[i]; // Cập nhật chiều cao hiện tại
  24. }
  25. }
  26.  
  27. cout << minSprays << endl;
  28.  
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0.01s 5304KB
stdin
5
5 4 4 3 6
stdout
4