akhauriyash/Code-Regression huggingface dataset:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define DEBUG 1
using namespace std;
constexpr int kMod = 1000000007;
typedef long long LL;

template<typename T>
auto makeNdVector(int n, const T& v) {
  return vector<T>(n, v);
}

template<typename T, typename N, typename... Tail>
auto makeNdVector(const N& n, const Tail&... tail) {
  auto vec = makeNdVector<T>(tail...);
  vector<decltype(vec)> vecs(n, vec);
  return vecs;
}

int main() {
  LL N, M; cin >> N >> M;
  if (N == 1 && M == 1) {
    cout << 1 << endl;
  } else if (N == 1 || M == 1) {
    LL v = max(N, M);
    cout << v - 2 << endl;
  } else {
    cout << (N-2LL)*(M-2LL) << endl;
  }
}


Measured memory_bytes: 3536
import java.util.Scanner;

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

		Scanner s = new Scanner(System.in);

		while (true) {
			int n = s.nextInt();
			int q = s.nextInt();

			if (n == 0 && q == 0)
				break;

			int[] day = new int[101];

			for (int i = 0; i < n; i++) {
				int m = s.nextInt();
				for (int j = 0; j < m; j++) {
					int d = s.nextInt();
					day[d]++;
				}
			}

			int max = 0;
			for (int j = 0; j < 101; j++) {
				if (max < day[j])
					max = day[j];
			}

			int ans = 0;
			for (int j = 0; j < 101; j++) {
				if (max == day[j]) {
					ans = j;
					break;
				}
			}

			if (max >= q)
				System.out.println(ans);
			else
				System.out.println(0);
		}
		s.close();
	}
}

Measured memory_bytes: 62332
#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int H,W,K;
ll dp[101][10];
ll mo=1000000007;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	
	cin>>H>>W>>K;
	
	vector<int> cand;
	FOR(i,1<<(W-1)) {
		int ng=0;
		FOR(j,8) if(((i>>j)&3)==3) ng=1;
		if(ng==0) cand.push_back(i);
	}

	
	dp[0][0]=1;
	FOR(i,H) {
		FOR(x,W) {
			FORR(c,cand) {
				int y=x;
				if(c&(1<<x)) y++;
				if(x && (c&(1<<(x-1)))) y--;
				(dp[i+1][y]+=dp[i][x])%=mo;
			}
		}
	}
	cout<<dp[H][K-1]<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


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

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define FOR(i,a,b) for (ll i = (a); i < (ll)(b); ++i)
#define EFOR(i,a,b) for (ll i = (a); i <= (ll)(b); ++i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } }


// dp[n][k]=先頭からn人でk個を分け合うパターン数
ll dp[110][100010];
ll mod_num = 1e9+7;
ll a[100010];

ll solve(int i, int j) {
    if(j == 0) return 1;
    if(j < 0) return 0;
    if(i == -1) return 0;
    if(dp[i][j] != -1) return dp[i][j];

    return dp[i][j] = ((solve(i - 1, j) + solve(i, j - 1)) % mod_num - solve(i - 1, j - a[i] - 1) + mod_num) % mod_num;
}


int main() {
    int n,k; cin >> n >> k;
    rep(i,n) cin >> a[i];

    // dpテーブル初期化
    EFOR(i,0,n) EFOR(j,0,k) dp[i][j] = -1;

    cout << solve(n, k) << endl;

    return 0;
}


Measured memory_bytes: 85632
import sys
import io, os
#input = sys.stdin.buffer.readline
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

q = int(input())

import heapq
L = []
R = []
heapq.heapify(L)
heapq.heapify(R)

B = 0

for i in range(q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        a, b = query[1], query[2]
        B += b
        heapq.heappush(L, -a)
        heapq.heappush(R, a)
        if -L[0] > R[0]:
            l = -heapq.heappop(L)
            r = heapq.heappop(R)
            B += l-r
            heapq.heappush(L, -r)
            heapq.heappush(R, l)
    else:
        print(-L[0], B)

Measured memory_bytes: 102328
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<ll, P>IP;
typedef vector<ll> V;
typedef vector<V> V2;
typedef vector<vector<P> > G;
void g_dir(G &graph, ll a, ll b, ll w = 1){graph[a].push_back(P(b, w));}
void g_undir(G &graph, ll a, ll b, ll w = 1){g_dir(graph, a, b, w);g_dir(graph, b, a, w);}
#define rep(i, n) for(ll (i) = 0; (i) < (n); (i)++)
#define rep1(i, n) for(ll (i) = 1; (i) <= (n); (i)++)
#define rrep(i, n) for(ll (i) = (n) - 1; (i) >= 0; (i)--)
#define rrep1(i, n) for(ll (i) = (n); (i) >= 1; (i)--)
template<class T> void chmax(T &a, const T &b){if(a < b){a = b;}}
template<class T> void chmin(T &a, const T &b){if(a > b){a = b;}}
const ll INF = 1145141919;
const ll MOD = 1000000007;
const ll NUM = 101010;

ll solve(ll H, ll W){
    if(H % 3 == 0 || W % 3 == 0)return 0;
    if(H == 2 && W == 2)return 1;
    ll ret = min(H, W);
    ll h1 = H / 2, h2 = (H + 1) / 2;
    ll w1 = W / 2, w2 = (W + 1) / 2;
    for(ll h = 1; h < H; h++){
        ll hh = H - h;
        ll a = h * W;
        ll b = hh * w1;
        ll c = hh * w2;
        chmin(ret, max(a, max(b, c)) - min(a, min(b, c)));
    }
    for(ll w = 1; w < W; w++){
        ll ww = W - w;
        ll a = H * w;
        ll b = h1 * ww;
        ll c = h2 * ww;
        chmin(ret, max(a, max(b, c)) - min(a, min(b, c)));
    }
    return ret;
}

int main(){

    ll H, W;
    cin >> H >> W;
    cout << solve(H, W) << endl;
    
    return 0;
}

Measured memory_bytes: 256
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

bool isnum(char c){
  return '0' <= c && c <= '9';
}

bool isCap(char c){
  return 'A' <= c && c <= 'Z';
}
bool isSmall(char c){
  return 'a' <= c && c <= 'z';
}
map<string,int> table;

int calc(string& mole, int beg, int ed){
  if(ed <= beg) return 0;

  if(mole[beg] == '('){
	int cnt = 0, i;
	for(i=beg;i<ed;++i)
	  if(mole[i] == '(') ++cnt;
	  else if(mole[i] == ')')
		if(--cnt == 0){
		  int res1 = calc(mole, beg+1, i);
		  if(res1 < 0) return -1;
		  ++i;
		  res1 *= atoi(mole.substr(i).c_str());
		  for(;i<ed;++i) if(!isnum(mole[i])) break;
		  int res2 = calc(mole, i, ed);
		  if(res2 < 0) return -1;
		  return res1 + res2;
		}
  }
  if(beg+1 == ed || !isSmall(mole[beg+1])){
	string atm = mole.substr(beg, 1);
	if(!table.count(atm)) return -1;
	int res = calc(mole, beg+1, ed);
	if(res < 0) return -1;
	return table[atm] + res;
  }
  else{
	string atm = mole.substr(beg, 2);
	if(!table.count(atm)) return -1;
	int res = calc(mole, beg+2, ed);
	if(res < 0) return -1;
	return table[atm] + res;
  }
}

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

  string atom;
  for(;;){
	cin >> atom;
	if(atom == "END_OF_FIRST_PART") break;
	int x; cin >> x;
	table[atom] = x;
  }

  string mole;
  while(cin>>mole, mole != "0"){
	for(int i=1;i<SZ(mole);++i)
	  if(isnum(mole[i]) && mole[i-1] != ')'){
		if(isCap(mole[i-1])){
		  mole.insert(i, ")");
		  mole.insert(i-1, "(");
		}
		else if(isSmall(mole[i-1])){
		  mole.insert(i, ")");
		  mole.insert(i-2, "(");
		}
	  }

	int wei = calc(mole, 0, SZ(mole));
	if(wei < 0) cout << "UNKNOWN" << endl;
	else cout << wei << endl;
  }

  return 0;
}

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

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
#define popcount(n) (__builtin_popcountll(n))

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}

