akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

char s[100002];

int main(){
  scanf("%s", s); int L = strlen(s);

  int ans = L;

  for(int i = 1; i < L; i++){
    if(s[i] != s[i - 1]) ans = min(ans, max(i, L - i));
  }

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

Measured memory_bytes: 256
#define _LIBCPP_DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
typedef vector<int> vi;
typedef long long ll;



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

int main(){
    ll A,B;cin>>A>>B;
    ll gcd=GCD(A,B),ans=1;
    for(ll i=2;i*i<=gcd;i++){
       if(gcd%i==0){
           ans++;
           while(gcd%i==0){
               gcd/=i;
           }
       }
    }
    if(gcd!=1)ans++;
    cout<<ans<<endl;
}







Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
#include <cstring>
#include <map>
#include <unordered_set>
#include <cmath>
#include <iomanip>

#define IO ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
typedef long long int ll;
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define f first
#define s second
#define linebreak "\n"
#define space " "
#define PI atan(1)*4
#define E 10
#define ACCURATE cout << fixed << setprecision(20)
#define MAX 1e9
#define MOD (int)(998244353 )
#define vi vector<int>
#define N (int)(1e7)
#define _DEBUG
using namespace std;


int main() {
#ifdef _DEBUG // place in cmake-build-debug folder
    //freopen("in.txt", "r", stdin);
    //freopen("out.txt", "w", stdout);
#endif
    IO;
    int a, b, c;
    cin >> a >> b >> c;
    if (a == b && b == c || (a!=b && a!=c && b!=c)) cout << "No " << endl;
    else cout << "Yes" << endl;
    return 0;
}


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

x = list()
for i in range(k):
  d = int(input())
  y = list(map(int,input().split()))
  x += y 
    
x = sorted(x)
cnt = 0
for i in range(n):
  if i+1 in x:
    cnt +=1
    
print(n-cnt)

Measured memory_bytes: 9296
import sys
from collections import defaultdict, deque
n = int(sys.stdin.readline().split()[0])
A = defaultdict(deque)
ans = []
for query in sys.stdin:
    if query[0] == '0':
        t, x = query[2:].split()
        A[t].append(x)
    elif query[0] == '1':
        if A[query[2:-1]]:
            ans.append(A[query[2:-1]][-1] + '\n')
    else:
        if A[query[2:-1]]:
            A[query[2:-1]].pop()
sys.stdout.writelines(ans)


Measured memory_bytes: 20132
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
typedef long long ll;
using namespace std;


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

    string s,t; cin >> s >> t;
    
    int ans=0;


    int mmin=min(s.size(),t.size());
    rep(i,mmin){
        if(s[i]!=t[i]){
            ans++;
        }
    }

    int tmp=s.size()-t.size();
    if(tmp!=0){
        ans += abs(tmp);
    }

    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 3792
import java.util.*;

class Main {
    public static void main(String args[]) {
	Scanner sc = new Scanner(System.in);
	loop: while (true) {
	    String s1 = sc.next();
	    if (s1.equals(".")) return;
	    String s2 = sc.next();
	    
	    if (s1.equals(s2)) {
		System.out.println("IDENTICAL");
		continue;
	    }

	    int s1count = 0, s2count = 0;
	    for (int i = 0; i < s1.length(); i++) {
		if (s1.charAt(i) == '"')
		    s1count++;
	    }
	    for (int i = 0; i < s2.length(); i++) {
		if (s2.charAt(i) == '"')
		    s2count++;
	    }
	    if (s1count != s2count) {
		System.out.println("DIFFERENT");
		continue;
	    }

	    int count = 0;
	    for (int i = 0, j = 0; i<s1.length()&&j<s2.length(); i++, j++) {
		if (s1.charAt(i) == '"' && s2.charAt(j) == '"') {
		    i++;
		    j++;
		    String t1 = "", t2 = "";
		    while (s1.charAt(i) != '"') {
			t1 += s1.charAt(i);
			i++;
		    }
		    while (s2.charAt(j) != '"') {
			t2 += s2.charAt(j);
			j++;
		    }
		    if (!t1.equals(t2)) count++;
		    if (count > 1) break;
		}

		else if (s1.charAt(i) != s2.charAt(j)) {
		    System.out.println("DIFFERENT");
		    continue loop;
		}
	    }
		    
	    if (count == 1)
		System.out.println("CLOSE");
	    else
		System.out.println("DIFFERENT");
	}
    }
}
			   
		    



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

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

const long INF = 1L << 60;

int weight[110];
int value[110];
long dp[2][110000];

int main(){
	int n, w;
	scanf("%d%d", &n, &w);
	for(int i = 0; i < n; i++) {
		scanf("%d%d", &weight[i], &value[i]);
	}
	for(int i = 1; i < 110000; i++) dp[0][i] = INF;
	for(int i = 1; i <= n; i++) {
		int t = i % 2;
		for(int j = 0; j < 110000; j++) dp[t][j] = INF;
		for(int j = 0; j < 110000; j++) {
			chmin(dp[t][j], dp[!t][j]);
			if(j - value[i-1] >= 0)
				chmin(dp[t][j], dp[!t][j-value[i-1]] + weight[i-1]);
		}
	}
	long ret = 0;
	for(int i=0; i < 110000; i++)
		if(dp[n%2][i] <= w) ret = i;
	printf("%ld", ret);
}


Measured memory_bytes: 1920
n, m = map(int, input().split())
ab = []
for _ in range(n):
    a, b = map(int, input().split())
    ab.append((a, b))
ab.sort()
ans = 0
for a, b in ab:
    buy = [m, b][0 <= m-b]
    m -= buy
    ans += a*buy
print(ans)


Measured memory_bytes: 16904
#include<bits/stdc++.h>
#define MAXN 100005
#define INF 1000000009
using namespace std;
typedef long long ll;
ll n,m,a[MAXN],res;
priority_queue<ll, vector<ll>, greater<ll> > pque;
vector<ll> G[MAXN];
bool used[MAXN];
void dfs(ll v,ll p)
{
	used[v]=true;
	if(a[v]<res)
	{
		if(res!=INF) pque.push(res);
		res=a[v];
	}
	else pque.push(a[v]);
	for(ll i=0;i<G[v].size();i++)
	{
		ll to=G[v][i];
		if(to==p) continue;
		if(!used[to]) dfs(to,v);
	}
	return;
}
int main()
{
	scanf("%lld%lld",&n,&m);
	for(ll i=0;i<n;i++)
		scanf("%lld",&a[i]);
	ll num=n-1;
	for(ll i=0;i<m;i++)
	{
		ll x,y;
		scanf("%lld%lld",&x,&y);
		G[x].push_back(y);
		G[y].push_back(x);
		num--;
	}
	num*=2;
	if(num>n)
	{
		printf("Impossible\n");
		return 0;
	}
	if(num==0)
	{
		printf("0\n");
		return 0;
	}
	ll ans=0;
	memset(used,false,sizeof(used));
	while(pque.size()) pque.pop();
	for(ll i=0;i<n;i++)
	{
		if(!used[i])
		{
			res=INF;
			dfs(i,-1);
			ans+=res;
			num--;
		}
	}
	for(ll i=0;i<num;i++)
	{
		ans+=pque.top();
		pque.pop();
	}
	printf("%lld\n",ans);
    return 0;
}


Measured memory_bytes: 8960
from itertools import accumulate
from bisect import bisect_right
N,M,K=map(int,input().split())
A=list(map(int,input().split()))
B=list(map(int,input().split()))
a=list(accumulate(A))
b=list(accumulate(B))
book=bisect_right(b,K)
for i in range(N):
    if a[i]>K:
        break
    j=bisect_right(b,K-a[i])
    book=max(book,(i+1)+j)
print(book)

Measured memory_bytes: 47388
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cassert>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define rep(i,n) FOR(i,0,n)
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define vint vector<int>
#define vdouble vector<double>
#define vstring vector<string>
using namespace std;

#include<map>
#include<set>
#include<queue>

typedef long long ll;
typedef unsigned long long ull;

void print(int A[], int N){
	rep(i, N){
		if (i > 0) printf(" ");
		printf("%d", A[i]);
	}
	printf("\n");
}

void bubbleSort(int A[], int N){
	int count = 0;

	bool flag = true;
	while (flag){
		flag = false;
		for (int i = N - 1; i >= 1; i--){
			if (A[i] < A[i - 1]){
				int temp = A[i];
				A[i] = A[i - 1];
				A[i - 1] = temp;
				flag = true;
				count++;
			}
		}
	}

	print(A, N);
	cout << count << endl;
}

int main() {

	int N;
	cin >> N;

	const int MAX_N = 10000;
	int A[MAX_N];

	rep(i, N){ cin >> A[i]; }

	bubbleSort(A, N);
}

Measured memory_bytes: 3180
#include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
#define ll long long
#define rep(i,n) for (int i = 0; i < n; i++)
#define MOD (1000000007)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()

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; }

