Problem:
Problem H:
Galaxy Wide Web Service
The volume of access to a web service varies from time to time in a day. Also, the hours with the highest
volume of access varies from service to service. For example, a service popular in the United States
may receive more access in the daytime in the United States, while another service popular in Japan may
receive more access in the daytime in Japan. When you develop a web service, you have to design the
system so it can handle all requests made during the busiest hours.
You are a lead engineer in charge of a web service in the 30th century. It’s the era of Galaxy Wide Web
(GWW), thanks to the invention of faster-than-light communication. The service can be accessed from
all over the galaxy. Thus many intelligent creatures, not limited to human beings, can use the service.
Since the volume of access to your service is increasing these days, you have decided to reinforce the
server system. You want to design a new system that handles requests well even during the hours with the
highest volume of access. However, this is not a trivial task. Residents in each planet have their specific
length of
a day
, say, a cycle of life. The length of
a day
is not always 24 hours. Therefore, a cycle of the
volume of access are different by planets of users.
You have obtained hourly data of the volume of access for all planets where you provide the service.
Assuming the volume of access follows a daily cycle for each planet, you want to know the highest
volume of access in one hour. It should be a quite easy task for you, a famous talented engineer in the
galaxy.
Input
The input consists of multiple datasets. Each dataset has the following format:
N
d
1
t
1
q
1,0
...
q
1,
d
1
-1
...
d
N
t
N
q
N
,0
...
q
N
,
d
N
-1
N
is the number of planets.
d
i
(1 ≤
i
≤
N
) is the length of a day in the planet
i
.
t
i
(0 ≤
t
i
≤
d
i
- 1) is the current time of the planet
i
.
q
i, j
is the volume of access on the planet
i
during from the
j
-th hour to the (
j
+1)-th hour.
You may assume that
N
≤ 100,
d
i
≤ 24,
q
i, j
≤ 1000000 (1 ≤
i
≤
N
, 0 ≤
j
≤
d
i
- 1).
The last dataset is followed by a line containing one zero. This line is not a part of any dataset and should not be processed.
Output
For each dataset, output the maximum volume of access in one hour in a line.
Sample Input
2
4 0 1 2 3 4
2 0 2 1
0
Output for the Sample Input
5

Submitted solutions:

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

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2017/11/15  Problem: AOJ 2162 / Link: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2162  ----- */
/* ------問題------



-----問題ここまで----- */
/* -----解説等-----

全周期をみたいけどLCM(1 to 24)は5*10^9overなのできびしい。
3,7だけ見ると愚直をしなくての3maxと7maxをとって足しても答えになる。これは互いに素の時のみできる
つまり2,4は無理
互いに素なものは別でやっていいのでなるべく取り除けばLCMを小さくできる。
今回はmaxが24なので13以上の素数を取り除くと、これらのLDMは 55440でハッピー
こっちは全探索をしてあとは独立にやってもよいから間に合ってくれる。

----解説ここまで---- */

LL N;

LL ans = 0LL;
bool doku(int a) { return (a == 13 || a == 17 || a == 19 || a == 23); }

int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);
	map<int, int>m;
	while (cin >> N, N) {
		VL a(55440 + 1, 0);
		VVL b(24 + 1, VL(24, 0));
		FOR(_, 0, N) {
			int d; cin >> d;
			int t; cin >> t;
			VL in(d);
			FOR(j, 0, d) {
				cin >> in[j];
				b[d][(j - t + d) % d] += in[j];
			}
		}
		ans = 0;
		FOR(i, 1, 24 + 1) {
			if (doku(i)) {
				ans += *max_element(ALL(b[i]));
			}
			else {
				FOR(j, 0, 55440 + 1) {
					a[j] += b[i][j%i];
				}
			}
		}
		ans += *max_element(ALL(a));
		cout << ans << endl;
	}
	return 0;

}
accuracy: 1
time_ms: 110
memory_kb: 3784
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define srep(i, s, t) for(int i = (s); i < (t); i++)
#define len(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
template < class T > bool chmin(T& a, T b) { if(a > b) { a = b; return true; } return false; }
template < class T > bool chmax(T& a, T b) { if(a < b) { a = b; return true; } return false; }
using i64 = long long;
using f64 = long double;
i64 floor_div(const i64 n, const i64 d) { assert(d != 0); return n / d - static_cast<i64>((n ^ d) <  0 && n % d != 0); }
i64  ceil_div(const i64 n, const i64 d) { assert(d != 0); return n / d + static_cast<i64>((n ^ d) >= 0 && n % d != 0); }

constexpr int M = 55440;
const set<int> S = {13, 17, 19, 23};
int solve(int N) {
    vector<i64> a(M, 0);
    map<int, vector<i64>> mp;
    for(int s : S) mp[s] = vector<i64>(M, 0);
    rep(i, N) {
        int d, t; cin >> d >> t;
        vector<int> q(d);
        for(auto& e : q) cin >> e;
        if(S.count(d)) {
            rep(k, d) mp[d][k] += q[(t + k) % d];
        } else {
            rep(m, M) a[m] += q[(t + m) % d];
        }
    }

    i64 ans = *max_element(a.begin(), a.end());
    for(int s : S) ans += *max_element(mp[s].begin(), mp[s].end());
    return ans;
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    
    while(true) {
        int N; cin >> N;
        if(N == 0) return 0;
        cout << solve(N) << "\n";
    }
}
accuracy: 1
time_ms: 200
memory_kb: 5480
final_rank: 19

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

using namespace std;

#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rrep(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define ALL(v) (v).begin(), (v).end()
#define UNIQUE(v) sort(ALL(v)), (v).erase(unique(ALL(v)), (v).end())
#define SZ(v) (int)v.size()
#define MIN(v) *min_element(ALL(v))
#define MAX(v) *max_element(ALL(v))
#define LB(v, x) int(lower_bound(ALL(v), (x)) - (v).begin())
#define UB(v, x) int(upper_bound(ALL(v), (x)) - (v).begin())

using uint = unsigned int;
using ll = long long int;
using ull = unsigned long long;
using i128 = __int128_t;
using u128 = __uint128_t;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;

