#P17442. 掐头去尾 / Delete and Backspace

    ID: 19955 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 显示难度普及− 上传者: 标签>贪心前缀和2026高校校赛

掐头去尾 / Delete and Backspace

Problem Description

You are given an array aa of length nn. For each k=1,2,…,nk=1,2,\ldots,n, consider the following process independently.

Initially, the array is aa. You must perform exactly kk operations. In each operation, you may choose one of the following:

  • Backspace: delete the first element of the current array.
  • Delete: delete the last element of the current array.

Your score is defined as the value of the element deleted in the kk-th operation.

For each k=1,2,…,nk=1,2,\ldots,n, find the maximum score you can obtain.

Input Format

The first line contains an integer nn, the length of the array (1≤n≤1051\le n\le 10^5).

The second line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n, representing the array aa (1≤ai≤1091\le a_i\le 10^9).

Output Format

Output nn integers. The kk-th integer should be the maximum score achievable when performing exactly kk operations.

5
2 7 8 1 4
4 7 8 8 8

Hint

For k=1k=1, you can only delete the leftmost 22 or the rightmost 44, so the maximum score is 44.

For k=2k=2, you can delete the leftmost 22 first, then delete the leftmost 77. The element deleted in the second operation is 77, so the maximum score is 77.

For k=3k=3, you can delete the leftmost 2,7,82,7,8 in order, making the element deleted in the third operation 88, so the maximum score is 88.

For k=4k=4 and k=5k=5, you can also arrange the earlier operations properly, so that the element deleted in the last operation is still 88.

Translated by ChatGPT 5