Many students live in a dormitory. A dormitory is a whole new world of funny amusements and possibilities but it does have its drawbacks.

There is only one shower and there are multiple students who wish to have a shower in the morning. That's why every morning there is a line of five people in front of the dormitory shower door. As soon as the shower opens, the first person from the line enters the shower. After a while the first person leaves the shower and the next person enters the shower. The process continues until everybody in the line has a shower.

Having a shower takes some time, so the students in the line talk as they wait. At each moment of time the students talk in pairs: the (2i - 1)-th man in the line (for the current moment) talks with the (2i)-th one.

Let's look at this process in more detail. Let's number the people from 1 to 5. Let's assume that the line initially looks as 23154 (person number 2 stands at the beginning of the line). Then, before the shower opens, 2 talks with 3, 1 talks with 5, 4 doesn't talk with anyone. Then 2 enters the shower. While 2 has a shower, 3 and 1 talk, 5 and 4 talk too. Then, 3 enters the shower. While 3 has a shower, 1 and 5 talk, 4 doesn't talk to anyone. Then 1 enters the shower and while he is there, 5 and 4 talk. Then 5 enters the shower, and then 4 enters the shower.

We know that if students i and j talk, then the i-th student's happiness increases by gij and the j-th student's happiness increases by gji. Your task is to find such initial order of students in the line that the total happiness of all students will be maximum in the end. Please note that some pair of students may have a talk several times. In the example above students 1 and 5 talk while they wait for the shower to open and while 3 has a shower.

Input

The input consists of five lines, each line contains five space-separated integers: the j-th number in the i-th line shows gij (0 ≤ gij ≤ 105). It is guaranteed that gii = 0 for all i.

Assume that the students are numbered from 1 to 5.

Output

Print a single integer — the maximum possible total happiness of the students.

Examples
Input

Copy
0 0 0 0 9
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
7 0 0 0 0
Output

Copy
32
Input

Copy
0 43 21 18 2
3 0 21 11 65
5 2 0 1 4
54 62 12 0 99
87 64 81 33 0
Output

Copy
620
Note

In the first sample, the optimal arrangement of the line is 23154. In this case, the total happiness equals:

(g23 + g32 + g15 + g51) + (g13 + g31 + g54 + g45) + (g15 + g51) + (g54 + g45) = 32.

 
next_permutation即可;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream> //#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++) inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
}
/*
int n, m;
int st, ed;
struct node {
int u, v, nxt, w;
}edge[maxn<<1]; int head[maxn], cnt; void addedge(int u, int v, int w) {
edge[cnt].u = u; edge[cnt].v = v; edge[cnt].w = w;
edge[cnt].nxt = head[u]; head[u] = cnt++;
} int rk[maxn]; int bfs() {
queue<int>q;
ms(rk);
rk[st] = 1; q.push(st);
while (!q.empty()) {
int tmp = q.front(); q.pop();
for (int i = head[tmp]; i != -1; i = edge[i].nxt) {
int to = edge[i].v;
if (rk[to] || edge[i].w <= 0)continue;
rk[to] = rk[tmp] + 1; q.push(to);
}
}
return rk[ed];
}
int dfs(int u, int flow) {
if (u == ed)return flow;
int add = 0;
for (int i = head[u]; i != -1 && add < flow; i = edge[i].nxt) {
int v = edge[i].v;
if (rk[v] != rk[u] + 1 || !edge[i].w)continue;
int tmpadd = dfs(v, min(edge[i].w, flow - add));
if (!tmpadd) { rk[v] = -1; continue; }
edge[i].w -= tmpadd; edge[i ^ 1].w += tmpadd; add += tmpadd;
}
return add;
}
ll ans;
void dinic() {
while (bfs())ans += dfs(st, inf);
}
*/ int g[10][10];
int a[6];
int main()
{
//ios::sync_with_stdio(0);
//memset(head, -1, sizeof(head));
for (int i = 1; i <= 5; i++)
for (int j = 1; j <= 5; j++)cin >> g[i][j];
for (int i = 1; i <= 5; i++) {
a[i] = i;
}
ll maxx = -inf;
do {
ll ans = 0;
ans += (g[a[1]][a[2]] + g[a[2]][a[1]] + g[a[3]][a[4]] + g[a[4]][a[3]]);
ans += (g[a[2]][a[3]] + g[a[3]][a[2]] + g[a[4]][a[5]]+g[a[5]][a[4]]);
ans += (g[a[3]][a[4]] + g[a[4]][a[3]]); ans += (g[a[4]][a[5]] + g[a[5]][a[4]]);
maxx = max(maxx, ans);
} while (next_permutation(a + 1, a + 1 + 5));
cout << maxx << endl;
return 0;
}