template <typename T> inline bool chmax(T &a, T b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T, typename U> T ceil(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <typename T, typename U> T floor(T x, U y) {
    assert(y != 0);
    if (y < 0)
        x = -x, y = -y;
    return (x > 0 ? x / y : (x - y + 1) / y);
}
template <typename T> int popcnt(T x) {
    return __builtin_popcountll(x);
}
template <typename T> int topbit(T x) {
    return (x == 0 ? -1 : 63 - __builtin_clzll(x));
}
template <typename T> int lowbit(T x) {
    return (x == 0 ? -1 : __builtin_ctzll(x));
}

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << "P(" << p.first << ", " << p.second << ")";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
    os << "{";
    for (int i = 0; i < vec.size(); i++) {
        os << vec[i] << (i + 1 == vec.size() ? "" : ", ");
    }
    os << "}";
    return os;
}
template <typename T, typename U>
ostream &operator<<(ostream &os, const map<T, U> &map_var) {
    os << "{";
    for (auto itr = map_var.begin(); itr != map_var.end(); itr++) {
        os << "(" << itr->first << ", " << itr->second << ")";
        itr++;
        if (itr != map_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
template <typename T> ostream &operator<<(ostream &os, const set<T> &set_var) {
    os << "{";
    for (auto itr = set_var.begin(); itr != set_var.end(); itr++) {
        os << *itr;
        ++itr;
        if (itr != set_var.end())
            os << ", ";
        itr--;
    }
    os << "}";
    return os;
}
#ifdef LOCAL
#define show(...) _show(0, #__VA_ARGS__, __VA_ARGS__)
#else
#define show(...) true
#endif
template <typename T> void _show(int i, T name) {
    cerr << '\n';
}
template <typename T1, typename T2, typename... T3>
void _show(int i, const T1 &a, const T2 &b, const T3 &...c) {
    for (; a[i] != ',' && a[i] != '\0'; i++)
        cerr << a[i];
    cerr << ":" << b << " ";
    _show(i + 1, a, c...);
}

/**
 * @brief template
 */

int main() {
    int MAXS = 55440;
while(1) {
    int N;
    cin >> N;
    if (N == 0) return 0;
    vector<ll> A13(13,0), A17(17,0), A19(19,0), A23(23,0), Other(MAXS,0);
    rep(i,0,N) {
        int D, T;
        cin >> D >> T;
        vector<ll> Q(D);
        rep(j,0,D) cin >> Q[(j+D-T)%D];
        if (D == 13) {
            rep(j,0,13) A13[j] += Q[j];
        }
        else if (D == 17) {
            rep(j,0,17) A17[j] += Q[j];
        }
        else if (D == 19) {
            rep(j,0,19) A19[j] += Q[j];
        }
        else if (D == 23) {
            rep(j,0,23) A23[j] += Q[j];
        }
        else {
            rep(j,0,MAXS) Other[j] += Q[j%D];
        }
    }
    ll ANS = 0, MAX = 0;
    rep(i,0,13) chmax(MAX,A13[i]);
    ANS += MAX, MAX = 0;
    rep(i,0,17) chmax(MAX,A17[i]);
    ANS += MAX, MAX = 0;
    rep(i,0,19) chmax(MAX,A19[i]);
    ANS += MAX, MAX = 0;
    rep(i,0,23) chmax(MAX,A23[i]);
    ANS += MAX, MAX = 0;
    rep(i,0,MAXS) chmax(MAX,Other[i]);
    ANS += MAX;
    cout << ANS << endl;
}
}
accuracy: 1
time_ms: 220
memory_kb: 3780
final_rank: 14

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

const int MAX = 56000;
const int prime[4] = {13, 17, 19, 23};
int tot[MAX];

void solve(int N) {
    vector<vector<int>> sp(4);
    for (int i = 0; i < 4; i++) sp[i].resize(prime[i]);
    for (; N--;) {
        int d, t;
        cin >> d >> t;
        vector<int> q(d);
        for (int i = 0; i < d; i++) cin >> q[i];
        bool flag = false;
        for (int i = 0; i < 4; i++) {
            if (d != prime[i]) continue;
            for (int j = 0; j < d; j++) {
                sp[i][j] += q[t];
                if (++t == d) t = 0;
            }
            flag = true;
        }
        if (flag) continue;
        for (int i = 0; i < MAX; i++) {
            tot[i] += q[t];
            if (++t == d) t = 0;
        }
    }
    int ans = 0;
    for (int i = 0; i < MAX; i++) {
        ans = max(ans, tot[i]);
        tot[i] = 0;
    }
    for (int i = 0; i < 4; i++) ans += *max_element(sp[i].begin(), sp[i].end());
    cout << ans << '\n';
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    while (cin >> N, N) solve(N);
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3564
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ALL(x) (x).begin(), (x).end()
#ifdef LOCAL
#include "debug.hpp"
#else
#define debug(...) void(0)
#endif

template <typename T> istream& operator>>(istream& is, vector<T>& v) {
    for (T& x : v) is >> x;
    return is;
}
template <typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    for (size_t i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "" : " ");
    }
    return os;
}

int topbit(signed t) { return t == 0 ? -1 : 31 - __builtin_clz(t); }
int topbit(long long t) { return t == 0 ? -1 : 63 - __builtin_clzll(t); }
int botbit(signed a) { return a == 0 ? 32 : __builtin_ctz(a); }
int botbit(long long a) { return a == 0 ? 64 : __builtin_ctzll(a); }
int popcount(signed t) { return __builtin_popcount(t); }
int popcount(long long t) { return __builtin_popcountll(t); }
bool ispow2(int i) { return i && (i & -i) == i; }
long long MSK(int n) { return (1LL << n) - 1; }

template <class T> T ceil(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? (x + y - 1) / y : x / y);
}
template <class T> T floor(T x, T y) {
    assert(y >= 1);
    return (x > 0 ? x / y : (x - y + 1) / y);
}

template <class T1, class T2> inline bool chmin(T1& a, T2 b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template <class T1, class T2> inline bool chmax(T1& a, T2 b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template <typename T> void mkuni(vector<T>& v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}
template <typename T> int lwb(const vector<T>& v, const T& x) { return lower_bound(v.begin(), v.end(), x) - v.begin(); }

const int INF = (1 << 30) - 1;
const long long IINF = (1LL << 60) - 1;
const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const int MOD = 998244353;
// const int MOD = 1000000007;

const int MAX = 56000;
const int prime[4] = {13, 17, 19, 23};
int tot[MAX];

void solve(int N) {
    vector<vector<int>> sp(4);
    for (int i = 0; i < 4; i++) sp[i].resize(prime[i]);
    for (; N--;) {
        int d, t;
        cin >> d >> t;
        vector<int> q(d);
        for (int i = 0; i < d; i++) cin >> q[i];
        bool flag = false;
        for (int i = 0; i < 4; i++) {
            if (d != prime[i]) continue;
            for (int j = 0; j < d; j++) {
                sp[i][j] += q[t];
                if (++t == d) t = 0;
            }
            flag = true;
        }
        if (flag) continue;
        for (int i = 0; i < MAX; i++) {
            tot[i] += q[t];
            if (++t == d) t = 0;
        }
    }
    int ans = 0;
    for (int i = 0; i < MAX; i++) {
        ans = max(ans, tot[i]);
        tot[i] = 0;
    }
    for (int i = 0; i < 4; i++) ans += *max_element(sp[i].begin(), sp[i].end());
    cout << ans << '\n';
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    while (cin >> N, N) solve(N);
    return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 3584
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    constexpr int M = 16*9*5*7*11;

    const int primes[] = {13, 17, 19, 23};

    while (true) {
        int N;
        cin >> N;
        if (N == 0) break;
        vector<ll> val(M);
        vector<vector<ll>> val2(4);
        rep(k,0,4) val2[k] = vector<ll>(primes[k]);
        rep(i,0,N) {
            int d, t;
            cin >> d >> t;
            vector<ll> q(d);
            for (auto& x : q) cin >> x;
            bool f = false;
            rep(k,0,4) {
                if (d == primes[k]) {
                    f = true;
                    rep(j,0,d) val2[k][j] += q[(t+j)%d];
                    break;
                }
            }
            if (!f) {
                rep(j,0,M) {
                    val[j] += q[(t+j)%d];
                }
            }
        }
        ll ans = 0;
        rep(k,0,4) ans += *max_element(all(val2[k]));
        ans += *max_element(all(val));
        cout << ans << endl;
    }
}
// 2, 3, 5, 7, 11, 13, 17, 19, 23
accuracy: 1
time_ms: 190
memory_kb: 3744
final_rank: 13

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

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  while (true) {
    int n;
    cin >> n;
    if (n == 0) return 0;

    vector<vector<int>> G(5, vector<int>(55440));
    // 2~5行目は13, 17, 19, 23に対応
    // 1行目はその他全てに対応 (これらの最小公倍数が55440)
    for (int i = 0; i < n; i++) {
      int d, t;
      cin >> d >> t;

      vector<int> q(d);
      for (int j = 0; j < d; j++) cin >> q.at(j);
      if (t > 0) rotate(q.begin(), q.begin() + t, q.end());

      if (d == 13) {
        for (int j = 0; j < d; j++) G[1][j] += q[j];
      }
      else if (d == 17) {
        for (int j = 0; j < d; j++) G[2][j] += q[j];
      }
      else if (d == 19) {
        for (int j = 0; j < d; j++) G[3][j] += q[j];
      }
      else if (d == 23) {
        for (int j = 0; j < d; j++) G[4][j] += q[j];
      }
      else {
        for (int j = 0; j < 55440; j++) G[0][j] += q[j % d];
      }
    }

    vector<int> ans(5, 0);
    for (int j = 0; j < 55440; j++) ans[0] = max(ans[0], G[0][j]);
    for (int j = 0; j < 13; j++) ans[1] = max(ans[1], G[1][j]);
    for (int j = 0; j < 17; j++) ans[2] = max(ans[2], G[2][j]);
    for (int j = 0; j < 19; j++) ans[3] = max(ans[3], G[3][j]);
    for (int j = 0; j < 23; j++) ans[4] = max(ans[4], G[4][j]);

    cout << ans[0] + ans[1] + ans[2] + ans[3] + ans[4] << '\n';
  }
}
accuracy: 1
time_ms: 230
memory_kb: 4316
final_rank: 16

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<double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

// 13,17,19,23 を除外

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int lc = 1;
    for(int i=1;i<=24;i++){
        if(i == 13 or i == 17 or i == 19 or i == 23) continue;
        lc = lc * i / __gcd(lc,i);
    }
    int n;
    while(cin >> n,n){
        vector<vector<int>> v(25,vector<int>(25));
        for(int i=0;i<n;i++){
            int d,t; cin >> d >> t;
            for(int j=0;j<d;j++){
                int x; cin >> x;
                v[d][(t-j+d)%d] += x;
            }
        }
        int res = 0;
        vector<int> cnt(lc);
        for(int i=1;i<=24;i++){
            if(i == 13 or i == 17 or i == 19 or i == 23){
                sort(v[i].begin(), v[i].end());
                res += v[i].back();
            }
            else{
                for(int j=0;j<lc;j++){
                    cnt[j] += v[i][j%i];
                }
            }
        }
        sort(cnt.begin(), cnt.end());
        cout << res + cnt.back() << "\n";
    }
}
accuracy: 1
time_ms: 210
memory_kb: 3536
final_rank: 10

code_snippet: /*	author:  Kite_kuma
	created: 2021.10.29 21:02:49 */

// #ifdef LOCAL
// #define _GLIBCXX_DEBUG
// #endif
#include <bits/stdc++.h>
using namespace std;

#pragma region macros

#define foa(s, v) for(auto &s : v)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define popcnt(n) __builtin_popcountll((long long)n)

#define REPname(a, b, c, d, e, ...) e
#define rep(...) REPname(__VA_ARGS__, REP3, REP2, REP1, REP0)(__VA_ARGS__)
#define REP0(x) for(int Counter_in_rep_macro = 0; Counter_in_rep_macro < (x); ++Counter_in_rep_macro)
#define REP1(i, x) for(int i = 0; i < (x); ++i)
#define REP2(i, l, r) for(int i = (l); i < (r); ++i)
#define REP3(i, l, r, c) for(int i = (l); i < (r); i += (c))

#define DREPname(a, b, c, d, e, ...) e
#define drep(...) DREPname(__VA_ARGS__, DREP3, DREP2, DREP1)(__VA_ARGS__)
#define DREP1(i, x) for(int i = (x)-1; i >= 0; --i)
#define DREP2(i, l, r) for(int i = (r)-1; i >= (l); --i)
#define DREP3(i, l, r, c) for(int i = (r)-1; i >= (l); i -= (c))

#pragma endregion

#pragma region aliases

using ll = long long;
using ld = long double;
using ull = unsigned long long;
using vi = std::vector<int>;
using vvi = std::vector<std::vector<int>>;
using vvvi = std::vector<std::vector<std::vector<int>>>;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
using pii = std::pair<int, int>;
using pll = std::pair<long long, long long>;
template <class T = ll>
using V = std::vector<T>;
template <class T = ll>
using VV = V<V<T>>;
template <class T = ll>
using VVV = V<V<V<T>>>;
template <class T = ll>
using pqup = std::priority_queue<T, std::vector<T>, std::greater<T>>;
template <class T = ll>
using pqdn = std::priority_queue<T>;

#pragma endregion

#pragma region constants

const int inf = 1e9;
const long long INF = 1e18;
const long double pi = acos(-1);
const char dl = '\n';
const char sp = ' ';
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};

const int mod_1000000007 = 1000000007;
const int mod_998244353 = 998244353;

#pragma endregion

#pragma region basic_operation

template <class T0, class T1, class T2>
inline bool in_range(T0 x, T1 lef, T2 rig) {
	return ((lef <= x) && (x < rig));
}

template <class T>
inline bool chmin(T &a, T b) {
	if(a > b) {
		a = b;
		return true;
	}
	return false;
}
template <class T>
inline bool chmax(T &a, T b) {
	if(a < b) {
		a = b;
		return true;
	}
	return false;
}

void Yes(bool f = 1) { std::cout << (f ? "Yes" : "No") << '\n'; }
void No() { std::cout << "No\n"; }
void YES(bool f = 1) { std::cout << (f ? "YES" : "NO") << '\n'; }
void NO() { std::cout << "NO\n"; }

template <class T>
void drop(T answer) {
	std::cout << answer << '\n';
	exit(0);
}

void err(bool flag = true) {
	if(!flag) return;
	std::cout << -1 << '\n';
	exit(0);
}

template <class T>
void vout(std::vector<T> const &v, bool vertically = 0) {
	if(vertically) {
		for(auto const &a : v) {
			std::cout << a << '\n';
		}
		return;
	}
	for(auto it = v.begin(); it != v.end(); it++) {
		std::cout << (*it);
		if(it != v.end() - 1) {
			std::cout << ' ';
		}
	}
	std::cout << '\n';
	return;
}

inline void print() { std::cout << '\n'; }
template <class T>
inline void print(T x) {
	std::cout << x << '\n';
	return;
}
template <typename Head, typename... Tail>
void print(Head H, Tail... T) {
	std::cout << H << " ";
	print(T...);
}

template <class T>
void add(std::vector<T> &v, T val) {
	for(auto &a : v) a += val;
	return;
}

template <class T>
T dup(T a, T b) {
	assert(b != 0);
	return (a + b - 1) / b;
}

template <class T>
T greatest_lower_multiple(T x, T d) {
	if(d == 0) return 0;
	if(d < 0) d *= -1;
	T y = x % d;
	if(y < 0) y += d;
	return x - y;
}

template <class T>
T least_upper_multiple(T x, T d) {
	return -greatest_lower_multiple(-x, d);
}

long long POW(long long a, long long n) {
	long long res = 1;
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}

long long modpow(long long a, long long n, long long mod) {	 // a^n mod
	assert(n >= 0 && mod);
	if(mod == 1) return 0LL;
	long long res = 1;
	a %= mod;
	while(n > 0) {
		if(n & 1) res = res * a % mod;
		a = a * a % mod;
		n >>= 1;
	}
	return res < 0 ? res + mod : res;
}

// return x which satisfies a * x % mod == gcd(a, mod)
// not (mod | a)
long long modinv(long long a, long long mod) {
	long long b = mod, u = 1, v = 0;
	while(b) {
		long long t = a / b;
		a -= t * b;
		std::swap(a, b);
		u -= t * v;
		std::swap(u, v);
	}
	u %= mod;
	if(u < 0) u += mod;
	return u;
}

template <class T>
int lb(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::lower_bound(a.begin(), a.end(), x));
}
template <class T>
int ub(const std::vector<T> &a, const T x) {
	return std::distance(a.begin(), std::upper_bound(a.begin(), a.end(), x));
}
template <class T>
void unq_sort(std::vector<T> &a) {
	std::sort(a.begin(), a.end());
	a.erase(std::unique(a.begin(), a.end()), a.end());
}
template <class T>
std::vector<int> press(std::vector<T> &a) {
	auto vec = a;
	unq_sort(vec);
	std::vector<int> ret;
	for(auto &v : a) ret.push_back(lb(vec, v));
	return ret;
}