using namespace std;

using node=struct{int to;};
using G=vector<vector<node>>;

tuple<vector<int>,G> scc(const G &graph){
	int n=graph.size(),k=0,m=0;
	vector<int> ord(n,-1),low(n),comp(n,-1),par(n);
	stack<int>s;

	function<void(int,int)> dfs=[&](int v,int p){
		ord[v]=k++,low[v]=ord[v],par[v]=p;
		s.push(v);

		for(auto &e:graph[v]){
			if(ord[e.to]==-1){
				dfs(e.to,v);
				chmin(low[v],low[e.to]);
			}else if(comp[e.to]==-1){
				chmin(low[v],ord[e.to]);
			}
		}

		if(ord[v]==low[v]){
			while(1){
				int u=s.top();s.pop();
				comp[u]=m;
				if(u==v) break;
			}
			m++;
		}
	};

	rep(v,n) if(ord[v]==-1) dfs(v,-1);
	G dag(m);
	rep(v,n) for(auto &e:graph[v]) if(comp[v]!=comp[e.to]) dag[comp[v]].push_back({comp[e.to]});
	return make_tuple(comp,dag);
}

vector<int> topological_sort(const G &graph) {
	int n=graph.size();
	vector<int> order,used(n,false);
	
	function<void(int)> dfs=[&](int v){
		used[v]=true;
		for(auto &e:graph[v]){
			if(!used[e.to])
				dfs(e.to);
		}
		order.push_back(v);
	};

	rep(v,n) if(!used[v]) dfs(v);
	reverse(_all(order));
	return order;
}

using ll=long long;
const ll mod=1000000007LL;

inline ll ADD(const ll &a, const ll &b,const ll &mod) { return a+b<mod?a+b:a+b-mod;}
inline ll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}

ll dp[1010][2];

