Problem:
Problem F:
KULASIS
Description
KULASISとは，全学共通科目に関する情報をWeb化し，学生・教員への支援やサービスの向上を目的に，高等教育研究開発推進機構で開発・運用しているシステムの名称です。　KULASISは2003年度のオンラインシラバス開発から始まり，Web掲示板・履修登録・成績関係（採点登録・学生からの採点確認）と順次システムを拡充してきました。
学生はパソコン・携帯電話から学内外を問わず，教務情報（休講・授業変更・レポート）の確認・履修登録・採点確認等の機能を利用することができます。ログイン件数は多い日には10,000件を超え，京都大学の教務情報システムとして浸透し，全学共通科目を履修するためには欠かせないものとなっています。このKULASISを全学共通科目のみにとどまらず，学部専門課程や大学院にも適用できるよう 開発を進めています。
http://www.z.k.kyoto-u.ac.jp/introduction_kulasis.html
京大生のQは後期のシラバスを組むためにKULASISにログインしていた．
どの科目を入れようとしているか悩んでいると，突然KULASISが眩い光を放ち，別のページへと遷移した．
遷移した先のページは下図のようなものであった．5x5のマスには科目名と評価(不可，可，良，優)が書かれており，格子上には
●
のボタンが16個配置されている．
いったいなにが起きたのか分からなかったQだったが，どうやら格子上にある
●
のボタンを押すと，その右上，右下，左上，左下にあるマスの科目の評価がそれぞれ 不可→可，可→良，良→優，優→不可 と入れ替わるようであった．
●
のボタンは何度でも押すことができる．
KULASISが何者かによって書き換えられてしまったのか自分が夢を見ているのかよく分からないが，これで成績を確定できるならば，出来るだけ良い成績を得たいとQは考えた．
単位を多く集めていることに自信があったQは，単位の数そのものよりも半年後の研究室配属に備えて
戦闘力
を最大にさせることにした．
戦闘力
とは各科目の評価を，不可→0点，可→60点，良→70点，優→80点と換算して合計した値のことで，これは研究室配属の際に自分がどの程度優位なのかを表すと(Qの学科では)思われている．
いま，KULASISの画面の表示されている科目とその評価が与えられるので，得ることが出来る
戦闘力
の最大値を出力するプログラムを記してほしい．
Input
入力の1行目にはテストケースの個数が与えられる．テストケースの数は100個以下であることが保障されている．
2行目以降は5x5の数字が並び，KULASISの画面に表示されている科目の評価が与えられ，1,2,3,4がそれぞれ不可，可，良，優に対応する．
0ならばそのコマには科目は登録されていない．
テストケース同士の間は空行で区切られている．
Output
各テストケースに対し，得ることの出来る
戦闘力
の最大値を出力せよ．
Sample Input
5

1 1 0 3 3
1 1 0 3 3
0 0 0 0 0
2 2 0 4 4
2 2 0 4 4

1 1 1 0 0
1 1 1 1 0
1 0 1 1 0
0 0 1 1 1
1 1 1 1 1

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

4 3 4 3 4
3 4 3 4 3
4 3 4 3 4
3 4 3 4 3
4 3 4 3 4

2 2 2 2 2
0 0 0 2 0
2 2 0 2 0
2 2 0 2 0
0 0 0 2 0
Output for Sample Input
1280
1420
0
1920
1020

Submitted solutions:

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
vector<int> reverse_hash(int x, int b, int l) {
	vector<int> ret(l);
	int mul = 1;
	for (int i = 0; i < l; i++) {
		ret[i] = x / mul % b;
		mul *= b;
	}
	return ret;
}
int transform(int x, int f) {
	vector<int> v1 = reverse_hash(x, 5, 5);
	vector<int> v2 = reverse_hash(f, 4, 4);
	for (int i = 0; i < 4; i++) {
		for (int j = 0; j < 2; j++) {
			if (v1[i + j] != 0) v1[i + j] = (v1[i + j] - 1 + v2[i]) % 4 + 1;
		}
	}
	int ret = 0, mul = 1;
	for (int i = 0; i < 5; i++) {
		ret += v1[i] * mul;
		mul *= 5;
	}
	return ret;
}
int getscore(int x) {
	vector<int> v = reverse_hash(x, 5, 5);
	int ret = 0;
	for (int i = 0; i < 5; i++) {
		if (v[i] == 2) ret += 60;
		if (v[i] == 3) ret += 70;
		if (v[i] == 4) ret += 80;
	}
	return ret;
}
int Q, x;
int main() {
	cin >> Q;
	while (Q--) {
		vector<int> a(5);
		for (int i = 0; i < 5; i++) {
			int mul = 1;
			for (int j = 0; j < 5; j++) {
				cin >> x;
				a[i] += x * mul;
				mul *= 5;
			}
		}
		vector<vector<int> > dp(5, vector<int>(3125, -999999999));
		dp[0][a[0]] = 0;
		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < 3125; j++) {
				if (dp[i][j] == -999999999) continue;
				for (int l = 0; l < 256; l++) {
					int z = transform(a[i + 1], l);
					dp[i + 1][z] = max(dp[i + 1][z], dp[i][j] + getscore(transform(j, l)));
				}
			}
		}
		int ret = 0;
		for (int i = 0; i < 3125; i++) ret = max(ret, dp[4][i] + getscore(i));
		cout << ret << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 1590
memory_kb: 3524
final_rank: 17

code_snippet: #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define f first
#define s second
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\n", x)
#define EPS (1e-4)
using namespace std;
typedef unsigned int uint;
typedef long long ll;
const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};
int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}
#define HASH(x) (x[0] + x[1] * 4 + x[2] * 16 + x[3] * 64)
int dp[200][4][256];
int rev[4][4];
int board[5][5];
int ten[4] = {0, 60, 70, 80};
int nn;
int solve(int now, int h) {
  if (dp[nn][now][h] != -1)
    return dp[nn][now][h];
  int ret = 0;
  int *x = rev[now];
  for (x[0] = 0; x[0] < 4; x[0]++) {
    for (x[1] = 0; x[1] < 4; x[1]++) {
      for (x[2] = 0; x[2] < 4; x[2]++) {
        for (x[3] = 0; x[3] < 4; x[3]++) {
          int point = 0;
          REP(i, 5) {
            int p = board[now][i];
            if (p == 0)
              continue;
            p = p - 1;
            if (now != 0) {
              if (i != 4)
                p += rev[now - 1][i];
              if (i != 0)
                p += rev[now - 1][i - 1];
            }
            if (i != 4)
              p += rev[now][i];
            if (i != 0)
              p += rev[now][i - 1];
            p = p % 4;
            point += ten[p];
          }
          if (now == 3) {
            REP(i, 5) {
              int p = board[now + 1][i];
              if (p == 0)
                continue;
              p = p - 1;
              if (i != 4)
                p += rev[now][i];
              if (i != 0)
                p += rev[now][i - 1];
              p = p % 4;
              point += ten[p];
            }
            ret = max(ret, point);
          } else {
            int hash = HASH(x);
            ret = max(ret, point + solve(now + 1, hash));
          }
        }
      }
    }
  }
  return dp[nn][now][h] = ret;
}
int main() {
  int n = getInt();
  memset(dp, -1, sizeof(dp));
  for (nn = 0; nn < n; nn++) {
    REP(i, 5) REP(j, 5) board[i][j] = getInt();
    print(solve(0, 0));
  }
  return 0;
}
accuracy: 1
time_ms: 210
memory_kb: 3916
final_rank: 10