#pragma endregion

#pragma region input
#define VEC(type, name, size) \
	vector<type> name(size);  \
	scanner::INPUT(name)
#define VVEC(type, name, h, w)                     \
	vector<vector<type>> name(h, vector<type>(w)); \
	scanner::INPUT(name)
#define INT(...)     \
	int __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LL(...)            \
	long long __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define STR(...)        \
	string __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define CHR(...)      \
	char __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define DBL(...)        \
	double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define LD(...)              \
	long double __VA_ARGS__; \
	scanner::INPUT(__VA_ARGS__)
#define TPL3(type0, type1, type2, name)   \
	std::tuple<type0, type1, type2> name; \
	scanner::INPUT(name);
#define TPL4(type0, type1, type2, type3, name)   \
	std::tuple<type0, type1, type2, type3> name; \
	scanner::INPUT(name);

namespace scanner {
template <class T>
void scan(T &a) {
	std::cin >> a;
}

template <class T, class L>
void scan(std::pair<T, L> &p) {
	scan(p.first);
	scan(p.second);
}

template <class T0, class T1, class T2>
void scan(std::tuple<T0, T1, T2> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	scan(t0);
	scan(t1);
	scan(t2);
	p = std::make_tuple(t0, t1, t2);
}

template <class T0, class T1, class T2, class T3>
void scan(std::tuple<T0, T1, T2, T3> &p) {
	T0 t0;
	T1 t1;
	T2 t2;
	T3 t3;
	scan(t0);
	scan(t1);
	scan(t2);
	scan(t3);
	p = std::make_tuple(t0, t1, t2, t3);
}

template <class T>
void scan(std::vector<T> &a) {
	for(auto &i : a) scan(i);
}

void INPUT() {}
template <class Head, class... Tail>
void INPUT(Head &head, Tail &... tail) {
	scan(head);
	INPUT(tail...);
}
}  // namespace scanner

