akhauriyash/Code-Regression huggingface dataset:
N, T = map(int, input().split())
t = list(map(int, input().split()))

ans = 0
for i in range(N - 1):
    if t[i + 1] - t[i] >= T:
        ans += T
    else:
        ans += (t[i + 1] - t[i])
    
print(ans + T)

Measured memory_bytes: 26836
#include <bits/stdc++.h>
const int INF = 1e9;
const int MOD = 1e9+7;
const long long LINF = 1e18;
#define dump(x)  cout << 'x' << ' = ' << (x) << ` `;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
#define FOREACH(x,a) for(auto& (x) : (a) )
typedef long long ll;
using namespace std;
typedef pair<ll, ll> P;

template<typename T> 
void print(vector<T> &x) {
    int n = x.size();
    REP(i,n) {
        if (i != n-1) cout << x[i] << " ";
        else cout << x[i] << endl;
    }
}

int main(int argc, char const *argv[]) {
    int n;
    cin >> n;
    vector<ll> t(n+2,0), a(n+2,0), dp(n+2, -1);
    for (int i = 1; i <= n; ++i) cin >> t[i];
    for (int i = 1; i <= n; ++i) cin >> a[i];
    if (n == 1) {
        if (t[1] != a[1]) {
            cout << 0 << endl;
            return 0;
        }
    }

    for (int i = 0; i <= n; ++i) {
        if (t[i] != t[i+1]) {
            if (t[i+1] <= a[i+1]) {
                dp[i+1] = 1;
            }
            else dp[i+1] = 0;
        }
    }

    // print(dp);
    for (int i = n+1; i >= 2; --i) {
        if (a[i] != a[i-1]) {
            if (a[i-1] <= t[i-1]) {
                dp[i-1] = 1;
            }
            else dp[i-1] = 0;
        }
    }

    // print(dp);
    for (int i = 1; i <= n; ++i) {
        if (dp[i] == -1) {
            dp[i] = min(a[i],t[i]);
        }
    }

    // print(dp);
    ll ans = 1ll;
    for (int i = 1; i <= n; ++i) {
        ans *= dp[i];
        ans %= MOD;
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 2560
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	int n,p=0; cin>>n;
	double b; cin>>b;
	for(int i=0;i<n;i++){
        double c,d; cin>>c>>d;
        c=sqrt(c*c+d*d);
        if(c<=b)p++;
	} cout<<p;
}


Measured memory_bytes: 3776
//Bismillah hirrahmanir Rahim
        #include<bits/stdc++.h>
        #define ll long long
        #define pb push_back
        #define dbug printf("I am here\n");
        #define Fast ios_base::sync_with_stdio(false); cin.tie(0);
        const int m = 10e6;
        using namespace std;

        int main(){
     long long int a,b,tst,k;
     cin>>k>>a>>b;
    ll x=min(a,b);
    ll y=max(a,b);
     for(int i=x;i<=y;i++){
        if(i%k==0){cout<<"OK"<<endl;
        return 0;
        }
     }
     cout<<"NG"<<endl;
       return 0;
        }


Measured memory_bytes: 3636
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program
{
    static void Main(string[] args)
    {
        var input = Console.ReadLine().Split(" ");
        var N = int.Parse(input[0]);
        var K = int.Parse(input[1]);

        var d = new List<int>();
        var A = new List<List<int>>();
        for (int i = 0; i < K; i++)
        {
            d.Add(int.Parse(Console.ReadLine()));
            var list = new List<int>();
            var line = Console.ReadLine().Split(" ");
            for (int j = 0; j < d[i]; j++)
            {
                list.Add(int.Parse(line[j]));
            };
            A.Add(list);
        }

        var result = new int[N];
        foreach(var item in A)
        {
            foreach(var s in item)
            {
                result[s - 1]++;
            }
        }

        Console.WriteLine(result.Count(x => x == 0));
    }
}


Measured memory_bytes: 28308
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		int m = sc.nextInt();
		int x = sc.nextInt();

		int l = 0;
		int r = 0;
		for (int i = 0; i < m; i++) {
			int a = sc.nextInt();
			if (a < x) {
				l++;
			}
			if (x < a) {
				r++;
			}
		}

		System.out.println(Math.min(l, r));

		sc.close();
	}
}


Measured memory_bytes: 23252
def get_i() #空白区切の入力を数値(整数)の配列で返す
  return gets.chomp.split(" ").map(&:to_i)
end
def get_f() #空白区切の入力を数値(実数)の配列で返す
  return gets.chomp.split(" ").map(&:to_f)
end
def get() #空白区切の入力を文字列の配列で返す
  return gets.chomp.split(" ")
end
def get_nsp() #入力されたものを一文字ずつに区切った文字列の配列で返す
  return gets.chomp.split("")
end
def yn_judge(bool,y="Yes",n="No") #boolに真偽を投げることで、trueならy、falseならnの値を出力する
  return bool ? y : n 
end
def array(size1,init=nil,size2=-1) #size2に二次元配列時の最初の要素数、size1に次の配列の大きさ、initに初期値を投げることでその配列を返す
  if size2==-1
    return Array.new(size1){init}
  else
    return Array.new(size2){Array.new(size1){init}}
  end
end

puts 2*Math::PI*gets.to_i

Measured memory_bytes: 14316
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {

	public static void main(String args[]) {

		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();

		Map<String,Integer> si = new HashMap<String,Integer>();
		for(int i=0;i<s.length();i++) {
			String temp = s.substring(i, i+1);
			if(si.containsKey(temp)) {
				int t = si.get(temp);
				if(t == 2) {
					System.out.println("No");
					return;
				}
				t++;
				si.put(temp, t);
			}else {
				si.put(temp, 1);
			}
		}

		if(si.size() != 2) {
			System.out.println("No");
		}else {
			System.out.println("Yes");
		}
	}
}


Measured memory_bytes: 23252
#include <iostream>
using namespace std;
typedef long long ll;

ll dp[87];

int main() {
	int n;
	cin >> n;
	dp[0] = 2;
	dp[1] = 1;

	for (int i = 2; i <= n; i++) {
		dp[i] = dp[i - 1] + dp[i - 2];
	}

	cout << dp[n] << endl;
}

Measured memory_bytes: 384
/*Hey, What's up?*/