CF431B Shower Line的更多相关文章

  1. Codeforces Round #247 (Div. 2) B - Shower Line

    模拟即可 #include <iostream> #include <vector> #include <algorithm> using namespace st ...

  2. codeforces 431 B Shower Line【暴力】

    题意:给出五个人的编号,分别为 1 2 3 4 5,他们在排队, 最开始的时候,1和2可以交谈,3和4可以交谈 然后1走了之后,2和3交谈,4和5可以交谈 2走了之后,3和4可以交谈, 3走了之后,4 ...

  3. codeforces B. Shower Line 解题报告

    题目链接:http://codeforces.com/contest/431/problem/B 题目意思:给出5 * 5 的矩阵.从这个矩阵中选出合理的安排次序,使得happiness之和最大.当第 ...

  4. codeforces431B

    Shower Line CodeForces - 431B Many students live in a dormitory. A dormitory is a whole new world of ...

  5. Codeforces Round #247 (Div. 2) ABC

    Codeforces Round #247 (Div. 2) http://codeforces.com/contest/431  代码均已投放:https://github.com/illuz/Wa ...

  6. Codeforces Round #247 (Div. 2) B

    B. Shower Line time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. POJ 3669 Meteor Shower(流星雨)

    POJ 3669 Meteor Shower(流星雨) Time Limit: 1000MS    Memory Limit: 65536K Description 题目描述 Bessie hears ...

  8. poj 3669 Meteor Shower

                                                                                                      Me ...

  9. POJ3669(Meteor Shower)(bfs求最短路)

    Meteor Shower Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12642   Accepted: 3414 De ...

随机推荐

  1. Eclipse中Next Difference的快捷键

    原文:http://stackoverflow.com/questions/10177460/is-there-a-key-binding-for-next-difference-and-previo ...

  2. python中not的用法

    python中的not具体表示是什么: 在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法: ...

  3. 全局事务/分布式事务 (Global Transaction/ A distributed transaction)之我见

    这里参考的是Oracle对于XA的支持,其他的应该雷同吧... 1个分布式事务由多个行为在不同的数据库上执行,1个分布式事务的执行成功意味着相关数据库上的行为执行均成功.“XA协定”(http://w ...

  4. day17-jdbc 3.jdbc快速入门

    通过java程序操作数据库. 对数据库操作是对记录的操作.记录就是DML和DCL. 只要Java程序跟任何设备进行了连接,用完之后必须释放资源.最简单基础班讲I/O流,Java跟文件进行了连接,用完之 ...

  5. ES6中的Set与Map数据结构

    本文实例讲述了ES6学习笔记之Set和Map数据结构.分享给大家供大家参考,具体如下: 一.Set ES6提供了新的数据结构Set.类似于数组,只不过其成员值都是唯一的,没有重复的值. Set本身是一 ...

  6. 用StringBuilder来实现经典的反转问题

    import java.util.Scanner; public class Practise03 { public static void main(String[] args) { //键盘录入一 ...

  7. 中华人民共和国建筑工业行业标准—IFC详细解读 第五篇

  8. Linux 内核与模块调试

    一.简介 内核开发比用户空间开发更难的一个因素就是内核调试艰难.内核错误往往会导致系统宕机,很难保留出错时的现场.调试内核的关键在于你的对内核的深刻理解.   二.方法总结 1)内核模块相关指令 ht ...

  9. R语言输出pdf时,中文乱码处理

    本文转载自:https://blog.csdn.net/hongweigg/article/details/47907555 1.使用基础包,使用函数pdf()输出 在使用pdf()函数时,要输出中文 ...

  10. netty源码阅读之UnpooledByteBufAllocator

    使用IDEA阅读源码Navigate下面的工具是个好东西 .可以帮助分析类的结构等 ByteBufAllocator主要用来生成三种ByteBuf :HeadBuffer,DirectBuffer,C ...