akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>

using namespace std;

int main() {
    long long N, S;
    cin >> N >> S;
    vector<long long> A (N);
    vector<vector<long long> > dp (N+1);
    for (long long i = 0; i < N; i++) {
        cin >> A[i];
        dp[i] = vector<long long> (S+1);
    }
    dp[N] = vector<long long> (S+1);
    dp[0][0] = 1;

    long long MOD = 998244353;

    for (long long n = 1; n <= N; n++) {
        long long an = A[n-1];
        for (long long s = 0; s <= S; s++) {
            long long part1 = dp[n-1][s];
            long long part2 = 0;
            if (s - an >= 0) {
                part2 = dp[n-1][s-an];
            }
            dp[n][s] = (2*part1 + part2) % MOD;
        }
    }
    cout << dp[N][S] << endl;
}


Measured memory_bytes: 74080
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double lb;
#define rep(i,j,n) for (ll i = j; i < (n); i++)
#define repr(i,j,n) for(ll i = j; i >= (n); i--)
#define each(i,mp) for(auto& i:mp)
#define all(x) (x).begin(),(x).end()
#define CLR(mat,f) memset(mat, f, sizeof(mat))
#define IN(a, b, x) (a<=x&&x<b)
#define out(ans) cout << ans << endl
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
typedef pair<ll,ll>P;
const lb pi=acos(-1);
const ll mod=1e9+7;
const ll INF = 1LL<<60;
const ll m=1LL<<32;

int main(){

  ll a,v,b,w,t;cin>>a>>v>>b>>w>>t;

  if(a==b)out("YES");
  else{
    if(w>=v)out("NO");
    else{
      if(abs(a-b)+w*t-v*t<=0)out("YES");
      else out("NO");
    }
  }

  return 0;
}


Measured memory_bytes: 3588
# -*- coding: utf-8 -*-
import bisect

N,M,K = list(map(int, input().rstrip().split()))
A_list = list(map(int, input().rstrip().split()))
B_list = list(map(int, input().rstrip().split()))
#-----

cum_sum_A = [0]
cum_sum_B = [0]

for i in range(len(A_list)):
    cum_sum_A.append( cum_sum_A[i] + A_list[i] )

for i in range(len(B_list)):
    cum_sum_B.append( cum_sum_B[i] + B_list[i] )


max_num_book = 0

for i_a in range(len(cum_sum_A)):
    if cum_sum_A[i_a] > K:
        break
    
    remain = K - cum_sum_A[i_a]
    i_b = bisect.bisect_right(cum_sum_B, remain)
    
    num_book = i_a + (i_b - 1)
    max_num_book = max(max_num_book, num_book)


print(max_num_book)


Measured memory_bytes: 47440
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;

using ll = long long;
using ld = long double;
const int INF = 1e9;
const double EPS = 1e-9;
const ll MOD = 1e9 + 7;

int main()
{
  long long N;
  scanf("%lld", &N);
  std::vector<double> p(N);
  for (int i = 0; i < N; i++)
  {
    scanf("%lf", &p[i]);
  }
  vector<vector<double>> dp(N + 1, vector<double>(N + 1, 0));
  dp[0][0] = 1;
  for (int i = 0; i < N; ++i)
  {
    for (int j = 0; j < N; ++j)
    {
      dp[i + 1][j] += dp[i][j] * (1.0 - p[i]);
      dp[i + 1][j + 1] += dp[i][j] * p[i];
    }
  }
  double ans = 0;
  for (int i = (N + 1) / 2; i <= N; ++i)
    ans += dp[N][i];
  printf("%.10lf\n", ans);
  return 0;
}


Measured memory_bytes: 70656
import sys


def LI():
    return list(map(int, input().split()))


def LSH(h):
    return [input() for _ in range(h)]


H, W = LI()
masu = LSH(H)
for i in range(H):
    for j in range(W):
        ok = 0
        if masu[i][j] == ".":
            continue
        if j+1 < W:
            if masu[i][j+1] == "#":
                ok = 1
        if j-1 >= 0:
            if masu[i][j-1] == "#":
                ok = 1
        if i+1 < H:
            if masu[i+1][j] == "#":
                ok = 1
        if i-1 >= 0:
            if masu[i-1][j] == "#":
                ok = 1
        if ok == 0:
            print("No")
            sys.exit()
print("Yes")


Measured memory_bytes: 9180
#include <algorithm>
#include <array>
#include <functional>
#include <iostream>
#include <math.h>
#include <vector>
#define ll long long int
#define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, n)

using namespace std;