int gcd(int a, int b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}

const ll INF = 1LL << 60;

int main(){
  int n; cin >> n;
  vi C(n);
  rep(i, n){
    int a; cin >> a;
    C[a]++;
  }
  bool ok = true;
  if(n%2==0){
    for(int i = 1; i < n; i += 2){
      if(C[i] != 2) ok = false;
    }
  }
  else{
    for (int i = 0; i < n; i += 2) {
      if(i == 0 && C[i] != 1) ok = false;
      if(i > 0 && C[i] != 2) ok = false;
    }
  }

  ll ans = 1LL;
  int num = n/2;
  rep(i, num){
    ans *= 2;
    ans %= MOD;
  }
  if(!ok) ans = 0;

  std::cout << ans << '\n';
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define pi (double) acos(-1)
#define P pair<int,int>
#define PiP pair<int,pair<int,int>>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;

int main() {
    string s;
    cin >> s;
    if(s[0] == s[1] && s[1] == s[2])cout << "No";
    else cout << "Yes";
}


Measured memory_bytes: 768
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#define in freopen("in.txt","r",stdin)
//#define out freopen("out.txt","w",stdout) 
ll n,m,j,k,t,l,r;
ll a,b,c,d,x;
char A,B,C,X,Y,Z;
ll ans,MAX,MIN=40,sum;
ll ax[1000000];
char str[100000];
ll dx[12];
ll bx[2000500];


int main()
{
    ll i;
    t=1;
    //scanf("lld",&t);
    while(t--)
    {
        ll flag=0;
        cin>>a>>b>>n;
        if(n>=b) n=b-1;
        cout<<n*a/b<<endl;
    }
	return 0;
}

Measured memory_bytes: 3544
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int A,B;
  cin >> A >> B;
  
  int s,t,u,v;
  int ans = 0;
  for (int i = A; i <= B; i++) {
    s = i % 10;
    t = i / 10000 % 10;
    u = i / 10 % 10;
    v = i / 1000 % 10;
    if (s==t && u==v) {
      ans++;
    }
  }
  
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
    // data loader
    int n;
    cin >> n;
    
    //main
    int ans = 0;
    for(int i=1;i<=n;i++){
        if (to_string(i).length()%2==1){
            ans++;
        }
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 256
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);

#include <complex>

typedef complex<double> P;

double dot(P a, P b) {
	return real(conj(a)*b);
}

int main() {
	double x1, y1, x2, y2, x3, y3, x4, y4;
	while(cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3 >> x4 >> y4) {
		P p1(x2-x1, y2-y1);
		P p2(x4-x3, y4-y3);
		if(abs(dot(p1, p2)) < EPS) {
			cout << "YES" << endl;
		} else {
			cout << "NO" << endl;
		}
	}

	return 0;
}

Measured memory_bytes: 1220
#include <stdio.h>

int main()
{
        double a, b, sum;
        int i, n;

        while ( scanf( "%lf", &a ) == 1 ) {
                sum = 0;
                for ( i = 0 ; i < 10 ; i++ ) {
                        sum = sum + a;
                        if ( i % 2 == 0 ) {
                                a = a * 2;
                        } else {
                                a = a / 3;
                        }
                }
                printf( "%.8lf\n", sum );
        }

        return 0;
}



Measured memory_bytes: 2000
import math
a,b,x=map(int, input().split()) 

threshold = (a*a*b)/2
if x <= threshold:
  rad = math.atan(2*x/(a*b*b))
  print(90-math.degrees(rad))
elif x == 2 * threshold:
  print(0)
else:
  at = (a*a*a)/(2*(a*a*b-x))
  rad = math.atan(at)
  print(90-math.degrees(rad))
  

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

using namespace std;

typedef long long int ll;
typedef pair<int,int> P;

#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<(b);i++)
#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main()
{
	int n;
	cin>>n;
	vector<int> a(n);
	rep(i,n)cin>>a[i];
	P first=P(-1,-1),second=P(-1,-1);
	rep(i,n){
		if(first.first<a[i]) first=P(a[i],i);
	}
	rep(i,n){
		if(second.first<a[i]&&i!=first.second) second=P(a[i],i);
	}
	rep(i,n) cout<<(i!=first.second?first.first:second.first)<<endl;
	return 0;
}

Measured memory_bytes: 2432
#include <bits/stdc++.h>
using namespace std;
#define x first
#define y second
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define pi pair<int,int>
#define pl pair<long long,long long>
#define lg length()
#define pb push_back
ifstream in("file.in");
ofstream out("file.out");
#define MAXN 100005
#define INF 1000000005
#define LINF 1000000000000000005
#define int long long

string s;

int n,ex=1,cnt['z'+5],p,v['z'+5],q,t['z'+5],m,nibab['z'+5];

char c;

vector <char> g[200005]['z'-'a'+5],ans;

int DFS(int n, int c){
    ans.push_back((char)c);
    if(n==m+1 && !nibab[c]) return 1;
    if(n==m+1){
        ans.pop_back();
        return 0;
    }
    for(int i : g[n][c-'a']){
        if(DFS(n+1,i)) return 1;
    }
    ans.pop_back();
    return 0;
}

int32_t main(){
    ios_base :: sync_with_stdio(0);
    cin >> s;
    n=s.length();
    for(int i=0;i<n;i++){
        cnt[s[i]]++; ex=1;
        for(int j='a';j<='z';j++){
            if(!cnt[j]) ex=0;
        }
        if(ex){
            m++;
            c='z'+1;
            q=0; for(int j='a';j<='z';j++) v[j]=0,t[j]=0;
            for(int j=i;j>=p;j--){
                t[s[j]]=j;
                if(!v[s[j]]) v[s[j]]=j;
            }
            for(int j='a';j<='z';j++){
                for(int k='a';k<='z';k++){
                    if(t[j]>=v[k]) g[m][j-'a'].push_back(k);
                }
            }
            for(int j='a';j<='z';j++) cnt[j]=0;
            p=i+1;
        }
    }
    for(int i=p;i<n;i++){
        nibab[s[i]]=1;
    }
    for(int j='a';j<='z';j++){
        if(DFS(1,j)){
            for(char i : ans){
                cout << i;
            }
            return 0;
        }
    }
}


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

int main(){
    int n,A[100];
    cin >> n;
    for(int i=0;i<n;i++)cin >> A[i];
    int ct=0;
 

    for(int i=0;i<n;i++){
        int min = i;
        for(int j=i+1;j<n;j++){
            if(A[j]<A[min])min=j;
        }
        if(i != min){
            int a=A[i];
            A[i]=A[min];
            A[min]=a;
            ct++;
        }
       

    }

    for(int b=0;b<n;b++){
        if(b== n-1)cout << A[b] << endl;
        else cout << A[b] << " " ;
    }

    cout << ct << endl;
    return 0;
}


Measured memory_bytes: 3096
n = gets.chomp.to_i
d = []
n.times do
    ele = gets.chomp.split
    ele[0] = ele[0].to_i
    ele[1] = ele[1].to_i
    d << ele
end

count = 0
result = false
n.times do |i|
    if d[i][0] == d[i][1]
        count += 1
    else
        count = 0
    end
    if count >= 3
        result = true
        break
    end
end

puts result ? "Yes" : "No"


Measured memory_bytes: 14416
import java.util.Scanner;



public class Main {
	public static void main(String[] args){
		Scanner sn = new Scanner(System.in);
		int n;
		while( (n=sn.nextInt()) != 0){
			int[] ices = new int[10];
			for(int i = 0; i < 10; i++) ices[i] = 0;
			for(int i = 0; i < n; i++) ices[sn.nextInt()]++;
			for(int i = 0; i < 10; i++) System.out.println(meter(ices[i]));
		}
	}
	static String meter(int n){
		if(n == 0) return "-";
		String out = "";
		for(int i = 0; i < n; i++) out+="*";
		return out;
	}
}

Measured memory_bytes: 64112
# aoj0056.py

LIM = 50001

# prepare primes

is_prime = [True for _ in range(LIM)]

is_prime[0] = is_prime[1] = False

# sieve of eratosthenes
for x in range(LIM//2 + 1):
    if is_prime[x]:
        for y in range(x*x, LIM, x):
            is_prime[y] = False

primes = [x for x in range(LIM) if is_prime[x]]

# END `prepare primes`

count_arr = [0 for x in range(LIM)]

len_primes = len(primes)

for x in range(len_primes):
    p = primes[x]
    for y in range(x, len_primes):
        q = primes[y]
        if p+q >= LIM:
            break
        count_arr[p+q] += 1

while True:
    n = int(input())
    if n == 0:
        break
    print(count_arr[n])

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


int main(){
	int N ;
	string A , B , C ;
	cin >> N >> A >> B >> C ;
	int ans = 0 ; 
	// cout << N << A << " " << B <<" "<< C<< endl  ;
	for( int i =  0 ; i < N ; i++ ){
		if( A[i] == B[i] && A[i] != C[i] ) ans++ ;
		else if( A[i] == C[i] && A[i] != B[i] ) ans++ ;
		else if( B[i] == C[i] && B[i] != A[i]) ans++ ;
		else if( A[i] != B[i] && B[i] != C[i] && A[i] != C[i]) ans = ans + 2 ;
	}
	cout << ans << endl ;
	return 0 ;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main() {
int N , bob=0 , alice=0;

  cin >> N;
  vector <int> card(N);
  for (int i=0; i<N;i++)
  {
   cin >> card.at(i);
  }

  sort(card.begin(),card.end());
      if(N%2==0)
      {
  for(int i;i<N;i++){

    if(i%2 == 0){
    bob+=card.at(i);
    }
      else{
      alice+=card.at(i);
      }
    }
  }
  else {  for(int i;i<N;i++){

    if(i%2 == 0){
      alice+=card.at(i);
      } 
      else {
    bob+=card.at(i);
    }
  }
  }
  cout << alice-bob <<endl;
}

Measured memory_bytes: 3576
#include <bits/stdc++.h>
#include<list>
#include<algorithm>
#define ll long long int
#define ld long double
#define pb push_back
#define ff first
#define ss second
#define rep(a,b,c) for(a=b;a<c;a++)
#define repj(a,b,c) for(a=b;a<c;a++)
#define repr for(i=n-1;i>=0;i--)
#define pii pair<ll,ll>
#define pll pair<ll, ll>
#define inf 100000001
#define mod 1000000007
#define lb lower_bound
#define up upper_bound
#define rep1 for(i=1;i<=n;i++)
#define vr vector
#define ef else if
#define SORT(A) sort(A.begin(), A.end());
#define REVERSE(A) reverse(A.begin(), A.end());
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll myXOR(ll x, ll y)
{
	return (x|y)&(~x|~y);	
}
ll powerLL(ll x, ll n) 
{ 
    ll result = 1; 
    while (n) { 
        if (n & 1) 
            result = result * x % mod; 
        n = n / 2; 
        x = x * x % mod; 
    }
    return result; 
}
int main()
{
	ll t,n,m,i,j,k,l,x,y,z,a,b,c,d,e,p,nl,np,q,r,s,h;
	string str,str1;
	cin>>n>>m;
	a=b=0;
	if(n>=2)
		a = (n*(n-1))/2;
	if(m>=2)
		b=(m*(m-1))/2;
	cout<<a+b;
}

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

struct ModInt {
  using M = ModInt;
  long long v;
  ModInt(long long _v = 0) : v(_v % MOD + MOD) { norm(); }
  M& norm() {
    v = (v < MOD) ? v : v - MOD;
    return *this;
  }
  M operator+(const M& x) const { return M(v + x.v); }
  M operator-(const M& x) const { return M(v + MOD - x.v); }
  M operator*(const M& x) const { return M(v * x.v % MOD); }
  M operator/(const M& x) const { return M(v * x.inv().v); }
  M& operator+=(const M& x) { return *this = *this + x; }
  M& operator-=(const M& x) { return *this = *this - x; }
  M& operator*=(const M& x) { return *this = *this * x; }
  M& operator/=(const M& x) { return *this = *this / x; }
  friend istream& operator>>(istream& input, M& x) {
    return input >> x.v, x.norm(), input;
  }
  friend ostream& operator<<(ostream& output, const M& x) {
    return output << x.v;
  }
  M pow(long long n) const {
    M x(v), res(1);
    while (n) {
      if (n & 1) res *= x;
      x *= x;
      n >>= 1;
    }
    return res;
  }
  M inv() const { return this->pow(MOD - 2); }

  static long long MOD;
  static vector<M> fact, finv;
  static void build(int n) {
    fact.assign(n + 1, 1);
    finv.assign(n + 1, 1);
    for (int i = 1; i < n + 1; i++) fact[i] = fact[i - 1] * M(i);
    for (int i = 0; i < n + 1; i++) finv[i] = fact[i].inv();
  }
  static M comb(int n, int k) {
    if (n < k || k < 0) return M(0);
    return fact[n] * finv[n - k] * finv[k];
  }
  static M extgcd(int a, int b, int* x, int* y) {
    M d(a);
    if (b) {
      d = extgcd(b, a % b, y, x);
      *y -= (a / b) * *x;
    } else {
      *x = 1, *y = 0;
    }
    return d;
  }
};
vector<ModInt> ModInt::fact = vector<ModInt>();
vector<ModInt> ModInt::finv = vector<ModInt>();
long long ModInt::MOD = 998244353;

int main() {
  int n;
  string x;
  cin >> n >> x;
  vector<int> div;
  for (int i = 1; i <= n; i++) {
    if (n % i == 0 && (n / i) % 2 == 1) div.push_back(i);
  }
  int m = div.size();
  vector<ModInt> w(m);
  for (int i = m - 1; i >= 0; i--) {
    w[i] = 2 * div[i];
    for (int j = i + 1; j < m; j++) {
      if (div[j] % div[i] == 0 && (div[j] / div[i]) % 2 == 1) w[i] -= w[j];
    }
  }
  ModInt ans = 0;
  for (int i = 0; i < m; i++) {
    ModInt c = 0;
    for (int j = 0; j < div[i]; j++) {
      c = c * 2 + (int)(x[j] - '0');
    }
    string y = x;
    for (int j = div[i]; j < n; j++) {
      y[j] = (char)(y[j - div[i]] ^ 1);
    }
    if (y <= x) c += 1;
    ans += c * w[i];
  }
  cout << ans << endl;
}


Measured memory_bytes: 772
#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;

int main(){
    string O, E;
    cin >> E;
    cin >> O;
    string ans;
    if (O.size() == E.size()){
        for (int i =0; i < O.size(); i++){
            ans.push_back(E[i]);
            ans.push_back(O[i]);
        }
    }else{
        for (int i = 0; i < O.size(); i++){
            ans.push_back(E[i]);
            ans.push_back(O[i]);
        }
        ans.push_back(E[E.size()-1]);
    }
    cout << ans << endl;
}

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

using namespace std;
typedef long long ll;

#define rep(i, N) for (int i = 0; i < (int)N; i++)
const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;


int main() {
  int N; cin >> N;
  double result = 0;
  rep(i,N) { double A; cin >> A; result+=1./A; }
  result = 1/result;
  cout << fixed << setprecision(10) << result << endl;
  return 0;
}

Measured memory_bytes: 256
S = list(input())
K = int(input())
cnt = 0
start = S[0]
for i in range(len(S)):
    if start != S[i]:
        break
    if i == len(S) - 1:
        print(len(S) * K // 2)
        exit()
ans = 0
now = start
for i in range(len(S)):
    if i == len(S) - 1:
        if now == S[i]:
            cnt += 1
        ans += cnt // 2
    else:
        if S[i] != now :
            ans += cnt // 2
            cnt = 1
            now = S[i]
        else:
            cnt += 1

end = S[-1]
start_cnt = 0
end_cnt = 0
for c in S:
    if c == start:
        start_cnt += 1
    else:
        break
for i in range(len(S) - 1 , - 1, -1):
    if S[i] == end:
        end_cnt += 1
    else:
        break
add = 0
if end_cnt % 2 == 1 and start_cnt % 2 == 1 and start == end:
    add = (K - 1)
print(ans * K + add)

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

int main() {
    int a, b, result;
    char op;

    while (true) {
        cin >> a >> op >> b;

        if (op == '?') {
            return 0;
        }

        switch (op) {
        case '+':
            result = a + b;
            break;
        case '-':
            result = a - b;
            break;
        case '*':
            result = a * b;
            break;
        case '/':
            result = a / b;
            break;
        default:
            return -1;
        }

        cout << result << endl;
    }
}



Measured memory_bytes: 3080
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
#define repbit(bit,n) for (int bit = 0; bit < (1<<n); bit++)
using namespace std;
typedef long long ll;

const int mod = 1000000007;

int main() {
	string s;
	cin >> s;

	string ans = "s";
	char end;
	end = s[s.size() - 1];
	if (end == 's') ans = "es";
	ans = s + ans;
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 3632
#include<bits/stdc++.h>
#define lli long long int
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL)
#define test lli t;cin>>t;while(t--)
#define arrin lli n;cin>>n;lli a[n];for(lli i=0;i<n;i++)cin>>a[i]
#define floop for(lli i=0;i<n;i++)
#define mod 1000000007
#define defvec vector<lli>v
#define ub upper_bound
#define pb push_back
#define bs binary_search
using namespace std;
lli him(lli a[],lli n)
{
    lli i,j,count=0;
    for(i=0;i<n;i++)
    {
        for( j=0;j<i;j++)
        {
            if(a[i]==a[j])
            break;
        }
        if(i==j)
        count++;
    }
    return count;
}
int main()
{  
    string s,c;
    cin>>s>>c;
    lli count=0;
    if(c.length()==(s.length()+1))
{
     for(int i=0;i<s.length();i++)
    {
        if(s[i]==c[i])
        count++;
    }
    if(count==s.length())
    cout<<"Yes"<<endl;
    else
    cout<<"No"<<endl;
    }
    else
    cout<<"No"<<endl;
    
}

Measured memory_bytes: 3620
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
using namespace std;

#define ll long long
#define INF (1 << 30)
#define INFLL (1LL << 60)

#define FOR(i,a,b) for(ll i = (a);i<(b);i++)
#define REP(i,a) FOR(i,0,(a))
#define MP make_pair

int main() {
	string str;
	cin >> str;
	bool used[101] = {};
	bool flag = false;
	for(int i = 0;i < str.size();i++){
		if(used[str[i] - 'a']) flag = true;
		used[str[i] - 'a'] = true;
	}
	if(flag) cout << "no" << endl;
	else cout << "yes" << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdlib>

using namespace std;
int main(){
    int A, P, part, apple_pie = 0;
    cin >> A >> P;
    part = 3*A+P;
    while(part >= 2){
        part = part - 2;
        apple_pie++;
    }
    cout << apple_pie << endl;
    return 0;
}

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

const int MOD = 1000000007;

int T[100010], A[100010], dp[100010];

int main() {
  int N; cin>>N;
  for (int i = 0; i < N; i++) cin >> T[i];
  for (int i = 0; i < N; i++) cin >> A[i];
  if (A[0] != T[N-1]) { cout<<0<<endl; return 0; }
  for (int i = 0; i < N; i++) dp[i] = 0;
  int prev_t = 0;
  for (int i = 0; i < N; i++) {
    if (T[i] != prev_t) {
      if (A[i] < T[i]) { cout<<0<<endl; return 0; }
      dp[i] = T[i];
    }
    prev_t = T[i];
  }
  int prev_a = 0;
  for (int i = N-1; i >= 0; i--) {
    if (A[i] != prev_a) {
      if (T[i] < A[i] || (dp[i]!=0 && dp[i]!=A[i])) { cout<<0<<endl; return 0; }
      dp[i] = A[i];
    }
    prev_a = A[i];
  }
  long prod = 1;
  for (int i = 0; i < N; i++) {
    if (dp[i] == 0) {
      prod = prod * min(T[i], A[i]) % MOD;
    }
  }
  cout<<prod<<endl;
}

Measured memory_bytes: 4792
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const ll INF=mod*mod;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(x) (x).begin(),(x).end()
ll min(ll a,ll b){
  if(a>b){
    return b;
    }
      else{
        return a;
      }
  }
  
  ll max(ll a,ll b){
    if(a>b){
      return a;
    }
    else{
      return b;
    }
  }

ll power(ll a,ll b){
 ll ans=1;
 for(ll i=1;i<=b;i++){
ans*=a;
  }
  return ans;
}

 ll gcd(ll m,ll n){
 	if(n==0){
       return m;
     }
   return gcd(n,m%n);
 }
ll lcm(ll a,ll b){
  ll g=gcd(a,b);
  return a/gcd(a,b)*b;
		

}
int main(){
  
  ll N,K;
  cin>>N>>K;
  
  
  if(N%K==0){
    cout<<0<<endl;
  }
else{
cout<<min(N%K,K-N%K)<<endl;

}
}


Measured memory_bytes: 256
//ABC115C
#include <bits/stdc++.h>
#include <iostream>
#include <iomanip>
#include <numeric>
#include <math.h>
#include<stdio.h>
using namespace std;

int main(){
	string s;
	cin >> s;
	int n = s.size();
	int x;
	int ans = 1000;
	for(int i=0;i<s.size()-2;i++){
		x = 100*(s.at(i)-'0') + 10*(s.at(i+1)-'0') + (s.at(i+2)-'0');
		ans = min(ans,abs(753-x));
	}
	cout << ans << endl;
	return 0;
}

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

using namespace std;

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

	int n, d, x;
	int a[100];
	cin >> n >> d >> x;
	for( int i = 0; i < n; i++ )cin >> a[i];
	for( int i = 0; i < n; i++ )x += 1 + ( d - 1 ) / a[i];
	cout << x << endl;

	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll i=0; i<n; ++i)
#define rep1(i,n) for(ll i=1; i<=n; ++i)
#define revrep(i,n) for(ll i=n-1; i>=0; --i)
inline constexpr ll Inf = (1ULL << 62) -1;
#define fastio cin.tie(nullptr); ios_base::sync_with_stdio(false);
#define endl '\n'
template <class T> bool updmax(T& a, T b) { if (b > a) { a = b; return true;} return false;}
template <class T> bool updmin(T& a, T b) { if (b < a) { a = b; return true;} return false;}
template <class T> T permutation(T n, T k, T mod) { T f(1); for (T i=0; i<k; ++i, --n) f = f*n % mod; return f; }
template <class T> T factorial(T n, T mod) { return permutation<T>(n,n,mod); }
template <class T> T powmod(T e, T p, T mod) { if(p == -1) p=mod-2; T a=1; while(p>0){ if(p&1){ a=a*e%mod; } e=e*e%mod; p>>=1; } return a; }
template <class T> T binomial(T n, T k, T mod) { return permutation<T>(n,k,mod) * powmod<T>(factorial<T>(k,mod), -1, mod) % mod; }

template <class T> T permutation(ll n, ll k) { T f(1); for (ll i=0; i<k; ++i, --n) f *= n; return f; }
template <class T> T factorial(ll n) { return permutation<T>(n,n); }
template <class T> T binomial(ll n, ll k) { return permutation<T>(n,k) /= factorial<T>(k); }

template <ll Mod>
class modint {
    public:
    ll n=0;
    modint(){}
    modint(ll n):n(n){ clamp(); }
    modint& operator = (modint const& a) = default;
    modint& operator = (int a) { n = a; clamp(); return *this; }
    modint operator - () { modint t = *this; t.n = -t.n; t.clamp(); return t; }
    modint& operator *= (modint const& a) { n *= a.n; clamp(); return *this; }
    modint& operator *= (ll a) { n *= a; clamp(); return *this; }
    modint& operator /= (modint const& a) { *this *= a.inv(); return *this; }
    modint& operator += (modint const& a) { n += a.n; if (n >= Mod) n -= Mod; return *this; }
    modint& operator -= (modint const& a) { n -= a.n; if (n < 0) n += Mod; return *this; }
    modint& operator %= (modint const& a) { n %= a.n; clamp(); return *this; }
    friend modint operator * (modint a, modint b) { modint t(a); t.n *= b.n; t.clamp(); return t; }
    friend modint operator + (modint a, modint b) { modint t(a); t.n += b.n; t.clamp(); return t; }
    friend modint operator - (modint a, modint b) { modint t(a); t.n -= b.n; t.clamp(); return t; }
    friend ostream& operator << (ostream& os, modint const& a) { os << a.n; return os; }
    friend bool operator == (modint const& a, int b) { return a.n == b; }
    void clamp() { n %= Mod; if (n < 0) n += Mod; }
    modint inv() const { return this->pow(Mod-2); }
    ll get() { return n; }
    modint pow(ll p) const {
        ll a=1, e=n;
        while (p > 0) {
            if (p&1) { a *= e; a %= Mod; }
            e *= e; e %= Mod; p >>= 1;
        }
        return modint(a);
    }
};

const ll Mod = 1'000'000'007;
using mint = modint<Mod>;

int main() {
    fastio;
    ll N,K;
    cin >> N >> K;
    rep1(i,K) {
        mint ans = binomial<mint>(K-1, i-1);
        if (N-K+1 >= i)
            ans *= binomial<mint>(N-K+1, i);
        else
            ans = 0;
        cout << ans << endl;
    }
}


Measured memory_bytes: 3648
import java.util.Scanner;

public class Main {

	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int intInput = scan.nextInt();
		int intSec = intInput % 60;
		int intMin = (intInput - intSec)/60;
		int intHour = intMin / 60;
		intMin = intMin - intHour * 60;
		System.out.println(intHour + ":" + intMin + ":" + intSec);
	}
}

Measured memory_bytes: 17164
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
int main() {
	LL n;
	scanf("%lld", &n);
	char buf[21];
	int len = sprintf(buf, "%lld", n + 1), tp = 0, ans = 0;
	for(int i = 0; i < len; ++i) {
		buf[i] -= '0';
		if(buf[i])
			ans = max(ans, tp + buf[i] - 1 + (len - 1 - i) * 9);
		tp += buf[i];
	}
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 764
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define FLSH fflush(stdout)
#define ll long long int
#define pb push_back
#define mp make_pair
#define in insert
#define MOD 1000000007
#define debug(x) cout << "[ " << #x  << " : " << x << " ]" << endl;

using namespace std;

int main(){

	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	ll a, b, n;
	cin >> a >> b >> n;

	if(n >= b-1){
		cout << a*(b-1)/b << endl;
	}
	else
		cout << a*n/b << endl;


}

Measured memory_bytes: 3612
n = int(input())
s = sorted(list(set(list(map(int, input().split())))))
q = int(input())
t = list(map(int, input().split()))

def bs(alist, num):
  left = 0
  right = len(alist)
  while left < right:
    mid = (left + right) // 2
    if num == alist[mid]:
      return 1
    else:
      if alist[mid] < num:
        left = mid + 1
      else:
        right = mid

  return 0

ans = 0

for num in t:
  ans += bs(s, num)

print(ans)


Measured memory_bytes: 16708
#include<iostream>
#include <climits>
#include<vector>
#include<list>
#include<functional>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<deque>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) for(int i=0;i<(n);i++)
#define INF (int)1e9
#define INFL (ll)1e11
using namespace std;
typedef long long ll;
typedef pair<int, int> P;



int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	
	int A, B;
	cin >> A >> B;
	int res = A*B - (A + B) + 1;
	cout << res << "\n";
	

	return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <inttypes.h>

int main(void){
	int n;
	long double a[100001]={0};
	int i;
	long double sum=1;
	int count=0;

	scanf("%d\n",&n);
	for(i=0;i<n;i++){
		scanf("%llf",&a[i]);
		if(a[i]==0){
			printf("0");
			return 0;
		}
		if(a[i]>1){
			count++;
		}
	}
	if(count>60){
		printf("-1");
		return 0;
	}
	for(i=0;i<n;i++){
			sum*=a[i];
			if(sum>1000000000000000000){
				printf("-1");
				return 0;
			}
		}
	printf("%lld",(long long int)sum);
	return 0;
}


Measured memory_bytes: 3308
#include <bits/stdc++.h>
#include <string>
#include <algorithm> 
#include <stdio.h>
#include <ctype.h>
using namespace std;


int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string a,b;
  cin >> a >> b;
  int ans=0;
  for(int i=0;i<a.length();i++){
    if(a[i]!=b[i])
      ans++;
  }
  cout << ans;
}


Measured memory_bytes: 3776
import java.util.*;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc= new Scanner(System.in);
		
		int D= sc.nextInt();
		int T= sc.nextInt();
		int S= sc.nextInt();
		
		sc.close();
		
		if(T*S >= D)System.out.println("Yes");
		else System.out.println("No");

	}

}

Measured memory_bytes: 35608
#!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
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
def A():
    n = I()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n-2):
        dp[i+1] = min(dp[i+1],dp[i]+abs(h[i+1]-h[i]))
        dp[i+2] = min(dp[i+2],dp[i]+abs(h[i+2]-h[i]))
    dp[n-1] = min(dp[n-1],dp[n-2]+abs(h[n-1]-h[n-2]))
    print(dp[n-1])
