akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define GET_MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) GET_MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#define fi first
#define se second
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cout<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cout<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

vector<int> tree[100005];

class LCA {
public:
  int n,ln; // number of nodes and log n
  vector<vector<int>> parent;
  vector<int> depth;
  LCA(int _n, int root=-1) : n(_n), depth(_n){
    ln=0;
    while(n>=(1<<ln)) ln++;  // calc log n
    parent = vector<vector<int>>(ln, vector<int>(n));
    if(root!=-1) init(root);
  }
  void dfs(int v, int p, int d){
    parent[0][v] = p;
    depth[v] = d;
    rep(i,tree[v].size()) if(tree[v][i]!=p) dfs(tree[v][i], v, d+1);
  }
  void init(int root){
    dfs(root, -1, 0);
    for(int k=0; k+1<ln; k++){
      for(int v=0; v<n; v++){
        if(parent[k][v] < 0) parent[k+1][v] = -1;
        else parent[k+1][v] = parent[k][parent[k][v]];
      }
    }
  }
  int query(int u, int v){
    if(depth[u] > depth[v]) swap(u,v);
    for(int k=0; k<ln; k++){
      if((depth[v]-depth[u])>>k & 1) v = parent[k][v];
    }
    if(u==v) return u;
    for(int k=ln-1; k>=0; k--){
      if(parent[k][u] != parent[k][v]){
        u = parent[k][u];
        v = parent[k][v];
      }
    }
    return parent[0][u];
  }
};

int main(){
  int n;
  cin>>n;
  rep(i,n){
    int k;
    cin>>k;
    rep(j,k){
      int d;
      cin>>d;
      tree[i].pb(d);
    }
  }

  LCA lca(n, 0);

  int q;
  cin>>q;
  rep(_,q){
    int a,b;
    cin>>a>>b;
    cout << lca.query(a,b) << "\n";
  }

  return 0;
}

Measured memory_bytes: 18520
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>

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

using namespace std;
int N,X;
int a[200000];

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


int main(){
	cin >> N >> X;
  REP(i,N){
    int c;
    cin >> c;
    a[i] = max(c-X,X-c);
  }
  int res = a[0];
  REP(i,N){
    res = gcd(res,a[i]);
  }
  cout << res << endl;
	return 0;
}


Measured memory_bytes: 640
#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
#include<math.h>
#include<map>

using namespace std;

int main(){
	int n;
	cin >> n;
	string s;
	cin >> s;
	
	int cnt=0;
	char now=NULL;
	for(int i=0;i<s.size();i++){
		if(now!=s[i]){
			cnt++;
			now=s[i];
		}
	}
	cout << cnt << endl;
	return 0;
}


Measured memory_bytes: 512
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>

#define endl '\n'
#define fi first
#define se second
#define pb push_back
#define debug(x) cout << "[debug] " << #x << ": " << x << endl
using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int MOD = 1e9 + 7;
const int maxn = 1e6 + 5;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-8;

int main() {
    int n; cin >> n;
    int a[25];
    int b[25];
    int c[25];
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    for(int i = 0; i < n; ++i){
        cin >> b[i];
    }
    for(int i = 0; i < n - 1; ++i){
        cin >> c[i];
    }
    int sum = 0;
    for(int i = 0; i < n; ++i){
        sum +=  b[a[i] - 1];
        if(i > 0 && a[i] == a[i - 1] + 1) sum += c[a[i] - 1 - 1];
    }
    cout << sum << endl;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long int
#define ordered_set tree < ll ,  null_type ,  less<ll> ,  rb_tree_tag ,  tree_order_statistics_node_update >
//added two more functions to set
//(1)*(set.find_by_order(k))[kth element in the sorted set] 
//(2)set.order_of_key(k)[count of elements strictly less than k]
typedef vector< int > vi;
typedef vector< vi > vvi;
typedef pair< int,int > ii;
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(),(c).end()
#define tr(c,i) for(auto i = (c).begin(); i != (c).end(); i++)//remember i is an iterator
#define present(c,x) ((c).find(x) != (c).end())//for sets,maps,multimaps
#define cpresent(c,x) (find(all(c),x) != (c).end())//for vectors
#define tc(t) int (t); cin>>(t);while((t)--)
#define ff first
#define ss second
#define mp make_pair
#define ld long double
#define ull unsigned long long int
#define mod 1000000007
int main()
{
    ios_base::sync_with_stdio(false);//this works only
    cin.tie(NULL);// when first all inputs are from user
    cout.tie(NULL);//you will see all outputs afterwards
    string s1,s2;
    cin>>s1>>s2;
    cout<<s2<<s1;
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main(int argc, char *argv[]){
	string s;
	int K;
	cin >> s;
	cin >> K;

	for (int i = 0; i < s.length(); i++) {
		int temp = 123 - s[i];
		if (s[i] != 'a' && K >= temp) {
			s[i] = 'a';
			K -= temp;
		}
	}
	s.back() += (K % 26);
	cout << s << endl;
}


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

using namespace std;
const int N=2e7+5;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    queue<long long>q;
    for(int i=1;i<=9;i++)
        q.push(i);
    int n;cin>>n;
    while(1)
    {
        long long cur=q.front();
        q.pop();
        //cout<<n<<" "<<cur<<endl;
        n--;
        if(!n)
        {
            cout<<cur<<endl;
            return 0;
        }
        for(int i=-1;i<=1;i++)
            if(cur%10+i>=0&&cur%10+i<=9)
                q.push(cur*10+(cur%10+i));

    }
    return 0;
}


Measured memory_bytes: 1792
let swap a i j =
  let temp = a.(i) in
  a.(i) <- a.(j);a.(j) <- temp;;

let rec find a x n =
  if a.(n) = x then n else find a x (n+1);;

let is_stable org c n =
  let rec loop = function
      i when i = n -> "Stable"
    | i ->
       if (snd c.(i)) = (snd c.(i-1)) then
         if find org c.(i) 0 > find org c.(i-1) 0 then loop (i+1)
         else "Not stable"
       else
         loop (i+1)
  in loop 1
;;

let partition a p r =
  let x = a.(r) and
      i = ref (p-1) in
  for j = p to (r-1) do
    if (snd a.(j)) <= (snd x) then
      begin
        i := (!i) + 1;
        swap a (!i) j;
      end;
  done;
  swap a ((!i)+1) r;
  (!i) + 1
;;

let rec quick_sort a p r =
  if p < r then
    let q = partition a p r in
    quick_sort a p (q-1);
    quick_sort a (q+1) r
;;

let () =
  let n = read_int () in
  let a1 = Array.init n (fun x -> Scanf.scanf "%s %d\n" (fun y z-> (y,z))) in
  let a2 = Array.copy a1 in
  quick_sort a2 0 (n-1);
  print_endline (is_stable a1 a2 n);
  Array.iter (fun x -> Printf.printf "%s %d\n" (fst x) (snd x)) a2
;;

Measured memory_bytes: 10772
program eee
implicit none
 
integer(16) :: n, w, i, j
integer(16),allocatable,dimension(:) :: ww, vv
integer(16),allocatable,dimension(:,:) :: dp
! 初期値の設定
read*, n, w
allocate(ww(n),vv(n))
do i=1, n
	read*, ww(i), vv(i)
end do
allocate(dp(0:n,0:1000*n))
dp=w*n+1
dp(0,0)=0
! end 初期値の設定
 
do i=1,n
	do j=0,1000*n
		if(j>=vv(i)) then					!追加可能パターン
        	dp(i,j)=dp(i-1,j-vv(i))+ww(i)	!i-1の場合の、価値j-v(i)から参照
        end if
        dp(i,j)=min(dp(i,j),dp(i-1,j))		!追加したものとしていないものの比較を行う
    end do
end do
 
do j=1000*n,0,-1
    if(dp(n,j)<=w) then
    	write(*,'(i0)') j
        return
    end if
end do
 
end program


Measured memory_bytes: 158080
import java.util.*;
import java.text.*;
/*
Main class for AOJ where there is no input.
*/
public class Main{
   public static void main(String[] args){
      Scanner sc = new Scanner(System.in);
      String[] arr;
      int n;
      int[] score;
      double avg;
      int sum;
      double stdDeviation;
      
      while(true){
         n = Integer.parseInt(sc.nextLine());
         if(n == 0){
            break;
         }
         
         sum = 0;
         stdDeviation = 0;
         
         arr = sc.nextLine().split(" ");
         score = new int[arr.length];
         
         for(int i = 0; i < n; i ++){
            score[i] = Integer.parseInt(arr[i]);
            sum += score[i];
         }
         
         avg = sum/(double)n;
         
         for(int i = 0; i < n; i ++){
            stdDeviation += Math.pow((double)score[i] - avg,2);
         }
         stdDeviation /= (double)n;
         stdDeviation = Math.sqrt(stdDeviation);
         
         System.out.println(String.format("%.6f",stdDeviation));
      }
   }//main
   
   
}//class

Measured memory_bytes: 28732
#include <cmath>
#include <iomanip>
#include <iostream>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main() {
  int l;
  std::cin >> l;

  double x, y, z;
  x = y = l / 3.0;
  z = l - (x + y);

  std::cout << std::fixed << std::setprecision(15) << x * y * z << '\n';

  return 0;
}