#include<bits/stdc++.h>
using namespace std;
#define pi acos(-1.0)
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
vector<long long>v;
int main()
{
    fastio;
    long long a=0,b=0,c,d,e,f=0,l,g,m,n,k,i,j,t,p,q;
    cin>>a>>b;
    a+=b;
    cout<<(6-a)<<'\n';
    return 0;
}


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

int main(){
  int N, X;
  cin >> N >> X;
  vector<int> L(N);
  for(int i=0; i<N; i++) cin >> L[i];
  vector<int> D(N+1);
  int bounce=1;
  for(int i=1; i<N+1; i++){
    D[i]=D[i-1]+L[i-1];
    if(D[i]<=X) bounce++;
    else break;
  }
  cout << bounce <<endl;
}

Measured memory_bytes: 256
# -*- coding: utf-8 -*-

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

S=list(input())
N=len(S)

cnt=cnta=0
i=0
while i<N:
    if S[i]=='A':
        cnta+=1
    else:
        cnta=0
    if i+2<N:
        if S[i]=='A' and S[i+1]=='B' and S[i+2]=='C':
            cnt+=cnta
            S[i]='B'
            S[i+1]='C'
            S[i+2]='A'
            i+=2
            cnta-=1
            continue
    i+=1
print(cnt)


Measured memory_bytes: 4968
import collections
n=int(input())
List=list(map(int,input().split()))
hashmap=collections.defaultdict(int)
q=int(input())
total=0
for x in List:
    total+=x
    hashmap[x]+=1
for _ in range(q):
    a,b=map(int,input().split())
    
    total=total-hashmap[a]*a+hashmap[a]*b
    hashmap[b]+=hashmap[a]
    hashmap[a]=0
    print(total)

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

int main(void){
  int a,b,c,x;
  cin >> a;
  cin >> b;
  cin >> c;
  if(a>b){
      x = a;
      a = b;
      b = x;
  }
  if(b>c){
      x = b;
      b = c;
      c = x;
  }
  if(a>b){
      x = a;
      a = b;
      b = x;
  }
  cout << a;
  cout << " ";
  cout << b;
  cout << " ";
  cout << c;
  cout << endl;
}


Measured memory_bytes: 3084
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>

char x, y, z;
int main()
{
	x = getchar();
	z = getchar();
	y = getchar();
	if(x > y)
	{
		printf(">");
	}
	else
	{
		if(x == y)
		{
			printf("=");
		}
		else
		printf("<");
	}
	printf("\n");
	return 0;
}

Measured memory_bytes: 128
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<limits>
#include<algorithm>
#include<numeric>
#include<utility>

using ull = unsigned long long int;
using ll = long long int; 

using namespace std;

int main(){
    string s;

    cin >> s;

    if(s=="Sunny") cout << "Cloudy" << endl;
    if(s=="Cloudy") cout << "Rainy" << endl;
    if(s=="Rainy") cout << "Sunny" << endl;
    
    return 0;
}

Measured memory_bytes: 256
n=int(input())  #数値入力 「N」だけの入力のとき
#n=11
l=len(str(n))
if l ==1:
    print(n)
elif l==2:
    print(9)
elif l==3:
    print(9+n-99)
elif l==4:
    print(9+900)
elif l==5:
    print(9+900+n-9999)
elif l==6:
    print(9+900+90000)
    

Measured memory_bytes: 2940
#include<bits/stdc++.h>
#define ll long long
#define N 100009
using namespace std;

int n,A,B,C; ll s[N];
priority_queue<ll,vector<ll>,greater<ll> > Q;
struct node{ int x,y; }a[N];
bool cmp(node u,node v){
	return u.x+v.y>u.y+v.x;
}
int main(){
	scanf("%d%d%d",&A,&B,&C); n=A+B+C;
	int i,x,y,z; ll res=0;
	for (i=1; i<=n; i++){
		scanf("%d%d%d",&x,&y,&z);
		res+=z; x-=z; y-=z;
		a[i].x=x; a[i].y=y;
	}
	sort(a+1,a+n+1,cmp);
	for (i=1; i<=A; i++){
		Q.push(a[i].x); s[i]=s[i-1]+a[i].x;
	}
	for (i=A+1; i<=n; i++){
		Q.push(a[i].x); s[i]=s[i-1]+a[i].x;
		s[i]-=Q.top(); Q.pop();
	}
	while (!Q.empty()) Q.pop();
	ll sum=0,ans=-1000000000000000000ll;
	for (i=n; i>A; i--){
		Q.push(a[i].y); sum+=a[i].y;
		if (i<=n-B){
			sum-=Q.top(); Q.pop();
		}
		if (i<=n-B+1) ans=max(ans,sum+s[i-1]);
	}
	printf("%lld\n",res+ans);
	return 0;
}

Measured memory_bytes: 2936
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

n = i()
h = intl()

cnt = 0
ans = 0
for i in range(1,n):
	if h[i] <= h[i-1] :
		cnt += 1
	else:
		cnt = 0
	ans = max(ans, cnt)
print(ans)


Measured memory_bytes: 20408
#include <stdio.h>

#define size 128
#define ZNA 1
#define ZNB 2
#define ZNC 4
#define ZND 8
#define abssub(a, b) (a > b ? (a-b) : (b-a))

int readint();
inline int readint()
{
    register int t = 0;
    register char c;
    //int minus = 0;
    while((c = getchar()) < '-');
    //if (c == '-') minus = 1, c = getchar();
    c -= '0';
    do
    {
        t = t * 10 + c;
    }while((c = getchar() - '0') >= 0);
    //return minus ? (-t) : t;
    return t;
}

struct point
{
    int x;
    int y;
    int z;
    int f;
};