int main(void){
	int n,m;
	cin >> n >> m;

	G graph(n);
	rep(loop,m){
		int a,b;
		cin >> a >> b;
		graph[a-1].push_back({b-1});
	}

	G dag;
	vector<int> comp,order;
	tie(comp,dag)=scc(graph);
	order=topological_sort(dag);
	
	rep(v,n) dp[v][0]=dp[v][1]=1LL;
	ll ans=1LL;

	for(auto &v:order){
		vector<int> already(dag.size(),false);
		for(auto &e:dag[v]){
			if(already[e.to]) continue;
			already[e.to]=true;
			dp[e.to][0]=MUL(dp[e.to][0],dp[v][0],mod);
			dp[e.to][1]=MUL(dp[e.to][1],ADD(dp[v][0],dp[v][1],mod),mod);
		}
		if(dag[v].empty()){
			ll cur=ADD(dp[v][0],dp[v][1],mod);
			ans=MUL(ans,cur,mod);
		}
	}

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

Measured memory_bytes: 3328
import fractions
def lcm(x, y):
    return (x * y) // fractions.gcd(x, y)

a,b,c,d=map(int,input().split())
print((b-a+1) - ((b//c-(a-1)//c) + (b//d-(a-1)//d) - (b//lcm(c,d) - (a-1)//lcm(c,d))))

Measured memory_bytes: 5432
import math
N = int(input())
H = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 0
h=0
a=0
L=[]
for i in range(1,N+1):
    L.append(i)
#print(L)
for i in range(N):
    for j in range(N-i):
        if(H[i]==L[j]):
            c=j

    h = h + math.factorial(N-1-i)*(c-1)
    L.remove(H[i])
for i in range(1,N+1):
    L.append(i)
for i in range(N):
    for j in range(N-i):
        if(A[i]==L[j]):
            c=j

    a = a + math.factorial(N-1-i)*(c-1)
    L.remove(A[i])
ans=h-a
if ans<0:
    ans = ans*(-1)
print(ans)


Measured memory_bytes: 3316
wt = str(input())
if wt == 'SSS':
    print(0)
elif wt == "RRR":
    print(3)
else:
    id = wt.index('R')
    if wt.count('R') == 2 and wt[id] == wt[id+1] == 'R':
        print(2)
    else:
        print(1)


Measured memory_bytes: 8868
import java.util.Scanner;

public class Main
{

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        for (int i = 0; i != 4; i++) {
            int t = scan.nextInt();
            int n = scan.nextInt();
            switch (t) {
                case 1:
                    System.out.println(n * 6000);
                    break;
                case 2:
                    System.out.println(n * 4000);

                    break;

                case 3:
                    System.out.println(n * 3000);

                    break;

                case 4:
                    System.out.println(n * 2000);

                    break;

            }
        }
    }
}



Measured memory_bytes: 26208
#include <bits/stdc++.h>
using namespace std;
const int MAX = (5e3)+1, INF = 1e9;

int n, x[MAX], dp[MAX], f[2][MAX];
vector<int> g[MAX];

void dfs(int u) {
	for(int v: g[u]) dfs(v);
	fill(f[0], f[0]+MAX, INF);
	fill(f[1], f[1]+MAX, INF);
	f[0][0] = 0;
	int pre = 0, curr = 1;
	for(int v: g[u]) {
		for(int i = MAX - 1 - min(dp[v], x[v]); i >= 0; i--) {
			if(i + dp[v] < MAX) f[curr][i + dp[v]] = min(f[pre][i] + x[v], f[curr][i + dp[v]]);
			if(i + x[v] < MAX) f[curr][i + x[v]] = min(f[pre][i] + dp[v], f[curr][i + x[v]]);
		}
		fill(f[pre], f[pre]+MAX, INF);
		swap(pre, curr);
	}
	if(u == 1 || g[u].size()) for(int i = 0; i <= x[u]; i++) dp[u] = min(f[pre][i], dp[u]);
	else dp[u] = 0;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for(int i = 2, p; i <= n; i++) {
		cin >> p;
		g[p].push_back(i);
	}
	for(int i = 1; i <= n; i++) cin >> x[i];
	fill(dp+1, dp+n+1, INF);
	dfs(1);

	bool valid = true;
	for(int i = 1; i <= n; i++) valid &= dp[i] < INF;
	if(valid) cout << "POSSIBLE";
	else cout << "IMPOSSIBLE";
}


Measured memory_bytes: 512
class UnionFind():
    def __init__(self,n):
        self.n=n
        self.parents = [i for i in range(n+1)]
        self.size = [1]*(n+1)
    def find(self,x):
        if self.parents[x]==x:
            return x
        else:
            self.parents[x]=self.find(self.parents[x])
            return self.parents[x]
    def unite(self,x,y):
        xRoot = self.find(x)
        yRoot = self.find(y)
        if xRoot == yRoot:
            return
        if self.size[xRoot]>self.size[yRoot]:
            self.parents[yRoot] = xRoot
            self.size[xRoot] += self.size[yRoot]
            self.size[yRoot] = 0
        else:
            self.parents[xRoot] = yRoot
            self.size[yRoot] += self.size[xRoot]
            self.size[xRoot] = 0

N = int(input())
XY = [[i]+list(map(int,input().split())) for i in range(N)]
edges = []
XY.sort(key=lambda x: x[1])
for j in range(1,N):
    edges.append((XY[j-1][0],XY[j][0],XY[j][1]-XY[j-1][1]))
XY.sort(key=lambda x: x[2])
for j in range(1,N):
    edges.append((XY[j-1][0],XY[j][0],XY[j][2]-XY[j-1][2]))

cost = 0
edges.sort(key=lambda x: x[2])
tree = UnionFind(N)
for i,j,c in edges:
    if tree.find(i)!=tree.find(j):
        tree.unite(i,j)
        cost += c

print(cost)

Measured memory_bytes: 134820
from fractions import*
n,*a=map(int,open(0).read().split())
b=[0]
for i in a:b+=gcd(b[-1],i),
w=m=0
for i in range(n):m=max(m,gcd(w,b[-i-2]));w=gcd(w,a[~i])
print(m)

Measured memory_bytes: 16144
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <random>
#include <cstdlib>
#include <cmath>
#include <sstream>
using namespace std;

#define R 10
#define PSIZE 5
//????????????????????????14*14????????????????????£????????????????????????
//??¢?´¢?????§????????????????????§?????????
//??¨?????£????????????????????????????????????????????????????????????????????£??????
int T[10][10];
int N;

//????????????????????¨???????????????????????????????????¶??????????????????????????????????????´
//?????´????????????center??§???????????´
int mid = (PSIZE - 1) / 2;
int p[3][5][5] = {
	{0,0,1,0,0,
	0,1,1,1,0,
	0,0,1,0,0,
	0,0,0,0,0,
	0,0,0,0,0},

	{0,0,1,1,1,
	0,0,1,1,1,
	0,0,1,1,1,
	0,0,0,0,0,
	0,0,0,0,0},

	{0,0,1,0,0,
	0,1,1,1,0,
	1,1,1,1,1,
	0,1,1,1,0,
	0,0,1,0,0}

};
//{2,0}???????????????????????????
int center[3][2] = { {0,1},{1,1},{0,2} };
int psize[] = { 3,3,5 };

void add_point(int x, int y, int point[5][5]) {
	for (int i = 0; i < PSIZE; i++) {
		for (int j = 0; j < PSIZE; j++) {
			if (point[i][j]) T[y + i][x + j - mid] += point[i][j];
		}
	}
}

void delete_point(int x, int y, int point[5][5]) {
	for (int i = 0; i < PSIZE; i++) {
		for (int j = 0; j < PSIZE; j++) {
			if (point[i][j]) T[y + i][x + j - mid] -= point[i][j];
		}
	}
}

bool can_delete(int x, int y, int point[5][5]) {
	for (int i = 0; i < PSIZE; i++) {
		for (int j = 0; j < PSIZE; j++) {
			if (y + i < 0 || y + i >= R ||
				x + j - mid < 0 || x + j - mid >= R)
				if (point[i][j]) return false;
			if (T[y + i][x + j - mid] - point[i][j] < 0) return false;
		}
	}
	return true;
}

bool checkT() {
	for (int i = 0; i < R; i++) {
		for (int j = 0; j < R; j++) {
			if (T[i][j] != 0) return false;
		}
	}
	return true;
}

vector<pair< pair<int, int>, int> > points;
bool rec(int x, int y, int cnt) {
	if (checkT()) return true;
	if (cnt == N) return false;

	for (; !T[y][x];) {
		y += (x == 9 ? 1 : 0);
		x = (x + 1) % 10;
		if (y == 10) return false;
	}

	for (int i = 0; i < 3; i++) {
		if (!can_delete(x, y, p[i])) continue;
		delete_point(x, y, p[i]);
		points.push_back(make_pair(make_pair(x + center[i][0], y + center[i][1]), i));
		if (rec(x, y, cnt + 1)) return true;
		points.pop_back();
		add_point(x, y, p[i]);
	}
	return false;
}

void q91() {
	cin >> N;
	for (int i = 0; i < R; i++) {
		for (int j = 0; j < R; j++) {
			cin >> T[i][j];
		}
	}
	bool ans = rec(0, 0, 0);
	for (auto it = points.begin(); it != points.end(); it++) {
		cout << (*it).first.first << " " << (*it).first.second << " " << (*it).second + 1 << endl;
	}

}
int main() {
	q91();
	return 0;
}

Measured memory_bytes: 3096
def ansstr(s,n,c):
    if len(s)==n:
        print(s)
    else:
        for i in range(c+1):
            a=0
            ch=chr(ord('a')+i)
            if i==c:
                a=1
            ansstr(s+ch,n,c+a)
    
    
n=int(input())
ansstr("a",n,1)

Measured memory_bytes: 4340
#include<stdio.h>

 int main(void)

 {

 int a,b;  scanf("%d %d",&a,&b);

 if(a<b)

  {

   printf("a < b\n");

  }

  else if(a>b)

  {

   printf("a > b\n");

  }

  else

  {

   printf("a == b\n");

  }

 return 0;

 }

Measured memory_bytes: 580
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        Scanner in = new Scanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        GuessTheNumber solver = new GuessTheNumber();
        solver.solve(1, in, out);
        out.close();
    }

    static class GuessTheNumber {
        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int n = in.nextInt();
            int m = in.nextInt();
            int[] num = new int[n + 1];
            Arrays.fill(num, -1);

            for (int i = 0; i < m; i++) {
                int s = in.nextInt();
                int c = in.nextInt();
                if (num[s] != -1 && num[s] != c) {
                    out.print(-1);
                    return;
                }
                num[s] = c;
            }
            long result = 0;
            for (int i = 1; i < num.length; i++) {
                if (num[i] != -1) {
                    result += pow(10, n - i) * num[i];
                }
            }
            if (String.valueOf(result).length() < n) {
                result += pow(10, n - 1);
            }
            if (num[1] == 0 && n > 1) {
                out.print(-1);
                return;
            }
            out.print(result);
        }

        static long pow(long x, long n) {

            if (n == 0) {
                return 1;
            }
            long result = pow(x, n / 2);

            if (n % 2 == 0) {
                return (result * result);
            }
            return ((result * result)) * x;
        }

    }
}



