EIULOGGING3 - LOGGING 3

Xem dạng PDF

Gửi bài giải

Điểm: 1,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 256M
Input: stdin
Output: stdout

Nguồn bài:
Hà Minh Ngọc
Dạng bài
Ngôn ngữ cho phép
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

There is a row of precious timber forests of ~n~ trees numbered from ~1~ to ~n~ and each tree has a monetary value of ~k~. Leo is about to get married, so he wants to choose some cut trees to sell for money. But he is a person who loves nature very much, he does not want to destroy all the forests, so he complies with ~2~ conditions:

  • Never cut down two consecutive trees
  • There are some trees of the rare species with the value ~k < 0~, if the Beo is cut down, he will be fined a mount of money which is equal the absolute value of that tree.

Calculate the maximum amount of money Beo can earn and how many ways he can earn that amount of money.

Input

  • The first line contains a single integer ~n~ (~1 \leq n \leq 10^5~)
  • The next line contains ~n~ integers ~k_i~ (~1 \leq i \leq n~ , ~|k| \leq 10^9~) -value of each tree.

Output

  • Output in a row the maximum amount Beo can get, and the number of ways Bell can choose to cut down the tree. The number of ways to cut a tree can be very large, the printed result is the remainder when divided by ~10^9+7~.

Example Input 1

6
3 1 3 3 2 2

Example Output 1

8 3

Bình luận

Hãy đọc nội quy trước khi bình luận.



  • 1
    eiuphattan2231200138  đã bình luận lúc 3, Tháng 11, 2026, 9:42 ← chỉnh sửa

    Dynamic Programming

    1. Chia nhỏ vấn đề
    2. Tính 1 lần duy nhất
    3. Lưu kết quả lại
    4. Gặp vấn đề lặp lại thì lấy kết quả đã lưu dùng, thay vì tính lại.

    "Never cut down two consecutive trees" Không chặt 2 cây liên tiếp nhau. Giải nghĩa: Ví dụ chọn chặt cây thứ i, thì cây i+1 không được chặt, cây tiếp theo có thể chặt là một cây trong đoạn từ cây thứ i+2 trở về sau.

    Ví dụ với dãy input: 3 1 3 3 2 2

    Chọn cây "3" đầu tiên, cây "1" không được chọn.

    Cây thứ 2 có thể chọn là một trong 4 cây "3 3 2 2"

    Chạy tay với input mẫu
    
    loop bắt đầu với index = 2 
      index:       0 1 2 3 4 5 6 7   
      forest[]:        3 1 3 3 2 2
      cache[]:     0 0 3 3 6 6 8 8
      ways[]:      1 1 1 1 1 2 1 3
    
    output là giá trị cuối mảng cache và ways: 8 3
    

    Pseudocode:

    1. Xử lý input đầu vào (read "forest" array)
    2. Khởi tạo mảng cache có kích thước (n + 2) (tạo 2 số 0 dư ra ở đầu mảng)
    3. Khởi tạo mảng ways để lưu số cách (kích thước n + 2). Gán ways[0], ways[1] = 1. 
    4. Vòng lặp i chạy từ 2 đến (độ dài của cache - 1):
    5. Trong vòng for:  
    
         Nếu cache[i - 1] > forest[i - 2] + cache[i - 2], 
    
             thì cache[i] = cache[i-1] và ways[i] = ways[i-1].
    
         Nếu cache[i - 1] < forest[i - 2] + cache[i - 2], 
    
             thì cache[i] = forest[i - 2] + cache[i - 2] và ways[i] = ways[i-2]
    
         Nếu cache[i - 1] = forest[i - 2] + cache[i - 2] (else), 
    
             thì cache[i] = cache[i-1] và ways[i] = (ways[i-1] + ways[i-2]) % 1000000007;
    
     6. Hết vòng for, Sout(cache[n+1] + " " + ways[n+1])