int main() {
    int x;
    cin >> x;
    cout << x / 500 * 1000 + x % 500 / 5 * 5 << endl;

    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef vector<int>vint;
typedef pair<int,int>pint;
typedef vector<pint>vpint;
typedef tuple<int,int,int>tint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define pb push_back
#define fi first
#define se second
template<typename A,typename B>inline void chmin(A &a,B b){if(a>b)a=b;}
template<typename A,typename B>inline void chmax(A &a,B b){if(a<b)a=b;}


signed main(){
    int cnt[26]={};
    string s;cin>>s;
    rep(i,s.size())cnt[s[i]-'a']++;
    rep(i,26)if(cnt[i]&1){
        cout<<"No"<<endl;
        return 0;
    }
    cout<<"Yes"<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <string>
#include <vector>
#include <math.h>
#include <queue>
#include <deque>
#include <stack>
#include <map>
const int MOD = 1e9+7;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

int mod[1000010];

int main(){
	int k;
	cin >> k;
	if (k % 2 == 0){
		cout << -1 << endl;
		return 0;
	}
	if (k == 7 || k == 1){
		cout << 1 << endl;
		return 0;
	}
	mod[1] = 7 % k;
	for (int i = 2; ; i++){
		mod[i] = mod[i-1]*10%k + mod[1];
		mod[i] %= k;
		if (mod[i] == 0){
			cout << i << endl;
			return 0;
		}
		if (i >= 1000000){
			cout << -1 << endl;
			return 0;
		}
	}
	return 0;
}

Measured memory_bytes: 7484
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 5;
const int MOD = 1e9 + 7;

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif

	ios_base::sync_with_stdio(false); cin.tie(NULL);

	long long n, q;
	long long sum = 0;
	cin >> n;
	vector<long long> A(n);
	unordered_map<long long, long long> freq;
	for (long long i = 0; i < n; i++) {
		cin >> A[i];
		freq[A[i]]++;
		sum += A[i];
	}
	cin >> q;
	while (q--) {
		long long a, b;
		cin >> a >> b;
		sum -= (a -	 b) * freq[a];
		freq[b] += freq[a];
		freq[a] = 0;
		cout << sum << endl;
	}

}


Measured memory_bytes: 7744
#include<bits/stdc++.h>
using namespace std;

int main() {
  int s;
  cin >> s;
  int count = 1;
  for(int i = 0; i < 1000000; i++) {
    if(s == 4 || s == 2 || s == 1) {
      cout << count + 3 << endl;
      break;
    }
    if(s % 2 == 0) {
      s = s / 2;
      count++;
    } else { 
      s = 3 * s + 1;
      count++;
    }
  }
}

Measured memory_bytes: 256
#define ONLINE_JUDGE
#include <algorithm>
#include <fstream>
#include <iostream>
#include <vector>
#include <cmath>
#include <iterator>
#include <map>
#include <set>
#ifndef ONLINE_JUDGE
#include <prettyprint/prettyprint.hpp>
#endif

#define FOR(i, n) for(int i = 0; i < n; ++i)
#define TEST int T; std::cin >> T; while(T--)
#define ALL(v) v.begin(), v.end()

using ll = long long;
using ull = unsigned long long;
using vi = std::vector<int>;
using vll = std::vector<ll>;
using vull = std::vector<ull>;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
#ifndef ONLINE_JUDGE
	ifstream fin("in.txt");
	ofstream fout("out.txt");
	auto cinbuf = cin.rdbuf(fin.rdbuf());    // save and redirect
	auto coutbuf = cout.rdbuf(fout.rdbuf()); // save and redirect
#endif	
	string s, t;
	cin >> s >> t;
	int n = s.size(), m = t.size();
	int dp[n + 1][m + 1], p[n + 1][m + 1];

	for(int i = 0; i <= m; ++i) {
		p[0][i] = dp[0][i] = 0;
	}
	for(int i = 0; i <= n; ++i) {
		dp[i][0] = 0;
		p[i][0] = 1;
	}

	for (int i = 1; i <= n; ++i) {
		for (int j = 1; j <= m; ++j) {
			if(s[i - 1] == t[j - 1]) {
				dp[i][j] = dp[i - 1][j - 1] + 1;
				p[i][j] = 2;
			} else {
				dp[i][j] = dp[i - 1][j];
				p[i][j] = 0;
				if(dp[i][j] < dp[i][j - 1]) {
					dp[i][j] = dp[i][j - 1];
					p[i][j] = 1;
				}
			}
		}
	}
	// p[n][m] tells us which direction to go in.
	// 2 <- diagonal, 1 <- left, 2 <- up
	int i = n, j = m;
	string ans;
	while(i != 0 && j != 0) {
		if(p[i][j] == 2) {
			ans.push_back(s[i - 1]);
			--i, --j;
		} else if(p[i][j] == 0) {
			--i;
		} else {
			--j;
		}
	}
	reverse(ans.begin(), ans.end());
	cout << ans << endl;
#ifndef ONLINE_JUDGE
	cin.rdbuf(cinbuf);    // restore
	cout.rdbuf(coutbuf); // restore
#endif	
}


Measured memory_bytes: 70656
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1LL<<60;

struct Edge {
    int to;
    ll cap;
    int rev;
};

int MAX_V;
vector<vector<Edge>> G;
vector<int> level;
vector<int> iter;

void add_edge(int from, int to, ll cap) {
    G[from].push_back(Edge{to, cap, G[to].size()});
    G[to].push_back(Edge{from, 0, G[from].size() - 1});
};

void bfs(int s) {
    level.assign(MAX_V, -1);
    queue<int> que;
    level[s] = 0;
    que.push(s);
    while( !que.empty() ) {
        int v = que.front(); que.pop();
        for ( int i = 0; i < G[v].size(); i++ ) {
            Edge &e = G[v][i];
            if ( e.cap > 0 && level[e.to] < 0 ) {
                level[e.to] = level[v] + 1;
                que.push(e.to);
            }
        }
    }
};

ll dfs (int v, int t, ll f) {
    if ( v == t ) return f;
    for ( int &i = iter[v]; i < G[v].size(); i++ ) {
        Edge &e = G[v][i];
        if ( e.cap > 0 && level[v] < level[e.to] ) {
            ll d = dfs(e.to, t, min(f, e.cap) );
            if ( d > 0 ) {
                e.cap -= d;
                G[e.to][e.rev].cap += d;
                return d;
            }
        }
    }
    return 0;
}

ll max_flow(int s, int t) {
    ll flow = 0;
    for(;;) {
        bfs(s);
        if ( level[t] < 0 ) return flow;
        iter.assign(MAX_V, 0);
        ll f;
        while ( ( f = dfs(s, t, INF) ) > 0 ) {
            flow += f;
        }
    }
}

ll solve() {
    ll N;
    cin >> N;
    vector<ll> A(N), B(N), C(N), D(N);
    for ( int i = 0; i < N; i++ ) {
        cin >> A[i] >> B[i];
    }
    for ( int i = 0; i < N; i++ ) {
        cin >> C[i] >> D[i];
    }

    MAX_V = 2*N + 2;
    G.assign(MAX_V, vector<Edge>());

    // red:0 .. N-1, blue:N .. 2N-1, s:2N, t:2N+1
    for ( int i = 0; i < N; i++ ) {
        add_edge(2*N, i, 1);
    }
    for ( int i = 0; i < N; i++ ) {
        add_edge(N+i, 2*N+1, 1);
    }

    for ( int i = 0; i < N; i++ ) {
        for ( int j = 0; j < N; j++ ) {
            if ( A[i] < C[j] && B[i] < D[j] ) {
                add_edge(i, N+j, 1);
            }
        }
    }
    
    ll ans = max_flow(2*N, 2*N+1);;
    return ans;
}