Measured memory_bytes: 35636
#coding:utf-8
 
n = int(input())
p = list(map(int,input().split()))
#print(p)
 
counter = 0
 
for i in range(1, n-1):
    #print(i)
    if (p[i] < p[i+1] and p[i] > p[i-1]) or (p[i] > p[i+1] and p[i] < p[i-1]):
        counter += 1
 
print(counter)

Measured memory_bytes: 3060
#define _GLIBCXX_DEBUG
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>
#include <map>
using namespace std;
typedef long long ll;
#define ALL(v) (v).begin(), (v).end()
const int inf = 1e9;
const int mod = 1e9+7;

int main() {
	int N, T;
	cin >> N >> T;
	int t[N];
	for (int i = 0; i < N; i++) cin >> t[i];

	ll sum = 0;
	for (int i = 0; i < N-1; i++) {
		if (t[i+1] >= (t[i]+T)) sum += T;
		else sum += (t[i+1] - t[i]);
	}
	sum += T;
	cout << sum << endl;
    return 0;
}


Measured memory_bytes: 1024
#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef unsigned long long int ulli;
#define vec(s) vector
;
#define vvec(s) vector<vector
> ;
typedef vector<lli> vi;
typedef vector<vi> vvi;
typedef pair<lli,lli> pii;
#define rep(i, n) for (lli i = 0; i < (lli)(n); i++)
#define YN(x) cout<<(bool x?"Yes":"No")<<endl;
#define out(s) cout<<s<<endl;
#define pb(s) push_back(s);
#define sp " ";
#define INF 10000000000
#define all(s) s.begin(),s.end()
void vout(vector<int> v){
    for(int i=0;i<v.size();i++)
    cout<<v.at(i)<<endl;
}
long long int mod=1000000007;
struct modint{
    long long int x;
    modint(long long int x=0):x(x%mod){}//メンバ変数を初期化する。
    modint& operator+= (const modint a){
        if((x+=a.x)>=mod){x-=mod;}//%は遅い
        return *this;//関数を終了するためにはreturnを返さなければならないが、return aとでもするとその値が代入されてしまう。そこで、計算された後の自分自身のアドレス、つまり何もしないようにしている？
    }
    modint& operator-= (const modint a){
        if((x+=mod-a.x)>=mod) x-=mod;
        return *this;
    }
    modint& operator*= (const modint a){
        x*=a.x;
        x%=mod;
        return *this;
    }
    modint operator+(const modint a) const{//引数の横のconstは関数自体がconstつまりメンバ変数の変更ができなくなるということ。バグ防止に役立つ
        modint copy(*this);//constがあるのでメンバ変数をいじれないから値だけコピーする
        return copy+=a;
    }
    modint operator-(const modint a)const{
        modint copy(*this);
        return copy-=a;
    }
    modint operator*(const modint a)const{
        modint copy(*this);
        return copy*=a;
    }
    modint pow(long long int t)const{//繰り返し二乗法
        if(t==0) return 1;
        modint a=pow(t>>1);//tを1/2倍したものの累乗
        a*=a;//aを二乗する
        if(t&1) a*=*this;//ビット形式での一桁目が1、つまりtが奇数ならば、もう一回掛ける。
        return a;
    }
    modint inv() const{
        return pow(mod-2);
    }
    modint operator/=(const modint a){
        return (*this)*=a.inv();
    }
    modint operator/(const modint a){
        modint copy(*this);
        return copy/=a;
    }
};