int main()
{
    int n, x1 = 0, x2 = 100, y1 = 0, y2 = 100, mi, dx, dy, dz;
    struct point point[size];
    n = readint();
    for (int i = 0; i < n; i++)
    {
        point[i].x = readint();
        point[i].y = readint();
        point[i].z = readint();
        point[i].f = 0;
        if (point[i].z)
            mi = i;
    }

    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
    {
        #define pout(i, x) (point[i].x <= x##1 || point[i].x >= x##2)
        if ((pout(i, x) && pout(i, y) && pout(j, x) && pout(j, y)) || point[i].z == 0 || point[j].z == 0)
            continue;
        dx = point[i].x - point[j].x;
        dy = point[i].y - point[j].y;
        dz = point[i].z - point[j].z;
        if (dx > 0)
        {
            if (dy > 0)
            {
                #define test(dx, dy, A, B, C, D) \
                if ((dx) + (dy) == dz) \
                { \
                    point[i].f = point[j].f = ZN##A|ZN##B|ZN##D; \
                } \
                else if ((dx) + (dy) == -dz) \
                { \
                    point[i].f = point[j].f = ZN##B|ZN##C|ZN##D; \
                }
                test(dx, dy, A, B, C, D);
            }
            else
            {
                test(dx, -dy, D, C, B, A);
            }
        }
        else if (dx < 0)
        {
            if (dy > 0)
            {
                test(-dx, dy, B, A, D, C);
            }
            else if (dy < 0)
            {
                test(-dx, -dy, C, D, A, B);
            }
        }
        #define update(i) \
        _update(i, ZNB|ZNC|ZND, x2, y2, min, min) \
        _update(i, ZNA|ZNC|ZND, x1, y2, max, min) \
        _update(i, ZNA|ZNB|ZND, x1, y1, max, max) \
        _update(i, ZNA|ZNB|ZNC, x2, y1, min, max)
        #define _update(i, z, _x, _y, xm, ym) \
        if (point[i].f == (z)) \
            _x = xm(_x, point[i].x), _y = ym(_y, point[i].y);
        #define max(a, b) (a > b ? a : b)
        #define min(a, b) (a < b ? a : b)
        update(i)
        update(j)
    }

    for (int x = x1; x <= x2; x++)
        for (int y = y1; y <= y2; y++)
    {
        int h = abssub(point[mi].x, x) + abssub(point[mi].y, y) + point[mi].z;
        for (int i = 0; i < n; i++)
        {
            int h2 = h - abssub(point[i].x, x) - abssub(point[i].y, y);
            if (h2 < 0) h2 = 0;
            if (point[i].z != h2)
                goto next;
        }
        printf("%d %d %d", x, y, h);
        goto end;
        next:
        continue;
    }
    end:
    return 0;
}


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

int main()
{
	string s,t;
    cin >> s >> t;
    int n = t.size();
    s.push_back(t[n-1]);
    if(s == t)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;
	return 0;
}


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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i <= n; i++)
#define FORR(i, m, n) for(int i = m; i >= n; i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define VSORTR(v) sort(v.rbegin(), v.rend());
#define ALL(v) (v).begin(),(v).end()
 
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vector<ll>>;
using P = pair<ll, ll>;

const string YES = "Yes";
const string NO = "No";
const ll mod = 1e9+7;



int main(){
	cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    
    ll a,b; cin >> a >> b;
    cout << a / __gcd(a,b) * b << endl;

    return 0;
    
}



Measured memory_bytes: 256
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace Program
{
    public class ABC064D
    {
        static class Console_
        {
            private static Queue<string> param = new Queue<string>();
            public static int NextInt() => int.Parse(NextString());
            public static long NextLong() => long.Parse(NextString());
            public static string NextString()
            {
                if (param.Count == 0) foreach (var item in Console.ReadLine().Split(' ')) param.Enqueue(item);
                return param.Dequeue();
            }
        }
        static public void Main(string[] args)
        {
            var N = Console_.NextInt();
            var S = Console_.NextString().ToArray();
            var ary = new int[N + 1];
            var min = 0;
            ary[0] = 0;
            for (int i = 1; i <= N; ++i)
            {
                ary[i] = (i != 0 ? ary[i - 1] : 0) + (S[i - 1] == '(' ? 1 : -1);
                if (min > ary[i])
                {
                    min = ary[i];
                }
            }
            min *= -1;
            for (int i = 0; i < ary[0] + min; ++i)
            {
                Console.Write("(");
            }
            Console.Write(new string(S));
            for (int i = 0; i < ary[N] + min; ++i)
            {
                Console.Write(")");
            }
            Console.WriteLine();
        }
    }
}


Measured memory_bytes: 13396
#include <iostream>
#include <string>
using namespace std;
int main(){
  string s, t;
  cin >> s;
  cin >> t;
  if( s[0] == t[2] && s[1] == t[1] && s[2] == t[0] )
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}

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

int main() 
{ 
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    ll a, b;
    cin >> a >> b;
    if((a+b)%2 == 0) {
        cout << (a+b)/2 << "\n";
    } else {
        cout << "IMPOSSIBLE\n";
    }
    return 0; 
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define MOD 1000000007
//#define MOD 998244353
#define INF 1145141919810893364
typedef pair<int,int> PP;
typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
#define int ll
#define setdouble setprecision
#define REP(i,n) for(int i=0;i<(n);++i)
#define OREP(i,n) for(int i=1;i<=(n);++i)
#define RREP(i,n) for(int i=(n)-1;i>=0;--i)
#define GOODBYE cout << -1 << endl;return 0
#define MM <<" "<<
#define Endl endl

signed main(void){
    int N;
    int Ans=0;
    int A[114514],B[114514];
    cin >> N;
    REP(i,N+1){
        cin >> A[i];
    }
    REP(i,N){
        cin >> B[i];
    }
    
    REP(i,N){
        if(A[i]<=B[i]){
            Ans+=A[i];
            B[i]-=A[i];
            A[i]=0;
        }else{
            Ans+=B[i];
            A[i]-=B[i];
            B[i]=0;
        }
        
        
        if(A[i+1]<=B[i]){
            Ans+=A[i+1];
            B[i]-=A[i+1];
            A[i+1]=0;
        }else{
            Ans+=B[i];
            A[i+1]-=B[i];
            B[i]=0;
        }
    }
    cout << Ans << endl;
    return 0;
}


Measured memory_bytes: 1792
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[][] arr = new int[N][N];
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N; j++) {
                arr[i][j] = 0;
            }
        }
        for(int i = 0; i < N; i++) {
            int a = sc.nextInt();
            int k = sc.nextInt();
            for(int j = 0; j < k; j++) {
                int t = sc.nextInt();
                arr[i][t-1] = 1;
            }
        }
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < N; j++) {
                if(j > 0) System.out.print(" ");
                System.out.print(arr[i][j]);
            }
            System.out.println();
        }
    }
}



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