int main() {
    auto ans = solve();
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 512
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<iostream>
using namespace std;
#define ll long long
#define REP(a,b,c) for(int a=b;a<=c;a++)
#define re register
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)
typedef pair<int,int> pii;
#define mp make_pair
inline int gi()
{
	int f=1,sum=0;char ch=getchar();
	while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0' && ch<='9'){sum=(sum<<3)+(sum<<1)+ch-'0';ch=getchar();}
	return f*sum;
}
const int N=510;
int n,a[N];
int main()
{
	n=gi();n*=2;
	for(int i=1;i<=n;i++)a[i]=gi();
	sort(a+1,a+n+1);ll ans=0;
	for(int i=1;i<=n;i+=2)ans+=a[i];
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <list>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

typedef long long ll;

using namespace std;
int main(){
    int N, D;
    cin >> N >> D;
    int ans = 1;
    int last = 0;

    for (int i = 0; i < 20; i++) {
    	last += 2 * D + 1;
    	if (last >= N) {
    		break;
    	}
    	else {
    		ans++;
    	}
    }
  
    cout << ans << endl;
}


Measured memory_bytes: 256
def main():
    n = int(input())
    lst =list(map(int,input().split()))
    mn = lst[0]
    counter = 1
    for i in range(1,len(lst)):
        if lst[i] < mn:
            mn = lst[i]
        if lst[i] <= mn:
            counter += 1

    print(counter)




if __name__ == '__main__':
    main()


Measured memory_bytes: 24744
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T>
void printv(const vector<T> &v)
{
    int sz = v.size();
    for (int i = 0; i < sz; i++)
    {
        cout << v[i] << " \n"[i == sz - 1];
    }
}

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

    int N;
    cin >> N;
    vector<int> b(N);
    for (int i = 0; i < N; i++)
        cin >> b[i];
    vector<int> ret;
    for (int i = N - 1; i >= 0; i--)
    {
        bool f = false;
        for (int j = i; j >= 0; j--)
        {
            if (b[j] == j + 1)
            {
                ret.push_back(j + 1);
                b.erase(b.begin() + j);
                f = true;
                break;
            }
        }
        if (!f)
        {
            cout << -1 << endl;
            return 0;
        }
    }
    reverse(ret.begin(), ret.end());
    for (int i = 0; i < N; i++)
        cout << ret[i] << endl;
}


Measured memory_bytes: 256
n,k = map(int,input().split())
a = list(map(int,input().split()))

cnt = [0 for _ in range(40)]
for ak in a:
    i = 0
    while(ak>0):
        cnt[i] = cnt[i] + ak%2
        ak //= 2
        i += 1

kn = k+1
fmax = 0
for i in range(40):
    f = 0
    if((kn >> i) % 2 == 0 ):
        continue
    for j in range(i):
        f += 2**j * max(cnt[j], n-cnt[j])
    f += 2**i * cnt[i]
    for j in range(i+1, 40):
        if (kn >> j) %2 == 1:
            f += 2**j * (n - cnt[j])
        else:
            f += 2**j * cnt[j]
    fmax = max(f, fmax)

print(fmax)

Measured memory_bytes: 14620
need = "keyence"
s = input()
n = len(s)
m = len(need)
ir = set()
ir.add(s)
for i in range(1, m+1):
    if n >= i and n - m + i + 1 >= i:
        ir.add(s[0:i] + s[n - m + i:])
#for i in ir:
    #print(i)
if need in ir:
    print("YES")
else:
    print("NO")

Measured memory_bytes: 3064
#include <iostream>
#include <string>
#define REP(val,times) for(int val = 0;val < times;val++)
using namespace std;

int main(){
    string str;
    int n,h;
    while(cin >> str){
        if(str == "-")
            break;
        cin >> n;
        REP(i,n){
            cin >> h;
            string strh = str.substr(0,h);
            str.erase(0,h);
            str += strh;
        }
        cout << str << endl;
    }

    return 0;
}


Measured memory_bytes: 3208
#include <bits/stdc++.h>
 
using namespace std;

const int MAXV = 100010;


int main(int argc, char *argv[])
{
    
 
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    string S, T;
    
    cin >> S >> T;
    int cnt = 0;
    
    for (int i = 0; i < S.length(); i++) {
        if (S[i] != T[i]) {
            cnt++;
        }
    }
    
    cout << cnt << endl;
    
    return 0;
}


Measured memory_bytes: 3796
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, s, n) for(int i = s; i < (int)n; i++)
#define per(i, n) for(int i = n; i >= 0; i--)
#define ROF(i, s, n) for(int i = s; i >= (int)n; i--)
#define FORIT(i, A) for (auto i : A)
#define PRINT(x) cout << (x) << "\n"
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define MP make_pair
#define EACH(i, n) for (__typeof((n).begin()) i = (n).begin(); i != (n).end(); ++i)
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())
#define CLR(a) memset((a), 0 ,sizeof(a))
#define dump(x) cout << #x << " = " << (x) << "\n";
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << "\n";
#define sq(n) (n) * (n)
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef priority_queue<int> maxpq;
typedef priority_queue< int, vector<int>, greater<int> > minpq;
static const double EPS = 1e-10;
static const double PI = acos( -1.0 );
static const int mod = 10007;
static const int INF = 1 << 25;
static const LL LL_INF = 1152921504606846976;
static const int dx[] = { -1, 0, 1, 0, 1, -1, 1, -1 };
static const int dy[] = { 0, -1, 0, 1, 1, 1, -1, -1 };

int q, n, ans;
set<int> s;

int main() {
	scanf( "%d", &q );
	for (; q--;) {
		scanf( "%d", &n );
		ans = 0;
		while ( n / 10 ) {
			ans++;
			if ( s.count( n ) ) {
				ans = -1;
				break;
			}
			s.insert( n );
			int k = 10;
			int _max = 0;
			while ( n / k ) {
				_max = max( _max, ( n / k ) * ( n % k ) );
				k *= 10;
			}
			n = _max;
		}
		printf( "%d\n", ans );
		s.clear();
	}
	return 0;
}