Measured memory_bytes: 256
use std::io;
use std::collections::VecDeque;

fn main() {
    let mut input = Input::new();
    let n: usize = input.next();
    let s: Vec<char> = input.next::<String>().chars().collect();
    let t: Vec<char> = input.next::<String>().chars().collect();
    for i in 0..n + 1 {
        if (0..n - i).all(|j| s[i + j] == t[j]) {
            println!("{}", n + i);
            return;
        }
    }
}

struct Input {
    queue: VecDeque<String>,
}

impl Input {
    fn new() -> Input {
        Input { queue: VecDeque::new() }
    }

    fn next<T>(&mut self) -> T
        where T: std::str::FromStr,
              T::Err: std::fmt::Debug
    {
        if self.queue.is_empty() {
            let mut line = String::new();
            let _ = io::stdin().read_line(&mut line);
            for x in line.split_whitespace() {
                self.queue.push_back(String::from(x));
            }
        }
        return self.queue.pop_front().unwrap().parse::<T>().unwrap();
    }
}



Measured memory_bytes: 4352
#include<stdio.h>
#include<string.h>
int main( void ){
    int n;
    char s1[100001];
    char s2[100001];
    scanf("%d", &n);
    scanf("%s", s1);
    int i;
    for(i = 0; i < strlen(s1); i++){
        if(s1[i]+n <= 90){
            s2[i] = s1[i]+n;
        } else {
            s2[i] = s1[i]+n-26;
        }
    }
    s2[i] = '\0';
    printf("%s\n", s2);
}

Measured memory_bytes: 256
from math import ceil


def main():
    n = int(input())

    min_value = n * 100 / 108
    max_value = (n + 1) * 100 / 108

    candidate = ceil(min_value)

    if candidate < max_value:
        print(candidate)
    else:
        print(":(")


if __name__ == "__main__":
    main()


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

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

struct BipartiteMatching {
    int m, n;
    vector<vector<int> > G;
    vector<bool> used; vector<int> match;
    BipartiteMatching(int m, int n) : m(m), n(n), G(m+n), used(m+n), match(m+n) {}
    void add_edge(int u, int v){
        if( !(0<=u&&u<m&&0<=v&&v<n) ) throw domain_error("invalid node"); 
        G[u].push_back(m+v); G[m+v].push_back(u);
    }
    bool dfs(int v){  // check if there exists a path from v to t
        used[v] = true;
        for(int i=0;i<G[v].size();++i){
            int u = G[v][i], w = match[u];
            if(w<0 || !used[w] && dfs(w)) { match[v] = u; match[u] = v; return true; }
        }
        return false;
    }
    int solve(){
        int nmatch = 0;
        fill(match.begin(), match.end(), -1);
        for(int v=0;v<m+n;++v)
            if(match[v]<0){ fill(used.begin(),used.end(),false); if(dfs(v)) nmatch++; }
        return nmatch;
    }
};






int main(){
  int m, n, E, x, y;
  cin >> m >> n >> E;
  BipartiteMatching bm(m,n);
  for(int i=0;i<E;++i){
    cin >> x >> y;
    bm.add_edge(x,y);
  }
  int ret = bm.solve();
  cout << ret << endl;
}