int main(){
    modint ans;
    ans.x=1;
    modint n;
    cin>>n.x;
    modint i;
    for(i.x=1;i.x<=n.x;i.x++){
        ans*=i;
    }
    out(ans.x);
}

Measured memory_bytes: 256
//============================================================================
// Name        : ALDS1_2_C.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <queue>
using namespace std;

int main() {
	int n;
	string temp;
	vector<int> numa, numb;
	vector<char> marka,markb;
	queue<char> aoq[9], boq[9];
	bool isstable;
	cin >> n;
	numa.resize(n);
	numb.resize(n);
	marka.resize(n);
	markb.resize(n);
	for(int i=0; i<n; ++i) {
		cin >> temp;
		marka[i]=temp[0];
		markb[i]=temp[0];
		numa[i]=atoi(&temp[1]);
		numb[i]=atoi(&temp[1]);
		aoq[numa[i]-1].push(marka[i]);
		boq[numb[i]-1].push(markb[i]);
	}
	// bubble sort
	for(int i=0; i<n-1; ++i) {
		for(int j=n-1; j>=i+1; --j) {
			if(numa[j]<numa[j-1]) {
				swap(numa[j],numa[j-1]);
				swap(marka[j],marka[j-1]);
			}
		}
	}
	// insertion sort
	for(int i=0; i<n; ++i) {
		int minj = i;
		for(int j=i+1; j<n; ++j) {
			if(numb[j]<numb[minj]) minj=j;
		}
		if(minj!=i) {
			swap(numb[i],numb[minj]);
			swap(markb[i],markb[minj]);
		}
	}
	isstable=true;
	for(int i=0; i<n-1; ++i) {
		printf("%c%d ",marka[i],numa[i]);
		if(marka[i]!=aoq[numa[i]-1].front()) isstable=false;
		aoq[numa[i]-1].pop();
	}
	if(isstable) {
		printf("%c%d\nStable\n",marka[n-1],numa[n-1]);
	} else {
		printf("%c%d\nNot stable\n",marka[n-1],numa[n-1]);
	}
	isstable=true;
	for(int i=0; i<n-1; ++i) {
		printf("%c%d ",markb[i],numb[i]);
		if(markb[i]!=boq[numb[i]-1].front()) isstable=false;
		boq[numb[i]-1].pop();
	}
	if(isstable) {
		printf("%c%d\nStable\n",markb[n-1],numb[n-1]);
	} else {
		printf("%c%d\nNot stable\n",markb[n-1],numb[n-1]);
	}
	return 0;
}

Measured memory_bytes: 3196
<?php

list($n , $a, $b) = explode(" ", fgets(STDIN));

function calc($n){
    $r = 0;
    while($n>0){
        $r += $n%10;
        $n /= 10;    
    }
    return $r;
}

$ttl = 0;
for($i=1; $i<=$n; ++$i){
    $r = calc($i);
    if($r>=$a && $r<=$b){
        $ttl += $i;
    }
}
echo $ttl;
?>


Measured memory_bytes: 3828
def main():
    X, K, D = map(int, input().split())
    X = abs(X)
    if X/D > K:
        print(X-K*D)
    else:
        E = int(X/D)
        K -=E
        X -=E*D
        if(K%2==0):
            print(X)
        else:
            print(abs(X-D))
if __name__ == "__main__":
    main()


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