int main(){
  char a,b;
  int cup[3] = {1,0,0};  

  while(scanf("%c,%c ",&a,&b) != EOF){
    swap( cup[a-'A'],cup[b-'A']);
  }

  for(int i = 0 ; i < 3 ; i++){
    if(cup[i])printf("%c\n",'A'+i);
  }

  return 0;
}

Measured memory_bytes: 1144
import queue 

N = int(input())
graph = [[] for i in range(N)]

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

f = [0]*N
vf = [False] * N
qf = queue.Queue()
qf.put(0)
vf[0] = True

while not qf.empty():
    now = qf.get()
    edge = len(graph[now])
    if not edge == 0:
        for i in range(edge):
            if not vf[graph[now][i]]:
                qf.put(graph[now][i])
                f[graph[now][i]] = f[now]+1
                vf[graph[now][i]] = True

s = [0]*N
vs = [False] * N
qs = queue.Queue()
qs.put(N-1)
vs[N-1] = True

while not qs.empty():
    now = qs.get()
    edge = len(graph[now])
    if not edge == 0:
        for i in range(edge):
            if not vs[graph[now][i]]:
                qs.put(graph[now][i])
                s[graph[now][i]] = s[now]+1
                vs[graph[now][i]] = True

score = 0
for i in range(N):
    if f[i] <= s[i]:
        score += 1
    else:
        score -= 1

if score > 0:
    print('Fennec')
else:
    print('Snuke')

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

typedef double du ;
typedef string st ;
typedef long long ll ;
typedef long long int lli ;

#define vi vector <lli>

ll solve1 (ll , ll , ll ) ;
ll solve2 (ll , ll , ll ) ;

int main ()
{
    lli cost , hy , times ;

    cin >> cost >> hy >> times ;

    //cout << solve1 (cost , hy , times) ;

    cout << solve2 (cost , hy , times) ;
    return 0 ;
}

/*___________________________________________________________________functions___________________________________________________________________________*/

ll solve1 (ll cost , ll hy , ll times )
{
    ll costt = 0 , cnt = 0 ;
    costt = cost ;

    if (cost <= hy)
    {
        cost = 0 ;
        for (;;)
        {
            ++ cnt ;
            cost += costt ;
            if (cost >= hy)
                break ;
            if (cnt == times)
                break ;
        }
    }
    return cnt ;
}

ll solve2 (lli cost , lli hy , lli times )
{
    lli n ;

    n = hy / cost ;

    return min (n , times ) ;
}




Measured memory_bytes: 256
import java.util.*;
 
public class Main{
	public static int[] p = new int[90001]; //累積和
	
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int H = sc.nextInt();
		int W = sc.nextInt();
		int D = sc.nextInt();
		int[] x = new int[H*W+1];
		int[] y = new int[H*W+1];
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				int a = sc.nextInt();
				x[a] = j;
				y[a] = i;
			}
		}		
		for(int i=D+1;i<=H*W;++i){
			p[i] = p[i-D] + dist(x[i],y[i],x[i-D],y[i-D]);
		}
		
		int Q = sc.nextInt();
		for(int i=0;i<Q;++i){
			System.out.println(-1*p[sc.nextInt()]+p[sc.nextInt()]);
		}
		return;
	}
	
	public static int dist(int x1,int y1,int x2, int y2){
		return Math.abs(x2-x1)+Math.abs(y2-y1);
	}
}

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

using ll = long long;
using pii  = pair<int, int>;
using pll = pair<ll, ll>;
#define ull unsigned long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define vc vector<char>
#define vs vector<string>
#define vpii vector<pii>
#define vpll vector<pll>

#define rep(i, n) for (int i = 0, i##_len = (n); i < i##_len; i++)
#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define repr(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rep1r(i, n) for (int i = ((int)(n)); i >= 1; i--)

#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define RSORT(x) sort(rall(x));
#define pb push_back
#define mp make_pair

#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)

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

int main(){
    int x;
    cin >> x;
    while (x > 1) {
        int t = x;
        vi prime;
        for (int i=2; i*i<=t; ++i) {
            if (t%i==0) prime.pb(0);
            while (t%i==0) {
                t /= i;
                prime.back()++;
            }
        }
        if (t > 1) prime.pb(1);
        int val = prime.front();
        bool same = (val > 1);
        rep1(i, sz(prime)-1) {
            if (prime[i] != val) same = false;
        }
        if ((sz(prime)==1&&prime.back()>1) || (sz(prime)>1&&same)) {
            cout << x << endl;
            return 0;
        }
        --x;
    }
    cout << 1 << endl;
    return 0;
}


Measured memory_bytes: 3608
/*==============================================*\
ID:          shahidul_brur
 
Name:     Md. Shahidul Islam
Study:      CSE, BRUR
Address:  Rangpur, Bangladesh
 
 mail: shahidul.cse.brur@gmail.com
 FB  : fb.com/shahidul.brur
 Blog: shahidul-brur.blogspot.com(in Bengali),
       shahidul-brur-en.blogspot.com(in English)
\*===============================================*/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp> // Common file
//#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update
//using namespace __gnu_pbds;
using namespace std;
//typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
typedef pair<int, ll> il;
typedef vector<ii> vii;
typedef vector<il> vil;
typedef vector<li> vli;
 
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define sz size()
#define all(a)  a.begin(), a.end()
#define mem(a, b)     memset(a, b, sizeof(a))
#define f0(i,b) for(int i=0;i<(b);i++)
#define f1(i,b) for(int i=1;i<=(b);i++)
#define f2(i,a,b) for(int i=(a);i<=(b);i++)
#define fr(i,b,a) for(int i=(b);i>=(a);i--)
#define rep(i,a,b,c) for(int i=(a);i!=(b);i+=(c))
 
int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1,-1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};

const double pi = acos(-1.0);
const double eps = 1e-6;
const int mod = (int)1e9+7;
const int maxn = (int)2e5+5;
const int logn = 20;

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n,d;
    cin>>n>>d;
    int r = n/(2*d+1) + (n%(2*d+1)>0);
    cout << r << "\n";
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
bool bo[100];
#define ll long long int
int main()
{
	string s;
	cin>>s;
	ll l=(int)s.size();
	if(s[0]==s[l-1])
	{
		if(l%2==0)
		{
			cout<<"First"<<endl;
		}
		else
		{
			cout<<"Second"<<endl;
		}
	}
	else
	{
		if(l%2!=0)
		{
			cout<<"First"<<endl;
		}
		else
		{
			cout<<"Second"<<endl;
		}
	}
}


