#P17012. [GESP202606 六级] 条形蛋糕

    ID: 19302 远端评测题 1000ms 512MiB 尝试: 0 已通过: 0 显示难度普及− 上传者: 标签>动态规划 DP背包 DP2026GESP

[GESP202606 六级] 条形蛋糕

Problem Description

Winter vacation has arrived, and student Xiao Yang plans to find a part-time job and experience what working life is like.

Xiao Yang sent a resume to a cake shop, hoping to help there during the winter vacation. The shop owner has recently run into a problem: the shop makes one long bar-shaped cake every day, but cake pieces of different lengths sell for different prices. How should the cake be cut to earn the most money?

Interestingly, the owner once studied computer science. Recently, he has become very interested in dynamic programming, so he decided to use this problem to test Xiao Yang. The problem is as follows:

  • Given a bar cake of length nn and a price list, where the price of a cake piece of length ii (i=1,2,,ni = 1, 2, \dots, n) is pip_i, find a cutting plan that maximizes the total selling price. Note that the length of each cake piece must be an integer.

Input Format

The first line contains a positive integer nn (1n1031 \le n \le 10^3), representing the total length of the bar cake.

The second line contains nn positive integers p1,p2,,pnp_1, p_2, \dots, p_n (1pi1051 \le p_i \le 10^5), representing the prices of cake pieces of different lengths.

Output Format

Output one positive integer in one line, representing the maximum total selling price.

4
1 5 8 9
10
10
1 5 8 9 10 17 17 20 24 30
30

Hint

Sample Explanation

In the first sample, a cake of length 11 is worth 11, length 22 is worth 55, length 33 is worth 88, and length 44 is worth 99.

For a bar cake with total length 44, there are five essentially different cutting methods: {4}\{4\}, {1,3}\{1, 3\}, {2,2}\{2, 2\}, {1,1,2}\{1, 1, 2\}, {1,1,1,1}\{1, 1, 1, 1\}.

Their corresponding total selling prices are 9,9,10,7,49, 9, 10, 7, 4, so the maximum total selling price is 1010.

In the second sample, for a bar cake of length 1010, the cutting method with the maximum selling price is {10}\{10\}, and the maximum total selling price is 3030.

Translated by ChatGPT 5