int main(){

    int N,cnt=1;
    cin >> N;
    vector<int> s(N);
    rep(i, N) cin >> s.at(i);
    sort(s.begin(), s.end());
    rep(i,N){
        if(i!=0&&s.at(i)!=s.at(i-1))
            cnt++;
    }
    cout << cnt << endl;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
using namespace std;
int N;
long X, A[2000];
long b[2000];

int main(){
    cin >> N >> X;
    for(int i=0; i<N; i++){
        cin >> A[i];
        b[i] = 1e15;
    }

    long min_sum = 1e15;
    for(int i=0; i<N; i++){
        long sum = 0;
        for(int j=0; j<N; j++){
            b[j] = min(A[(j-i+N)%N],b[j]);
            sum += b[j];
        }
        sum += i*X;
        min_sum = min(min_sum, sum);
    }

    cout << min_sum << endl;

    return 0;
}


Measured memory_bytes: 256
program test 
implicit none
integer(8) :: n,a(100000)
integer(8) :: i,j,k,aa,bb,cc

read(*,*) n
read(*,*) (a(i),i=1,n)

do i=1,n-1
	aa = max(a(i),a(i+1))
	bb = min(a(i),a(i+1))
	
	do while(aa*bb > 0)
		cc = mod(aa,bb)
		aa = bb
		bb = cc
	enddo
	
	a(i+1) = max(aa,bb)
enddo

write(*,*) a(n)


end program

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

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

    long long count_left = 0; 
    long long a = 0;

    for(long long i = 0; i < s.size(); i++){
        if(s[i] == '('){
            count_left++;
        }
        else{
            if(count_left == 0){
                a++;
            }
            else{
                count_left--;
            }
        }
    }

    long long count_right = 0; 
    long long b = 0;

    for(long long i = s.size()-1; i >= 0; i--){
        if(s[i] == ')'){
            count_right++;
        }
        else{
            if(count_right == 0){
                b++;
            }
            else{
                count_right--;
            }
        }
    }

    string ans;
    for(long long i = 0; i < a; i++){
        ans.push_back('(');
    }

    ans = ans + s;

    for(long long i = 0; i < b; i++){
        ans.push_back(')');
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
    #define int long long
    #define all(c) (c).begin(), (c).end()
    #define cpresent(c,x) ((c).find(x) != (c).end()) 
    #define vpresent(c,x) (find(all(c),x) != (c).end())
    #define endl '\n'
    #ifdef LOCAL
    #include "/home/iggaboi/Desktop/CP/debug.h"
    #else
    #define debug(x...)
    #endif
    using namespace std;

    char c;

    void solve(){
        cin >> c;
        if(c > 96)
            cout << 'a' << endl;
        else
            cout << 'A' << endl;
    }

    int32_t main(){
        ios_base::sync_with_stdio(false),cin.tie(nullptr);
        // int t; cin >> t;
        // while(t--)
            solve();
        return 0;
    }


Measured memory_bytes: 3660
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x) {for(auto i: x){cout << i << " ";} cout << endl;}
using namespace std;
using ll = long long;
using P = pair<int, int>;

const int MOD = 1000000007;
struct mint {
    ll x; // typedef long long ll;
    mint(ll x=0):x((x%MOD+MOD)%MOD){}
    mint operator-() const { return mint(-x);}
    mint& operator+=(const mint a) {
        if ((x += a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += MOD-a.x) >= MOD) x -= MOD;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= MOD; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }

    // for prime MOD
    mint inv() const { return pow(MOD-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
// combination MOD prime
struct combination {
    vector<mint> fact, ifact;
    combination(int n):fact(n+1),ifact(n+1) {
        assert(n < MOD);
        fact[0] = 1;
        for (int i = 1; i <= n; ++i) fact[i] = fact[i-1]*i;
        ifact[n] = fact[n].inv();
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i]*i;
    }
    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[k]*ifact[n-k];
    }
    mint p(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n]*ifact[n-k];
    }
} ; //comb(10000007);
// comb(5, 2) → 10, comb.p(5, 2) → 20
// comb.fact[4] → 24, 2のN乗 → mint(2).pow(N)

int main() {
    int N;
    cin >> N;
    vector<ll> A(N);
    rep(i, N) cin >> A[i];
    mint total = 0;
    rep(i, N) total += A[i];
    mint ans = 0;
    rep(i, N) ans += total * A[i];
    rep(i, N) ans -= (mint)A[i] * A[i];
    ans /= 2;
    cout << ans << '\n';
    return 0;
}

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

int main(){
  int N;
  cin >> N;
  if( N == ( N / 1000 ) * 1000){
    cout << 0 << endl;
  } else{
    cout << 1000 - (N - ( N / 1000 ) * 1000 )  << endl;
  }
}





Measured memory_bytes: 3624
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,-1,0,1,1,-1,1,-1};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
int main(){
    ll n,t;cin >> n >>t;
    t--;
    ll dp[n+1][t+1];
    rep(i,n+1)rep(j,t+1)dp[i][j]=0;
    vpl AB(n);
    vl a(n),b(n);
    rep(i,n)cin >> AB[i].fi >>AB[i].se;
    sort(all(AB));
    rep(i,n){
        a[i]=AB[i].fi;
        b[i]=AB[i].se;
    }
    ll ans=0;
    rep(i,n){
        rep(j,t+1){
            chmax(dp[i+1][j],dp[i][j]);
            chmax(ans,dp[i+1][j]+b[i]);
            if(j-a[i]>=0)chmax(dp[i+1][j],dp[i][j-a[i]]+b[i]);
        }
    }
    cout <<ans <<endl;
}

Measured memory_bytes: 70656
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(s...)  fprintf(stderr, s)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }

int H, W;
char F[99][99];

int main() {
    scanf("%d%d", &H, &W);
    REP (i, H) scanf("%s", F[i]);

    bool yes = true;
    int y = 0, x = 0;

    while (1) {
	if (F[y][x] == '.') {
	    yes = false;
	    break;
	}
	F[y][x] = '-';
	if (y == H - 1 && x == W - 1) break;

	int cnt = 0;
	if (F[y+1][x] == '#') cnt++;
	if (F[y][x+1] == '#') cnt++;
	if (cnt != 1) {
	    yes = false;
	    break;
	}

	if (F[y+1][x] == '#') {
	    y++;
	} else if (F[y][x+1] == '#') {
	    x++;
	}
    }

    {
	int cnt = 0;
	REP (i, H) REP (j, W) if (F[i][j] == '#') cnt++;
	if (cnt) yes = false;
    }

    puts(yes? "Possible": "Impossible");


    return 0;
}



Measured memory_bytes: 764
import std.stdio, std.string, std.conv, std.range, std.algorithm;

void main() {
	auto _n = readln;
	string[] n = [];
	foreach (string l; lines(stdin)) n ~= l.chomp;

	foreach (_s; ["S", "H", "C", "D"])
		foreach (_i; 1..14) {
			auto t = "%s %d".format(_s, _i);
			if (!n.canFind(t)) t.writeln;
		}
}

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

using namespace std;

int main(){

  string search;
  string word;
  vector<string> result(1024);
  int num=0;
  
  getline(cin,search);

  for(int i=0;i<search.size();++i){
   
	if(search[i]==' ' || search[i]== ',' || search[i]=='.'){
	  if(word.size() >= 3 && word.size() <= 6){
		result[num] = word;
		num++;
	  }
	  word.clear();
	}

	else
	  word.push_back(search[i]);
	
  }

  for(int i=0;i<num;++i){
	cout<<result[i];
	if(i != num-1)
	  cout<<' ';
  }
  
  cout<<endl;
  
}


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

using namespace std;
using ll = long long;
ll mod = 1000000007;


int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll s;
    cin >> s;
    if(s < 3) {
        cout << "0\n";
        return 0;
    }
    ll ans[s + 1];
    ans[0] = 1;
    ans[1] = 0;
    ans[2] = 0;
    for(ll i = 3; i <= s; i++) {
        ans[i] = 0;
        for(ll j = 3; j <= i; j++) {
            ans[i] += ans[i - j];
            ans[i] %= mod;
        }
    }
    cout << ans[s] << "\n";
    return 0;
}

Measured memory_bytes: 3664
#include <bits/stdc++.h>
using namespace std;
 
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define int long long

const int N = 1e5 + 5;

int n;
int a[N];

int32_t main()
{
	IOS;
	cin >> n;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	int cur = 1, ans = 1;
	for(int i = 2; i <= n; i++)
	{
		if(a[i] <= a[i - 1])
			cur++;
		else
		{
			ans = max(ans, cur);
			cur = 1;
		}
	}
	ans = max(ans, cur);
	cout << ans - 1;
	return 0;
}