template <typename T1, typename T2>
std::istream &operator>>(std::istream &is, std::pair<T1, T2> &p) {
	is >> p.first >> p.second;
	return is;
}
#pragma endregion

#pragma region debug

#pragma region output
template <typename T1, typename T2>
std::ostream &std::operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
	os << p.first << " " << p.second;
	return os;
}
template <class T>
std::ostream &std::operator<<(std::ostream &os, const std::vector<T> &v) {
	for(int i = 0; i < (int)v.size(); i++) {
		if(i) os << " ";
		os << v[i];
	}
	return os;
}
#pragma endregion

#pragma region view

namespace viewer {
void view(const long long e) {
	if(e == INF)
		std::cerr << "INF";
	else if(e == -INF)
		std::cerr << "-INF";
	else
		std::cerr << e;
}

void view(const int e) {
	if(e == inf)
		std::cerr << "inf";
	else if(e == -inf)
		std::cerr << "-inf";
	else
		std::cerr << e;
}

template <typename T>
void view(const T e) {
	std::cerr << e;
}

template <typename T, typename U>
void view(const std::pair<T, U> &p) {
	std::cerr << "(";
	view(p.first);
	std::cerr << ", ";
	view(p.second);
	std::cerr << ")";
}

template <class T0, class T1, class T2>
void view(const std::tuple<T0, T1, T2> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ")";
}

template <class T0, class T1, class T2, class T3>
void view(const std::tuple<T0, T1, T2, T3> &p) {
	std::cerr << "(";
	view(std::get<0>(p));
	std::cerr << ", ";
	view(std::get<1>(p));
	std::cerr << ", ";
	view(std::get<2>(p));
	std::cerr << ", ";
	view(std::get<3>(p));
	std::cerr << ")";
}

template <typename T>
void view(const std::set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::unordered_set<T> &s) {
	if(s.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(auto &t : s) {
		view(t);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<T> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << "{ ";
	for(const auto &e : v) {
		view(e);
		std::cerr << ", ";
	}
	std::cerr << "\b\b }";
}

template <typename T>
void view(const std::vector<std::vector<T>> &vv) {
	std::cerr << "{\n";
	for(const auto &v : vv) {
		std::cerr << "\t";
		view(v);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::vector<std::pair<T, U>> &v) {
	std::cerr << "{\n";
	for(const auto &c : v) {
		std::cerr << "\t(";
		view(c.first);
		std::cerr << ", ";
		view(c.second);
		std::cerr << ")\n";
	}
	std::cerr << "}";
}

template <class T0, class T1, class T2>
void view(const std::vector<std::tuple<T0, T1, T2>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <class T0, class T1, class T2, class T3>
void view(const std::vector<std::tuple<T0, T1, T2, T3>> &v) {
	if(v.empty()) {
		std::cerr << "{ }";
		return;
	}
	std::cerr << '{';
	for(const auto &t : v) {
		std::cerr << "\n\t";
		view(t);
		std::cerr << ",";
	}
	std::cerr << "\n}";
}

template <typename T, typename U>
void view(const std::map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}

template <typename T, typename U>
void view(const std::unordered_map<T, U> &m) {
	std::cerr << "{\n";
	for(const auto &t : m) {
		std::cerr << "\t[";
		view(t.first);
		std::cerr << "] : ";
		view(t.second);
		std::cerr << '\n';
	}
	std::cerr << "}";
}
}  // namespace viewer

#pragma endregion

// when debugging : g++ foo.cpp -DLOCAL
#ifdef LOCAL
void debug_out() {}
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
	viewer::view(H);
	std::cerr << ", ";
	debug_out(T...);
}
#define debug(...)                                                \
	do {                                                          \
		std::cerr << __LINE__ << " [" << #__VA_ARGS__ << "] : ["; \
		debug_out(__VA_ARGS__);                                   \
		std::cerr << "\b\b]\n";                                   \
	} while(0)
#define dump(x)                                      \
	do {                                             \
		std::cerr << __LINE__ << " " << #x << " : "; \
		viewer::view(x);                             \
		std::cerr << '\n';                           \
	} while(0)

#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif

#pragma endregion

ll solve(int n) {
	vector<vll> planets;
	rep(n) {
		int day;
		cin >> day;
		int time;
		cin >> time;
		vll res(day);
		rep(i, day) { cin >> res[(i - time + day * 2) % day]; }
		planets.push_back(res);
	}

	// debug(planets);

	auto beki_max = [](int d, int base) {
		int ret = 1;
		while(d % (ret * base) == 0) ret *= base;
		return ret;
	};

	ll ans = -1;

	const int daytime_max = 24;
	rep(two, 32) rep(three, 27) {
		vvll values(daytime_max + 1);
		rep(d, 1, daytime_max + 1) { values[d].resize(d); }

		foa(p, planets) {
			int ps = p.size();
			int t2 = beki_max(ps, 2);
			int t3 = beki_max(ps, 3);
			int base = ps / t2 / t3;
			// debug(ps, t2, t3, base);
			int cnt = 0;
			rep(j, ps) {
				if(j % t2 == two % t2) {
					if(j % t3 == three % t3) {
						values[base][j % (base)] += p[j];
					}
				}
			}
			// assert(cnt == base);
		}

		ll res = 0;
		foa(v, values){
			if(!v.empty()) res += *max_element(all(v));
		}
		chmax(ans, res);
	}

	return ans;
}

int main() {
	int n;
	while(cin >> n && n) {
		cout << solve(n) << dl;
	}
}
accuracy: 1
time_ms: 180
memory_kb: 3464
final_rank: 8

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

using ll = long long;
using ull = unsigned long long;
using PII = pair<ll, ll>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define POPCOUNT(x) __builtin_popcount(x)
template <typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template <typename T> void chmax(T &a, const T &b) { a = max(a, b); }

const ll INF = 1LL << 60;

struct FastIO {
    FastIO() {
        cin.tie(0);
        ios::sync_with_stdio(0);
    }
} fastiofastio;

const ull MOD = 1e9 + 7;

// BEGIN CUT
ll modpow(ll x, ll y, ll m) {
    ll a = 1, p = x;
    while (y > 0) {
        if (y % 2 == 0) {
            p = (p * p) % m;
            y /= 2;
        } else {
            a = (a * p) % m;
            y--;
        }
    }
    return a;
}
// END CUT

ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {
    int N;
    while (cin >> N, N) {
        vector<vector<ll>> data(25);
        for (int i = 1; i <= 24; i++)
            data[i].resize(i);
        for (int i = 0; i < N; i++) {
            int d, t;
            cin >> d >> t;
            for (int j = 0; j < d; j++) {
                int x;
                cin >> x;
                data[d][(j + d - t) % d] += x;
            }
        }
        ll ans = 0;
        for (int i = 0; i < 55440; i++) {
            ll sum = 0;
            for (int j = 1; j <= 24; j++) {
                if (j != 13 && j != 17 && j != 19 && j != 23)
                    sum += data[j][i % j];
            }
            ans = max(ans, sum);
        }
        vector<int> vec = {13, 17, 19, 23};
        for (int d : vec) {
            ll ma = 0;
            for (int i = 0; i < d; i++)
                ma = max(ma, data[d][i]);
            ans += ma;
        }
        cout << ans << '\n';
    }
}
accuracy: 1
time_ms: 150
memory_kb: 3360
final_rank: 5

code_snippet: #pragma GCC ta g("avx2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define DEBUG
#ifdef DEBUG
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
    os << '(' << p.first << ',' << p.second << ')';
    return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
    os << '{';
    for(int i = 0; i < (int)v.size(); i++) {
        if(i) { os << ','; }
        os << v[i];
    }
    os << '}';
    return os;
}
void debugg() { cerr << endl; }
template <class T, class... Args>
void debugg(const T &x, const Args &... args) {
    cerr << " " << x;
    debugg(args...);
}
#define debug(...)                                                             \
    cerr << __LINE__ << " [" << #__VA_ARGS__ << "]: ", debugg(__VA_ARGS__)
#define dump(x) cerr << __LINE__ << " " << #x << " = " << (x) << endl
#else
#define debug(...) (void(0))
#define dump(x) (void(0))
#endif
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = (1<<30)-1;
const ll linf = 1LL<<61;
const int MAX = 510000;
int dy[8] = {0,1,0,-1,1,-1,-1,1};
int dx[8] = {-1,0,1,0,1,-1,1,-1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    int sz = a.size();
    for(auto itr = a.begin(); itr != a.end(); itr++){
		cout << *itr;
        sz--;
        if(sz) cout << " ";
	}
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){
	cout << a << " " << b << "\n";
}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
void mark() {cout << "#" << "\n";}
ll pcount(ll x) {return __builtin_popcountll(x);}
//#include <atcoder/maxflow>
//using namespace atcoder;
//const int mod = 1e9 + 7;
const int mod = 998244353;

ll GCD(ll a, ll b){
    if(b==0) return a;
    else return GCD(b,a%b);
}

ll LCM(ll a, ll b){
    return a/GCD(a,b)*b;
}

bool isPrime(ll x){
    if(x==1 || x==0) return false;
    else{
        for(ll i=2; i*i<=x; i++){
            if(x % i == 0) return false;
        }
        return true;
    }
}

const int M = 55440;
int s[25][24];
int res[M];

int main(){
    while(1){
        int n; cin >> n;
        if(!n) break;
        REP(i,1,25) rep(j,24) s[i][j] = 0;
        rep(j,M) res[j] = 0;
        rep(i,n){
            int d,t; cin >> d >> t;
            vector<int> a(d); rep(j,d) cin >> a[j];
            rep(j,d) s[d][j] += a[(j+t)%d];
        }
        int ans = 0;
        REP(i,1,25){
            if(i>=13 && isPrime(i)){
                ans += *max_element(s[i],s[i]+i);
            }else{
                int ptr = 0;
                rep(j,M){
                    res[j] += s[i][ptr];
                    if(ptr < i-1) ptr++;
                    else ptr = 0;
                }
            }
        }
        ans += *max_element(res,res+M);
        cout << ans << "\n";
    }
}
accuracy: 1
time_ms: 40
memory_kb: 3352
final_rank: 4

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int N;
int p[25];
int T[25][24];
int S[55440];
main()
{
	p[13]=1;
	p[17]=2;
	p[19]=3;
	p[23]=4;
	while(cin>>N,N)
	{
		for(int i=0;i<55440;i++)S[i]=0;
		for(int i=0;i<25;i++)for(int j=0;j<24;j++)T[i][j]=0;
		for(int i=0;i<N;i++)
		{
			int d;cin>>d;
			int X[24]={};
			int t;cin>>t;
			for(int j=0;j<d;j++)
			{
				cin>>X[(j-t+d)%d];
			}
			while(d*2<=24)
			{
				for(int j=0;j<d;j++)X[d+j]=X[j];
				d*=2;
			}
			for(int j=0;j<d;j++)T[d][j]+=X[j];
		}
		int M[4]={};
		for(int i=13;i<=24;i++)
		{
			if(p[i])
			{
				int id=p[i]-1;
				for(int j=0;j<i;j++)if(M[id]<T[i][j])M[id]=T[i][j];
			}
			else
			{
				for(int j=0;j<55440;j+=i)
				{
					for(int k=0;k<i;k++)S[j+k]+=T[i][k];
				}
			}
		}
		int ans=0;
		for(int i=0;i<55440;i++)if(ans<S[i])ans=S[i];
		for(int i=0;i<4;i++)ans+=M[i];
		cout<<ans<<endl;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 3184
final_rank: 1

code_snippet: #include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-12;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

int gcd(int a, int b) {
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	int a = 1, b = 1;
	vector<int>box(25);
	box[13] = box[17] = box[19] = box[23] = 1;
	for (int i = 2; i <= 24; i++) {
		if (box[i]) {
			b *= i;
		}
		else {
			a = i * a / gcd(i, a);
		}
	}
	while (cin >> N, N) {
		vector<int>adp(a);
		vector<int>bdp(b);
		while (N--) {
			cin >> M >> K;
			vector<int>in(M);
			for (auto &i : in)cin >> i;
			if (M == 0)continue;
			if (box[M]) {
				for (int i = 0; i < b; i++) {
					bdp[i] += in[(i + K) % M];
				}
			}
			else {
				for (int i = 0; i < a; i++) {
					adp[i] += in[(i + K) % M];
				}
			}
		}
		cout << *max_element(adp.begin(), adp.end()) + *max_element(bdp.begin(), bdp.end()) << endl;
	}
}
accuracy: 1
time_ms: 1130
memory_kb: 3688
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;
int main(){
  while (1){
    int N;
    cin >> N;
    if (N == 0){
      break;
    }
    vector<int> d(N), t(N);
    vector<vector<int>> q(N);
    for (int i = 0; i < N; i++){
      cin >> d[i] >> t[i];
      q[i] = vector<int>(d[i]);
      for (int j = 0; j < d[i]; j++){
        cin >> q[i][j];
      }
    }
    vector<vector<int>> sum(2);
    int A = 16 * 9 * 5 * 7 * 11;
    int B = 13 * 17 * 19 * 23;
    sum[0] = vector<int>(A, 0);
    sum[1] = vector<int>(B, 0);
    for (int i = 0; i < N; i++){
      int id;
      if (d[i] == 13 || d[i] == 17 || d[i] == 19 || d[i] == 23){
        id = 1;
      } else {
        id = 0;
      }
      int sz = sum[id].size();
      for (int j = 0; j < sz; j++){
        sum[id][j] += q[i][(t[i] + j) % d[i]];
      }
    }
    int mx1 = 0;
    for (int i = 0; i < A; i++){
      mx1 = max(mx1, sum[0][i]);
    }
    int mx2 = 0;
    for (int i = 0; i < B; i++){
      mx2 = max(mx2, sum[1][i]);
    }
    cout << mx1 + mx2 << endl;
  }
}
accuracy: 1
time_ms: 470
memory_kb: 3580
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mp make_pair
#define si(x) int(x.size())
const int mod=1000000007,MAX=205,INF=1<<30;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int N;cin>>N;
        if(N==0) break;
        vector<ll> sum(55440);
        vector<vector<ll>> T(24,vector<ll>(24));
        
        for(int i=0;i<N;i++){
            int d,now;cin>>d>>now;
            if(d==13||d==17||d==19||d==23){
                for(int j=0;j<d;j++){
                    ll x;cin>>x;
                    T[d][(j+d-now)%d]+=x;
                }
            }else{
                for(int j=0;j<d;j++){
                    ll x;cin>>x;
                    for(int k=(j+d-now)%d;k<55440;k+=d){
                        sum[k]+=x;
                    }
                }
            }
        }
        
        ll ans=0;
        
        for(int i=0;i<55440;i++) chmax(ans,sum[i]);
        
        for(int i=0;i<24;i++){
            ll ma=0;
            for(int j=0;j<24;j++) chmax(ma,T[i][j]);
            ans+=ma;
        }
        
        cout<<ans<<endl;
    }
}
accuracy: 1
time_ms: 190
memory_kb: 3572
final_rank: 9

code_snippet: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);
//const ll mod = 1000000007;
ll N;
ll ans;
const ll T = 55440;
ll a[T];
ll A[13], B[17], C[19], D[23];
int main() {
    while(cin >> N) {
        if(N == 0) break;
        for(int i = 0; i < T; i++) {
            a[i] = 0;
        }
        for(int i = 0; i < 13; i++) A[i] = 0;
        for(int i = 0; i < 17; i++) B[i] = 0;
        for(int i = 0; i < 19; i++) C[i] = 0;
        for(int i = 0; i < 23; i++) D[i] = 0;
        ans = 0;
        for(int i = 0; i < N; i++) {
            ll d;
            cin >> d;
            ll t;
            cin >> t;
            vector<ll> q(d);
            for(int j = 0; j < d; j++) cin >> q[j];
            if(d == 13 or d == 17 or d == 19 or d == 23) {
                for(int j = 0; j < d; j++) {
                    if(d == 13) A[j] += q[(j+t)%d];
                    if(d == 17) B[j] += q[(j+t)%d];
                    if(d == 19) C[j] += q[(j+t)%d];
                    if(d == 23) D[j] += q[(j+t)%d];
                }
                continue;
            }
            for(int j = 0; j < T; j++) {
                a[j] += q[(t + j) % d];
            }
        }
        ll maxi = 0;
        for(auto tmp : a) {
            chmax(maxi, tmp);
        }
        ans += maxi;
        maxi = 0;
        for(int i = 0; i < 13; i++) chmax(maxi, A[i]);
        ans += maxi;
        maxi = 0;
        for(int i = 0; i < 17; i++) chmax(maxi, B[i]);
        ans += maxi;
        maxi = 0;
        for(int i = 0; i < 19; i++) chmax(maxi, C[i]);
        ans += maxi;
        maxi = 0;
        for(int i = 0; i < 23; i++) chmax(maxi, D[i]);
        ans += maxi;
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 850
memory_kb: 3428
final_rank: 17

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<ll, ll>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const ll INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2>
bool chmax(T1 &a, const T2 &b) {
  if(a < b) {a = b; return true;}
  return false;
}
template <typename T1, typename T2>
bool chmin(T1 &a, const T2 &b) {
  if(a > b) {a = b; return true;}
  return false;
}
template<typename T1, typename T2>
ostream& operator<<(ostream &os, const pair<T1, T2> p) {
  os << p.first << ":" << p.second;
  return os;
}
template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
  for(ll i=0;i<((ll)(v.size()));++i) {
    if(i) os << " ";
    os << v[i];
  }
  return os;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  while(1) {
    ll n;
    cin >> n;
    if (n == 0) break;
    map<ll, bool> mp;
    mp[13] = true;
    mp[17] = true;
    mp[19] = true;
    mp[23] = true;
    ll l = 55440;
    vvi v(n);
    for(ll i=0;i<(n);++i) {
      ll d;
      cin >> d;
      v[i].resize(d);
      ll t; cin >> t;
      for(ll j=0;j<(d);++j) {
        cin >> v[i][(j + d - t) % d];
      }
    }
    vi tmp(l);
    vi tmp13(13), tmp17(17), tmp19(19), tmp23(23);
    ll ans = 0;
    for(ll i=0;i<(n);++i) {
      if(mp[(ll)(v[i].size())]) {
        ll sz = (ll)(v[i].size());
        if(sz == 13) {
          for(ll j=0;j<(13);++j) {
            tmp13[j] += v[i][j];
          }
        } else if(sz == 17) {
          for(ll j=0;j<(17);++j) {
            tmp17[j] += v[i][j];
          }
        } else if(sz == 19) {
          for(ll j=0;j<(19);++j) {
            tmp19[j] += v[i][j];
          }
        } else {
          for(ll j=0;j<(23);++j) {
            tmp23[j] += v[i][j];
          }
        }
      } else {
        for(ll j=0;j<(l);++j) {
          tmp[j] += v[i][j%(ll)(v[i].size())];
        }
      }
    }
    ll ma = 0;
    for(ll i=0;i<(l);++i) {
      chmax(ma, tmp[i]);
    }
    ans += ma;
    vi hoge = {13, 17, 19, 23};
    for(ll i=0;i<(4);++i) {
      ma = 0;
      if(hoge[i] == 13) {
        for(ll j=0;j<(hoge[i]);++j) {
          chmax(ma, tmp13[j]);
        }
      } else if(hoge[i] == 17) {
        for(ll j=0;j<(hoge[i]);++j) {
          chmax(ma, tmp17[j]);
        }
      } else if(hoge[i] == 19) {
        for(ll j=0;j<(hoge[i]);++j) {
          chmax(ma, tmp19[j]);
        }
      } else {
        for(ll j=0;j<(hoge[i]);++j) {
          chmax(ma, tmp23[j]);
        }
      }
      ans += ma;
    }
    cout << ans << endl;
  }
}
accuracy: 1
time_ms: 850
memory_kb: 3704
final_rank: 18

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<(n);++i)
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define RREP(i, n) for(int i=(int)(n)-1;i>=0;--i)
#define RFOR(i, a, b) for(int i=(int)(b)-1;i>=(a);--i)
#define ALL(a) a.begin(),a.end()
#define DUMP(x) cerr<<#x<<" = "<<(x)<<endl
#define DEBUG(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;
template <typename T1, typename T2>
bool chmax(T1 &a, const T2 &b) {
    if (a < b) {a = b; return true;}
    return false;
}
template <typename T1, typename T2>
bool chmin(T1 &a, const T2 &b) {
    if(a > b) {a = b; return true;}
    return false;
}
template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if(i) os << ", ";
        os << v[i];
    }
    return os << "]";
}
template<typename T, typename U>
ostream &operator<<(ostream &os, const map<T,U> &mp) {
    os << "{";
    int a = 0;
    for (auto &tp : mp) {
        if (a) os << ", "; a = 1;
        os << tp.first << ":" << tp.second;
    }
    return os << "}";
}
template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
    os << p.first << ":" << p.second;
    return os;
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    const int MAX = 24;

    for (;;) {
        int N; cin >> N;
        if (N == 0) break;
        vector<vector<ll>> sum(MAX);
        FOR(i, 1, MAX+1) {
            sum[i-1].resize(i);
        }
        REP(i, N) {
            int d, t; cin >> d >> t;
            REP(j, d) {
                ll q; cin >> q;
                sum[d-1][(j + d - t) % d] += q;
            }
        }
        // REP(d, MAX) DEBUG(sum[d]);

        int LCM = 1;
        FOR(i, 1, MAX) {
            if (i == 13 or i == 17 or i == 19 or i == 23) continue;
            LCM = LCM / __gcd(LCM, i) * i;
        }

        ll ans = 0;

        REP(k, LCM) {
            ll s = 0;
            FOR(i, 1, MAX+1) {
                if (i == 13 or i == 17 or i == 19 or i == 23) continue;
                s += sum[i-1][k % i];
            }
            chmax(ans, s);
        }

        for (int i : {13, 17, 19, 23}) {
            ans += *max_element(ALL(sum[i-1]));
        }

        // DEBUG(ans);
        cout << ans << endl;

    }

}
accuracy: 1
time_ms: 150
memory_kb: 3104
final_rank: 3

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    while(1){
        int n;
        cin >> n;
        if(n == 0) break;

        int v[25][25] = {};
        for(int i=0; i<n; i++){
            int d,t;
            cin >> d >> t;
            for(int j=0; j<d; j++){
                int vol;
                cin >> vol;
                v[d][(j-t+d)%d] += vol;
            }
        }

        int ans = 0;
        // 13,17,19,23以外の周期
        for(int i=0; i<55400; i++){
            int sub = 0;
            for(int j=1; j<=24; j++){
                if(j==13 or j==17 or j==19 or j==23){
                    continue;
                }
                sub += v[j][i%j];
            }
            ans = max(ans, sub);
        }
        for(int i=0; i<=24; i++){
            if(i==13 or i==17 or i==19 or i==23){
                ans += *max_element(v[i], v[i]+i);
            }
        }
        cout << ans << endl;
    }
    return 0;
}
accuracy: 1
time_ms: 170
memory_kb: 3052
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
#define YES() printf("YES\n")
#define NO() printf("NO\n")
#define isYES(x) printf("%s\n",(x) ? "YES" : "NO")
#define Yes() printf("Yes\n")
#define No() printf("No\n")
#define isYes(x) printf("%s\n",(x) ? "Yes" : "No")
#define isIn(x,y,h,w) (x >= 0 && x < h && y >= 0 && y < w)