Measured memory_bytes: 512
#include <iostream>

using namespace std;

int main() {
    
   int n;
   cin>>n;
   int a[10004]={0};
   for(int i=1;i<101;i++)
   {
       for(int j=1;j<101;j++)
       {
           for(int k=1;k<101;k++)
           {
               if((i*i+j*j+k*k+i*j+j*k+k*i)<=10000)
               {
                   int z=i*i+j*j+k*k+i*j+j*k+k*i;
                   a[z]++;
               }
           }
       }
   }
    for(int i=1;i<=n;i++)
    {
        cout<<a[i]<<endl;
    }
    
    
}

Measured memory_bytes: 3672
x, y, z = input().split()
a = "No"
if x != y:
    if x == z:
        a = "Yes"
    elif y == z:
        a = "Yes"
    else:
        a = "No"
if x == y:
    if x == z:
        a = "No"
    elif y == z:
        a = "No"
    else:
        a = "Yes"
print(a)

Measured memory_bytes: 3060
import java.util.Scanner;

class Main{


	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);			//文字の入力
		int x =sc.nextInt();
		int a = sc.nextInt();
		int b = sc.nextInt();
	if(Math.abs(a-x) > Math.abs(b-x)){
		System.out.println("B");
	}else{
		System.out.println("A");

	}

	}

}

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

#define int long long
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define allrev(x) (x).rbegin(), (x).end()
#define pb push_back
#define pf push_front
#define fori(i, a, n) for(int i = a; i < n; ++i)
#define MAX(x) (*max_element(all(x)))
#define MIN(x) (*min_element(all(x)))
#define sortarr(a,n) sort(a, a+n)
#define sortvec(V) sort(all(V))

typedef vector<int> V;
typedef pair<int , int> pairi;
typedef set<int> seti;
typedef deque<int> deqi;

const int MOD = 1e9+7;
const int INF = 1e9+8;
const double pi = 3.14159265359;

template <class T>
T fac(T n)      // template function to find factorial of a number
{ 
    T res = 1, i; 
    for (i = 2; i <= n; i++) 
        res *= i; 
    return res; 
}
template<class T>
T lcm(T a, T b)     //template function to find lcm of two numbers 
{
    return (a * b) / __gcd(a,b);
}
template<class T>
T digitsum(T x)
{
    T sum = 0;
    while(x)
    {
        sum += x%10;
        x /= 10;
    }
    return sum;
}
// ternary search
/*double ternary(double l, double r){
    double eps = 1e-9;
    while(r - l > eps){
        double m1 = l + (r - l)/3;
        double m2 = r - (r - l)/3;
        double f1 = f(m1);
        double f2 = f(m2);
        if(f1 < f2)
            l = m1;
        else r = m2;
    }
    return f(l);
}*/
void nPermute(string str, int n)  //function to find nth  lexi string
{ 
    sort(all(str));
    int i = 1; 
    do {
        if (i == n) 
            break; 
  
        i++; 
    } while (next_permutation(str.begin(), str.end())); 
    cout<<str<<endl;
} 
vector<int> factors(int n) 
{ 
    vector<int> f; 
    for(int x = 2; x * x <= n; x++) { 
        while (n % x == 0) { 
            f.push_back(x); 
            n /= x; 
        } 
    } 
    if (n > 1) f.push_back(n); 
    return f; 
}
bool prime(int n) 
{ 
    if (n < 2)
        return false;
    for(int x = 2; x*x <= n; x++) 
        if (n%x == 0) 
            return false; 
    return true; 
}
// binomial coeficient in O(K) 
int ncr(int n, int k){
    int res = 1;
    if(k > n - k)
        k = n - k;
    for(int i = 0; i < k; i++){
        res *= (n - i);
        res /= (i + 1);
    }
    return res;
}
tuple<int, int, int> Gcd(int a, int b)      //extended Euclids Algorithm
{
    if(b == 0)
        return {1, 0, a};
    else{
        int x, y, g;
        tie(x, y, g) = Gcd(b, a%b);
        return {y, x - (a/b)*y, g};
    }
}
template<class T>       // modular Exponentiation x^n mod m = x^(n mod (m - 1)) mod m : if n  is very large;
T binpow(T a, T b, T m) {
    a %= m;
    T res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a % m;
        a = a * a % m;
        b >>= 1;
    }
    return res;
}
void solve()
{
    char c;
    cin >> c;
    if(isupper(c))  cout << "A" << endl;
    else cout <<"a"<<endl;
}
signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    solve();
    cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
}

Measured memory_bytes: 3792
//51
#include<iostream>
#include<numeric>
#include<algorithm>

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    int d[10][10];
    for(int i=0;i<10;i++){
      for(int j=0;j<10;j++){
	d[i][j]=(i==j)?0:1<<29;
      }
    }
    int tn=0;
    while(n--){
      int a,b,c;
      cin>>a>>b>>c;
      tn=max(tn,max(a,b));
      d[a][b]=d[b][a]=c;
    }
    for(int i=0;i<10;i++){
      for(int j=0;j<10;j++){
	for(int k=0;k<10;k++){
	  d[j][k]=min(d[j][k],d[j][i]+d[i][k]);
	}
      }
    }
    int m=1<<30;
    int t;
    for(int i=0;i<tn+1;i++){
      int s=accumulate(d[i],d[i]+tn+1,0);
      if(s<m){
	m=s;
	t=i;
      }
    }
    cout<<t<<' '<<m<<endl;
  }
  return 0;
}

Measured memory_bytes: 1160
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <list>
#include <set>
#include <numeric>

#include <map>
#include <math.h>
#include <iomanip>
#include <stack>
#include <queue>
#include <bitset>
#include <math.h>

#define INF 100100100

typedef long long int llint;

using namespace std;

typedef pair<int, int>Pii;

#define pi 3.141592653589793
#define mod 1000000007

string abc = "abcdefghijklmnopqrstuvwxyz";

int main() {
	string s;
	cin >> s;
	for (int i = 0; i < abc.size(); i++) {
		bool flag = true;
		for (int j = 0; j < s.size(); j++) {
			if (abc[i] == s[j]) flag = false;
		}
		if (flag) {
			cout << abc[i] << endl;
			return 0;
		}
	}
	puts("None");
	return 0;
}