code_snippet: #include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define f first
#define s second
#define mp make_pair
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define rep(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOR(i, c)                                                              \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()
#define IN(x, s, g) ((x) >= (s) && (x) < (g))
#define ISIN(x, y, w, h) (IN((x), 0, (w)) && IN((y), 0, (h)))
#define print(x) printf("%d\n", x)
#define EPS (1e-4)
using namespace std;
typedef unsigned int uint;
typedef long long ll;
const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};
int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c))
    c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}
int dp[4][256];
int board[5][5];
int ten[4] = {0, 60, 70, 80};
#define H(h, x) ((h >> (x + x)) & 3)
int solve(int now, int h) {
  if (dp[now][h] != -1)
    return dp[now][h];
  int ret = 0;
  int tmp[5] = {0};
  if (board[now][0] != 0)
    tmp[0] = 1 + ((board[now][0] - 1) + H(h, 0)) % 4;
  if (board[now][1] != 0)
    tmp[1] = 1 + ((board[now][1] - 1) + H(h, 0) + H(h, 1)) % 4;
  if (board[now][2] != 0)
    tmp[2] = 1 + ((board[now][2] - 1) + H(h, 1) + H(h, 2)) % 4;
  if (board[now][3] != 0)
    tmp[3] = 1 + ((board[now][3] - 1) + H(h, 2) + H(h, 3)) % 4;
  if (board[now][4] != 0)
    tmp[4] = 1 + ((board[now][4] - 1) + H(h, 3)) % 4;
  REP(x, 256) {
    int rtmp = 0;
    if (tmp[0] != 0)
      rtmp += ten[((tmp[0] - 1) + H(x, 0)) % 4];
    if (tmp[1] != 0)
      rtmp += ten[((tmp[1] - 1) + H(x, 0) + H(x, 1)) % 4];
    if (tmp[2] != 0)
      rtmp += ten[((tmp[2] - 1) + H(x, 1) + H(x, 2)) % 4];
    if (tmp[3] != 0)
      rtmp += ten[((tmp[3] - 1) + H(x, 2) + H(x, 3)) % 4];
    if (tmp[4] != 0)
      rtmp += ten[((tmp[4] - 1) + H(x, 3)) % 4];
    if (now == 3) {
      if (board[4][0] != 0)
        rtmp += ten[((board[4][0] - 1) + H(x, 0)) % 4];
      if (board[4][1] != 0)
        rtmp += ten[((board[4][1] - 1) + H(x, 0) + H(x, 1)) % 4];
      if (board[4][2] != 0)
        rtmp += ten[((board[4][2] - 1) + H(x, 1) + H(x, 2)) % 4];
      if (board[4][3] != 0)
        rtmp += ten[((board[4][3] - 1) + H(x, 2) + H(x, 3)) % 4];
      if (board[4][4] != 0)
        rtmp += ten[((board[4][4] - 1) + H(x, 3)) % 4];
      ret = max(ret, rtmp);
    } else {
      ret = max(ret, rtmp + solve(now + 1, x));
    }
  }
  return dp[now][h] = ret;
}
int main() {
  int n = getInt();
  while (n-- > 0) {
    memset(dp, -1, sizeof(dp));
    REP(i, 5) REP(j, 5) board[i][j] = getInt();
    print(solve(0, 0));
  }
  return 0;
}
accuracy: 1
time_ms: 100
memory_kb: 3048
final_rank: 3