Measured memory_bytes: 1024
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x)-1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

def cum_2d(field:list):
    row = len(field)
    col = len(field[0])
    
    # 横方向に累積和
    for r in range(row):
        for c in range(col-1):
            field[r][c+1] += field[r][c]
    
    # 縦方向に累積和
    for c in range(col):
        for r in range(row-1):
            field[r+1][c] += field[r][c]
            
    return field


n,m,q = li()
grid = [[0 for _ in range(n+1)] for _ in range(n+1)]

for _ in range(m):
    l,r = li()
    grid[l][r] += 1
    
grid = cum_2d(grid)


for _ in range(q):
    p,q = li()
    print(grid[q][q] - grid[q][p-1] - grid[p-1][q] + grid[p-1][p-1])


Measured memory_bytes: 13756
#include <bits/stdc++.h>
using namespace std;
int main(void){
    int N;
    cin >> N;
    pair<string, pair<int,int>> restaurant[N];
    for (int i = 0; i < N; i++){
        cin >> restaurant[i].first >> restaurant[i].second.first;
        restaurant[i].second.second = i+1;
    }
    for (int i = 0; i < N; i++){
        restaurant[i].second.first = 100 - restaurant[i].second.first;
    }
    sort(restaurant, restaurant + N);
    
    for (int i = 0; i < N; i++){
        printf("%d\n", restaurant[i].second.second);
    }

}

Measured memory_bytes: 256
fun main(args: Array<String>) {

    val (N, W) = readLine()!!.split(" ").map { it.toInt() }

    val dp = Array(N+1) { LongArray(W+1) { 0 } }

    for (i in 1..N) {
        val (w, v) = readLine()!!.split(" ").map { it.toInt() }
        for (j in 1..W) {

            // itemを選ぶとき
            if (j - w >= 0) {
                dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j - w] + v)
                continue
            }
            // itemを選ばないとき
            dp[i][j] = dp[i-1][j]
        }
    }

    println(dp[N][W])
}

Measured memory_bytes: 155136
#include <bits/stdc++.h>
#define loop2(i, s, n, a) for (int i = int(s); i < int(n); i += a)
#define loop(i, s, n) loop2(i, s, n, 1)
#define rep(i, n) loop(i, 0, n)

#define pb push_back
#define all(in) in.begin(),in.end()

using ll = long long;
using ull = unsigned long long;
using namespace std;

int main(){
    int k; cin >> k;
    vector<ll> data(k); rep(i,k) cin >> data[i];

    ll mmin = 2, mmax = 2;
    bool check = true;
    for(int i = k-1; i >= 0; i--){
        ll x = (mmin+data[i]-1)/data[i];
        ll y = mmax/data[i];
        if(x > y){ check = false; break; }
        mmin = x*data[i];
        mmax = y*data[i] + (data[i]-1);
    }
    if(check) cout << mmin << " " << mmax << endl;
    else cout << -1 << endl;
}



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

#define REP(i, n) for(decltype(n) i = 0; i < n; i++)

#define ALL(c)   c.begin(), c.end()
#define SORT(c)  std::sort(ALL(c))
#define RSORT(c) std::sort(ALL(c), std::greater<decltype(c)::value_type>())

using namespace std;
using ll = long long;

const int MOD  = (int)1e9 + 7;
const int INF  = (int)1e9 + 1;
const ll  LINF = (ll)1e18 + 1;

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

    int N, C;
    std::cin >> N >> C;

    vector<vector<int>> cost_table(C, vector<int>(C, 0));
    REP(i, C) {
        REP(j, C) {
            std::cin >> cost_table[i][j];
        }
    }

    vector<vector<int>> cnt_table(3, vector<int>(C, 0));
    REP(i, N) {
        REP(j, N) {
            int c;
            std::cin >> c;
            cnt_table[(i + j) % 3][c - 1]++;
        }
    }

    vector<vector<ll>> sum_cost_table(3, vector<ll>(C, 0));
    REP(i, 3) {
        REP(j, C) {
            REP(k, C) {
                sum_cost_table[i][j] += cnt_table[i][k] * cost_table[k][j];
            }
        }
    }

    ll ans = LINF;
    for(int i = 0; i < C - 2; i++) {
        for(int j = i + 1; j < C - 1; j++) {
            for(int k = j + 1; k < C; k++) {
                vector<int> color{i, j, k};
                do {
                    ll cost = 0;
                    REP(l, 3) {
                        cost += sum_cost_table[l][color[l]];
                    }
                    ans = min(ans, cost);
                } while(std::next_permutation(color.begin(), color.end()));
            }
        }
    }

    std::cout << ans << std::endl;

    return 0;
}


Measured memory_bytes: 256
A, B, C, X, Y = map(int, input().split())

v1 = (C * 2) * max(X, Y)
v2 = (C * 2) * min(X, Y)
if X > Y:
    v2 += (X - Y) * A
else:
    v2 += (Y - X) * B
v3 = (A * X) + (B * Y)

print(min(v1, v2, v3))

Measured memory_bytes: 3064
#include <stdio.h>
#include <string.h>

#define N	200000
#define A	26
#define K	100

int max(int a, int b) { return a > b ? a : b; }

int period(char *cc, int n) {
	static int zz[N];
	int i, l, r;

	for (i = 1, l = r = 0; i < n; i++)
		if (zz[i - l] < r - i)
			zz[i] = zz[i - l];
		else {
			l = i, r = max(r, l);
			while (r < n && cc[r] == cc[r - l])
				r++;
			zz[i] = r - l;
		}
	for (i = 1; i < n; i++)
		if (zz[i] == n - i)
			break;
	return i;
}

long long ans[A];