Measured memory_bytes: 512
#include <bits/stdc++.h>
using namespace std;
int main(){
    double n; cin>>n;
    for(int i=1;i<=3500;i++){
        for(int j=1;j<=3500;j++){
            double m = 4*i*j-(n*i+n*j);
            if(m<=0)
                continue;
            double w = n*i*j/m;
            if(ceil(w) == floor(w)&&w>=1&&w<=3500){
                cout<<i<<" "<<j<<" "<<w;
                return 0;
            }
        } 
    }
}

Measured memory_bytes: 256
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	var top, bottom, front, back, left, right int
	var command string
	fmt.Scan(&top, &front, &right, &left, &back, &bottom, &command)

	for i := 0; i < len(command); i++ {
		switch string(command[i]) {
		case "S":
			top, front, bottom, back = back, top, front, bottom
		case "N":
			top, front, bottom, back = front, bottom, back, top
		case "E":
			top, left, bottom, right = left, bottom, right, top
		case "W":
			top, left, bottom, right = right, top, left, bottom
		}
	}
	fmt.Println(top)
}

var reader = bufio.NewScanner(os.Stdin)

func read() string {
	reader.Scan()
	return reader.Text()
}



Measured memory_bytes: 1152
/****************************************************************************
Is this the real life? Is this just fantasy?
Caught in a landslide, no escape from reality
Open your eyes, look up to the skies and see
I'm just a poor boy, I need no sympathy
Because I'm easy come, easy go, little high, little low
Any way the wind blows doesn't really matter to me, to me!!!
--------------------------------------------------
CP is quite cool.It becomes cooler when you enjoy learning new things
******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define mp make_pair
#define endl "\n"
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,a,b) for(int i=a;i<=b;i++)
#define pool(i,a,b) for(int i=a;i>=b;i--)
#define pi pair<int,int>
#define pll pair<ll,ll>
#define vi vector<int>
#define vpi vector<pair<int,int>>
#define vll vector<ll>
#define vb vector<bool>
#define all(x) x.begin(),x.end()
#define sz(a) a.size()
#define ff first
#define ss second
#define debug(x) cout<<#x<<" = "<<x<<endl;
const int mod=1e9+7;

void solve()
{
  int n;
  cin>>n;
  double dp[n+1][n+1],a[n+1],ans=0.0;
  memset(dp,0.0,sizeof(dp));
  
  rep(i,n+1)
    cin>>a[i+1];
  dp[0][0]=1.0;
  loop(i,1,n)
  {
    loop(j,0,i)
    {
      if(j!=0)
        dp[i][j]=dp[i-1][j-1]*a[i]+dp[i-1][j]*(1-a[i]);
      else
        dp[i][j]=dp[i-1][j]*(1-a[i]);
    }
  }


  loop(i,n/2+1,n)
  {
    ans+=dp[n][i];
  }

  printf("%.14f\n",ans);

} 

int main()
{

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

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

  int t=1;
  // cin>>t;
  while(t--)
  {
    solve();
  }

  return 0;
}


Measured memory_bytes: 74092
#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const int INF=1001001001;
const int mod=1e9+7;

int main(){
  int N;
  cin>>N;
  vector<int64_t>A(N);
  for(int i=0;i<N;i++){
    cin>>A[i];
  }
  sort(A.begin(),A.end());
  vector<int64_t>r(N),l(N);
  for(int i=0;i<N-1;i++){
    if(i%2==0){l[i+1]++;l[i]--;}
    else{l[i]++;l[i+1]--;}
  }
  for(int i=0;i<N-1;i++){
    if(i%2==0){r[i]++;r[i+1]--;}
    else{r[i]--;r[i+1]++;}
  }
  sort(l.begin(),l.end());
  sort(r.begin(),r.end());
  int64_t suml=0,sumr=0;
  for(int i=0;i<N;i++){
    suml+=(A[i]*l[i]);
    sumr+=(A[i]*r[i]);
  }
  cout<<max(suml,sumr)<<endl;
  return 0;                       
}

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

int main() {
int N;
cin>>N;
int C,D;
bool singi=0;
  
  for(int c=0;c<=N;c++){
    for(int d=0;d<=N;d++){
        int total =4*c+7*d; //合計
        if (total==N)singi++;
      }
    }
  if(singi==true){
   cout<<"Yes"<<endl;
  }
  else{
  cout<<"No"<<endl;}
        
}

Measured memory_bytes: 256
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial
from heapq import heappop, heappush, heappushpop
inf=float('inf')
mod = 10**9+7
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    A, B = ST().split()
    if ord(A) > ord(B):
        print(">")
    elif ord(A) < ord(B):
        print("<")
    else:
        print("=")
if __name__ == '__main__':
    main()

Measured memory_bytes: 38256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MOD_BY 1000000009ULL

typedef unsigned long long ull;

typedef struct {
	int x;
	ull y;
} xy_t;

ull matrix[64][75][75];

void matrix_mul(ull in1[75][75],ull in2[75][75],ull out[75][75],int n) {
	ull result[75][75];
	int i,j,k;
	for(i=0;i<n;i++) {
		for(j=0;j<n;j++) {
			result[i][j]=0;
			for(k=0;k<n;k++) {
				result[i][j]+=(in1[i][k]*in2[k][j])%MOD_BY;
			}
			result[i][j]%=MOD_BY;
		}
	}
	memcpy(out,result,sizeof(result));
}

void matvec_mul(ull mat[75][75],ull vec[75],ull out[75],int n) {
	ull result[75];
	int i,j;
	for(i=0;i<n;i++) {
		result[i]=0;
		for(j=0;j<n;j++) {
			result[i]+=(mat[i][j]*vec[j])%MOD_BY;
		}
		result[i]%=MOD_BY;
	}
	memcpy(out,result,sizeof(result));
}

void make_matrix(int n) {
	int i;
	for(i=0;i<n;i++) {
		if(i>0)matrix[0][i][i-1]=1;
		matrix[0][i][i]=1;
		if(i<n-1)matrix[0][i][i+1]=1;
	}
	for(i=1;i<64;i++)matrix_mul(matrix[i-1],matrix[i-1],matrix[i],n);
}

void matrix_kaizyo(ull out[75][75],int n,ull kai) {
	int i;
	for(i=0;i<n;i++)out[i][i]=1;
	for(i=0;i<64;i++) {
		if(kai & (1ULL<<i))matrix_mul(matrix[i],out,out,n);
	}
}

/* syo-zyun */
int qsort_comp(const void* x,const void* y) {
	const xy_t* a=(const xy_t*)x;
	const xy_t* b=(const xy_t*)y;
	if((a->y)>(b->y))return 1;
	if((a->y)<(b->y))return -1;
	if((a->x)>(b->x))return 1;
	if((a->x)<(b->x))return -1;
	return 0;
}