Measured memory_bytes: 3272
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb(x) push_back(x)
#define rep(a,b) for(ll int a=0;a<b;a++)
#define Tcase int t;cin>>t;while(t--)
int isPrime(long int x){
int i=0;
  for( i=2;i<=sqrt(x);i++){
  if(x%i==0)
    break;
  }
  if(i<=sqrt(x))
    return 0;
  else
    return 1;
}
int main(){
  long int x;
  cin>>x;
  while(!isPrime(x)){
  x++;
  }
  cout<<x<<endl;
return 0;
}


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

int f(int n) {
  if (n%2 == 0) {
    return n/2;
  }
  else {
    return 3*n+1;
  }
}

int main() {
  int s;
  cin >> s;
  const int INF = 1000000;
  vector<int> a(INF);
  for (int i=0; i<INF; i++) {
    if (i == 0) {
      a.at(i) = s;
    }
    else {
      a.at(i) = f(a.at(i-1));
    }
  }
  int answer;
  for (int i=0; i<INF; i++) {
    if (a.at(i) == 1) {
      answer = i+2;
      break;
    }
  }
  if (s == 1 || s == 2) {
    answer = 4;
  }
  cout << answer << endl;
}

Measured memory_bytes: 4096
d,n=map(int,input().split())
if d==0:
    if n==100:print(101)
    else:print(n)
elif d==1:
    if n==100:print(10100)
    else:print(n*100)
else:
    if n==100:print(1010000)
    else:print(n*10000)

Measured memory_bytes: 3060
import sys
import os



N, M = map(int, input().split())

KS=[0]*(M)
C=[0]*M
S=[0]*M

for i in range(M):
    KS[i] = list(map(int, input().split()))
    S[i]=KS[i][1:]
p = list(map(int,input().split()))
onsum=0
ans=0
for i in range(2**N):
    x=format(i, 'b')
    x_zero=x.zfill(N)
    s=list(x_zero)
    s=list(map(int,s))#スイッチの状態01010
    onsum=0
    #print(s)
    for j in range(M):#あるスイッチの状態に対して電球jがつくかどうか調べる
        on=0
        for k in S[j]:
            #print(s[k-1])
            on=on+s[k-1]
            #print("on=",on)
        if on%2==p[j]:
            onsum+=1
    if onsum==M:
        ans+=1

#print(N,M)
#print(KS)
#print(S)
print(ans)

Measured memory_bytes: 3188
#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+100;
const int mod = 1e9+7;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const ll llINF = 0x3f3f3f3f3f3f3f3f;
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define fep(i,a,b) for(int i=(a);i>=(b);i--)
inline bool read(ll &num) {
    char in;bool IsN=false;
    in=getchar();
    if(in==EOF) return false;
    while(in!='-'&&(in<'0'||in>'9')) in=getchar();
    if(in=='-'){ IsN=true;num=0;}
    else num=in-'0';
    while(in=getchar(),in>='0'&&in<='9'){
            num*=10,num+=in-'0';
    } 
    if(IsN) num=-num;
    return true;
}
int main(){
    //freopen("1.in", "r", stdin);
    ll a,b;
    cin>>a>>b;
    for(int i=0;;i++){
        if(a*i-(i-1)>=b){
            cout<<i<<endl;
            return 0;
        }
    }
    return 0;
}

Measured memory_bytes: 256
from itertools import accumulate

N, K = list(map(int, input().split()))
P = list(map(lambda x: int(x) + 1, input().split()))
P_cumsum = [0] + list(accumulate(P))
print(max([P_cumsum[i+K]-P_cumsum[i] for i in range(N-K+1)])/2)

Measured memory_bytes: 31756
#include <iostream>
#include <string>
#include <vector>
#include <utility>
using namespace std;

int main()
{
    int N, Q;
    string S;
    cin >> N >> Q >> S;
    vector<pair<int, int>> ques;
    for (int i = 0; i < Q; i++)
    {
        int l,r;
        cin >> l >> r;
        pair<int, int> p;
        p.first=l-1;
        p.second=r-1;
        ques.push_back(p);
    }
    int max = 0;
    bool Aflag = false;
    vector<int> sum;
    for (int i = 0; i < N; i++)
    {
        if (Aflag && S[i] == 'C')
        {
            max++;
            Aflag = false;
        }
        else if (S[i] == 'A')
        {
            Aflag = true;
        }
        else{
             Aflag = false;
        }
        sum.push_back(max);
       // cout <<"i:"<<i<<"max"<<sum[i]<<endl;
    }
    for (int i = 0; i < Q; i++)
    {
        cout<<sum[ques[i].second]-sum[ques[i].first]<<endl;
    }
}

Measured memory_bytes: 2676
#include<stdio.h>

int fib(int);

int main(){
  int n;
  scanf("%d",&n);
  printf("%d\n",fib(n));
}

int fib(int n){
  static int dp[45];
  if(dp[n] > 0) return dp[n];
  if(n == 0 || n == 1) return 1;
  return dp[n] = fib(n - 1) + fib(n - 2);
}



Measured memory_bytes: 2112
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>


using namespace std;