#B
def B():
    n,k = LI()
    h = LI()
    dp = [float("inf") for i in range(n)]
    dp[0] = 0
    for i in range(n):
        for j in range(1,min(n-i,k+1)):
            dp[i+j] = min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
    print(dp[n-1])

#C
def C():
    n = I()
    v = LIR(n)
    dp = [[0,0,0] for i in range(n+1)]
    for i in range(n):
        dp[i+1][0] = max(dp[i+1][0],dp[i][1]+v[i][0],dp[i][2]+v[i][0])
        dp[i+1][1] = max(dp[i+1][1],dp[i][2]+v[i][1],dp[i][0]+v[i][1])
        dp[i+1][2] = max(dp[i+1][2],dp[i][0]+v[i][2],dp[i][1]+v[i][2])
    print(max(dp[n]))

#D
def D():
    return

#E
def E():
    return

#F
def F():
    return

#G
def G():
    return

#H
def H():
    return

#Solve
if __name__ == "__main__":
    C()


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

int main(){
    string s;
    cin >> s;

    int ans = 0;
    for(int i = 0; i < 4; i++){
        if(s[i] == '+') ans++;
        if(s[i] == '-') ans--;
    }

    cout << ans << endl;

    return 0;

}

Measured memory_bytes: 256
import math
#import numpy as np
import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    dir = [(-1,0),(1,0),(0,-1),(0,1)]
    n,m = map(int,ipt().split())
    xb = []
    yb = []
    xs = []
    ys = []
    for i in range(n):
        a,b,c = map(int,ipt().split())
        xb.append((a,b,c))
        xs.append(a)
        xs.append(b)
        ys.append(c)
    for j in range(m):
        d,e,f = map(int,ipt().split())
        yb.append((d,e,f))
        xs.append(d)
        ys.append(e)
        ys.append(f)
    xs = sorted(list(set(xs)))
    ys = sorted(list(set(ys)))
    lx = len(xs)
    ly = len(ys)

    dix = dict()
    diy = dict()

    for i,xi in enumerate(xs):
        dix[xi] = i
    for i,yi in enumerate(ys):
        diy[yi] = i

    brd = [[1]*(ly*2-1) for _ in range(lx*2-1)]
    for a,b,c in xb:
        xa = dix[a]
        xb = dix[b]
        xc = diy[c]
        for i in range(xa,xb):
            brd[2*i+1][xc*2] = 0
    for d,e,f in yb:
        xd = dix[d]
        xe = diy[e]
        xf = diy[f]
        for i in range(xe,xf):
            brd[xd*2][2*i+1] = 0
    for i,xi in enumerate(xs[1::]):
        for j,yj in enumerate(ys[1::]):
            brd[2*i+1][2*j+1] = (yj-ys[j])*(xi-xs[i])

    sx = bisect.bisect_left(xs,0)
    sy = bisect.bisect_left(ys,0)
    if sx == 0 or sy == 0 or sx == lx or sy == ly:
        print("INF")
        exit()
    q = []
    q.append((sx*2-1,sy*2-1))
    ans = brd[sx*2-1][sy*2-1]
    brd[sx*2-1][sy*2-1] = 0
    while q:
        qx,qy = q.pop()
        for dx,dy in dir:
            nx = qx+dx
            ny = qy+dy
            if brd[nx][ny]:
                if nx == 0 or nx == lx*2-2 or ny == 0 or ny == ly*2-2:
                    print("INF")
                    exit()
                nx += dx
                ny += dy
                if brd[nx][ny]:
                    ans += brd[nx][ny]
                    brd[nx][ny] = 0
                    q.append((nx,ny))
        #print(qx,qy,q)
    print(ans)


    return None