void solve(char *cc, int n, int m, long long r, int sign) {
	static long long len[K], cnt[K][A];
	int h, i, a, k;

	len[0] = m, memset(cnt[0], 0, sizeof cnt[0]);
	for (i = 0; i < m; i++)
		cnt[0][cc[i] - 'a']++;
	len[1] = n, memset(cnt[1], 0, sizeof cnt[1]);
	for (i = 0; i < n; i++)
		cnt[1][cc[i] - 'a']++;
	k = 2;
	while (len[k - 1] < r) {
		len[k] = len[k - 1] + len[k - 2];
		for (a = 0; a < A; a++)
			cnt[k][a] = cnt[k - 1][a] + cnt[k - 2][a];
		k++;
	}
	for (h = k - 1; h >= 0; h--)
		if (r >= len[h]) {
			for (a = 0; a < A; a++)
				ans[a] += cnt[h][a] * sign;
			r -= len[h];
		}
	for (i = 0; i < r; i++)
		ans[cc[i] - 'a'] += sign;
}

int main() {
	static char cc[N + 1];
	int n, m, i, a;
	long long l, r;

	scanf("%s%lld%lld", cc, &l, &r), n = strlen(cc) / 2, l--;
	m = period(cc, n);
	if (n % m == 0) {
		while (l < r && l % m != 0)
			ans[cc[l++ % m] - 'a']++;
		while (l < r && r % m != 0)
			ans[cc[--r % m] - 'a']++;
		for (i = 0; i < m; i++)
			ans[cc[i] - 'a'] += (r - l) / m;
	} else
		solve(cc, n, m, r, 1), solve(cc, n, m, l, -1);
	for (a = 0; a < A; a++)
		printf("%lld ", ans[a]);
	printf("\n");
	return 0;
}


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

using namespace std;

int a, b, c;
int ans;

int main()
{
    scanf("%d %d %d", &a, &b, &c);
    if ((a & 1) == 0 && a == b && b == c)
    {
        printf("-1\n");
    }
    else
    {
        while ((a & 1) == 0 && (b & 1) == 0 && (c & 1) == 0)
        {
            int A = (b + c) / 2,
                B = (c + a) / 2,
                C = (a + b) / 2;
            a = A;
            b = B;
            c = C;
            ++ ans;
        }
        printf("%d\n", ans);
    }
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define mod 998244353
int main() 
{
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
    LL N;
    cin>>N;
    LL x;
    LL count=0;
    x=1;
    while(x<=N)
    {
        LL y;
        cin>>y;
        if(x%2 and y%2)
            count++;
        x++;
    }
    cout<<count;
}


Measured memory_bytes: 3664
#include <bits/stdc++.h>
#define int long long
using namespace std;
int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

signed main(){
    int H,W,N;
    cin >> H >> W >> N;
    
    cout << min((N+(H-1))/H,(N+(W-1))/W) << endl;
    return 0;
}


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

struct Solver {
	void solve() {
		int N;
		cin >> N;

		vector<array<int, 2>> D( N );
		for( int i = 0; i < N; ++i ) {
			cin >> D[i][0] >> D[i][1];
		}

		bool ans = false;
		for( int i = 2; i < N; ++i ) {
			if( D[i][0] == D[i][1] && D[i-1][0] == D[i-1][1] && D[i-2][0] == D[i-2][1] ) {
				ans = true;
				break;
			}
		}

		cout << (ans ? "Yes" : "No") << endl;
	}
};

int main() {
	ios::sync_with_stdio( false );
	cout << fixed << setprecision( 10 );
	Solver().solve();
	return 0;
}


Measured memory_bytes: 3660
@d = gets.to_i
@c = gets.split.map &:to_i
@s = @d.times.map { gets.split.map &:to_i }

def score(schedule, range = 0...@d, last = [0] * 26)
  sat = 0
  range.each do |day|
    type = schedule[day]
    sat += @s[day][type]
    day += 1
    last[type] = day
    sat -= 26.times.sum { |i, day_last| @c[i] * (day - last[i]) }
  end
  sat
end

schedule = [0] * @d
last = [0] * 26
@d.times do |day|
  s_max, i_max = -10000000, 0
  26.times do |i|
    schedule[day] = i
    s = score(schedule, day..day, last.dup)
    s_max, i_max = s, i if s > s_max
  end
  schedule[day] = i_max
  last[i_max] = day + 1
end

schedule.each do |type|
  p type + 1
end


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

unsigned long long a,b;
unsigned long long gcd(unsigned long long x,unsigned long long y)
{
	if(y==0) return x;
	if(x < y) return gcd(y,x);
	else return gcd(y,x%y);
}

int main()
{
	cin>>a>>b;
	cout<<a*b/gcd(a,b);
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
using ll = long long;
#define vl vector<long long>
#define vvl vector<vector<long long>>
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, s, n) for (ll i = (s); i < (ll)(n); i++)
using pll = pair<long long,long long>;
#define cY cout<<"Yes"<<endl
#define cN cout<<"No"<<endl
const long long INF = 1LL << 60;
const long long MOD = 1000000007;
const long double pi = (acos(-1));
#define pb push_back

//最大最小の入れ替え
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

//約数列挙
vector<long long> divisor(long long n) {
    vector<long long> ret;
    for (long long i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    sort(ret.begin(), ret.end()); // 昇順に並べる
    return ret;
}
//コンビネーション
ll nCr(ll n, ll r) {
    ll ans = 1;
    for (ll i = n; i > n - r; --i) {
        ans = ans*i;
    }
    for (ll i = 1 ; i < r + 1; ++i) {
        ans = ans / i;
    }
    return ans;
}
//素因数分解
map< ll, ll > prime_factor(ll n) {
  map< ll, ll > ret;
  for(ll i = 2; i * i <= n; i++) {
    while(n % i == 0) {
      ret[i]++;
      n /= i;
    }
  }
  if(n != 1) ret[n] = 1;
  return ret;
}

//切り上げの割り算は(a + b - 1) / bでできる

//ここから
int main(){
    ll A,B,C,K;
    cin>>A>>B>>C>>K;
    ll Ans=0;
    while(K>0&&A>0){
        Ans++;
        A--;
        K--;
    }
    while(K>0&&B>0){
        B--;
        K--;
    }
    while(K>0&&C>0){
        C--;
        K--;
        Ans--;
    }
    cout<<Ans<<endl;
}


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

int main() {
  int N, K, x, d = 0;
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> x;
    if (x < (K - x)) {
      d += 2 * x;
    }
    else {
      d += 2 * (K - x);
    }
  }
  cout << d;
}
      
  

Measured memory_bytes: 256