int main()
{
    string s,t,ch;
    int a,b;
    cin>>s>>t;
    cin>>a>>b;
    cin>>ch;
    if(ch==s)
        a--;
    else if(ch==t)
        b--;
    cout<<a<<" "<<b;
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi=vector<bool>;
using vvi=vector<vi>;
using pii=pair<int,int>;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define INF 1e9
#define EPS 1e-9
#define MOD (1e9+7)
void put(string d){}template<class H,class...T>void put(string d,H&h,T&...t){cout<<h;if(sizeof...(t))cout<<d;put(d,t...);}
template<class T>void puti(T&a,string d=" "){bool f=1;for(auto&_:a)cout<<(exchange(f,0)?"":d)<<_;cout<<endl;}
template<class T>void putii(T&a,string d=" "){for(auto&_:a)puti(_,d);}

int vx[]={-1,0,1,0};
int vy[]={0,1,0,-1};

int main(){
    int w,h;
	while(cin>>w>>h,w){
        int sx,sy,gx,gy;
        vvi a;
		a=vvi(h,vi(w,0));
		rep(i,h)rep(j,w){
		    int t; cin>>t;
		    if(t==2) sy=i,sx=j;
		    if(t==3) gy=i,gx=j;
		    if(t==1) a[i][j]=1;
		}
		using data=tuple<int,int,int,vvi>;
		queue<data> q;
		q.emplace(0,sy,sx,a);
		while(!q.empty()){
		    int t,y,x,nx,ny; vvi f;
		    tie(t,y,x,f)=q.front(); q.pop();
		    //cout<<t<<","<<y<<","<<x<<endl;
		    //putii(f);
		    if(t==10){
		        cout<<-1<<endl;
		        goto fin;
		    }
		    rep(i,4){
		        nx=x+vx[i],ny=y+vy[i];
		        if(!(ny>=h or ny<0 or nx>=w or nx<0) and f[ny][nx]==1) continue;
		        while(1){
		            if(gx==nx and gy==ny){
		                cout<<t+1<<endl;
		                goto fin;
		            }
		            if(ny>=h or ny<0 or nx>=w or nx<0) break;
		            if(f[ny][nx]==1){
		                auto nf=f;
		                nf[ny][nx]=0, ny-=vy[i], nx-=vx[i];
		                q.emplace(t+1,ny,nx,nf);
		                break;
		            }
		            nx+=vx[i],ny+=vy[i];
		        }
		    }
		}
		cout<<-1<<endl;
		fin:;
	}
	return 0;
}



Measured memory_bytes: 111536
import java.util.Scanner;

class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int S[] = new int[n];
		int i;
		for(i=0; i<n; i++){
			S[i] = sc.nextInt();
		}
		int q = sc.nextInt();
		int T[] = new int[n];
		int j;
		for(j=0; j<q; j++){
			T[j] = sc.nextInt();
		}
		int C=0;
		for(j=0; j<q; j++){
			for(i=0; i<n; i++){
				if(T[j]==S[i]){
					C++;
					break;
				}
			}
		}
		System.out.println(C);
	}
}

Measured memory_bytes: 31104
#include <bits/stdc++.h>
using namespace std;
#define debug(a) cout << #a": " << a << endl;
#define mst(a, b) memset(a, b, sizeof(a))
#define lc rt << 1
#define rc rt << 1 | 1
#define X first
#define Y second
inline int lowbit(int x) { return x & -x; }
typedef long long LL;
typedef pair<int, int> pii;
const int N = 3000 + 10;
const int M = 2e6 + 10;
const int inf = 0x3f3f3f3f;
const int mod = 998244353;

char s[N];
char t[N];
int dp[N][N];
stack<char> sta;