if __name__ == '__main__':
    main()


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

int d[31][31];
int g[501][501];
map<int,int> m[3];//(index,color),num

int main() {

    int n,c;

    cin >> n >> c;

    for(int i=0; i<c; i++){
        for(int j=0; j<c; j++){
            cin >> d[i][j];
        }
    }

    for(int i=1; i<=n; i++){
        for(int j=1; j<=n; j++) {
            cin >> g[i][j];
            g[i][j]--;
            m[(i+j)%3][g[i][j]]++;
        }
    }

    /*
    for(int i=0; i<3; i++){
        for(auto itr = m[i].begin(); itr != m[i].end(); itr++){
            cout << i << " " << itr->first << " " << itr->second << endl;
        }
    }
    */

    /*
    for(int k=0; k<c; k++){
        for(int i=0; i<c; i++){
            for(int j=0; j<c; j++){
                d[i][j] = min(d[i][j],d[i][k] + d[k][j]);
            }
        }
    }
    */

    const int inf = 900000000;
    int ans = inf;

    for(int fc = 0; fc < c; fc++){
        for(int sc = 0; sc < c; sc++){
            for(int tc = 0; tc < c; tc++){

                if(fc == sc || fc == tc || sc == tc){
                    continue;
                }

                int tmp_ans = 0;
                for(auto itr = m[0].begin(); itr != m[0].end(); itr++){
                    tmp_ans += d[itr->first][fc]*itr->second;
                }

                for(auto itr = m[1].begin(); itr != m[1].end(); itr++){
                    tmp_ans += d[itr->first][sc]*itr->second;
                }

                for(auto itr = m[2].begin(); itr != m[2].end(); itr++){
                    tmp_ans += d[itr->first][tc]*itr->second;
                }

                //cout << fc << " " << sc << " " << tc <<  " " << tmp_ans << endl;

                ans = min(ans,tmp_ans);

            }
        }
    }



    cout << ans << endl;

    return 0;
}

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