int main(void) {
	int case_num;
	int w,n;
	ull h;
	int i;
	ull now[75];
	ull now_pos;
	xy_t noentry[30];
	ull now_matrix[75][75];
	for(case_num=1;;case_num++) {
		scanf("%d%llu%d",&w,&h,&n);
		if(w==0 && h==0 && n==0)break;
		for(i=0;i<n;i++)scanf("%d%llu",&noentry[i].x,&noentry[i].y);
		qsort(noentry,n,sizeof(xy_t),qsort_comp);
		make_matrix(w);
		now_pos=1;
		memset(now,0,sizeof(now));
		now[0]=1;
		for(i=0;i<n;i++) {
			if(noentry[i].y>now_pos) {
				memset(now_matrix,0,sizeof(now_matrix));
				matrix_kaizyo(now_matrix,w,noentry[i].y-now_pos);
				matvec_mul(now_matrix,now,now,w);
				now_pos=noentry[i].y;
			}
			now[noentry[i].x-1]=0;
		}
		if(now_pos<h) {
			memset(now_matrix,0,sizeof(now_matrix));
			matrix_kaizyo(now_matrix,w,h-now_pos);
			matvec_mul(now_matrix,now,now,w);
		}
		printf("Case %d: %llu\n",case_num,now[w-1]);
	}
	return 0;
}

Measured memory_bytes: 3324
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ll a,b,k;
  cin >> a >> b >> k;
  if(a > 0) {
    if(a >= k) {
      a -= k;
    }
    else {
      k -= a;
      a = 0;
      if(b >= k) b -= k;
      else b = 0;
    }
  }
  else {
    if(b >= k) b -= k;
      else b = 0;
  }
  cout << a << " " << b << endl;
  return 0;
}

Measured memory_bytes: 256
#![allow(non_snake_case)]

fn read_line() -> String {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

fn main() {
    let mut S: Vec<char> = read_line().chars().collect();
    S.sort();
    if S[0] == S[1] && S[1] != S[2] && S[2] == S[3] {
        println!("Yes");
    } else {
        println!("No");
    }
}


Measured memory_bytes: 4352
#include<iostream>
#include<string>
#include<vector>
#include<cstdio>
#include<sstream>
#include<algorithm>
#include<cmath>
#include<map>
#include<functional>
using namespace std;
int stoi(string x){stringstream ss;ss<<x;int tmp;ss>>tmp;return tmp;}
string itos(int x){stringstream ss;ss<<x;return ss.str();}
int main(){
int mtt;cin>>mtt;
for(int mttt=0;mttt<mtt;mttt++){
	string n,a,b;
	cin>>n;
	sort(n.begin(),n.end());
	a=n;
	sort(n.begin(),n.end(),greater<int>());
	b=n;
	cout<<stoi(b)-stoi(a)<<endl;
}
	return 0;
}

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

#define dbg(x) cerr << #x"=" << x << endl;
typedef long long LL;
char maze[450][450];
bool has[450][450];
int h, w;
LL ans, x, y;
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

void dfs(int sx, int sy, int fa){
    has[sx][sy] = true;
    if(maze[sx][sy] == '.') ++x;
    else ++y;
    for(int i = 0; i < 4; ++i){
        int xx = sx+dir[i][0];
        int yy = sy+dir[i][1];
        if(xx >= 0 && xx < h && yy >= 0 && yy < w && maze[xx][yy] != fa && !has[xx][yy]){
            dfs(xx, yy, maze[xx][yy]);
        }
    }
}

int main() {
	//freopen("in.txt", "r" , stdin);
    ios::sync_with_stdio(0); cin.tie(0);
    while(cin >> h >> w){
        for(int i = 0; i < h; ++i){
            cin >> maze[i];
        }
        ans = 0;
        memset(has, false, sizeof has);
        for(int i = 0; i < h; ++i){
            for(int j = 0; j < w; ++j){
                x = 0; y = 0;
                if(!has[i][j]){
                    dfs(i, j, maze[i][j]);
                }
                ans += x*y;
            }
        }
        cout << ans << endl;
    }
	return 0;
}


Measured memory_bytes: 8192
#include <iostream>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <string>
#include <map>
using namespace std;
int main() {
	int N,cnt = 0,a;
	bool flag = false;
	cin >> N;
	for (int i = 1; i <= N;i++) {
		cin >> a;

		if (a == i && flag == false) {
			flag = true;
		}
		else if(flag == true){
			flag = false;
			cnt++;
		}
	}
	if (flag == true) {
		cnt++;
	}
	cout << cnt << endl;

	return 0;
}



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

#define ll long long
#define N 100010
int n, a[N];
ll b[N];

int main() {
	while (scanf("%d", &n) != EOF) {
		for (int i = 1; i <= n; ++i) {
			scanf("%d", a + i);
		}
		b[1] = 0;
		for (int i = 1; i <= n; ++i) {
			b[1] += a[i] * ((i & 1) ? 1 : -1);
		}
		b[n] = 2ll * a[n] - b[1]; 
		for (int i = n - 1; i > 1; --i) {
			b[i] = 2ll * a[i] - b[i + 1];
		}
		for (int i = 1; i <= n; ++i) printf("%lld%c", b[i], " \n"[i == n]);	
	}
	return 0;
}


Measured memory_bytes: 2432
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	static class Edge implements Comparable<Edge>{
		int v1, v2;
		int weight;

		Edge (int v1, int v2, int weight) {
			this.v1 = v1;
			this.v2 = v2;
			this.weight = weight / 100 - 1;
		}

		public int compareTo(Edge o) {
			return this.weight - o.weight;
		}
	}

	static class Vertex {
		Vertex parent;

		Vertex () {
			this.parent = this;
		}
	}

	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int n, m;
		Vertex vertex[];
		Edge edge[];
		int sumWeight;

		n = sc.nextInt();
		while (n != 0) {
			m = sc.nextInt();
			vertex = new Vertex[n];
			edge = new Edge[m];
			sumWeight = 0;
			
			for (int i = 0; i < n; i++) {
				vertex[i] = new Vertex();
			}

			for (int i = 0; i < m; i++) {
				String s[] = sc.next().split(",");
				edge[i] = new Edge(Integer.parseInt(s[0]), Integer.parseInt(s[1]), Integer.parseInt(s[2]));
			}
			Arrays.sort(edge);

			for (int i = 0; i < m; i++) {
				if (find(vertex[edge[i].v1]) != (find(vertex[edge[i].v2]))) {
					sumWeight += edge[i].weight;
					union(vertex[edge[i].v1], vertex[edge[i].v2]);
				}
			}
			System.out.println(sumWeight);
			n = sc.nextInt();
		}
	}

	public static Vertex find(Vertex vertex) {
		if (vertex.parent == vertex) {
			return vertex;
		} else {
			return find(vertex.parent);
		}
	}
	
	public static void union (Vertex v1, Vertex v2) {
		Vertex vertex1 = find(v1);
		Vertex vertex2 = find(v2);
		vertex1.parent = vertex2;
	}
}