int main() {
#ifdef purple_bro
    freopen("in.txt","r",stdin);
#endif
    scanf("%s%s", s + 1, t + 1);

    int lens = strlen(s + 1);
    int lent = strlen(t + 1);

    for (int i = 1; i <= lens; i++) {
        for (int j = 1; j <= lent; j++) {
            if (s[i] == t[j])
                dp[i][j] = dp[i - 1][j - 1] + 1;
            else
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
    }

    int now = dp[lens][lent] + 1;

    for (int i = lens; i >= 1; i--) {
        for (int j = lent; j >= 1; j--) if (dp[i][j] == now - 1 && s[i] == t[j]) {
            sta.push(s[i]);
            now--;
            break;
        }
    }

    for (; !sta.empty(); sta.pop())
        printf("%c", sta.top());

    puts("");

    return 0;
}


Measured memory_bytes: 35584
N = int(input())
AB = []
for _ in range(N):
    ab = list(map(int, input().split()))
    AB.append(ab)

AB.sort(key=lambda x:x[1])
sum_t = 0

for ab in AB:
    sum_t += ab[0]
    if sum_t > ab[1]:
        print("No")
        exit()

print("Yes")

Measured memory_bytes: 116224
#include <iostream>
using namespace std;
int main()
{
  int A,B,C,X,res=0;
  cin>>A>>B>>C>>X;
  X/=50;
  for (int i=0;i<=min(A,X/10);i++)
  {
    for (int j=0;j<=min(B,(X-10*i)/2);j++)
    {
      int k=X-10*i-2*j;
      if (k<=C) res++;
    }
  }
  cout<<res<<endl;
  //system("pause");
  return 0;
}

Measured memory_bytes: 256
#include <vector>
#include <iostream>
#include <cmath>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <fstream>
#include <unistd.h>
#include <string>
#include <numeric>
#include <queue>
#include <deque>
#include <sstream>
#include <iomanip>
#include <set>
#include <stack>
#include <cassert>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<vvd> vvvd;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
typedef vector<P> vp;
typedef vector<PL> vpl;
typedef vector<vector<P>> vvp;
typedef vector<vector<PL>> vvpl;
const int INF = 1001001001;
const ll LINF = 1e16;
const double pi = 3.1415926535897932;
const string endstr = "\n";
#define FOR(i, a, b) for(ll i = (a); i < b; i++)
#define REP(i, n) for(ll i = 0; i < n; i++)
#define FORMAP(it, m) for(auto it = m.begin(); it != m.end(); it++)
#define ff first
#define ss second
#define pb push_back

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

bool p_comp_fs(const PL p1, const PL p2){ return p1.first < p2.first;};
bool p_comp_fg(const PL p1, const PL p2){ return p1.first > p2.first;};
bool p_comp_ss(const PL p1, const PL p2){ return p1.second < p2.second;};
bool p_comp_sg(const PL p1, const PL p2){ return p1.second > p2.second;};
template <typename T>
vector<T> uniquen(vector<T> vec){
    vec.erase(unique(vec.begin(), vec.end()), vec.end());
    return vec;
}


int main(){
    ll H, W; cin >> H >> W;
    unordered_map<char, ll> cnt;
    REP(i, H) REP(j, W){
        char c; cin >> c; cnt[c]++;
    }
    vl mod(4,0);
    FORMAP(it, cnt){
        mod[it->ss%4]++;
    }
    
    bool ok = true;
    if(H%2 == 0 && W%2 == 0){
        if(mod[1]>0 || mod[2]>0 || mod[3]>0) ok  = false;
    }
    else if (H%2 == 0 || W%2 == 0){
        ll even = H%2 == 0 ? H : W;
        if(mod[3] > 0 || mod[1] > 0) ok = false;
        else if(mod[2] > even/2) ok = false;
    }
    else{
        if(mod[1] + mod[3] != 1) ok = false;
        else if(mod[2] > (H+W-1)/2) ok = false;
    }
    cout << (ok ? "Yes" : "No") << endl;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
#define ii pair<int, int>
#define N 40123
#define vi vector<int>
#define vii vector<ii>
#define vll vector<ll>
#define pb push_back
#define fs first
#define sc second
#define oo 1000000123
#define sz(x) ((int)((x).size()))
#define OUT -oo
#define left(x) (x << 1)
#define right(x) ((x << 1) | 1)
#define mid(a, b) ((a + b) >> 1)

using namespace std;

string s;
int ag[N], ar[N], ab[N];

int main(){
    ios_base::sync_with_stdio(0);
    int n;
    cin >> n;
    cin >> s;
    int t = s.size();
    s = "#" + s;
    for(int i = 1; i <= t; ++i){
        s[i] = tolower(s[i]);
        ag[i] = ag[i-1];
        ar[i] = ar[i-1];
        ab[i] = ab[i-1];
        if(s[i] == 'g') ag[i]++;
        else if(s[i] == 'r') ar[i]++;
        else ab[i]++;
    }
    int mid, l, r;
    ll ans = 0;
    for(int i = 1; i <= t; ++i){
        for(int j = i + 2; j <= t; ++j){
            if((s[i] == 'r' && s[j] == 'b') || (s[i] == 'b' && s[j] == 'r')){ //rgb
                ans += ag[j-1] - ag[i];
                if((i&1) == (j&1)){
                    mid = (i + j) / 2;
                    if(s[mid] == 'g') ans--;
                }
            }
            else if((s[i] == 'r' && s[j] == 'g') || (s[i] == 'g' && s[j] == 'r')){
                ans += ab[j-1] - ab[i];
                if((i&1) == (j&1)){
                    mid = (i + j) / 2;
                    if(s[mid] == 'b') ans--;
                }
            }
            else if((s[i] == 'b' && s[j] == 'g') || (s[i] == 'g' && s[j] == 'b')){
                ans += ar[j-1] - ar[i];
                if((i&1) == (j&1)){
                    mid = (i + j) / 2;
                    if(s[mid] == 'r') ans--;
                }
            }
        }
    }
    cout << ans << '\n';
    return 0;
}


Measured memory_bytes: 3660
#include <algorithm>
#include <iostream>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;

int main()
{
    int A, B, C, D;
    cin >> A >> B >> C >> D;
    cout << min(A, B) + min(C, D) << endl;
    return 0;
}

Measured memory_bytes: 256
import scala.io.Source

object Main{
  def main(args: Array[String]){
    val lines = for{
      in <- Source.stdin.getLines.toList
    }yield if(isPrime(in.toInt)) in else -1

    println(lines.tail.filter(_ != -1).length)
  }

  def isPrime(n: Int): Boolean = (2 to Math.sqrt(n).toInt).forall(n % _ != 0)
}

Measured memory_bytes: 119692
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
#define REP(i, a, n) for(int i=a; i<n; i++)

int main()
{
	int N;
	int A[100001], B[100001];
	vector<pair<int, int>> vp;
	cin >> N;
	REP(i, 0, N) {
		int a, b;
		cin >> a >> b;
		vp.push_back({ a, b });
	}
	sort(vp.begin(), vp.end());

	cout << vp[N - 1].first + vp[N - 1].second << endl;
	return 0;
}

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

using namespace std;

const int mod = 998244353;
inline int pls( int a , int b ) { a += b - mod; return a + ( a >> 31 & mod ); }
inline int mns( int a , int b ) { a -= b; return a + ( a >> 31 & mod ); }
inline void inc( int & a , int b ) { a += b - mod; a += a >> 31 & mod; }
inline void dec( int & a , int b ) { a -= b; a += a >> 31 & mod; }
inline int fpow( int base , int k ) {
  int res = 1;
  while( k ) {
    if( k & 1 ) res = (long long)res * base % mod;
    base = (long long)base * base % mod; k >>= 1;
  }
  return res;
} int _w;

const int N = 3e2 + 5;

char str[N];
bool g[N][N][N];
int f[N][N][N] , n;

int main( void ) {
  _w = scanf("%s",str+1); 
  n = strlen( str + 1 );
  f[n + 1][0][0] = 1;
  for( int i = n ; i ; --i )
    for( int j = 0 ; j <= n - i ; ++j )
      for( int k = 0 ; j + k <= n - i ; ++k ) {
        inc( f[i][j][k] , f[i + 1][j][k] );
        if( str[i + j + k] == '0' ) 
          inc( f[i][j + 1][k] , f[i + 1][j][k] );
        else inc( f[i][j][k + 1] , f[i + 1][j][k] );
      }
  g[0][0][0] = 1;
  for( int i = 1 ; i <= n ; ++i ) {
    for( int j = 0 ; j <= i ; ++j )
      for( int k = 0 , a , b ; j + k <= i ; ++k ) {
        g[i][j][k] |= g[i - 1][j][k];
        if( i > 1 ) {
          a = str[i - 1] - '0' , b = str[i] - '0';
          if( a || b ) g[i][j + 1][k] |= g[i - 2][j][k];
          if( !(a&b) ) g[i][j][k + 1] |= g[i - 2][j][k];
        }
        if( str[i] == '1' )
          g[i][j + 1][k] |= g[i - 1][j][k + 1];
        else
          g[i][j][k + 1] |= g[i - 1][j + 1][k];
      }
  }
  int ans = 0;
  for( int i = 0 ; i <= n ; ++i )
    for( int j = 0 ; j <= i ; ++j )
      for( int k = 0 ; k + j <= i ; ++k )
        if( g[i][j][k] )
          inc( ans , f[i + 1 - j - k][j][k] );
  dec( ans , 1 );
  printf("%d\n",ans);
  return 0;
}


Measured memory_bytes: 73424
#include <bits/stdc++.h>
using namespace std;
#define SORT(a) sort((a).begin(), (a).end())
#define RSORT(a) reverse((a).begin(), (a).end())
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
const long long INF = 1LL << 60;

const ll MOD = 1000000007;
long repeatExp(long long n, long long k) {
  long long res = 1;
  while (k > 0) {
    if (k & 1) res = res * n % MOD;
    n = n * n % MOD;
    k = k >> 1;
  }
  return res % MOD;
}

long comb(int n, int x) {
  long long xx = 1;
  long long y = 1;
  for (int i = 0; i < x; ++i) {
    xx = (xx * (n - i)) % MOD;
    y = (y * (i + 1)) % MOD;
  }
  return (xx * repeatExp(y, MOD - 2)) % MOD;
}

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

int main() {
  ll n, a, b;
  cin >> n >> a >> b;
  ll ans1 = modpow(2, n, MOD);

  cout << (ans1 + MOD + MOD - comb(n, a) % MOD - comb(n, b) % MOD - 1) % MOD
       << endl;
}

Measured memory_bytes: 256
#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))
//-------------------------------------------------------

ll mo=1000000007;
int A,B;
ll memo[2020][2020];
ll C[2040][2040];
ll CS[2040][2040];

ll combi(ll N_, ll C_) {
	const int NUM_=400001;
	static ll fact[NUM_+1],factr[NUM_+1],inv[NUM_+1];
	if (fact[0]==0) {
		inv[1]=fact[0]=factr[0]=1;
		for (int i=2;i<=NUM_;++i) inv[i] = inv[mo % i] * (mo - mo / i) % mo;
		for (int i=1;i<=NUM_;++i) fact[i]=fact[i-1]*i%mo, factr[i]=factr[i-1]*inv[i]%mo;
	}
	if(C_<0 || C_>N_) return 0;
	return factr[C_]*fact[N_]%mo*factr[N_-C_]%mo;
}

ll dfs(int A,int B) {
	if(A==0 || B==0) return 1;
	if(memo[A][B]>=0) return memo[A][B];
	// take A;
	ll ret=dfs(A-1,B);
	// take B;
	int a=A,b=B-1;
	// take until a+b==A;
	//for(int i=max(0,a-b);i<=a;i++) (ret += C[b][a-i])%=mo;
	ret += CS[b][min(a,b)+1]-CS[b][0]+mo;
	return memo[A][B]=ret%mo;
}


void solve() {
	int i,j,k,l,r,x,y; string s;
	
	MINUS(memo);
	cin>>A>>B;
	if(A==0 || B==0) {
		cout<<1<<endl;
		return;
	}
	for(i=0;i<=2020;i++) {
		C[i][0]=C[i][i]=1;
		if(i) for(j=1;j<i;j++) (C[i][j]=C[i-1][j-1]+C[i-1][j])%=mo;
		FOR(j,2020) (CS[i][j+1]=CS[i][j]+C[i][j])%=mo;
	}
	
	ll ret=0;
	B--;
	for(int a=0;a<=A;a++) {
		for(i=0;i<=a;i++) if(a-i<=B) {
			(ret += C[B][a-i]*dfs(i,a-i))%=mo;
		}
	}
	
	cout<<ret<<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: 98688
#include <algorithm>
#include <bitset>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define MOD (1000000007l)
#define ll long long
#define rep(i, n) for (ll i = 0; i < (n); i++)

using namespace std;

void solve() {
  ll N, W;
  cin >> N >> W;
  vector<ll> result(W+1);
  rep (n, N) {
    ll w, v;
    cin >> w >> v;
    for (ll i = W; i - w >= 0; i--) result[i] = max(result[i], result[i-w]+ v);
  }
  cout << result[W] << endl;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout.precision(12);
  cout << fixed;
  solve();
  return 0;
}


Measured memory_bytes: 1024
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
  int x[1000], y[1000];
  int i,n,p=0;
  double output=0,max;

  scanf("%d",&n);
  for(i=0; i<n; i++)
    {
      scanf("%d",&x[i]);
    }
   for(i=0; i<n; i++)
    {
      scanf("%d",&y[i]);
    }
      
      //ここまででxとyの要素数を読み取らせる
    
   while(1)
    {
      if(p == 0)//1のとき
	{
	  for(i=0; i<n; i++)
	    {
	      output +=  abs(x[i] - y[i]);
	    }
	  printf("%lf\n",output);
	  p++;
	}
      if(p == 1)//2のとき
	{
	  output = 0;
	  for(i=0; i<n; i++)
	    {
	      output += abs(x[i] - y[i])*abs(x[i]-y[i]);
	    }
	  printf("%lf\n",sqrt(output));
	  p++;
	}
      if(p == 2)//3のとき
	{
	  output = 0;
	   for(i=0; i<n; i++)
	    {
	      output += abs(x[i] - y[i])*abs(x[i]-y[i])*abs(x[i]-y[i]);
	    }
	   printf("%lf\n",pow(output,1.0/3.0));//三乗根
	  p++;
	}
      if(p == 3)//∞のとき
	{
	  output = 0;
	   for(i=0; i<n; i++)
	    {
	      if(max<abs(x[i]-y[i]))
		max=abs(x[i]-y[i]);
	    }
	  printf("%lf\n",max);
	  break;
	}
    }
  
  return 0;
}



Measured memory_bytes: 2104
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

const int MAX_N = 160;

int LongestCommonSubsequence(string s1, string s2) {
    int N = (int) s1.size();
    int M = (int) s2.size();
    vector < vector < int > > dp(N + 10, vector < int > (M + 10, 0));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            if (s1[i] == s2[j]) {
                dp[i + 1][j + 1] = dp[i][j] + 1;
            } else {
                dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
            }
        }
    }
    return dp[N][M];
}