typedef long long   signed int LL;
typedef long long unsigned int LU;

#define incID(i, l, r) for(int i = (l)    ; i <  (r); i++)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); i++)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); i--)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); i--)
#define  inc(i, n) incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define  dec(i, n) decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)

#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))

#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

int n, x[100], y[100];
map<double, int> mp;
double TAU = atan2(0, -1) * 2;

void add(double l, double r) {
	if(l < 0.0) { add(1.0 + l, 1.0); add(0, r      ); return; }
	if(r > 1.0) { add(      l, 1.0); add(0, r - 1.0); return; }
	mp[l]++;
	mp[r]--;
}

int main() {
	cin >> n;
	inc(i, n) { cin >> x[i] >> y[i]; }
	
	inc(i, n) {
		mp.clear();
		mp[0.0] = 0;
		mp[1.0] = 0;
		
		inc(j, n) {
			if(i == j) { continue; }
			
			double v = atan2(y[j] - y[i], x[j] - x[i]) / TAU + 0.5;
			add(v - 0.25, v + 0.25);
		}
		
		double ans = 0.0, pre = 0.0;
		int c = 0;
		for(auto && e: mp) {
			if(c == 0) { ans += e.FI - pre; }
			c += e.SE;
			pre = e.FI;
		}
		
		printf("%.8f\n", ans);
	}
	
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
using namespace std;
const int INF=1145141919,MOD=1e9+7;
const long long LINF=8931145141919364364,LMOD=998244353;
inline long long mod(long long n,long long m){return(n%m+m)%m;}
// const int dx[]={1,0,-1,0,1,1,-1,-1},dy[]={0,-1,0,1,1,-1,-1,1};

struct UnionFind{
    const int V;
    vector<int> par;
    vector<int> sz;
    UnionFind(const int V):V(V){
        par.resize(V+1);
        for(int i=0;i<=V;i++) par[i]=i;
        sz.resize(V+1,1);
    }
    bool unite(int x,int y){
        x=root(x),y=root(y);
        if(same(x,y)) return false;
        if(y<x) swap(x,y);
        par[y]=x;
        sz[x]+=sz[y];
        return true;
    }
    int root(int x){
    	if(par[x]==x) return x; // xはroot
    	return (par[x]=root(par[x])); // xの親のroot
    }
    bool same(int x,int y){
    	return (root(x)==root(y));
    }
    int size(int x){
        return sz[root(x)];
    }
};

int main(){
    long long n,m; cin>>n>>m;
    UnionFind uf(n);
    long long a[m],b[m],ans[m];
    rep(i,m) cin>>a[i]>>b[i];
    ans[m-1]=n*(n-1)/2;
    for(int q=m-2;q>=0;q--){
        ans[q]=ans[q+1];
        if(!uf.same(a[q+1],b[q+1])) ans[q]-=(long long)uf.size(a[q+1])*(long long)uf.size(b[q+1]);
        uf.unite(a[q+1],b[q+1]);
    }
    rep(q,m) cout<<ans[q]<<endl;
    return 0;
}


Measured memory_bytes: 4480
#include <algorithm>
#include <bitset>
#include <cmath>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <tuple>
#include <vector>
using namespace std;
#define rep(i,n) for(int i = 0; i < (n); i++)
#define all(a) begin(a),end(a)
typedef long long ll;
typedef pair<int,int> P;
const int inf=1e+9;
const ll mod=1000000007;
const double PI=3.14159265358979323846;
int dx[] = {1,-1,0,0};
int dy[] = {0,0,1,-1};
ll gcd(ll a, ll b){
  if (b==0) return a;
  else return gcd(b,a%b);
}
int cans(bool f){
  if(f) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}




int main() {
  int N;cin >> N;
  int a[N],b[N],c[N];
  rep(i,N) cin >> a[i];sort(a,a+N);
  rep(i,N) cin >> b[i];sort(b,b+N);
  rep(i,N) cin >> c[i];sort(c,c+N);
  ll ans = 0;
  rep(i,N) {
    ll na = (lower_bound(a,a+N,b[i]) - a);
    ll nc = N - (upper_bound(c,c+N,b[i]) - c);
    ans += na * nc;
    // cout << na << endl;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 2040
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <bitset>
#include <cmath>
#include <cstdlib>
#include <cstdio>

using namespace std;

const long long INF = 1000000000000;
const long long MOD = 1000000007;

//書くくらいの輪を返す
long long f(long long N){
  string s = to_string(N);
  long long slen = s.length();
  long long ret=0;
  for (int i = 0; i < slen; i++)
    ret += (s[i] - '0');
  return ret;
}

int main()
{
  long long N;
  cin >> N;
  long long mi = INF;
  long long ma = -1 * INF;
  for (int i = 0; i < N; i++)
  {
    long long a;
    cin >> a;
    ma = max(a, ma);
    mi = min(mi, a);
  }
  cout << abs(ma - mi) << endl;
}


Measured memory_bytes: 256