OPTCUT - Chặt cây

You have to cut a piece of wood into n smaller pieces, each piece is of length ai. The pieces must be cut in order exactly a1, a2 ... an from left to right.

At each step, you may cut a piece into two smaller pieces. The cost for that cut is the length of the piece before cutting.

Different cut order will result in different costs.

For example, you need to cut a wooden stick of length 20 into 4 pieces of length 3, 5, 2 and 10 in order.

When cutting from left to right:

  • 20 cuts into 3 and 17, cost 20.
  • 17 cuts into 5 and 12, cost 17.
  • 12 cuts into 2 and 10, cost 12.

Total cost: 49

When cutting from right to left:

  • 20 cuts into 10 and 10, cost 20.
  • 10 cuts into 8 and 2, cost 10.
  • 8 cuts into 3 and 5, cost 8.

Total cost: 38

Find the minimum cost to cut the initial piece into n given smaller pieces.

Input

Line 1: n (1 ≤ n ≤ 2000)
Line 2: n positive integers a1, a2 ... an, knowing that the length of the wooden bar a1 + a 2 + ... + an ≤ 500000

Output

A single integer which is the smallest cost found.

Example

Input:
4
3 5 2 10

Output:
37

Được gửi lên bởi:Jimmy
Ngày:2009-03-03
Thời gian chạy:0.200s
Giới hạn mã nguồn:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Ngôn ngữ cho phép:Tất cả ngoại trừ: ERL GOSU JS-RHINO PERL6 PYPY RUST SED

hide comments
2021-05-27 18:02:48
Tham khảo: https://vnspoj.github.io/problems/OPTCUT
2021-01-15 10:17:37
qua de voi tran duc manh
2020-03-18 16:36:54
sao cứ bị WA test 1 mãi thế huhu :((
2018-01-09 15:24:11
Làm sao O(N^2) nhỉ. Chẳng lẽ mỗi lần đều chặt rìa.
2017-08-29 03:31:06
O(n) và dell qua test 1. đm
2017-03-01 09:30:51
ai biết test 1 là j ko v @@
2016-11-21 16:48:50
test kiểu gì mà WA ở test 1 thế nhỉ
2016-07-09 21:11:11 noname00.pas
Mình có tham khảo bài viết về "Quy học động tăng tốc"
Code
http://ideone.com/DE4kj0
2015-09-08 14:10:34
https://thewizard6296.wordpress.com/2015/09/04/5/
2015-05-07 04:09:12 lucky++
A di đà phật. Nhị phân sai, O(N3) quá lâu, O(N2) AC, mất toi cả ngày.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.