int main(int argc, char const *argv[])
{
    int q, ans[MAX_N];
    cin >> q;
    for (int i = 0; i < q; ++i) {
        string a, b;
        cin >> a >> b;
        ans[i] = LongestCommonSubsequence(a,b);
    }

    for (int i = 0; i < q; ++i) {
        cout << ans[i] << endl;
    }
	return 0;
}



Measured memory_bytes: 6740
// Problem : B - Frog 2
// Contest : AtCoder - Educational DP Contest
// URL : https://atcoder.jp/contests/dp/tasks/dp_b
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
using namespace std;
 
#define endl "\n"
#define int long long
#define FAST {ios_base::sync_with_stdio(false);cin.tie(NULL);}
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
#define rep(n) for(int i=0;i<(n);i++)
#define rrep(n) for(int i=(n)-1;i>=0;i--)
#define mem(a,b) memset((a),(b),sizeof (a))
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define INF (long long) 1e18
#define MOD 1000000007


void solve(){
      int n;
      cin>>n;
      int arr[n],dp[n];
      rep(n) cin>>arr[i];
      for(int i=0;i<n;i++){
            if(i==0) {dp[i] = 0;continue;}
            else dp[i] = INT_MAX;
            for(int j=i-1;j>=max((int)0,i-2);j--){
                  dp[i] = min(dp[i],dp[j] + abs(arr[i]-arr[j]));
            }
      }
      cout<<dp[n-1]<<endl;
}