Measured memory_bytes: 1188
/******************************************
* AUTHOR : RAJAGOPALAN *
* NICK : ARNO *
* INSTITUTION : VIT *
******************************************/
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define alli(a, n, k) (a+k),(a+n+k)
#define FP(i, a, b, k) for(__typeof(a) i = a;i < b;i += k)
#define FS(i, a, b, k) for(__typeof(a) i = a;i > b;i -= k)
#define IT(it, a) for(__typeof(a.begin()) it = a.begin();it != a.end(); ++it)

#define eps 1e-6
#define pi 3.141592653589793
using namespace std;

template<class T> inline T gcd(T x, T y) { if (!y) return x; return gcd(y, x%y);}
typedef vector<int> VII;
typedef vector<ll> VLL;
typedef pair<int, int> PII;
typedef vector< pair<int, int> > VPII;
typedef vector< pair<int, PII > > VPPI;
const int MOD = 1e9 + 7;
const int INF = 1e9;
ll fastpower(ll x,ll y)
{
    ll res=1;
    while(y>0)
    {
        if(y&1)
        res*=x;

        y>>=1;
        x=x*x;
    }
    return res;
}

void swap(ll &a,ll &b)
{
    a ^= b; 
    b ^= a; 
    a ^= b;
}
int main(int argc, char* argv[])
{
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    ll h;
    int n;
    cin>>h>>n;
    VII arr(n);
    ll ans=0;
    for(auto &x:arr){
        cin>>x;
        ans+=x;
    }
    if(ans>=h)cout<<"Yes";

    else cout<<"No";
    return 0;
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<int,int> P;
#define Mod 1000000007


int main(){
    ll i,j;
    ll n,k;
    cin>>n>>k;
    k=k+1;

    ll w[n];
    ll v[n];

    for(i=0;i<n;i++) cin>>w[i]>>v[i];

    ll dp[k];
    ll dp2[k];
    

    for(j=0;j<k;j++){
        dp[j]=0;
        dp2[j]=0;
    }
    dp[w[0]]=v[0];
    dp2[w[0]]=v[0];

    for(i=1;i<n;i++){
        for(j=0;j<k;j++){
            if(j-w[i]>=0) dp2[j]=max(dp[j],dp[j-w[i]]+v[i]);
        }

        for(j=0;j<k;j++){
            dp[j]=dp2[j];
            
        }
       
    }
    
    ll ans=-1;
    for(i=0;i<k;i++){
        ans=max(ans,dp[i]);
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 1792
from collections import Counter
n = int(input())
d = list(map(int,input().split()))
mod = 998244353

c = Counter(d)
max_d = max(d)#最大の深さを求める
ans = 1
previous_leaf = 1

#頂点1から頂点1の距離が0以外だったらansを0にする
if d[0] != 0:
    ans = 0
#距離0のノードは頂点1のみの1個でなければ不可能
elif c[0] != 1:
    ans = 0
#前の距離のノードa個あったら場所でa通りあり、さらに前の距離のノード1個につき今の距離のノードb個の子がいるので、全部でa**b通り増えてく
else:
    for i in range(1,max_d+1):
        ans *= (previous_leaf**c[i])
        ans %= 998244353

        #前の距離のノード
        previous_leaf = c[i]
print(ans)


Measured memory_bytes: 19936
#include<bits/stdc++.h>
#define ll long long  
using namespace std;
int main()
{
  string s; cin>>s;
  ll val=0;
  for(ll i=0;i<s.size();i++)
  {
      val = (val + (s[i]-'0'))%9;
  }
  
  if(val==0)cout<<"Yes\n";else cout<<"No\n";
  return 0;
}

Measured memory_bytes: 3692
#include <iostream>
#include <math.h>
using namespace std;
int main() {
	int x, a, b;
	cin >> x >> a >> b;

	if (abs(x - a) < abs(x - b))
		cout << 'A' << endl;
	else
		cout << 'B' << endl;

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
  using namespace std;
  #define int long long int 
  #define vi vector<int>
  #define pii pair<long long ,long long>
  #define pll pair<int,int>
  #define mod 1000000007
  #define hell 998244353
  #define all(x) x.begin(),x.end()
  #define mp(x,y) make_pair(x,y)
  #define mem(a,val) memset(a,val,sizeof(a))
  #define eb push_back
  #define ff first
  #define ss second
  #define rep(n) for(int i=0;i<n;i++)
  #define lb lower_bound
  #define up upper_bound
  
  void solve()
  { 
    
      string s;
      cin>>s;
      if(s=="Sunny")
        cout<<"Cloudy";
      else
        if(s=="Cloudy")
          cout<<"Rainy";
        else
          cout<<"Sunny";

  }



  signed main()
  {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);


  
     
    
    int T;
    T=1;
    
    while(T--)
    {
      solve();
    }
    return 0;
  }

Measured memory_bytes: 256
#include <bits/stdc++.h>
            using namespace std;
            int main(){
				string s;
				int ans=1000;
				cin>>s;
				for(int i=0;i<s.size()-2;i++){
					if(abs(753-((s[i]-'0')*100+(s[i+1]-'0')*10+s[i+2]-'0'))<ans){
						ans=abs(753-((s[i]-'0')*100+(s[i+1]-'0')*10+s[i+2]-'0'));
					}
				}
				cout<<ans<<endl;
        		return(0);
            }

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner in = new Scanner(System.in)) {
            int N = in.nextInt();
            long[] a = new long[N];
            for (int i = 0; i < N; i++) {
                a[i] = in.nextInt();
            }

            long[] cumsum = new long[N];
            for (int i = 0; i < N; i++) {
                cumsum[i] = (i - 1 < 0 ? 0 : cumsum[i - 1]) + a[i];
            }

            long min = (long) 1e18;
            for (int i = 0; i < N - 1; i++) {
                long snuke = cumsum[i];
                long other = cumsum[N - 1] - cumsum[i];
                min = Math.min(min, Math.abs(snuke - other));
            }

            System.out.println(min);
        }
    }
}


