using System; using System.Collections.Generic; using System.Security.Cryptography.X509Certificates; namespace Csharp { class Program { static int T, K; static int[] Sums = new int[501]; static int[,] Memo = new int[501, 501]; static void Main(string[] args) { T = Convert.ToInt32(Console.ReadLine()); while (T --> 0) { K = Convert.ToInt32(Console.ReadLine()); var inputs = Console.ReadLine().Split(); for (int i = 1; i <= K; ++i) Sums[i] = Sums[i - 1] + Convert.ToInt32(inputs[i - 1]); for (int j = 1; j < K; ++j) { for (int i = 1; i <= K - j; ++i) { Memo[i, i + j] = int.MaxValue; for (int k = i; k < i + j; ++k) Memo[i, i + j] = Math.Min(Memo[i, i + j], Memo[i, k] + Memo[k + 1, i + j]); Memo[i, i + j] += Sums[i + j] - Sums[i - 1]; } } Console.WriteLine(Memo[1, K]); } } } } |