code_snippet: #include <bits/stdc++.h>
using namespace std;
int dp[40][1 << 14];
int v[40];
int pnt(int c) { return c > 0 ? 50 + c * 10 : 0; }
int toBit(int c[7], int p) {
  int bit = 0;
  for (int i = 1; i < 7; i++) {
    bit = (bit << 2) | c[i];
  }
  bit = (bit << 2) | (~v[p + 7] ? v[p + 7] : 0);
  return bit;
}
void solve() {
  memset(v, -1, sizeof(v));
  for (int i = 0; i < 25; i++)
    cin >> v[i], v[i]--;
  memset(dp, -1, sizeof(dp));
  int start = 0;
  for (int i = 0; i < 7; i++)
    start = (start << 2) | (~v[i] ? v[i] : 0);
  dp[0][start] = 0;
  for (int i = 0; i < 25; i++) {
    for (int S = 0; S < (1 << 14); S++) {
      if (dp[i][S] == -1)
        continue;
      int c[7];
      for (int j = 0; j < 7; j++)
        c[6 - j] = S >> (j * 2) & 3;
      if (i % 5 == 4 || i >= 20) {
        dp[i + 1][toBit(c, i)] =
            max(dp[i + 1][toBit(c, i)], dp[i][S] + pnt(c[0]));
        continue;
      }
      for (int j = 0; j < 4; j++) {
        dp[i + 1][toBit(c, i)] =
            max(dp[i + 1][toBit(c, i)], dp[i][S] + pnt(c[0]));
        for (int k = 0; k < 7; k++)
          if (k < 2 || k >= 5) {
            if (~v[i + k])
              c[k] = (c[k] + 1) % 4;
          }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < (1 << 14); i++)
    ans = max(ans, dp[25][i]);
  cout << ans << endl;
}
int main() {
  int n;
  cin >> n;
  while (n--)
    solve();
  return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 5576
final_rank: 16

code_snippet: #include <bits/stdc++.h>
using namespace std;
int dp[40][1 << 14];
int v[40];
int pnt(int c) { return c > 0 ? 50 + c * 10 : 0; }
int toBit(int c[7], int p) {
  int bit = 0;
  for (int i = 1; i < 7; i++) {
    bit = (bit << 2) | c[i];
  }
  bit = (bit << 2) | (~v[p + 7] ? v[p + 7] : 0);
  return bit;
}
void solve() {
  memset(v, -1, sizeof(v));
  for (int i = 0; i < 25; i++)
    cin >> v[i], v[i]--;
  memset(dp, -1, sizeof(dp));
  int start = 0;
  for (int i = 0; i < 7; i++)
    start = (start << 2) | (~v[i] ? v[i] : 0);
  dp[0][start] = 0;
  for (int i = 0; i < 25; i++) {
    for (int S = 0; S < (1 << 14); S++) {
      if (dp[i][S] == -1)
        continue;
      int c[7];
      for (int j = 0; j < 7; j++)
        c[6 - j] = S >> (j * 2) & 3;
      int to = toBit(c, i);
      if (i % 5 == 4 || i >= 20) {
        dp[i + 1][to] = max(dp[i + 1][to], dp[i][S] + pnt(c[0]));
        continue;
      }
      for (int j = 0; j < 4; j++) {
        to = toBit(c, i);
        dp[i + 1][to] = max(dp[i + 1][to], dp[i][S] + pnt(c[0]));
        if (~v[i])
          c[0] = (c[0] + 1) % 4;
        if (~v[i + 1])
          c[1] = (c[1] + 1) % 4;
        if (~v[i + 5])
          c[5] = (c[5] + 1) % 4;
        if (~v[i + 6])
          c[6] = (c[6] + 1) % 4;
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < (1 << 14); i++)
    ans = max(ans, dp[25][i]);
  cout << ans << endl;
}
int main() {
  int n;
  cin >> n;
  while (n--)
    solve();
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5548
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
struct NODE {
  int m[7];
  bool operator<(const NODE &n) const {
    for (int i = 0; i < 7; i++)
      if (m[i] != n.m[i])
        return m[i] < n.m[i];
    return false;
  }
};
int to[] = {0, 2, 3, 4, 1};
int table[] = {0, 60, 10, 10, -80};
int score[] = {0, 0, 60, 70, 80};
int fld[5][5];
void solve() {
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
      cin >> fld[i][j];
  map<NODE, int> prev;
  NODE start;
  for (int i = 0; i < 5; i++)
    start.m[6 - i] = fld[0][i];
  for (int i = 0; i < 2; i++)
    start.m[1 - i] = fld[1][i];
  int sum = 0;
  for (int i = 0; i < 5; i++)
    for (int j = 0; j < 5; j++)
      sum += score[fld[i][j]];
  prev[start] = sum;
  for (int i = 0; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      map<NODE, int> next;
      for (map<NODE, int>::iterator it = prev.begin(); it != prev.end(); it++) {
        NODE d = it->first;
        int val = it->second;
        for (int k = 0; k < 4; k++) {
          for (int l = 0; l < 7; l++)
            if (l < 2 || l >= 5) {
              val += table[d.m[l]];
              d.m[l] = to[d.m[l]];
            }
          NODE e = d;
          if (j != 3) {
            for (int l = 6; l > 0; l--)
              e.m[l] = e.m[l - 1];
            e.m[0] = fld[i + 1][j + 2];
          } else {
            for (int l = 6; l > 1; l--)
              e.m[l] = e.m[l - 2];
            e.m[1] = fld[i + 2][0];
            e.m[0] = fld[i + 2][1];
          }
          if (next.find(e) != next.end())
            next[e] = max(next[e], val);
          else
            next[e] = val;
        }
      }
      prev = next;
    }
  }
  int ans = 0;
  for (map<NODE, int>::iterator it = prev.begin(); it != prev.end(); it++) {
    ans = max(ans, it->second);
  }
  cout << ans << endl;
}
int main() {
  int n;
  cin >> n;
  while (n--)
    solve();
  return 0;
}
accuracy: 1
time_ms: 280
memory_kb: 3268
final_rank: 5

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int dp[17][1<<14];

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    while(q--){
        int n = 25;
        vector<int> v(n+5,-1);
        for(int i=0;i<n;i++){
            cin >> v[i]; v[i]--;
        }
        for(int i=0;i<17;i++){
            for(int j=0;j<(1<<14);j++){
                dp[i][j] = -1;
            }
        }
        int bit = 0;
        for(int i=0;i<7;i++){
            bit <<= 2;
            bit |= (v[i]>=0?v[i]:0);
        }
        dp[0][bit] = 0;
        auto score=[&](int idx)->int{
            if(idx == -1)return 0;
            else if(idx == 0)return 0;
            else if(idx == 1)return 60;
            else if(idx == 2)return 70;
            else return 80;
        };
        for(int i=0;i<16;i++){
            for(int j=0;j<(1<<14);j++){
                if(dp[i][j] == -1)continue;
                array<int, 7> a;
                for(int k=0;k<7;k++){
                    a[6-k] = (j>>(2*k))&3;
                }
                for(int k=0;k<7;k++){
                    if(v[i/4*5+i%4+k] == -1)a[k] = -1;
                }
                auto cal=[](array<int, 7> &a, int nxt, int nx2 = 4)->int{
                    int bit = 0;
                    for(int i=1;i<7;i++){
                        if(nx2 != 4 and i == 1)continue;
                        bit <<= 2;
                        bit |= (a[i]>=0?a[i]:0);
                    }
                    bit <<= 2;
                    bit |= (nxt>=0?nxt:0);
                    if(nx2 != 4){
                        bit <<= 2;
                        bit |= (nx2>=0?nx2:0);
                    }
                    return bit;
                };
                if(i < 12){
                    for(int _=0;_<5;_++){
                        int val = dp[i][j] + score(a[0]);
                        if(i%4 == 3)val += score(a[1]);
                        int nxt;
                        if(i%4 != 3)nxt = cal(a,v[i/4*5+i%4+7]);
                        else nxt = cal(a,v[i/4*5+i%4+7],v[i/4*5+i%4+8]);
                        dp[i+1][nxt] = max(dp[i+1][nxt], val);
                        for(int k=0;k<7;k++){
                            if(a[k] == -1)continue;
                            if(2 <= k and k < 5)continue;
                            a[k]++;
                            if(a[k] == 4)a[k] = 0;
                        }
                    }
                }
                else{
                    for(int _=0;_<5;_++){
                        int val = dp[i][j] + score(a[0]) + score(a[5]);
                        if(i%4 == 3)val += score(a[1]) + score(a[6]);
                        int nxt = cal(a,v[i/4*5+i%4+7]);
                        dp[i+1][nxt] = max(dp[i+1][nxt], val);
                        for(int k=0;k<7;k++){
                            if(a[k] == -1)continue;
                            if(2 <= k and k < 5)continue;
                            a[k]++;
                            if(a[k] == 4)a[k] = 0;
                        }
                    }
                }
            }
        }
        int res = 0;
        for(int i=0;i<(1<<14);i++){
            res = max(res,dp[16][i]);
        }
        cout << res << "\n";
    }
}
accuracy: 1
time_ms: 20
memory_kb: 4232
final_rank: 11

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 1000000000000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int POW[5];
int table[5][5];
int dp[2][256];

void func(){

	for(int row = 0; row < 5; row++){
		for(int col = 0; col < 5; col++){
			scanf("%d",&table[row][col]);
		}
	}

	int CURRENT = 0,NEXT = 1;
	for(int state = 0; state < POW[4]; state++){

		dp[CURRENT][state] = -BIG_NUM;
		dp[NEXT][state] = -BIG_NUM;
	}


	int work[5],work2[5];

	//1行目を計算
	for(int state = 0; state < POW[4]; state++){

		for(int i = 0; i < 5; i++){

			work[i] = table[0][i];
		}

		int tmp = state;

		for(int i = 0; i <= 3; i++){

			int add = tmp/POW[3-i];

			if(work[i] != 0){
				work[i] += add;
				if(work[i] >= 5){

					work[i] -= 4;
				}
			}

			if(work[i+1] != 0){
				work[i+1] += add;
				if(work[i+1] >= 5){

					work[i+1] -= 4;
				}
			}
			tmp %= POW[3-i];
		}

		int sum = 0;
		for(int i = 0; i < 5; i++){
			switch(work[i]){
			case 0:
			case 1:
				//Do nothing
				break;
			case 2:
				sum += 60;
				break;
			case 3:
				sum += 70;
				break;
			case 4:
				sum += 80;
				break;
			}
		}
		dp[CURRENT][state] = sum;
	}

	int ans = 0;

	for(int row = 1; row <= 4; row++){
		for(int pre = 0; pre < POW[4]; pre++){
			if(dp[CURRENT][pre] == -BIG_NUM)continue;

			for(int i = 0; i < 5; i++){

				work[i] = table[row][i];
			}

			int tmp = pre;

			for(int i = 0; i <= 3; i++){

				int add = tmp/POW[3-i];

				if(work[i] != 0){
					work[i] += add;
					if(work[i] >= 5){

						work[i] -= 4;
					}
				}

				if(work[i+1] != 0){
					work[i+1] += add;
					if(work[i+1] >= 5){

						work[i+1] -= 4;
					}
				}
				tmp %= POW[3-i];
			}

			int sum = 0;
			for(int i = 0; i < 5; i++){
				switch(work[i]){
				case 0:
				case 1:
					//Do nothing
					break;
				case 2:
					sum += 60;
					break;
				case 3:
					sum += 70;
					break;
				case 4:
					sum += 80;
					break;
				}
			}

			if(row == 4){

				ans = max(ans,dp[CURRENT][pre]+sum);
				continue;
			}

			for(int next = 0; next < POW[4]; next++){

				for(int k = 0; k < 5; k++){

					work2[k] = work[k];
				}

				int tmp2 = next;

				for(int k = 0; k <= 3; k++){

					int add2 = tmp2/POW[3-k];

					if(work2[k] != 0){
						work2[k] += add2;
						if(work2[k] >= 5){

							work2[k] -= 4;
						}
					}

					if(work2[k+1] != 0){
						work2[k+1] += add2;
						if(work2[k+1] >= 5){

							work2[k+1] -= 4;
						}
					}
					tmp2 %= POW[3-k];
				}

				int sum2 = 0;
				for(int k = 0; k < 5; k++){
					switch(work2[k]){
					case 0:
					case 1:
						//Do nothing
						break;
					case 2:
						sum2 += 60;
						break;
					case 3:
						sum2 += 70;
						break;
					case 4:
						sum2 += 80;
						break;
					}
				}

				dp[NEXT][next] = max(dp[NEXT][next],dp[CURRENT][pre]+sum2);
			}
		}
		swap(CURRENT,NEXT);
		for(int state = 0; state < POW[4]; state++){

			dp[NEXT][state] = -BIG_NUM;
		}
	}

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

int main(){

	POW[0] = 1;
	for(int i = 1; i < 5; i++){

		POW[i] = POW[i-1]*4;
	}

	int num_case;
	scanf("%d",&num_case);

	for(int i = 0; i < num_case; i++){

		func();
	}

	return 0;
}
accuracy: 1
time_ms: 760
memory_kb: 3208
final_rank: 13

code_snippet: // 基本テンプレート
 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;
 
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
typedef pair<int, int> pii;
typedef long long ll;
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;

const int S = 4000;
const int N = 5;
using vector2D = vector< vector<int> >;

int base_x[] = {0, 0, 1, 1};
int base_y[] = {0, 1, 0, 1};
vector< vector<int> > cand;

void init_cand() {
    for(int i=0; i<4; i++) {
        for(int j=0; j<4; j++) {
            for(int k=0; k<4; k++) {
                for(int x=0; x<4; x++) {
                    vector<int> v = {i, j, k, x};
                    cand.push_back(v);
                }
            }
        }
    }
}

int get_hash(vector<int> v) {
    int ret = 0;
    for(int i=0; i<N; i++) {
        ret *= 5;
        ret += v[i];
    }
    return ret;
}

int get_point(vector<int> v) {
    int ret = 0;
    vector<int> pts = {0, 0, 60, 70, 80};
    for(int i=0; i<N; i++) {
        ret += pts[ v[i] ];
    }
    return ret;
}

int solve(vector2D board) {
    map< vector<int>, int > dp, swp;
    dp[ board[0] ] = 0;
    for(int k=0; k<4; k++) {
        for(auto e : dp) {
            vector<int> pat = e.first;
            for(auto v : cand) {
                vector< vector<int> > tmp(2, vector<int>(N));
                tmp[0] = pat;
                tmp[1] = board[k+1];

                for(int i=0; i<4; i++) {
                    int shift = v[i];
                    for(int j=0; j<4; j++) {
                        int x = base_x[j], y = base_y[j] + i;

                        if(tmp[x][y] == 0) continue;
                        tmp[x][y] = (tmp[x][y] - 1 + shift) % 4 + 1;
                    }
                }

                int score = dp[pat] + get_point(tmp[0]);
                if(k == 3) score += get_point(tmp[1]);

                vector<int> nxt = tmp[1];
                if(!swp.count(nxt) || swp[nxt] < score) {
                    swp[nxt] = score;
                }
            }
        }

        swap(dp, swp);
    }

    int ans = 0;
    for(auto x : dp) {
        chmax(ans, x.second);
    }
    return ans;
}
 
signed main() {
    init_cand();
    int T; cin >> T;
    while(T--) {
        vector2D board(N, vector<int>(N));
        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                cin >> board[i][j];
            }
        }

        cout << solve(board) << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 3710
memory_kb: 3216
final_rank: 18

code_snippet: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
#include <set>
#include <map>
#include <time.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int table[5][5],push_table[256][4];
int dp[3125],next_dp[3125];
int index = 0,push_index = 0;
map<int,int>MAP,rev_MAP;


int makeNUM(int array[5]){
	return 10000*array[0]+1000*array[1]+100*array[2]+10*array[3]+array[4];
}

int getPoint(int num){

	switch(num){
	case 0:return 0;
	case 1:return 0;
	case 2:return 60;
	case 3:return 70;
	case 4:return 80;
	}
	return -1;//must not reach here
}


void func(){

	for(int row = 0; row < 5; row++){
		for(int col = 0; col < 5; col++)scanf("%d",&table[row][col]);
	}

	int work[2][5];

	for(int i = 0; i < 3125; i++)dp[i] = -1;


	int sum,code;

	for(int i = 0; i < 256; i++){
		for(int a = 0; a < 2; a++){
			for(int b = 0; b < 5; b++)work[a][b] = table[a][b];
		}

		for(int k = 0; k < 4; k++){
			if(work[0][k] != 0){
				work[0][k] += push_table[i][k];
				if(work[0][k] > 4)work[0][k] -= 4;
			}
			if(work[0][k+1] != 0){
				work[0][k+1] += push_table[i][k];
				if(work[0][k+1] > 4)work[0][k+1] -= 4;
			}
			if(work[1][k] != 0){
				work[1][k] += push_table[i][k];
				if(work[1][k] > 4)work[1][k] -= 4;
			}
			if(work[1][k+1] != 0){
				work[1][k+1] += push_table[i][k];
				if(work[1][k+1] > 4)work[1][k+1] -= 4;
			}
		}
		sum = 0;
		for(int k = 0; k < 5; k++)sum += getPoint(work[0][k]);
		code = MAP[makeNUM(work[1])];
		dp[code] = max(dp[code],sum);
	}


	int number,tmp;
	int ans = 0;

	for(int row = 1; row <= 3; row++){

		for(int state = 0; state < 3125; state++)next_dp[state] = -1;

		for(int pre_state = 0; pre_state < 3125; pre_state++){
			if(dp[pre_state] == -1)continue;

			number = rev_MAP[pre_state];

			for(int i = 0; i < 256; i++){
				tmp = number;
				for(int k = 0; k < 5; k++){
					work[0][4-k] = tmp%10;
					tmp /= 10;
				}

				for(int k = 0; k < 5; k++){
					work[1][k] = table[row+1][k];
				}

				for(int k = 0; k < 4; k++){
					if(work[0][k] != 0){
						work[0][k] += push_table[i][k];
						if(work[0][k] > 4)work[0][k] -= 4;
					}
					if(work[0][k+1] != 0){
						work[0][k+1] += push_table[i][k];
						if(work[0][k+1] > 4)work[0][k+1] -= 4;
					}
					if(work[1][k] != 0){
						work[1][k] += push_table[i][k];
						if(work[1][k] > 4)work[1][k] -= 4;
					}
					if(work[1][k+1] != 0){
						work[1][k+1] += push_table[i][k];
						if(work[1][k+1] > 4)work[1][k+1] -= 4;
					}
				}
				sum = 0;
				for(int k = 0; k < 5; k++)sum += getPoint(work[0][k]);

				if(row <= 2){
					code = MAP[makeNUM(work[1])];

					next_dp[code] = max(next_dp[code],sum+dp[pre_state]);

				}else{
					for(int k = 0; k < 5; k++)sum += getPoint(work[1][k]);
					ans = max(ans,sum+dp[pre_state]);
				}
			}
		}

		for(int state = 0; state < 3125; state++)dp[state] = next_dp[state];
	}

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


void recursive(int array[5],int count){

	if(count == 5){
		int new_num = makeNUM(array);
		MAP[new_num] = index;
		rev_MAP[index] = new_num;
		index++;
		return;
	}

	for(int i = 0; i <= 4; i++){
		int next_array[5];
		for(int k = 0; k < count; k++)next_array[k] = array[k];
		next_array[count] = i;
		recursive(next_array,count+1);
	}
}

void makeCode(){
	int first_array[5];
	recursive(first_array,0);
}


void makePush(int array[4],int count){

	if(count == 4){
		for(int i = 0; i < 4; i++){
			push_table[push_index][i] = array[i];
		}
		push_index++;
		return;
	}

	for(int i = 0; i <= 3; i++){
		int next_array[4];
		for(int k = 0; k < count; k++)next_array[k] = array[k];
		next_array[count] = i;
		makePush(next_array,count+1);
	}
}

int main(){

	makeCode();

	int first_array[4];
	makePush(first_array,0);

	int case_num;
	scanf("%d",&case_num);

	for(int i = 0; i < case_num; i++)func();

	return 0;
}
accuracy: 1
time_ms: 470
memory_kb: 3520
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 2147483600
#define long long long // for codeforces

int solve(){
  vector<vector<int>> vec(5,vector<int>(5));
  rep(i,5)rep(j,5) scanf("%d", &vec[i][j]);
  rep(i,5)rep(j,5) vec[i][j]--;

  const int masks[] = {0x3fc, 0x3f3, 0x3cf, 0x33f, 0x0ff};
  const int points[] ={0,60,70,80};

  vector<vector<int>> dp(1<<10, vector<int>(4, -1));
  dp[0][0] = 0;
  // dp[mask][i] : 2bit?????¨??¨???????????????????????????????????¨??????????????¨???????¢???????????????§????????¢
  // i : ?????????????????¢????????????????????????

  rep(i,4)rep(j,4){
    vector<vector<int>> nxt(1<<10, vector<int>(4, -1));
    rep(mask, 1<<10) rep(k,4) if(dp[mask][k]>=0){
      int l = (mask>>(j*2))%4;
      int r = (mask>>(j*2+2))%4;
      rep(d,4){ // # switch push
        int pl=0,pr=0;
        if(vec[i][j]!=-1) pl = points[(vec[i][j]+l+k+d)%4];
        if(vec[i][j+1]!=-1) pr = points[(vec[i][j+1]+r+d)%4];
        int nmask = (mask&masks[j]) + (((k+d)%4)<<(j*2));
        if(j<3) nxt[nmask][d] = max(nxt[nmask][d], dp[mask][k] + pl);
        if(j==3){
          nmask = (nmask&masks[4]) + (d<<8);
          nxt[nmask][0] = max(nxt[nmask][0], dp[mask][k] + pl + pr);
        }
      }
    }
    swap(nxt, dp);
  }

  int ans = 0;
  rep(_mask, 1<<10){
    int mask = _mask;
    int crnt=dp[_mask][0];
    if(crnt<0) crnt=0;
    rep(i,5){
      int d = mask%4;
      mask /= 4;
      if(vec[4][i]!=-1){
        crnt += points[(vec[4][i]+d)%4];
      }
    }
    ans = max(ans, crnt);
  }
  return ans;
}

int main(){
  int n;
  cin>>n;
  rep(_,n) cout<<solve()<<endl;

  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3280
final_rank: 4

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int dp[6][3125], n, x[7][5], pp[5] = { 0,0,60,70,80 };
int power4[6] = { 1,4,16,64,256,1024 }, power5[6] = { 1,5,25,125,625,3125 };
int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < 5; j++) {
			for (int k = 0; k < 5; k++)cin >> x[j][k];
		}
		for (int j = 0; j < 6; j++) { for (int k = 0; k < 3125; k++) { dp[j][k] = -1; } }
		for (int j = 0; j < 256; j++) {
			int bit[4]; for (int k = 0; k < 4; k++)bit[k] = (j / power4[k]) % 4;
			int y[7][5]; for (int k = 0; k < 35; k++)y[k / 5][k % 5] = x[k / 5][k % 5];
			for (int k = 0; k < 4; k++) {
				for (int l = 0; l < bit[k]; l++) {
					if (y[0][k] >= 1) { y[0][k]++; if (y[0][k] == 5)y[0][k] -= 4; }
					if (y[1][k] >= 1) { y[1][k]++; if (y[1][k] == 5)y[1][k] -= 4; }
					if (y[0][k + 1] >= 1) { y[0][k + 1]++; if (y[0][k + 1] == 5)y[0][k + 1] -= 4; }
					if (y[1][k + 1] >= 1) { y[1][k + 1]++; if (y[1][k + 1] == 5)y[1][k + 1] -= 4; }
				}
			}
			int W = 0; for (int k = 0; k < 5; k++)W += power5[k] * y[1][k];
			int W2 = 0; for (int k = 0; k < 5; k++)W2 += pp[y[0][k]];
			dp[0][W] = max(dp[0][W], W2);
		}
		for (int j = 1; j <= 3; j++) {
			for (int k = 0; k < 3125; k++) {
				if (dp[j - 1][k] == -1)continue;
				int bit[5]; for (int l = 0; l < 5; l++)bit[l] = (k / power5[l]) % 5;
				for (int l = 0; l < 256; l++) {
					int y[7][5]; for (int m = 0; m < 35; m++)y[m / 5][m % 5] = x[m / 5][m % 5];
					for (int m = 0; m < 5; m++)y[j][m] = bit[m];
					int bit2[4]; for (int m = 0; m < 4; m++) { bit2[m] = (l / power4[m]) % 4; }
					for (int m = 0; m < 4; m++) {
						for (int o = 0; o < bit2[m]; o++) {
							if (y[j + 0][m + 0] >= 1) { y[j + 0][m + 0]++; if (y[j + 0][m + 0] == 5)y[j + 0][m + 0] -= 4; }
							if (y[j + 1][m + 0] >= 1) { y[j + 1][m + 0]++; if (y[j + 1][m + 0] == 5)y[j + 1][m + 0] -= 4; }
							if (y[j + 0][m + 1] >= 1) { y[j + 0][m + 1]++; if (y[j + 0][m + 1] == 5)y[j + 0][m + 1] -= 4; }
							if (y[j + 1][m + 1] >= 1) { y[j + 1][m + 1]++; if (y[j + 1][m + 1] == 5)y[j + 1][m + 1] -= 4; }
						}
					}
					int W = 0; for (int m = 0; m < 5; m++) W += power5[m] * y[j + 1][m];
					int W2 = 0; for (int m = 0; m < 5; m++)W2 += pp[y[j][m]];
					dp[j][W] = max(dp[j][W], dp[j - 1][k] + W2);
				}
			}
		}
		int maxn = 0;
		for (int j = 0; j < 3125; j++) {
			if (dp[3][j] == -1)continue;
			int sum1 = 0; for (int k = 0; k < 5; k++)sum1 += pp[(j / power5[k]) % 5];
			maxn = max(maxn, dp[3][j] + sum1);
		}
		cout << maxn << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 890
memory_kb: 3156
final_rank: 14

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt" > "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\b.answer"

map<int, int>mp;

int getans(const vector<vector<int>>&field,const int row, const map<vector<int>, int>&memo) {
	if (row == 4) {
		int ans = 0;
		for (auto m : memo) {
			vector<int>changed(m.first);
			int nextscore(m.second);
			for (int j = 0; j < 5; ++j) {
				if (field[row][j]) {
					const int number = (field[row][j] + changed[j]) % 4;
					nextscore += mp[number];
				}
			}
			ans = max(ans, nextscore);
			
		}
		return ans;
	}
	map<vector<int>, int>newmemo;
	for (auto m : memo) {
		for (int i = 0; i < 256; ++i) {
			int num(i);
			vector<int>push(4);
			for (int j = 0; j < 4; ++j) {
				push[j] = num % 4;
				num /= 4;
			}
			vector<int>fliped(5);
			for (int j = 0; j < 4; ++j) {
				fliped[j] += push[j];
				fliped[j + 1] += push[j];
			}
			vector<int>changed(m.first);
			for (int j = 0; j < 5; ++j) {
				changed[j] += fliped[j];
			}
			for (int j = 0; j < 5; ++j)changed[j] %= 4;
			int nextscore(m.second);
			for (int j = 0; j < 5; ++j) {
				if (field[row][j]) {
					const int number = (field[row][j] + changed[j]) % 4;
					nextscore += mp[number];
				}
			}
			newmemo[fliped] = max(newmemo[fliped], nextscore);
		}
	}
	return getans(field, row + 1, newmemo);
	
}

int getans(const vector<vector<int>>field) {
	map<vector<int>, int>amap;
	amap[vector<int>(5, 0)] = 0;
	return getans(field, 0, amap);
}

int main() {
	mp[1] = 0;
	mp[2] = 60;
	mp[3] = 70;
	mp[0] = 80;
	int N; cin >> N;
	while (N--) {
		vector<vector<int>>field(5, vector<int>(5));
		for (int y = 0; y < 5; ++y) {
			for (int x = 0; x < 5; ++x) {
				cin >> field[y][x];
			}
		}
		int ans = getans(field);
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 4030
memory_kb: 3200
final_rank: 19

code_snippet: // template {{{

#include <bits/stdc++.h>
using namespace std;

#define loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) loop(i, 0, n)
#define rloop(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); i--)
#define rrep(i, n) rloop(i, 0, n)
#define eb emplace_back
#define ef emplace_front
#define pb pop_back
#define pf pop_front
#define all(c) std::begin(c), std::end(c)
#define mp std::make_pair
#define mt std::make_tuple
#define fi first
#define se second
#define popcnt __builtin_popcountll

using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vd = vector<double>;
using vvd = vector<vd>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using pd = pair<double, double>;
using tp = tuple<int, int, int>;
using tp4 = tuple<int, int, int, int>;
using tp5 = tuple<int, int, int, int, int>;

template<typename T>
using max_pq = priority_queue<T, vector<T>, less<T>>;

template<typename T>
using min_pq = priority_queue<T, vector<T>, greater<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
const ll LLINF = 1e18 + 10;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template<typename T>
inline T sq(T x){ return x * x; }

template<typename T, typename U>
inline bool chmax(T &x, U y){ if (x >= y) return false; x = y; return true; }

template<typename T, typename U>
inline bool chmin(T &x, U y){ if (x <= y) return false; x = y; return true; }

template<typename T>
inline void sort(T &c){ std::sort(std::begin(c), std::end(c)); }

template<typename T>
inline void reverse(T &c){ std::reverse(std::begin(c), std::end(c)); }

template<typename T>
inline void unique(T &c){ std::sort(std::begin(c), std::end(c)); c.erase(std::unique(all(c)), std::end(c)); }

// }}}

const int t[] = {0, 60, 70, 80};
bool f[5][5];
int a[5][5];
int dp[17][65536];

int calc(int p, int st)
{
	if (~dp[p][st]) return dp[p][st];
	if (p == 16) return 0;
	
	int res = 0;
	int ds = 0;
	int x = p / 4, y = p % 4;
	rep(i, 4){
		chmax(res, ds + calc(p + 1, (st * 4 + i) % 65536));
		rep(dx, 2) rep(dy, 2){
			if (f[x + dx][y + dy]){
				ds -= t[a[x + dx][y + dy]];
				++a[x + dx][y + dy] %= 4;
				ds += t[a[x + dx][y + dy]];
			}
		}
	}

	return dp[p][st] = res;
}

int main()
{
	int n;
	scanf("%d", &n);
	while (n--){
		rep(i, 5) rep(j, 5) scanf("%d", &a[i][j]), a[i][j]--;
		rep(i, 5) rep(j, 5) f[i][j] = a[i][j] >= 0;
		int sum = 0;
		rep(i, 5) rep(j, 5){
			if (f[i][j]) sum += t[a[i][j]];
		}
		memset(dp, -1, sizeof(dp));
		printf("%d\n", sum + calc(0, 0));
	}
}
accuracy: 1
time_ms: 1800
memory_kb: 7572
final_rank: 20

code_snippet: #include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

int table[5][5];
int dp[5][1<<8];
int score[4]={0,60,70,80};

int main(void){
	int T;
	cin >> T;
	rep(loop,T){
		rep(i,5)rep(j,5) cin >> table[i][j];
		rep(i,5)rep(j,1<<8) dp[i][j]=-1;
		dp[0][0]=0;
		rep(i,4)rep(mask,1<<8){
			if(dp[i][mask]==-1) continue;
			rep(mask2,1<<8){
				int sw[4]={0,0,0,0};
				rep(j,4) sw[j]+=(mask>>(2*j))&3;
				rep(j,4) sw[j]+=(mask2>>(2*j))&3;
				rep(j,4) sw[j]%=4;
				int cur=0;
				rep(j,5){
					if(table[i][j]==0)continue;
					int index=table[i][j]-1;
					if(j-1>=0) index=(index+sw[j-1])%4;
					if(j<4) index=(index+sw[j])%4;
					cur+=score[index];
				}
				dp[i+1][mask2]=max(dp[i+1][mask2],dp[i][mask]+cur);
			}
		}
		int ans=0;
		rep(mask,1<<8){
			int sw[4]={0,0,0,0};
			rep(j,4) sw[j]+=(mask>>(2*j))&3;
			rep(j,4) sw[j]%=4;
			int cur=0;
			rep(j,5){
				if(table[4][j]==0)continue;
				int index=table[4][j]-1;
				if(j-1>=0) index=(index+sw[j-1])%4;
				if(j<4) index=(index+sw[j])%4;
				cur+=score[index];
			}
			ans=max(ans,dp[4][mask]+cur);
		}
		cout << ans << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 1164
final_rank: 1

code_snippet: #include<bits/stdc++.h>
using namespace std;

int dp[40][1<<14];
int v[40];

inline int pnt(int c){
    return c>0?50+c*10:0;
}

int toBit(int c[7],int p){
    int bit=0;
    for(int i=1;i<7;i++){
        bit=(bit<<2)|c[i];
    }
    bit=(bit<<2)|(~v[p+7]?v[p+7]:0);
    return bit;
}

void solve(){
    memset(v,-1,sizeof(v));
    for(int i=0;i<25;i++)cin>>v[i],v[i]--;
    memset(dp,-1,sizeof(dp));
    int start=0;
    for(int i=0;i<7;i++)start=(start<<2)|(~v[i]?v[i]:0);
    dp[0][start]=0;
    for(int i=0;i<25;i++){

        for(int S=0;S<(1<<14);S++){
            if(dp[i][S]==-1)continue;
            int c[7];
            for(int j=0;j<7;j++)c[6-j]=S>>(j*2)&3;

            int to=toBit(c,i);
            if(i%5==4||i>=20){
                dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));
                continue;
            }
            for(int j=0;j<4;j++){
                to=toBit(c,i);
                dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));

                if(~v[i])c[0]=(c[0]+1)%4;
                if(~v[i+1])c[1]=(c[1]+1)%4;
                if(~v[i+5])c[5]=(c[5]+1)%4;
                if(~v[i+6])c[6]=(c[6]+1)%4;
            }

        }
    }

    int ans=0;
    for(int i=0;i<(1<<14);i++)ans=max(ans,dp[25][i]);

    cout<<ans<<endl;

}

int main(){
    int n;cin>>n;
    while(n--)solve();
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3724
final_rank: 6

code_snippet: #include<bits/stdc++.h>
using namespace std;

int dp[40][1<<14];
int v[40];

inline int pnt(int c){
    return c>0?50+c*10:0;
}

int toBit(int c[7],int p){
    int bit=0;
    for(int i=1;i<7;i++){
        bit=(bit<<2)|c[i];
    }
    bit=(bit<<2)|(~v[p+7]?v[p+7]:0);
    return bit;
}

void solve(){
    memset(v,-1,sizeof(v));
    for(int i=0;i<25;i++)cin>>v[i],v[i]--;
    memset(dp,-1,sizeof(dp));
    int start=0;
    for(int i=0;i<7;i++)start=(start<<2)|(~v[i]?v[i]:0);
    dp[0][start]=0;
    for(int i=0;i<25;i++){

        for(int S=0;S<(1<<14);S++){
            if(dp[i][S]==-1)continue;
            int c[7];
            for(int j=0;j<7;j++)c[6-j]=S>>(j*2)&3;

            int to=((S<<2)&((1<<14)-1))|(~v[i+7]?v[i+7]:0);
            if(i%5==4||i>=20){
                dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));
                continue;
            }
            for(int j=0;j<4;j++){
                to=toBit(c,i);
                dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));

                if(~v[i])c[0]=(c[0]+1)%4;
                if(~v[i+1])c[1]=(c[1]+1)%4;
                if(~v[i+5])c[5]=(c[5]+1)%4;
                if(~v[i+6])c[6]=(c[6]+1)%4;
            }

        }
    }

    int ans=0;
    for(int i=0;i<(1<<14);i++)ans=max(ans,dp[25][i]);

    cout<<ans<<endl;

}

int main(){
    int n;cin>>n;
    while(n--)solve();
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3728
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;

int dp[40][1<<14];
int v[40];

int pnt(int c){
    return c>0?50+c*10:0;
}

int toBit(int c[7],int p){
    int bit=0;
    for(int i=1;i<7;i++){
        bit=(bit<<2)|c[i];
    }
    bit=(bit<<2)|(~v[p+7]?v[p+7]:0);
    return bit;
}

void solve(){
    memset(v,-1,sizeof(v));
    for(int i=0;i<25;i++)cin>>v[i],v[i]--;
    memset(dp,-1,sizeof(dp));
    int start=0;
    for(int i=0;i<7;i++)start=(start<<2)|(~v[i]?v[i]:0);
    dp[0][start]=0;
    for(int i=0;i<25;i++){

        for(int S=0;S<(1<<14);S++){
            if(dp[i][S]==-1)continue;
            int c[7];
            for(int j=0;j<7;j++)c[6-j]=S>>(j*2)&3;

            int to=toBit(c,i);
            if(i%5==4||i>=20){
                dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));
                continue;
            }
            for(int j=0;j<4;j++){
                to=toBit(c,i);
                dp[i+1][to]=max(dp[i+1][to],dp[i][S]+pnt(c[0]));

                if(~v[i])c[0]=(c[0]+1)%4;
                if(~v[i+1])c[1]=(c[1]+1)%4;
                if(~v[i+5])c[5]=(c[5]+1)%4;
                if(~v[i+6])c[6]=(c[6]+1)%4;
            }

        }
    }

    int ans=0;
    for(int i=0;i<(1<<14);i++)ans=max(ans,dp[25][i]);

    cout<<ans<<endl;

}

int main(){
    int n;cin>>n;
    while(n--)solve();
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 3728
final_rank: 7

code_snippet: #include<bits/stdc++.h>
using namespace std;

int dp[40][1<<14];
int v[40];

int pnt(int c){
    return c>0?50+c*10:0;
}

int toBit(int c[7],int p){
    int bit=0;
    for(int i=1;i<7;i++){
        bit=(bit<<2)|c[i];
    }
    bit=(bit<<2)|(~v[p+7]?v[p+7]:0);
    return bit;
}

void solve(){
    memset(v,-1,sizeof(v));
    for(int i=0;i<25;i++)cin>>v[i],v[i]--;
    memset(dp,-1,sizeof(dp));
    int start=0;
    for(int i=0;i<7;i++)start=(start<<2)|(~v[i]?v[i]:0);
    dp[0][start]=0;
    for(int i=0;i<25;i++){

        for(int S=0;S<(1<<14);S++){
            if(dp[i][S]==-1)continue;
            int c[7];
            for(int j=0;j<7;j++)c[6-j]=S>>(j*2)&3;

            if(i%5==4||i>=20){
                dp[i+1][toBit(c,i)]=max(dp[i+1][toBit(c,i)],dp[i][S]+pnt(c[0]));
                continue;
            }
            for(int j=0;j<4;j++){
                dp[i+1][toBit(c,i)]=max(dp[i+1][toBit(c,i)],dp[i][S]+pnt(c[0]));

                for(int k=0;k<7;k++)if(k<2||k>=5){
                    if(~v[i+k])c[k]=(c[k]+1)%4;
                }
            }

        }
    }

    int ans=0;
    for(int i=0;i<(1<<14);i++)ans=max(ans,dp[25][i]);

    cout<<ans<<endl;

}

int main(){
    int n;cin>>n;
    while(n--)solve();
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3728
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;

struct NODE{
    int m[7];

    bool operator<(const NODE &n)const{
        for(int i=0;i<7;i++)if(m[i]!=n.m[i])return m[i]<n.m[i];
        return false;
    }
};

int to[]={0,2,3,4,1};
int table[]={0,60,10,10,-80};
int score[]={0,0,60,70,80};
int fld[5][5];

void solve(){
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            cin>>fld[i][j];

    map<NODE,int>prev;
    NODE start;
    for(int i=0;i<5;i++)start.m[6-i]=fld[0][i];
    for(int i=0;i<2;i++)start.m[1-i]=fld[1][i];
    int sum=0;
    for(int i=0;i<5;i++)
        for(int j=0;j<5;j++)
            sum+=score[fld[i][j]];
    prev[start]=sum;

    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            map<NODE,int>next;
            for(map<NODE,int>::iterator it=prev.begin();it!=prev.end();it++){

                NODE d=it->first;
                int val=it->second;
                for(int k=0;k<4;k++){
                    for(int l=0;l<7;l++)if(l<2||l>=5){

                        val+=table[d.m[l]];
                        d.m[l]=to[d.m[l]];
                    }
                    NODE e=d;
                    if(j!=3){
                        for(int l=6;l>0;l--)e.m[l]=e.m[l-1];
                        e.m[0]=fld[i+1][j+2];
                    }
                    else{
                        for(int l=6;l>1;l--)e.m[l]=e.m[l-2];
                        e.m[1]=fld[i+2][0];
                        e.m[0]=fld[i+2][1];
                    }
                    if(next.find(e)!=next.end())next[e]=max(next[e],val);
                    else next[e]=val;
                }
            }
            prev=next;
        }

    }

    int ans=0;
    for(map<NODE,int>::iterator it=prev.begin();it!=prev.end();it++){
        ans=max(ans,it->second);
    }

    cout<<ans<<endl;


}
int main(){
    int n;
    cin>>n;

    while(n--)solve();
    return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 1356
final_rank: 2