Measured memory_bytes: 51976
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Atcoder
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');

            int X = int.Parse(input[0]);
            int A = int.Parse(input[1]);

            //if((X <= 0 && X >= 10 ) && (A <= 0 && A >= 10))
           
                if(X < A)
                {
                    Console.WriteLine(0);
                }
                if(X >=A)
                {
                    Console.WriteLine(10);
                }
          


        }
        }
    }



Measured memory_bytes: 11092
#include<stdio.h>

int main() {
    int N = 0;
    scanf("%d", &N);
    int a[N - 1];
    for (int n = 0; n < N; n++) {
        scanf("%d", &a[n]);
    }
    int check = 1;
    int num = 0, count = 0;
    while(check) {
        num = a[num];
        count++;
        if (num == 2) {
            check = 0;
        }
        num--;
        if (count > N) {
            break;
        }
    }
    
    if (check) {
        printf("-1\n");
    } else {
        printf("%d\n", count);
    }

    return 0;
}

Measured memory_bytes: 512
#include <iostream>
#include <vector>
using namespace std;

int main()
{
    int N,K;
    cin>>N>>K;
    vector<int> v(N,1);
    for (int i=0;i<K;i++){
        int x;
        cin>>x;
        for (int y=0; y<x;y++){
            int z;
            cin>>z;
            v[z-1]=0;
        }
    }
        int a=0;
        for(int i=0;i<N;i++){
            a+=v[i];
        }
        cout<<a;
    }


Measured memory_bytes: 3632
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a = input().split()
    a = list(map(lambda x: x.capitalize(), a))
    a,b,c = a
    print(a[0]+b[0]+c[0])
    return

#B
def B():
    a = II()
    b = II()
    if a > b:
        print("GREATER")
    if a < b:
        print("LESS")
    if a == b:
        print("EQUAL")
    return

#C
def C():
    return

#D
def D():
    s = S()
    for i in range(len(s) - 1):
        if s[i] == s[i+1]:
            print(i + 1, i + 2)
            return
    for i in range(len(s) - 2):
        if s[i] == s[i + 2]:
            print(i + 1, i + 3)
            return
    print(-1, -1)

    return


#Solve
if __name__ == '__main__':
    B()


Measured memory_bytes: 38640
N = int(input())
S = list(input())
n = len(S)
pairs = []
for i, c1 in enumerate(S):
    for j, c2 in enumerate(S[i + 1:], start=i + 1):
        if c1 != c2:
            pairs.append((i, j))
prefix = [[0] * 3 for _ in range(len(S) + 1)]
for i, c in enumerate(S, start=1):
    if c == 'R':
        prefix[i][0] += prefix[i - 1][0] + 1
        prefix[i][1] += prefix[i - 1][1]
        prefix[i][2] += prefix[i - 1][2]
    elif c == 'G':
        prefix[i][1] += prefix[i - 1][1] + 1
        prefix[i][0] += prefix[i - 1][0]
        prefix[i][2] += prefix[i - 1][2]
    else:
        prefix[i][2] += prefix[i - 1][2] + 1
        prefix[i][0] += prefix[i - 1][0]
        prefix[i][1] += prefix[i - 1][1]
res = 0
for i, j in pairs:
    if {S[i], S[j]} == {'R', 'G'}:
        res += prefix[-1][2] - prefix[j + 1][2]
        if 2 * j - i < n and S[2 * j - i] == 'B':
            res -= 1
    elif {S[i], S[j]} == {'R', 'B'}:
        res += prefix[-1][1] - prefix[j + 1][1]
        if 2 * j - i < n and S[2 * j - i] == 'G':
            res -= 1
    elif {S[i], S[j]} == {'B', 'G'}:
        res += prefix[-1][0] - prefix[j + 1][0]
        if 2 * j - i < n and S[2 * j - i] == 'R':
            res -= 1
print(res)

Measured memory_bytes: 349640
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize("Ofast")
//*********************************************DO IT NOW****************************************************************
#include<bits/stdc++.h> 
#include<random>
#define int long long   
#define pp pair<int,int> 
#define ss second 
#define ff first 
#define pb push_back
#define mod 1000000007
#define pi 3.14159265359
#define mk(arr,n,type)  type *arr=new type[n];
#define sl s1.length();
#define yes cout<< "YES"<<endl
#define no  cout<< "NO"<<endl
#define all(v) (v).begin(),(v).end()
#define s(v)   sort(v,v+n)
#define  mt mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define read(a,n) for(int i=0;i<n;i++)cin>>a[i]
#define print(a,n) for(int i=0;i<n;i++)cout<<a[i]<<" "
using namespace std;
//**********************************************DO IT NOW***************************************************************
void fastio()
{  
  #ifndef ONLINE_JUDGE
  freopen("INPUT.txt","r",stdin);
  freopen("OUTPUT.txt","w",stdout); 
  #endif
  ios_base :: sync_with_stdio(false); 
  cin.tie(NULL) ; 
  cout.tie(NULL);
}
int dp[101][1001],ans1=1e9,dp1[1000001];
int num[10];
int a[1000000];
int pr(int n)
{
 for(int i=2;i*i<=n;i++)
      {
          if(n%i==0)
          return 0;
      }
      return 1;
}
signed main()
{   
    fastio();
    int t=1;
   // cin>>t;
    while(t--)
    {
      string s;
      cin>>s;
      int c=0;
      for(int i=0;i<s.length();i++)
      {
          if(s[i]=='7')
          c++;
          
      }
      if(c)
      cout<<"Yes";
      else
      cout<<"No";
    }
    
}
// 🍪 🍪 🍪


Measured memory_bytes: 3620
use std::env;
use std::io::*;
use std::str::*;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let s = stdin.bytes().map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>();
    s.parse::<T>().ok().unwrap()
}

fn main() {
    // main の引数
    /*
        引数なしのときargs.len() = 1;
    */
    let args: Vec<String> = env::args().collect();

    let a = read::<i32>();
    let b = read::<i32>();

    if a*b % 2 == 0 {
        println!("Even");
    } else{
        println!("Odd");
    }

}


