• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Uva 10755 Garbage Heap

Garbage Heap

Time limit: ? seconds
Memory limit: 64 megabytes

Farmer John has a heap of garbage formed in a rectangular parallelepiped.

It consists of $A\times B\times C$ garbage pieces each of which has a value. The value of a piece may be 0, if the piece is neither profitable nor harmful, and may be negative which means that the piece is not just unprofitable, but even harmful (for environment).

The farmer thinks that he has too much harmful garbage, so he wants to decrease the heap size, leaving a rectangular nonempty parallelepiped of smaller size cut of the original heap to maximize the sum of the values of the garbage pieces in it. You have to find the optimal parallelepiped value. (Actually, if any smaller parallelepiped has value less than the original one, the farmer will leave the original parallelepiped).

Input

The first line of the input contains the number of the test cases, which is at most 15. The descriptions of the test cases follow. The first line of a test case description contains three integers A, B, and C (1 ≤  A, B, C ≤  20). The next lines contain $A\cdot B\cdot C$ numbers, which are the values of garbage pieces. Each number does not exceed $2^{31}$ by absolute value. If we introduce coordinates in the parallelepiped such that the cell in one corner is (1,1,1) and the cell in the opposite corner is (A,B,C), then the values are listed in the order

$$
\begin{gathered}
(1,1,1),(1,1,2),\dots,(1,1,C),\\
(1,2,1),\dots,(1,2,C),\dots,
(1,B,C),\\
(2,1,1),\dots,(2,B,C),\dots,(A,B,C).
\end{gathered}
$$

The test cases are separated by blank lines.

Output

For each test case in the input, output a single integer denoting the maximal value of the new garbage heap. Print a blank line between test cases.

Examples

Input Output
1

2 2 2
-1 2 0 -3 -2 -1 1 5
6
 1 #pragma comment(linker,"/STACK:102400000,102400000")
 2 #include <cstdio>
 3 #include <vector>
 4 #include <cmath>
 5 #include <queue>
 6 #include <cstring>
 7 #include <iostream>
 8 #include <algorithm>
 9 using namespace std;
10 #define INF 0x7fffffff
11 #define mod 1000000007
12 #define ll long long
13 #define maxn 25
14 #define pi acos(-1.0)                                             
15 #define FF(i,n) for(int i=0;i<n;i++)
16 int n, m, s, z;
17 ll a[maxn][maxn][maxn], sum[maxn][maxn][maxn],dp[maxn],c[maxn];
18 int main(){
19     int cas = 1,t;
20     scanf("%d", &t);
21     while (t--){
22         scanf("%d%d%d", &n,&m,&z);
23         for(int i=1;i<=n;i++)
24         for (int j = 1; j <= m; j++)
25         for (int k = 1; k <= z; k++){
26             scanf("%lld", &a[i][j][k]);
27             sum[i][j][k] = sum[i][j][k - 1] + a[i][j][k];
28         }
29         ll ans = -(1LL<<60);
30         for (int x = 1; x <= z; x++){
31             for (int y = x; y <= z; y++){
32                 for (int i = 1; i <= n; i++){
33                     memset(c, 0, sizeof c);
34                     for (int j = i; j <= n; j++){
35                         dp[0] = 0;
36                         for (int k = 1; k <= m; k++){
37                             c[k] += sum[j][k][y]-sum[j][k][x-1];
38                             dp[k] = max(dp[k - 1] + c[k], c[k]);
39                             ans = max(ans, dp[k]);
40                         }
41                     }
42                 }
43             }
44         }
45         printf("%lld\n", ans);
46         if (t)printf("\n");
47     }
48     return 0;
49 }
View Code
posted @ 2013-12-30 22:01  HaibaraAi  阅读(139)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3