Measured memory_bytes: 13000
#include <stdio.h>

int main(){
    int n, i, j, cnt=0, yakusu;

    scanf("%d", &n);

    for(i=1; i<=n; i+=2){
        yakusu=0;
        for(j=1; j<=n; j++){
            if(i%j == 0)
                yakusu++;
        }

        if(yakusu == 8)
            cnt++;
    }

    printf("%d\n", cnt);

    return 0;
}

Measured memory_bytes: 128
#define _CRT_SECURE_NO_WARNINGS
#include"bits/stdc++.h"
#define INF          1e9
#define INFLL        1e18
#define EPS          1e-9
#define MOD          1000000007
#define REP(i,n)     for(ll i=0,i##_len=(n);i<i##_len;++i)
#define REP1(i,n)    for(ll i=1,i##_len=(n);i<=i##_len;++i)
#define REPR(i,n)    for(ll i=(n)-1;i>=0;--i)
#define REPR1(i,n)   for(ll i=(n);i>0;--i)
#define REPC(i,obj)  for(auto i:obj)
#define ALL(obj)     (obj).begin(),(obj).end()
#define SETP(n)      cout<<fixed<<setprecision(n)
#define DELNL        cin.ignore(numeric_limits<streamsize>::max(),'\n')
#define VV(T,h,w)    vector<vector<T>>(h,vector<T>(w))
#define VVI(T,h,w,i) vector<vector<T>>(h,vector<T>(w,i))
using namespace std;
using ll = long long;
template<typename T = ll>inline T in() { T ret; cin >> ret; return ret; }

int main()
{
    string s; cin >> s; sort(ALL(s));
    cout << (s[0] == s[1] && s[1] != s[2] && s[2] == s[3] ? "Yes" : "No") << endl;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int b = sc.nextInt();
		if (a == b) {
			System.out.println("Draw");
		} else if (b == 1) {
			System.out.println("Bob");
		} else if (a == 1) {
			System.out.println("Alice");
		} else if (a > b) {
			System.out.println("Alice");
		} else if (a < b) {
			System.out.println("Bob");

		}
	}
}

Measured memory_bytes: 22740
k,n = map(int, input().split())
lst = list(map(int, input().split()))

maxDis = (k-lst[-1]) + lst[0]
for i in range(n-1):
  if lst[i+1] - lst[i] > maxDis:
    maxDis = lst[i+1] - lst[i]
print(k-maxDis)

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

using namespace std;

const int inf = 1e9;

const int MAXN = 1e5 + 10;

int dp[MAXN + 10];
int h[MAXN + 10];
int main() {
    #ifdef BLAT
        freopen("input", "r", stdin);
    #endif

    dp[1] = 0;
    dp[0] = inf;

    int n;
    cin >> n;

    cin >> h[1];
    for(int i = 2; i <= n; ++i) {
    	cin >> h[i];
    	dp[i] = min(dp[i-1] + abs(h[i] - h[i-1]), dp[i-2] + abs(h[i] - h[i-2]));
    }

    cout << dp[n] << '\n';
}  

Measured memory_bytes: 1024
import itertools

n = int(input())
p = tuple(map(int, input().split()))
q = tuple(map(int, input().split()))

base = list(range(1, n+1))

for i, v in enumerate(itertools.permutations(base, n)):
    if v == p:
        p_id = i+1
    if v == q:
        q_id = i+1

print(abs(p_id - q_id))

Measured memory_bytes: 3060
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>

using namespace std;

struct pos {
    int x, y;
};
const vector<pos> moves{{1,  0},
                        {-1, 0},
                        {0,  1},
                        {0,  -1}};

int h, w;

bool inRange(pos p) {
    return 0 <= p.x && p.x < h && 0 <= p.y && p.y < w;
}

int calc(pos p) {
    return min({p.x, h - p.x - 1, p.y, w - p.y - 1});
}

int main() {
    int k;
    cin >> h >> w >> k;
    pos start;
    vector<vector<bool>> isWall(h, vector<bool>(w));
    for (int i = 0; i < h; i++) {
        string s;
        cin >> s;
        for (int j = 0; j < w; j++) {
            if (s[j] == '#')isWall[i][j] = true;
            if (s[j] == 'S')start = {i, j};
        }
    }

    queue<pos> que;
    que.push(start), que.push({-1, -1});
    int dis = 0;
    int ret = 1145141919;
    while (dis <= k) {
        pos now = que.front();
        que.pop();
        if (now.x < 0) {
            dis++;
            que.push({-1, -1});
        } else {
            int cost = (calc(now) + k - 1) / k;
            ret = min(ret, 1 + cost);

            for (auto move:moves) {
                pos next = {now.x + move.x, now.y + move.y};
                if (inRange(next) && !isWall[next.x][next.y]) {
                    isWall[next.x][next.y] = true;
                    que.push(next);
                }
            }
        }
    }

    cout << ret << endl;
    return 0;
}


Measured memory_bytes: 384