Measured memory_bytes: 4352
#include <bits/stdc++.h>
#include<iostream>
using namespace std;

int main(int argc, char const *argv[])
{
	string str;
	cin >> str;
	int w[30];
	for (int i = 0; i < 30; i++) w[i] = 0;

	for (int i = 0, e = str.size(); i < e; i++) {
		w[str[i] - 'a']++;
	}
	for (int i = 0; i < 30; i++) {
		if (w[i] % 2 != 0) {
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;

	return 0;
}


Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10**9)

N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    a, b = a-1, b-1
    G[a].append(b)
    G[b].append(a)

# 0は未着色を表す
color = [0] * N


# 二部グラフ判定dfs
def dfs(v, c):
    color[v] = c
    for to in G[v]:
        if color[to] == c:
            return False

        if color[to] == 0 and not dfs(to, -c):
            return False

    return True


print(N * (N - 1) // 2 - M if not dfs(0, 1) else color.count(1) * color.count(-1) - M)


Measured memory_bytes: 31084
#include <iostream>
using namespace std;

int main(){
	int a, b, sum;

	while(1){
		cin >> a >> b;
		if(cin.eof()){
			break;
		}

		sum = a + b;

		for(int i=1; ; ++i){
			if(sum<10){
				cout << i << endl;
				break;
			}else{
				sum /= 10;
			}
		}
	}

	return 0;
}

Measured memory_bytes: 0
#include<iostream>
#include<stdio.h>
using namespace std ;

int n , m ;
int data[101][101][2] ;

void f(){
	for( int k=1 ; k<=m ; k++ ){
		for( int i=1 ; i<=m ; i++ ){
			for( int j=1 ; j<=m ; j++ ){
				if( data[i][j][0] > data[i][k][0] + data[k][j][0] ){
					data[i][j][0] = data[i][k][0] + data[k][j][0] ;
				}
				if( data[i][j][1] > data[i][k][1] + data[k][j][1] ){
					data[i][j][1] = data[i][k][1] + data[k][j][1] ;
				}
			}
		}
	}
}

int main(){
	
	int a , b ;
	int k , r ;
	
	while(1){
		cin >> n >> m ;
		if( n==0 && m==0 ) break ;
		
		for( int i=0 ; i<101 ; i++ ){
			for( int j=0 ; j<101 ; j++ ){
				data[i][j][0] = 100000000 ;
				data[i][j][1] = 100000000 ;
			}
			data[i][i][0] = 0 ;
			data[i][i][1] = 0 ;
		}
		
		for( int i=0 ; i<n ; i++ ){
			cin >> a >> b ;
			cin >> data[a][b][0] >> data[a][b][1] ;
			data[b][a][0] = data[a][b][0] ;
			data[b][a][1] = data[a][b][1] ;
		}
		
		f() ;
		
		/*for( int i=1 ; i<m ; i++ ){
			for( int j=1 ; j<m ; j++ ){
				printf("data[%d][%d][0] = %d\n" ,i ,j ,data[i][j][0] ) ;
				printf("data[%d][%d][1] = %d\n" ,i ,j ,data[i][j][1] ) ;
			}
		}*/
		
		cin >> k ;
		//cout << "k=" << k << endl ;
		
		for( int i=0 ; i<k ; i++ ){
			cin >> a >> b >> r ;
			cout << data[a][b][r] << endl ;
		}
	}
	
return 0 ;
}

Measured memory_bytes: 1240
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  int l,r;
  int M=0 ;
  cin >> N;
  for (int i=0;i<N;i++){
    cin >> l >> r;
    M+=r-l+1;
  }
  cout << M << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) ((int)(x).size())

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;

template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

i64 a, b, n;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout << fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "r", stdin);
#endif

    cin >> a >> b >> n;
    i64 temp = min(b-1, n);
    i64 res = (a*temp)/b - a*(temp/b);
    cout << res << '\n';

#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


Measured memory_bytes: 3648
#include <iostream>
using namespace std;


int main(int argc, const char * argv[]) {
    int a,b,c = 0;
    cin >> a >> b >> c;

    int remain = a - b;
    int output = (c - remain) > 0 ? c - remain : 0;
    cout << output << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;

typedef long long int ll;
#define rep(i,n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
const ll mod = 1000000007;
const ll INF = 10010010010010010;


int main() {
    int a,b; cin >> a >> b;
    cout << (b+a-3) / (a-1) << endl;
    return 0;
}


Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define REP(i, a, n) for(ll i=a; i<n; i++)
#define RREP(i, a, n) for(ll i=n-1; i>=a; i--)
typedef long long ll;
const ll mod =1e9+7;
const ll inf =1e18;
using namespace std;

ll n;
ll l[101];
int main() {
    cin>>n;
    REP(i, 0, n) cin>>l[i];

    sort(l, l+n);

    ll sum=0;
    REP(i, 0, n-1){
        sum+=l[i];
    }

    if(sum > l[n-1]) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}

Measured memory_bytes: 256
judge = 'Yes'
N = int(input())
W = [None] * N

W[0] = input()

for i in range(1, N):
	w = input()
	
	if w in W:
		judge = 'No'
	
	if w[0] != W[i-1][-1]:
		judge = 'No'
	
	W[i] = w
	
print(judge)

Measured memory_bytes: 3060
#[allow(unused_imports)]
use std::cmp::{max, min};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet};

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn get<T: FromStr>() -> T
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().parse().unwrap()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
}

fn main() {
    let n: usize = util::get();
    for h in 1..3501 {
        for m in h..3501 {
            if 4 * h * m <= n * (m + h) {
                continue;
            }
            let l = 4 * h * m - n * (m + h);
            let r = n * h * m;
            if r % l == 0 {
                let w = r / l;
                println!("{} {} {}", h, m, w);
                return;
            }
        }
    }
}


Measured memory_bytes: 4352
#include <iostream>
#include <vector>

using namespace std;


