report VI VI EN
Register | Login
  • HOME
  • PROBLEMSET
  • ROADMAP
  • COMPETITION
  • TOPIC
  • RANKING
  • GUIDE
  • MASHUP
  • ABOUT
  • CONTACT
  • Problem
  • Submit
  • Results
Merging elements - MarisaOJ: Marisa Online Judge

Merging elements

Time limit: 1000 ms
Memory limit: 256 MB
You are given an array $A$ of $n$ integers. Merging 2 adjacent elements $A_i$ and $A_{i + 1}$ costs $A_i + A_{i + 1}$. Perform the above operation until there is only $1$ element left in $A$, what is the minimum cost? ### Input - The first line contains an integers $n$. - The second line contains $n$ integers $A_i$. ### Output - Print the minimum cost. ### Constraints - $1 \le n \le 500$. - $1 \le A_i \le 10^9$. ### Example Input: ``` 4 10 20 30 40 ``` Output: ``` 190 ``` The merging happens as follows (bolded elements are merged elements): - (**10, 20**, 30, 40) → (**30**, 30, 40) - (**30, 30**, 40) → (**60**, 40) - (**60, 40**) → (**100**)
Introduction to dynamic programming
Hakurei Shrine
Buying tickets
Reading 2
Longest increasing subsequence
Jealousy
Maximum path 2
Fences painting
Hall
Knapsack 2
Longest common subsequence
Yet another build array problem
Rectangle cutting
Palindrome query
Marisa
Merging elements
Brewing potion 8
Topic
Dynamic Programming
Rating 1200
Source Atcoder
Solution (1) Solution