#define int long long
//using ll = long long;
using P = pair<int,int>;

ostream &operator<<(ostream &os,const P &p){ return os << "(" << p.first << "," << p.second << ")"; }

template<class T> T &chmin(T &a,const T &b){ return a = min(a,b); }
template<class T> T &chmax(T &a,const T &b){ return a = max(a,b); }
 
const int INF=1e+18;
const double EPS=1e-9;
const int MOD=1000000007;

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

template<class T> T gcd(T a,T b){ return b ? gcd(b,a % b) : a; }
template<class T> T lcm(T a,T b){ return a / gcd(a,b) * b; }

inline bool isSkip(int x){
	return x == 13 || x == 17 || x == 19 || x == 23;
}

int n;

void solve(){
	int q[30][30] = {};
	for(int i = 0;i < n;i++){
		int d,t,p[30];
		cin >> d >> t;
		for(int j = 0;j < d;j++){
			cin >> p[j];
			q[d][(t - j + d) % d] += p[j];
		}
	}
	int ans = q[1][0],l = 1;
	for(int i = 2;i <= 24;i++){
		if(isSkip(i)){
			int ma = 0;
			for(int j = 0;j < i;j++) chmax(ma,q[i][j]);
			ans += ma;
		}else l = lcm(l,i);
	}
	int ma = 0;
	for(int i = 0;i < l;i++){
		int sum = 0;
		for(int j = 2;j <= 24;j++){
			if(!isSkip(j)) sum += q[j][i % j];
		}
		chmax(ma,sum);
	}
	ans += ma;
	cout << ans << endl;
}

signed main(){
	while(cin >> n,n) solve();
}
accuracy: 1
time_ms: 420
memory_kb: 3112
final_rank: 12