int main(){
    int n,m;
    cin>>n>>m;
    vector <vector <int> > a;
    vector<int> b;

    for(int i=0;i<n;i++){
        vector <int > r;
        for(int j=0;j<m;j++){
            int A;
            cin>>A;
            r.push_back(A);
        }
        a.push_back(r);
    }

    for(int i=0;i<m;i++){
        int B;
        cin>>B;
        b.push_back(B);
    }

    for(int i=0;i<n;i++){
        int s=0;
        for(int j=0;j<m;j++){
            s+=a[i][j]*b[j];
        }
        cout<<s<<endl;
    }
}

Measured memory_bytes: 1276
import sys
from fractions import gcd

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60


def lcm(x, y):
    return x * y // gcd(x, y)


def main():
    N, M = map(int, input().split())
    A = list(map(int, input().split(" ")))
    A = [a // 2 for a in A]

    semi_lcm = A[0]
    for a in A[1:]:
        semi_lcm = lcm(semi_lcm, a)
        if semi_lcm > M or semi_lcm // a % 2 == 0:
            print(0)
            return

    print((M // semi_lcm + 1) // 2)
    return


if __name__ == '__main__':
    main()


Measured memory_bytes: 16344
#include<iostream>
using namespace std;

int main(){
	int n;
	cin >> n;
    int maxw;
    cin >> maxw;
    int w[n];
	int v[n];
	int maxv = 0;
	for(int a = 0;a < n;a++){
		cin >> w[a] >> v[a];
		maxv += v[a];
	}
	int dp[n+1][maxv+1];
	for(int a = 0;a <= maxv;a++){
		dp[0][a] = 1e9;
	}
	for(int a = 0;a <= n;a++){
		dp[a][0] = 0;
	}
	for(int a = 1;a <= n;a++){
		for(int b = 1;b <= maxv;b++){
			if (v[a-1] > b){
				dp[a][b] = dp[a-1][b];
			}
			else{
				dp[a][b] = min(dp[a-1][b],dp[a-1][b-v[a-1]]+w[a-1]);
			}
			//cout << dp[a][b] << ' ';
	   }
		//cout << '\n';
	}
	int ans = 0;
	for(int a = 0;a <= maxv;a++){
		if (dp[n][a] <= maxw){
			ans = a;
		}
	}
	cout << ans;
}


Measured memory_bytes: 42984
import java.util.*;

public class Main {
    public static void main(String args[] ) throws Exception {

        Scanner sc = new Scanner(System.in);
        String M = sc.nextLine();
        int N = Integer.parseInt(M);
        

        for (int i=0; i<N; i++) {
        	String[] array = sc.nextLine().split(" ");
        	List<Integer> triangle = new ArrayList<Integer>();

            triangle.add(Integer.parseInt(array[0]));
            triangle.add(Integer.parseInt(array[1]));
            triangle.add(Integer.parseInt(array[2]));
            
            Collections.sort(triangle);
            
            int A = (int)triangle.get(0);
            int B = (int)triangle.get(1);
            int C = (int)triangle.get(2);
           
            if((B*B + A*A) == C*C){
            	System.out.println("YES");
            }else{
            	System.out.println("NO");
            }
        }
    }
}

Measured memory_bytes: 31496
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <limits.h>
#include <cinttypes>
#include <string.h>
#include <bits/stdc++.h>

#define countof(a) (sizeof(a)/sizeof(*a))

#define vi vector<int>
#define vvi vector<vector<int> >
#define pi pair<int,int>
#define fi first
#define se second

#define FOR(var, to)          for (register s64 var = 0; var < (s64)to; var++)
#define FROMTO(var, from, to) for (register s64 var = from; var <= (s64)to; var++)

#define INIT(var) FOR(i,countof(var)) var[i] = 0
#define INPUT(var) FOR(i,countof(var)) var[i] = ri()
#define INPUT1(var) FOR(i,countof(var)) var[i] = ri() - 1

#define SORT(v) qsort(v,countof(v),sizeof(*v),int_less)
#define SORTT(v) qsort(v,countof(v),sizeof(*v),int_greater)

#define MOD 1000000007
#define INF (1 << 30)

typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;

using namespace std;

static inline int ri() {
  int a;
  scanf("%d", &a);
  return a;
}

static inline s64 rs64() {
  s64 a;
  scanf("%" SCNd64, &a);
  return a;
}

static inline void wi(int a) {
  printf("%d", a);
}

static inline void wu64(u64 a) {
  printf("%" PRIu64, a);
}

static inline void ws64(s64 a) {
  printf("%" PRId64, a);
}

int int_less(const void *a, const void *b) {
  return (*((const int*)a) - *((const int*)b));
}
int int_greater(const void *a, const void *b) {
  return (*((const int*)b) - *((const int*)a));
}

int main() {
    int n = ri();
    int c = ri();
    int d[c][c];
    FOR(i,c) FOR(j,c) d[i][j] = ri();
    int cc[n][n];
    FOR(i,n) FOR(j,n) cc[i][j] = ri()-1;
    int res = INF;
    int colors[3][30] = { {0} };
    FOR(i,n)FOR(j,n) {
        // cout << i << " " << j << " " << cc[i][j] << endl;
        colors[(i+j)%3][cc[i][j]]++;
    }
    
    FOR(i,3) {
        FOR(j,c) {
            // cout << i << " " << j << " " << colors[i][j] << endl; 
        }
    }
    FOR(c1, c) {
        FOR(c2, c) {
            if (c1 == c2) continue;
            FOR(c3,c) {
                if (c2 == c3) continue;
                if (c1 == c3) continue;
                int cur = 0;
                FOR(i,c) {
                    cur += colors[0][i]*d[i][c1];
                }
                FOR(i,c) {
                    cur += colors[1][i]*d[i][c2];
                }
                FOR(i,c) {
                    cur += colors[2][i]*d[i][c3];
                }
                // cout << cur << endl;
                res = min(res, cur);
            }
        }
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 1280
//2018-03-11
//LZYeah
//
#include<bits/stdc++.h>
#define ll long long
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define Forr(i,a,b) for(register int i=a;i>=b;i--)
using namespace std;
inline void File()
{
	freopen(".in","r",stdin);
	freopen(".out","w",stdout);
}
inline void read(int &x)
{
	int p=1;
	x=0;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')p=-1;c=getchar();}
	while(isdigit(c)){x=(x<<3)+(x<<1)+(c^'0');c=getchar();}
	x*=p;
}
int n,k;
ll ans;
int main()
{
//	File();
	read(n),read(k);
	if(k==0){printf("%lld\n",1ll*n*n);return 0;}
	Forr(i,n,k+1)
	{
		int OmO=n/i;
		int yoy=n%i;
		ans+=1ll*OmO*(i-k);
		if(yoy>=k)ans+=1ll*(yoy-k+1);
//		cout<<i<<' '<<ans<<endl;
	}
	printf("%lld\n",ans);
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int dp[100002][2];
int main(){
	string l;
	cin>>l;
	dp[0][0]=1;
	for(int n=0;n<l.size();n++)
			for(int smaller=0;smaller<2;smaller++)
				for(int x=0;x<=(smaller?1:l[n]-'0');x++)
					(dp[n+1][smaller||x<l[n]-'0']+=dp[n][smaller]*(x+1)%mod)%=mod;
	cout<<(dp[l.size()][0]+dp[l.size()][1])%mod<<endl;
}


Measured memory_bytes: 1280
#include <iostream>

int main(int argc, char const *argv[]) {
    int A, B;
    char S[A + B + 2];
    scanf("%d%d", &A, &B);
    scanf("%s", S);
    if (S[A + B + 1] != 0 || S[A] != '-') {
        printf("No\n");
        return 0;
    }
    for (int i = 0; i < A + B + 1; i++) {
        if (S[i] == '-' && i != A) {
            printf("No\n");
            return 0;
        }
    }
    printf("Yes\n");
    return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;

typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define REP(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) std::cout << x << '\n'
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define sz(x) ((int)(x).size())
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { ll g = gcd(a, b); return a / g * b; }
const int dx[4] = { 1, 0, -1,  0 };
const int dy[4] = { 0, 1,  0, -1 };
const int INF = (int)1e9;
const ll  inf = (ll)1e18;
const int mod = 1000000007;
const int MAX_N = 100010;
const double PI = acos(-1);

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

	int N;
	cin >> N;

	ll res = 0;
	vector<vector<ll>> num(10, vector<ll>(10, 0));
	for (int n = 1; n <= N; n++) {
		vector<int> d;
		int nn = n;
		while (nn) {
			d.push_back(nn % 10);
			nn /= 10;
		}
		int a = d[0];		//最上位の値
		int b = d.back();	//一の位の値

		if (!a || !b) continue;
		++num[a][b];		//先頭がa,末尾がbの数
	}

	for (int a = 1; a < 10; a++) {
		for (int b = 1; b < 10; b++) {
			res += num[a][b] * num[b][a];
		}
	}

	out(res);

	return 0;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using psi = pair<string, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1E9
#define EPS 1E-9
#define MOD (ll)(1E9+7)
#define PI 3.1415926535897932384

template <class T>ostream &operator<<(std::ostream &o, const vector<T> &v)
{
	rep(i, v.size()) {
		o << (i > 0 ? " " : "") << v[i];
	}
	return o;
}

int main() {
	string str;
	cin >> str;
	bool s = false, n = false, w = false,e = false;
	for (auto c : str) {
		if (c == 'N') n = true;
		if (c == 'S') s = true;
		if (c == 'W') w = true;
		if (c == 'E') e = true;
	}
	cout << (((n^s) || (w^e)) ? "No" : "Yes") << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define INF 1LL<<62
#define inf 1000000007 

int main() {
	ll n,k,ans=0;
	cin>>n>>k;
	for(ll i=0;i<n;i++){
		ll x;
		cin>>x;
		if(x>=k){
			ans++;
		}
	}
	cout <<ans;
	// your code goes here
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define REP(i, m, n) for(int i = m; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define dup(x, y) (((x) + (y)-1) / (y))
#define PI 3.14159265359
typedef long long LL;
typedef vector<vector<int>> Graph;
const LL MOD = 1e9 + 7;
const LL LLINF = 1LL << 60;
const int INF = 1 << 30;
template <class T> inline void chmax(T &a, T b) {
    if(a < b) {
        a = b;
    }
}
template <class T> inline void chmin(T &a, T b) {
    if(a > b) {
        a = b;
    }
}
template <class T> inline T gcd(T x, T y) {
    if(y == 0) {
        return x;
    } else if(x == 0) {
        return y;
    }
    return gcd(y, x % y);
}
template <class T> inline T lcm(T x, T y) { return (x * y) / gcd(x, y); }
template <class T> inline void print_vector(vector<T> vec) {
    for(int i = 0; i < vec.size(); i++) {
        cout << vec[i] << " ";
    }
    cout << endl;
}
template <class T> inline T mpower(T x, T n) {
    if(n == 0) {
        return 1;
    } else if(n % 2 == 0) {
        return mpower(x * x % MOD, n / 2);
    } else {
        return x * mpower(x, n - 1) % MOD;
    }
}
LL mfrac(LL x) {
    LL res = 1;
    for(LL i = x; i >= 1; i--) {
        res *= i;
        res %= MOD;
    }
    return res;
}
LL mcomb(LL n, LL r) {

    if(n == 0 and r == 0) {
        return 1;
    } else if(n < r || n < 0) {
        return 0;
    }

    LL res = mfrac(n) * mpower(mfrac(r), MOD - 2) % MOD;
    res *= mpower(mfrac(n - r), MOD - 2) % MOD;

    return res;
}

int main(void) {
    string S;
    cin >> S;

    if(S == "SUN"){
        cout << 7 << endl;
    }else if(S == "MON"){
        cout << 6 << endl;
    }else if(S == "TUE"){
        cout << 5 << endl;
    }else if(S == "WED"){
        cout << 4 << endl;
    }else if(S == "THU"){
        cout << 3 << endl;
    }else if(S == "FRI"){
        cout << 2 << endl;
    }else if(S == "SAT"){
        cout << 1 << endl;
    }

    return 0;
}


Measured memory_bytes: 256