# ccc 16 s4 Combining Riceballs

Alphonse has N rice balls of various sizes in a row. He wants to form the largest rice ball possible for his friend to eat. Alphonse can perform the following operations:

• If two adjacent rice balls have the same size, Alphonse can combine them to make a new rice ball. The new rice ball’s size is the sum of the two old rice balls’ sizes. It occupies the position in the row previously occupied by the two old rice balls.

• If two rice balls have the same size, and there is exactly one rice ball between them,Alphonse can combine all three rice balls to make a new rice ball. (The middle rice ball does not need to have the same size as the other two.) The new rice ball’s size is the sum of the three old rice balls’ sizes. It occupies the position in the row previously occupied by the three old rice balls.

Alphonse can perform each operation as many times as he wants.Determine the size of the largest rice ball in the row after performing 0 or more operations.

Input Specification

The first line will contain the integer, N (1 ≤ N ≤ 400).The next line will contain N space separated integers representing the sizes of the rice balls, in order from left to right. Each integer is at least 1 and at most 1 000 000.

Output Specification

Output the size of the largest riceball Alphonse can form.

Sample Input 1

7 4 7 12 12 3 9 9 3

Output for Sample Input

48

Explanation for Output for Sample Input 1

One possible set of moves to create a riceball of size 48 is to combine 12 and 12, forming a riceball of size 24. Then, combine 9 and 9 to form a riceball of size 18. Then, combine 3, 18 and 3 to forma riceball of size 24. Finally, combine the two riceballs of size 24 to form a riceball of size 48.

Sample Input 2

4

1 2 3 1

Output for Sample Input 2

3

Explanation for Output for Sample Input 2There are no moves to make, thus the largest riceball in the row is size 3

Solution: dynamic programming

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 400 + 5;
int n; int a[maxn]; int s[maxn];

void init() {
scanf("%d", &n);
for (int i = 0 ; i < n; i++) scanf("%d", &a[i]);
s[0] = a[0];
for (int i = 1; i < n; i++) s[i] = s[i - 1] + a[i];
}

int sum(int i, int j) {
return s[j] - s[i] + a[i];
}

bool f[maxn][maxn];

void solve() {
int ans = 0;
memset(f, false, sizeof(f));
for (int i = 0; i < n; i++) f[i][i] = true;

for (int len = 1; len <= n; len ++ ) {
for (int i = 0; i + len - 1 < n; i ++ ) {
int j = i + len - 1;

if (f[i][j]) { ans = max(ans, sum(i, j)); continue; }

for (int k = i; k < j; k ++ ) {
if (f[i][k] && f[k + 1][j] && sum(i, k) == sum(k + 1, j)) { f[i][j] = true; break; }
}

if (f[i][j]) { ans = max(ans, sum(i, j)); continue; }

for (int len2 = 1; len2 <= len - 2; len2 ++ ) {
if (f[i][j]) break;
for (int k = i + 1; k + len2 <= j; k ++ ) {
int t = k + len2 - 1;

if (f[i][k - 1] && f[k][t] && f[t + 1][j] && sum(i, k - 1) == sum(t + 1, j)) {
f[i][j] = true; break;
}
}
}

if (f[i][j]) ans = max(ans, sum(i, j));
}
}

printf("%d\n", ans);
}

int main() {
init(); solve();
return 0;
}

• 本文已收录于以下专栏：

## ccc 2016 s4 Combining Riceballs

Problem Description Alphonse has N rice balls of various sizes in a row. He wants to form the large...
• alancodes
• 2016年08月30日 23:07
• 85

## 洛谷2964 A Coin Game

• sdfzyhx
• 2017年03月20日 15:07
• 221

## ccc 2016 s4

#include #include #include using namespace std; const int maxn = 400 + 5; int n; int a[maxn]; in...
• alancodes
• 2016年08月30日 23:11
• 77

## SAP发布S4/HANA 意义超过R3

• zhongguomao
• 2017年01月10日 09:19
• 3179

## Combining Sketch and Tone for Pencil Drawing Production的优化过程

Combining Sketch and Tone for Pencil Drawing Production, 铅笔画生成算法的优化过程。
• qingshan0201
• 2016年04月25日 23:08
• 2123

## HH的一万天

• zl_666
• 2015年11月22日 21:30
• 225

## 改进的《Combining Sketch and Tone for Pencil Drawing Production》铅笔画算法

• u013085897
• 2017年02月04日 21:04
• 1069

## Ccc

• yayaleon801030
• 2008年07月15日 14:33
• 73

## Combining sketch and tone for pencil drawing production简述

• houtianqian
• 2017年07月04日 21:18
• 142

## 深入了解R语言-S4

R语言中的S4相比S3的具有层次结构，它有着明确的类定义、参数定义以及参数检查、继承关系、接口函数等，其编程特点都是基于泛型函数的面向对象编程。趁着假期整理一下近期对S4的学习整理。什么时候使用S4当...
• qq_27755195
• 2017年02月02日 16:48
• 944

举报原因： 您举报文章：ccc 16 s4 Combining Riceballs 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)