int32_t main(){
      FAST
      int t=1;
      //cin>>t;
      while(t--)
            solve();
      return 0;
}




Measured memory_bytes: 5184
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline") //Optimization flags
#pragma GCC option("arch=native","tune=native","no-zero-upper") //Enable AVX
#pragma GCC target("avx2")  //Enable AVX

using namespace std;

using ll = long long;

#define mp make_pair

const int p = 1000000007;


int mul(int a, int b) {
    return (1LL * a * b) % p;
}

int add(int a, int b) {
    int s = (a+b);
    if (s>=p) s-=p;
    return s;
}

int sub(int a, int b) {
    int s = (a+p-b);
    if (s>=p) s-=p;
    return s;
}

int po(int a, int deg)
{
    if (deg==0) return 1;
    if (deg%2==1) return mul(a, po(a, deg-1));
    int t = po(a, deg/2);
    return mul(t, t);
}

int inv(int n)
{
    return po(n, p-2);
}

/*struct DSU {
    vector<int> sz;
    vector<int> parent;

    void make_set(int v) {
        parent[v] = v;
        sz[v] = 1;
    }

    int find_set(int v) {
        if (v == parent[v])
            return v;
        return parent[v] = find_set(parent[v]);
    }

    void union_sets(int a, int b) {
        a = find_set(a);
        b = find_set(b);
        if (a != b) {
            if (sz[a] < sz[b])
                swap(a, b);
            parent[b] = a;
            sz[a] += sz[b];
        }
    }

    DSU(int n) {
        sz.resize(n);
        parent.resize(n);
        for (int i = 0; i < n; i++) make_set(i);
    }
};*/

/*const int N = 1000000;

vector<int> facs(N), invfacs(N);

void init()
{
    facs[0] = 1;
    for (int i = 1; i<N; i++) facs[i] = mul(facs[i-1], i);
    invfacs[N-1] = inv(facs[N-1]);
    for (int i = N-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1);
}

int C(int n, int k)
{
    return mul(facs[n], mul(invfacs[k], invfacs[n-k]));
}*/



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

    int n, k;
    cin>>n>>k;
    vector<int> a; int t;
    for (int i = 0; i<n; i++)
    {
        cin>>t; if (t<k) a.push_back(t);
    }

    n = a.size();
    if (a.size()==0) {cout<<0; return 0;}
    int answer = 0;
    vector<bitset<5001>> pref(n+1);
    vector<bitset<5001>> suf(n+1);
    pref[0][0] = 1; suf[n][0] = 1;
    for (int i = 1; i<=n; i++) pref[i] = pref[i-1]|(pref[i-1]<<a[i-1]);
    for (int i = n-1; i>=0; i--) suf[i] = suf[i+1]|(suf[i+1]<<a[i]);

    for (int i = 0; i<n; i++)
    {
        bool check = false;
        int cnt = 0;
        for (int j = k-a[i]; j<=k-1; j++) cnt+=suf[i+1][j];
        if (cnt>0) continue;
        for (int l = 1; l<=k-1; l++)
        {
            cnt-=suf[i+1][k-l];
            if (k-a[i]-l>=0) cnt+=suf[i+1][k-a[i]-l];
            if (cnt>0 && pref[i][l]==1) check = true;
        }
        if (!check) answer++;
    }
    cout<<answer;

}

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

int main() {
   int a,b,c,k;
  cin >> a >> b >> c >>k;
 int answer=a+b+c;
  int d=max(max(a,b),c);
 int e=d;
  for(int i=0;i<k;i++){
    e=e*2;
  }
  cout << answer+e-d << endl;
}

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

using namespace std;

const long long N = 100005;
const long long K = 105;
long long dp[N];
long long niz[N];
int n,k;

long long cost[K];

void sett () {
	for (long long i = 0;i < k;i++) {
		cost[i] = -10;
	}
}

long long mini(long long ind1, long long ind2) {
	long long ret = 9999999999LL;
	for (long long i = ind1;i <= ind2;i++) {

		if (cost[i] < 0) continue;
		if (cost[i] < ret) {
			ret = cost[i];
		}
	}

	return ret;
}

void ispis() {
	for (long long i = 0;i < n;i++) {
		cout << dp[i] << endl;
	}
}

int main () {
	
	cin >> n >> k;
	for (long long i = 0;i < n;i++) {
		cin >> niz[i];
	}
	dp[0] = 0;
	dp[1] = abs(niz[1] - niz[0]);
	for (int i = 2;i < n;i++) {
		sett();
		for (int j = i - 1;j >= max(0,i - k);j--) {
			cost[j] = dp[j] + abs(niz[i] - niz[j]);
		}
		dp[i] = mini(max(0,i - k),i - 1);
	}
	

	cout << dp[n - 1] << endl;
	
	return 0;
}

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

//template
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
typedef long long int ll;
const int inf = 0x3fffffff; const ll INF = 0x1fffffffffffffff; const double eps=1e-12;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
//end

int dist[510][510],cnt[510][510]={};

int main(){
   int n; cin>>n;
   rep(i,0,n)rep(j,0,n){
      dist[i][j]=min({i,n-1-i,j,n-j-1});
   }
   ll res=0;
   rep(_,0,n*n){
      int t; cin>>t; t--;
      res+=dist[t/n][t%n]; cnt[t/n][t%n]++;
      queue<pair<int,int>> que; que.push({t/n,t%n});
      while(!que.empty()){
         auto [x,y]=que.front(); que.pop();
         if(x and chmin(dist[x-1][y],dist[x][y]+1-cnt[x][y])){
            que.push({x-1,y});
         }
         if(y and chmin(dist[x][y-1],dist[x][y]+1-cnt[x][y])){
            que.push({x,y-1});
         }
         if(x+1<n and chmin(dist[x+1][y],dist[x][y]+1-cnt[x][y])){
            que.push({x+1,y});
         }
         if(y+1<n and chmin(dist[x][y+1],dist[x][y]+1-cnt[x][y])){
            que.push({x,y+1});
         }
      }
   }
   cout<<res<<endl;
   return 0;
}

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

#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repA(i, a, n) for(int i = a; i <= (n); ++i)
#define repD(i, a, n) for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof (a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);
	int x; cin >> x;
	int g = __gcd(x,360);
	cout << 360/g << endl;
	return 0;
}



Measured memory_bytes: 3644
#include <cassert>
#include <algorithm>
#include <cctype>
#include <complex>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <tuple>
#include <typeinfo>
#include <unordered_set>
#include <unordered_map>
#include <vector>
using namespace std;

#define REP2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define REPD(i, n) for(int i = (int)(n) - 1; i >= 0; i--)
#define REP(i, n) REP2(i, 0, n)
#define ALL(c) (c).begin(), (c).end()
#define PB(e) push_back(e)
#define FOREACH(i, c) for(auto i = (c).begin(); i != (c).end(); ++i)
#define MP(a, b) make_pair(a, b)
#define BIT(n, m) (((n) >> (m)) & 1)

typedef long long ll;

template <typename S, typename T> ostream &operator<<(ostream &out, const pair<S, T> &p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

template <typename T> ostream &operator<<(ostream &out, const vector<T> &v) {
  out << "[";
  REP(i, v.size()){
    if (i > 0) out << ", ";
    out << v[i];
  }
  out << "]";
  return out;
}

int main(int argc, char *argv[])
{
  int a, b;
  cin >> a >> b;
  if (a + b == 15) {
    cout << "+" << endl;
  } else if (a * b == 15) {
    cout << "*" << endl;
  } else {
    cout << "x" << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#![allow(non_snake_case)]
use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let n :i64 = read();
    let mut ans = 0;
    // nは入らないから入れなくてよい
    for i in 1..n {
        let mul = n / i;
        ans += mul;
        let surplus = n % i;
        if surplus == 0 {
            ans -= 1;
        }
    }
    print!("{}", ans);
}


Measured memory_bytes: 2156
import java.util.Scanner;
import java.util.stream.IntStream;

class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = in.nextInt(),
                M = in.nextInt(),
                Q = in.nextInt();

        // memo
        int[][] train = new int[N + 1][N + 1];

        // input
        while (M-- > 0) ++train[in.nextInt()][in.nextInt()];

        // prefix sum
        int[][] psum = new int[N + 1][N + 1];

        // horizontal sum
        IntStream.rangeClosed(1, N).parallel().forEach(l ->
                IntStream.rangeClosed(1, N).forEach(r ->
                        psum[l][r] = psum[l][r - 1] + train[l][r]
                ));

        // 2D sum
        IntStream.rangeClosed(1, N).forEach(l ->
                IntStream.rangeClosed(1, N).parallel().forEach(r ->
                        psum[l][r] += psum[l - 1][r]
                ));

        // answering queries
        while (Q-- > 0) {
            int l = in.nextInt() - 1,
                    r = in.nextInt();
            System.out.println(
                    psum[r][r] + psum[l][l] - psum[l][r] - psum[r][l]
            );
        }

    }
}

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

int main(void) {
  int x[5];
  for (int i = 0; i < 5; i++) {
    scanf("%d", &x[i]);
  }
  for (int i = 0; i < 5; i++) {
    if (x[i] == 0) {
      printf("%d\n", i + 1);
    }
  }
  return 0;
}

Measured memory_bytes: 1732
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <string.h>
#include <vector>
#include <queue>
#include <cmath>
#include <bitset>
#include <complex>
#include <functional>
#include <numeric>
#include <iomanip>

// output
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline T chmax(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T chmin(T& a, const T b) { return a = (a > b) ? b : a; }

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT { 
  INIT(){cin.tie(0); ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
}}INIT;

int A, B;
string solve(){
  if(A <= 0 && 0 <= B) return "Zero";
  if(0 < A) return "Positive";
  int cnt = B-A+1;
  if(cnt%2 == 0) return "Positive";
  return "Negative";
}

signed main() {
  cin >> A >> B;

  cout << solve() << "\n";

  return 0;
}

Measured memory_bytes: 256
a,b = map(int,input().split())
ans = 0
if a > b:
    ans += a
    a -= 1
    if a > b:
        ans += a
    else:
        ans += b
else:
    ans += b
    b -= 1
    if a > b:
        ans += a
    else:
        ans += b
print(ans)

Measured memory_bytes: 38256
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <math.h>
#include <vector>
#include <map>
#include <queue>
#include <utility>
#include <algorithm>
#include <string>
#include <set>
#include <random>
using namespace std;

int main() {
  int M1, D1, M2, D2;
  cin >> M1 >> D1 >> M2 >> D2;
  cout << (M1 != M2) << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
 
int main() {
  int n;
  cin >> n;
 
  ll r = 0;
  vector<ll> v(n + 1);
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j += i) v[j]++;
    r += i * v[i];
  }
  cout << r << endl;
}

Measured memory_bytes: 81400
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
const ll INF = 1e18;
#define print(x) cout << (x) << endl;
#define rep(i,m,n) for(ll i = (m); i < (n); i++)

ll LCM(ll a, ll b) {
    ll x = a*b;
    ll tmp;
    if (a < b) {
        tmp = a;
        a = b;
        b = tmp;
    }
    ll r = a % b;
    while(r != 0){
    a = b;
    b = r;
    r = a % b;
    }
    return x/b;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll A,B,C,D,answer;
    cin >> A >> B >> C >> D;
    A--;
    cout << (B - B/C - B/D + B/LCM(C,D)) - (A - A/C - A/D + A/LCM(C,D)) << endl;
}

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

int main()
{
  int N;
  cin >> N;
  int X = N/1.08;
  if (N == static_cast<int>(1.08*X))
    cout << X;
  else if (N == static_cast<int>(1.08*(X + 1)))
    cout << X + 1;
  else
    cout << ":(";
  return 0;
}


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

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

	int ans = 1;
	if( n == 0 || n == 1 ) {
	}
	else {
		vector<int> dp( n + 1 );
		dp[0] = 1;
		dp[1] = 1;
		for( int i = 2; i < n + 1; i++ ) {
			dp[i] = dp[i - 1] + dp[i - 2];
		}
		ans = dp[n];
	}

	cout << ans << endl;
}



Measured memory_bytes: 3104