akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include<stdlib.h>
#include <math.h>
using namespace std;
int main() {
    string T;

    long long a,b,c ,k;
    a = 0;
     b = 0;
     c = 0;
     k = 1;
    cin >> T;
    for(int i = 0;i<T.length();i++){
        if(T[i]=='C'){
            c= (c+b);
        }
        if(T[i]=='B'){
            b+=a;
        }
        if(T[i]=='A'){
            a +=k;
        }
        if(T[i]=='?'){
           c = (3*c +b);
           b = 3*b +a;
            a = 3*a + k;
            k = 3*k;
        }
        a = a%1000000007;
        b =b%1000000007;
        c = c%1000000007;
        k = k%1000000007;
       
    }

     cout << c;
        return 0;
}


Measured memory_bytes: 512
import sys
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

def renzoku(n):
    i = 0
    while n - (i+1) >= 0:
        n = n-(i+1)
        i += 1
    return i
      
N = int(input())
if N == 1:
  print("0")
  sys.exit()
oriN = N*1
A = factorization(N)
#print(A)
M = len(A)

ans = 0
for i in range(M):
  temp = A[i][1]
  f = renzoku(temp)
  ans += f
print(ans)

Measured memory_bytes: 63428
<?php

fscanf(STDIN, '%d%d', $a, $b);

if (($a + $b) > ($a - $b)) {
    if (($a + $b) > ($a * $b)) {
        echo ($a + $b);
    } else {
        echo ($a * $b);
    }
} else {
    if (($a - $b) > ($a * $b)) {
        echo ($a - $b);
    } else {
        echo ($a * $b);
    }
}

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

struct peo {
	int no, c;
	peo() { no = 0; c = 0; }
	peo(int _n, int _c) :no(_n), c(_c) {  }
	bool operator<(const peo& p) {
		return c < p.c;
	}
};

int main() {
	int n;
	cin >> n;
	vector<peo> v;
	for (int i = 1; i <= n; i++) {
		int a;
		cin >> a;
		peo p(i, a);
		v.push_back(p);
	}
	sort(v.begin(), v.end());
	cout << v[0].no;
	for (int i = 1; i < n; i++)
		cout << " " << v[i].no;
	cout << endl;

	return 0;
}

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

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

    public static void solve2552() {
        Scanner sc = new Scanner(System.in);
        String left = "qwertasdfgzxcvb";
//        String right = "yuiophjklnm";
        while (true) {
            String string = sc.next();
            if ("#".equals(string)) {
                break;
            }
            String ans = "";
            for (int i = 0; i < string.length(); i++) {
                ans += left.contains(string.substring(i, i + 1)) ? "l" : "r";
            }
            int count = 0;
            char[] charArr = ans.toCharArray();
            for (int j = 1; j < charArr.length; j++) {
                if (charArr[j - 1] != charArr[j]) {
                    count++;
                }
            }
            System.out.println(count);
        }

    }
}


Measured memory_bytes: 29508
def xor_next(values):
    result = [0] * len(values)
    for i in range(len(values)):
        result[i] = values[i] ^ values[(i+1) % len(values)]
    return result


def make_mp_table(values):
    result = [-1] * (len(values) + 1)
    j = -1
    for i in range(len(values)):
        while j != -1 and values[j] != values[i]:
            j = result[j]
        j += 1
        result[i+1] = j
    return result


def mp_find(target, pattern, table):
    result = []
    j = 0
    for i in range(len(target)):
        while(j != -1 and pattern[j] != target[i]):
            j = table[j]
        j += 1
        if j == len(pattern):
            result.append(i-(j-1))
            j = table[j]
    return result


def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = list(map(int, input().split()))

    xa = xor_next(a)
    xb = xor_next(b)
    mp_t = make_mp_table(xa)
    xb += xb
    res = mp_find(xb, xa, mp_t)
    res.sort(reverse=True)
    for i in range(len(res)):
        k = n-res[i]
        if(k >= n):
            continue
        x = a[k] ^ b[0]
        print(str(k) + ' ' + str(x))


main()


Measured memory_bytes: 44408
#include<stdio.h>
int main(){
  int n,m,a,b,i,j,c[50],d[50];
  while(scanf("%d %d",&n,&m),n||m){
    for(i=0;i<n;i++)c[i]=n-i;
    //for(i=0;i<n;i++)printf("%d ",c[i]);printf("\n");
    while(m--){
      scanf("%d %d",&a,&b);
      for(i=0;i<b;i++)d[i]=c[a+i-1];//printf("%d\n",c[a+i-1]);}
      for(j=0;j<a;j++)c[a+b-j-2]=c[a-j-2];//printf("%d\n",c[a+b-j-2]);}
      for(i=0;i<b;i++)c[i]=d[i];
    }
    printf("%d\n",c[0]);
  }
  return 0;
}
      

Measured memory_bytes: 600
r,g,b=map(int,input().split())
k=int(input())
cnt=0
while(True):
    #print(r,g,b)
    if(g>r and b>g):
        break
    else:
        if(g<=r):
            g=g*2
            cnt+=1
        else:
            if(b<=g):
                b=b*2
                cnt+=1
if(cnt<=k):
    print("Yes")
else:
    print("No")

Measured memory_bytes: 9188
#include<iostream>
#include<string>
using namespace std;
int main(){
  string str[9];
  str[0] = ".,!? ";
  str[1] = "abc";
  str[2] = "def";
  str[3] = "ghi";
  str[4] = "jkl";
  str[5] = "mno";
  str[6] = "pqrs";
  str[7] = "tuv";
  str[8] = "wxyz";

  string a;
  int h,t = 0;
  cin >> h;
  while (t < h) {
    cin >> a;
    int i = 0;
    do {
      int n = 0,m = 0;
      while (a[i] != '0') {
        m = a[i] - '0';
        n++;
        i++;
      }
      int j;
      if (m == 7 || m == 9) {
        j = n%4 - 1;
        if (j == -1) {
          j = 3;
        }
        cout << str[m-1][j];
      }
      else if (m == 1) {
        j = n%5 - 1;
        if(j == -1){
          j = 4;
        }
        cout << str[m-1][j];
      }
      else if (m == 0) {

      }
      else{
        j = n%3 - 1;
        if (j == -1) {
          j = 2;
        }
        cout << str[m-1][j];
      }
      if (a[i+1]) {
        i++;
      }
    }while(a[i+1]);
    cout << endl;
    t++;
  }
}

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define rrev(i, m, n) for(int (i)=(n)-1; (i)>=(m); (i)--)
#define erev(i, m, n) for(int (i)=(n); (i)>=(m); (i)--)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T, class S> inline bool minup(T& m, S x){ return m>(T)x ? (m=(T)x, true) : false; }
template<class T, class S> inline bool maxup(T& m, S x){ return m<(T)x ? (m=(T)x, true) : false; }

const int    INF = 1000000000;
const ll     MOD = 1000000007LL;
const double EPS = 1E-12;

const int MAX_M = 9;
const int MAX_N = 10;
const int MAX_S = 100;
const int M = MAX_M;
const int N = MAX_N;
const int S = MAX_S;
ll dp[MAX_N+1][MAX_S+1];
int n, s;

int main()
{
  dp[0][0] = 1LL;
  rep(i, M+1) rev(j, N) rep(k, S-i+1) dp[j+1][k+i] += dp[j][k];

  while((cin >> n >> s) && n){
    cout << dp[n][s] << endl;
  }


  return 0;
}

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

#define rep(i,x,y) for(ll i=(x);i<(y);i++)
#define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--)
#define all(x) (x).begin(),(x).end()
#define itrout(x) for(int i=0;i<x.size();i++) {cout << x[i] << (i==x.size()-1 ? "\n" : " ");}
#ifdef LOCAL
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define debugbit(x, n) cerr << #x << " = " << bitset<n>(x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define itrdebug(x) cerr << #x << " "; for (auto & el : (x)) {cerr << (el) << " ";} cerr << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define debug(x)
#define debugbit(x, n)
#define itrdebug(x)
#define dassert(...)
#endif
//#define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

void solve(long long K){
    ll remain = 0, answer = 0;
    const ll LIMIT = 1e6+1;
    rep(i,0,LIMIT) {
        answer++;
        remain = (remain*10 + 7) % K;
        if (!remain) break;
    }
    if (answer == LIMIT) answer = -1;
    cout << answer << endl;
}

signed main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    long long K;
    scanf("%lld",&K);
    solve(K);
    return 0;
}


Measured memory_bytes: 3756
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
using Map = map<string,ll>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
ll INF=1LL<<60;
ll MOD=1000000007;


int main(){
    ll A;
    cin >> A;
    ll P;
    cin >> P;
    cout<<(3*A+P)/2<<endl;


    return 0;
}


Measured memory_bytes: 256
a, b, c, d = gets.split.map(&:to_i)
mc = (Rational(b, c).floor) - (Rational(a, c).ceil) + 1
md = (Rational(b, d).floor) - (Rational(a, d).ceil) + 1
mcd = (Rational(b, c.lcm(d)).floor) - (Rational(a, c.lcm(d)).ceil) + 1
multiples = mc + md - mcd
puts (b - a + 1 - multiples)

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



int main(){    
    
    int a, b, c;
    cin >> a >> b >> c;
    
    int maximo = 0;
    
    if((a+b+1) >= c){
        maximo = b+c; //comes todas las galletas
    }else{
        maximo = (a+b+b+1);
    }
    
    cout << maximo << endl;
    
    return 0;
    
}


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

int main(){
    int n,T;
    cin>>n>>T;
    long long t[n];
    long long ans=0;
    cin>>t[0];
    for(int i=1;i<n;i++){
        cin>>t[i];
        if(t[i]-t[i-1]<=T) ans+=t[i]-t[i-1];
        else ans+=T;
    }
    cout<<ans+T<<endl;
}

Measured memory_bytes: 1792
#include <iostream>
#include <map>

using namespace std;

const int N = 3;

int main () {
    int a;
    map<int, bool> mp;

    for (int i = 0; i < N; i++) {
        cin >> a;
        if (mp.count(a) == 0) {
            mp[a] = true;
        }
    }

    cout << mp.size() << endl;
}


Measured memory_bytes: 256
n = int(input())
sl = list(input() for _ in range(n))
m = int(input())
tl = list(input() for _ in range(m))

sl_s = list(set(sl))
tl_s = list(set(tl))

ans = 0
for s in sl_s:
    ans = max(ans, sl.count(s) - tl.count(s))

print(ans)

Measured memory_bytes: 9176
#include<cstdio>
#include<functional>
#include<algorithm>
using namespace std;
int main(void)
{
	int x,n,f[102],p,mi,mn,a,i;
	scanf("%d %d",&x,&n);
	for(i=0;i<=101;i++)	f[i]=0;
	for(i=0;i<n;i++)	{
		scanf("%d",&p);
		f[p]=1;
	}
	mn=1000;
	for(i=0;i<=101;i++)	{
		if(f[i]==0)	{
			a=i-x;
			if(a<0)	a=-a;
			if(mn>a)	{
				mn=a;	mi=i;
			}
		}
	}
	printf("%d\n",mi);
	
	return 0;
}

Measured memory_bytes: 1732
#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define rrep(i, n) for (int i = (int)n-1; i >= 0; --i)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;

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

__attribute__ ((constructor))
void init() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed << setprecision(10);
}

signed main(){
  int n;
  cin >> n;
  vector<int> v(n);
  ll ans = 0;
  rep(i, n) cin >> v[i];
  FOR(i, 1,n){
    if(v[i] >= v[i-1])
      continue;
    else
    {
      ans += v[i - 1] - v[i];
      v[i] = v[i - 1];
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3952
mod=1000000007

def P(x):
    ans=1
    for i in range(x):
        ans=ans*(i+1)%mod
    return(ans)

n,m=map(int, input().split())

if abs(n-m)>1:
    print(0)
else:
    if n==m:
          print((P(n)*P(n)*2)%mod)
    else:
        print((P(n)*P(m))%mod)

Measured memory_bytes: 3064
import itertools as itr
n,y = map(int,input().split())

for i in range(0,n  +1):
	for j in range(0,n-i  +1):
		yen = 10000*i + 5000*j + 1000*(n-i-j)
		if y == yen:
			print(i,j,n-i-j)
			exit()
print(-1,-1,-1)


Measured memory_bytes: 40812
import std.stdio;
import std.conv;
import std.string;
import std.algorithm;

void main()
{
    string s;
    while( (s=readln.strip) != "0 0") {
        auto n = map!(to!int)(s.strip.split);
        for(int i=0;i<n[0];i++) {
            for(int j=0;j<n[1];j++) {
                write("#");
            }
            writeln();
        }
        writeln();
    }
}

Measured memory_bytes: 2480
#include <stdio.h>

int main()
{
	int i,j,k,n,m;
	int A[101][101],B[1001],C[1001]={0};
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			scanf("%d",&A[i][j]);
		}
	}
	for(i=0;i<m;i++){
		scanf("%d",&B[i]);
	}
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			C[i] += A[i][j]*B[j];
		}
		printf("%d\n",C[i]);
	}
	return 0;
}

Measured memory_bytes: 648
<?php
list($X, $Y, $Z, $K) = explode(" ", trim(fgets(STDIN)));
$a = explode(" ", trim(fgets(STDIN)));
$b = explode(" ", trim(fgets(STDIN)));
$c = explode(" ", trim(fgets(STDIN)));
rsort($a);
rsort($b);
rsort($c);
foreach($a as $i => $_a){
    foreach($b as $j => $_b){
        if($i*$j > 3000)break;
        $ab[] = $_a + $_b;
    }
}
rsort($ab);
foreach($ab as $i => $_ab){
    if($i == 3000)break;
    foreach($c as $j => $_c){
        if($i*$j > 3000)break;
        $abc[] = $_ab + $_c;
    }
}
rsort($abc);
for($i = 0; $i < $K;$i++){
    echo $abc[$i],"\n";
}

Measured memory_bytes: 6648
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define print(s) cout << s << endl
#define acc(v) accumulate(v.begin(), v.end(), 0)
#define cinv(n,v) rep(i,n) cin >> v[i]
using namespace std;
const int INF = 1e9;
typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> vint;
typedef vector<string> vstr;
typedef vector<char> vchar;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
int x_pos[4] = {1,0,-1,0}, y_pos[4] = {0,1,0,-1};
int ctoi(char c) {
	if (c >= '0' && c <= '9') {
		return c - '0';
	}
	return 0;
}

char upper(char c){
	return c-0x20;
}

char lower(char c){
	return c+0x20;
}

void unique_vector(vector<int>& v){
	sort(all(v));
	v.erase(unique(all(v)), v.end());
}

// n次元配列の初期化。第２引数の型のサイズごとに初期化していく。
template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int main() {
    string s;cin>>s;
    int n = s.size();
    int g=0,p=0;
    rep(i,n){
        if(s[i]=='g')g++;
        else p++;
    }
    cout << (g-p)/2 << endl;
}



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

using Graph = vector<vector<int>>;

vector<long long> counters;
vector<bool> has_visited;
void DFS(const Graph &G, int v) {
  has_visited[v] = true;
  for (auto w: G[v]) {
    if (has_visited[w]) continue;
    else {
      counters[w] += counters[v];
      DFS(G, w);
    }
  }
}

int main() {
  int N, Q; cin >> N >> Q;
  Graph G(N);
  has_visited.resize(N);
  has_visited.assign(N, false);
  counters.resize(N);
  counters.assign(N, 0);
  for (int i = 0; i < N-1; i++) {
    int a, b; cin >> a >> b; a--; b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  for (int i = 0; i < Q; i++) {
    int p; cin >> p; p--;
    long long x; cin >> x;
    counters[p] += x;
  }
  DFS(G, 0);
  for (int i = 0; i < N; i++) {
    if (i != N-1) cout << counters[i] << " ";
    else cout << counters[i] << endl;
  }
  return 0;
}

Measured memory_bytes: 24320
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <string>
#include <tuple>
#include <vector>
#include <map>
#include <unordered_map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <random>
#include <cassert>
using namespace std;
#define LL long long
#define MP(a, b) make_pair(a, b)
#define MMP(a, b, c) make_pair(make_pair(a, b), c)
#define MAX 1000000000
#undef INT_MIN
#undef INT_MAX
#define INT_MIN -2147483648
#define INT_MAX 2147483647
#define LL_MIN -9223372036854775808
#define LL_MAX 9223372036854775807
#define PI 3.14159265359

int main(){
    iostream::sync_with_stdio(false);
    
    int N;
	cin >> N;
	int A[100000];
	A[0] = 0;
	A[N+1] = 0;
	for(int i=1; i<N+1; i++){
		cin >> A[i];
	}
	vector<pair<int,bool>> delta;
	int tmp = 0;
	bool downup = true;
	for(int i=1; i<N+1; i++){
		if(downup == false && tmp < A[i]){//下がってる最中なのにA[i]のほうが大きい
			delta.push_back(MP(tmp, false));
			downup = true;			
		}
		else if(downup == true && tmp > A[i]){
			delta.push_back(MP(tmp, true));
			downup = false;
		}
		tmp = A[i];
	}
	if(downup==true) delta.push_back(MP(tmp, true));
	delta.push_back(MP(0, false));
	sort(delta.begin(), delta.end());
	reverse(delta.begin(), delta.end());

	int ans = 0;
	int now = 0;
	for(int i=0; i<delta.size(); i++){
		if(delta[i].second == true) now++;
		else now--;
		if(i==delta.size()-1 || delta[i].first!=delta[i+1].first) ans = max(ans, now);
	}
	cout << ans << endl;



    return 0;
}



Measured memory_bytes: 4296
/*
AuThOr Gwj
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define KEEP while(1)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
#define debug_pair(A) cerr<<A.FIR<<" "<<A.SEC<<endl;
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
typedef pair<mp,mp> superpair;
int main(){
	fastio;
	int a,b;
	cin>>a>>b;
	cout<<a*b<<endl;
	return 0;
}


Measured memory_bytes: 3668
s=input()[::-1]
de=['maerd','esare']
dr='remaerd'
er='resare'
def check(a,x):
	if a > x:
		print("NO")
		exit(0)
i=0
n=len(s)
while i<n:
	check(i,n-5)
	if s[i:i+5] in de:
		i+=5
		continue
	check(i,n-6)
	if s[i:i+6] == er:
		i+=6
		continue
	check(i,n-7)
	if s[i:i+7]  == dr:
		i+=7
		continue
	print('NO')
	break
else:
	print("YES")
	

Measured memory_bytes: 3188
#include <bits/stdc++.h> 
using namespace std; 
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
struct greateri
{
    template<class T>
    bool operator()(T const &a, T const &b) const { return a > b; }
};
ll dp[200001];
int main()
{
  //flash;
    ll n,x,y;
    cin>>n>>x>>y;
    x--,y--;
    ll ans=0;
    for (ll i = 0; i < n; ++i)
    {
        for (ll j = i+1; j < n; ++j)
        {
            ll shortest = (j-i);
            shortest=min(shortest,abs(i-x)+abs(j-y)+1);
            shortest=min(shortest,abs(i-y)+abs(j-x)+1);

            dp[shortest]++;
        }
    }
    for (ll i = 1; i < n; ++i)
    {
        cout<<dp[i]<<'\n';
    }
  return 0;
}
//code the AC sol !
// BS/queue/map

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

int n;
string s;
double ans, x;

int main(){
  scanf("%d", &n);
  ans = 0;
  for(int i =0 ; i < n; i++){
    cin >> x >> s;
    if(s=="JPY") ans += x;
    else ans += 380000*x;
  }

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

Measured memory_bytes: 256
n = int(input())

tree = []
for i in range(n):
  data = list(map(int,input().split()))
  node = data[0]
  depth = data[1]
  child = []
  parent = -1
  for j in data[2:]:
    child.append(j)
  tree.append([node,depth,child,parent])

tree.sort()

for i in range(len(tree)):
#  print(tree[i])
  for j in tree[i][2]:
    tree[j][3] = i

def rooting(tree,node,depth):
  tree[node][1] = depth
  if len(tree[node][2]) == 0:
    return
  for i in tree[node][2]:
    rooting(tree,i,depth+1)

for i in range(n):
  if tree[i][3] == -1:
    root = i
rooting(tree,root,0)


for i in range(n):
  print("node %d: parent = %d, depth = %d," %(tree[i][0],tree[i][3],tree[i][1]),end=" ")
  if tree[i][3] == -1:
    print("root,",end=" ")
  elif tree[i][3] != -1 and len(tree[i][2]) != 0:
    print("internal node,",end=" ")
  else:
    print("leaf,",end=" ")
  print(tree[i][2])



Measured memory_bytes: 31888
#define _USE_MATH_DEFINES
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
#include<set>
#include<map>
#include<stdbool.h>
#include<math.h>
#include<sstream>
#include<math.h>
#include<vector>
#include<functional>
#include<queue>
#include<utility>
#include<bitset>
typedef long long ll;
#define N (1000000000+7)
#define INF 1e16
using namespace std;
typedef pair<ll, ll> P;

int main(void) {
	string s;
	int n ,k;
	cin >> n >> k;
	cin >> s;
	s[k - 1] = tolower(s[k - 1]);
	cout << s << endl;
	return 0;
}

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

int main()
{
    int N, K, sum = 0;
    cin >> N >> K;
    vector<int> x(N);
    for (int i = 0; i < N; i++)
    {
        cin >> x.at(i);
    }
    for (int i = 0; i < N; i++)
    {
        sum += min(x.at(i), K - x.at(i)) * 2;
    }
    cout << sum << endl;
}

Measured memory_bytes: 256
import java.util.*;

class Main{
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int a=sc.nextInt(),b=sc.nextInt(),c=sc.nextInt();
				
		if(a<b&&b<c)System.out.println("Yes");
		else System.out.println("No");
	
	}
}

Measured memory_bytes: 17160
a, b, c, x = [int(input()) for i in range(4)]
ans = 0
coins = [500, 100, 50]

for i in range(a + 1):
    for j in range(b + 1):
        for k in range(c + 1):
            if 500 * i + 100 * j + 50 * k == x:
                ans += 1
print(ans)

Measured memory_bytes: 3064
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define fo(i, a, b) for (int i=a; i<(b); i++)
#define forev(i, b, a) for (int i = (b)-1; i >= a; i--)
typedef long long int 	lli;
typedef long double 	ld;
#define mp 				make_pair
#define pb 				push_back
#define ff 				first
#define ss 				second
#define lb 				lower_bound
#define ub 				upper_bound
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define endl 			"\n"
#define fast 			ios_base::sync_with_stdio(0); cin.tie(0);
// #define PI 3.1415926535897932384626
// const int MOD = 1000000007;
// const ll INF = 1e18;
// const int MX = 100001;
//==================================================================================

int main() {
	fast;
	// int t; cin >> t; while (t--)
	int x, n; cin >> x >> n;
	if (n == 0) {
		cout << x;
		return 0;
	}
	int hash[1000] = {0};
	int arr[n]; fo(i, 0, n) {
		cin >> arr[i];
		hash[arr[i]] = 1;
	}
	int diff = 999;
	int ans;
	fo(i, 0, 200) {
		if (hash[i] == 1) continue;
		int temp = abs(i - x);
		if (diff > temp) {
			diff = temp;
			ans = i;
		}
	}

	cout << ans;

	return 0;
}

Measured memory_bytes: 3656
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces


b = input()
if b == "A":
    print("T")
elif b == 'T':
    print("A")
elif b == "G":
    print("C")
elif b == "C":
    print("G")

Measured memory_bytes: 3060
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
#define INT(x) int x; scanf("%d",&x)
#define INPUT(x) scanf("%d",&x)
#define REP1(x,n) for(int x = 0; x < n; x++)
#define REP2(x,s,e) for(int x = s; x <= e; x++)
#define RREP1(x,n) for(int x = n-1; x >= 0; x--)
#define RREP2(x,s,e) for(int x = s; x >= e; x--)
#define BR printf("\n")
#define INF 2000000000

typedef long long ll;

int main(){
    ll A[100000];
    INT(N);
    REP1(i,N){
        INT(a);
        A[i] = a;
    }
    ll sum = A[0]-1;
    ll mini = 1;
    
    REP2(i,1,N-1){
        if (A[i] > mini + 1) {
            if (A[i] == mini + 2) {
                sum++;
                A[i] = 1;
            }else if(A[i]%(mini+1) == 0){
                sum++;
                A[i] -= mini+2;
                sum += A[i]/(mini+1);
                A[i] %= mini+1;
                mini = max(mini,A[i]);
            }else{
                sum += A[i]/(mini+1);
                A[i] %= mini+1;
                mini = max(mini,A[i]);
            }
            
        }else{
            mini = max(mini,A[i]);
        }
    }
    cout << sum <<endl;
    return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <functional>
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
using namespace std;
#define ll long long int
#define rep(i,n) for( int i = 0; i < n; i++ )
#define rrep(i,n) for( int i = n; i >= 0; i-- )
#define REP(i,s,t) for( int i = s; i <= t; i++ )
#define RREP(i,s,t) for( int i = s; i >= t; i-- )
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

signed main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int h, w;
    cin >> h >> w;
    if(h % 3 == 0 || w % 3 == 0) {
        cout << 0 << endl;
        return 0;
    }
    int ans = INF2;
    int loop = 2;
    while(loop--) {
        REP(i, 1, w - 1) {
            int S1 = h * i;
            int S2 = h / 2 * (w - i);
            int S3 = (h + 1) / 2 * (w - i);
            ans = min(ans, max({S1, S2, S3}) -  min({S1, S2, S3}));
            //cout << S1 <<" " << S2 <<  " " << S3 << endl;
        }
        ans = min(ans, (h + 2)/3 * w - h/3 * w);
        swap(h, w);
    }
    cout << ans << endl;;

    return 0;
}


Measured memory_bytes: 384
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <cfloat>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <iostream>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

int H,W,N;
int** table;
int** dp;

void func(){

	for(int i = 0; i < H; i++){
		for(int k = 0; k < W; k++){
			scanf("%d",&table[i][k]);
			dp[i][k] = 0;
		}
	}

	dp[0][0] = N-1;
	for(int i = 1; i < W; i++){
		if(dp[0][i-1]%2 == 0){
			dp[0][i] = dp[0][i-1]/2;
		}else{
			if(table[0][i-1] == 1){
				dp[0][i] = (dp[0][i-1]+1)/2;
			}else{
				dp[0][i] = (dp[0][i-1]-1)/2;
			}
		}
	}

	for(int row = 1; row < H; row++){
		for(int col = 0; col < W; col++){

			if(dp[row-1][col]%2== 0){
				dp[row][col] += dp[row-1][col]/2;
			}else{
				if(table[row-1][col] == 0){
					dp[row][col] += (dp[row-1][col]+1)/2;
				}else{
					dp[row][col] += (dp[row-1][col]-1)/2;
				}
			}

			if(col > 0){
				if(dp[row][col-1]%2 == 0){
					dp[row][col] += dp[row][col-1]/2;
				}else{
					if(table[row][col-1] == 1){
						dp[row][col] += (dp[row][col-1]+1)/2;
					}else{
						dp[row][col] += (dp[row][col-1]-1)/2;
					}
				}
			}
		}
	}

	for(int row = 0; row < H; row++){
		for(int col = 0; col < W; col++){
			if(dp[row][col]%2==1){
				table[row][col] = 1 - table[row][col];
			}
		}
	}

	int ans_row = 0,ans_col = 0;
	while(ans_row < H && ans_col < W){
		if(table[ans_row][ans_col] == 0){
			ans_row++;
		}else{
			ans_col++;
		}
	}
	printf("%d %d\n",ans_row+1,ans_col+1);

}


int main(){

	table = new int*[1001];
	dp = new int*[1001];
	for(int i = 0; i < 1001; i++){
		table[i] = new int[1001];
		dp[i] = new int[1001];
	}

	while(true){
		scanf("%d %d %d",&H,&W,&N);
		if(H == 0 && W == 0 && N == 0)break;

		func();
	}


	return 0;
}

Measured memory_bytes: 10972
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for(int i=a; i<b; i++)

#define N 5123
int n, m;
int l[N][N], r[N][N];

int main(){
	scanf("%d%d", &n, &m);
	REP(i, N) REP(j, N) l[i][j]=r[i][j]=0;
	REP(i, m){
		int a, b, x;
		scanf("%d%d%d", &a, &b, &x); a--; b--;
		l[a][b]++;
		r[a][b+1]++;
		l[a+x+1][b]--;
		r[a+x+1][b+x+2]--;
	}
	int ans = 0;
	FOR(i, 1, n){
		REP(j, i+2){
			l[i][j]+=l[i-1][j];
			r[i][j+2]+=r[i-1][j+1];
		}
	}
	REP(i, n){
		int a = 0;
		REP(j, i+1){
			a+=l[i][j];
			a-=r[i][j];
			if(a) ans++;
		}
	}
	printf("%d\n", ans);
}

Measured memory_bytes: 208240
#pragma GCC optimize("O3")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")
#define io_init cin.tie(0);ios::sync_with_stdio(0);cout<<setprecision(10)
#include <bits/stdc++.h>
constexpr int INF = 2147483647;
constexpr long long int INF_LL = 9223372036854775807;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;


struct Mo {
    int width;
    vector< int > left, right, order;
    vector< bool > v;

    Mo(int N, int Q) : width((int)sqrt(N)), order(Q), v(N) {
        iota(begin(order), end(order), 0);
    }

    void add(int l, int r) { // [l, r) 
        left.emplace_back(l);
        right.emplace_back(r);
    }

  	template<class ADD, class DEL, class REM>
    int run(const ADD& add, const DEL& del, const REM& rem) {
        assert(left.size() == order.size());
        sort(begin(order), end(order), [&](int a, int b) {
            int ablock = left[a] / width, bblock = left[b] / width;
            if (ablock != bblock) return ablock < bblock;
            if (ablock & 1) return right[a] < right[b];
            return right[a] > right[b];
            });
        int nl = 0, nr = 0;
        auto push = [&](int idx) {
            v[idx].flip();
            if (v[idx]) add(idx);
            else del(idx);
        };
        for (auto idx : order) {
            while (nl > left[idx]) push(--nl);
            while (nr < right[idx]) push(nr++);
            while (nl < left[idx]) push(nl++);
            while (nr > right[idx]) push(--nr);
            rem(idx);
        }
        return 0;
    }
};
int main() {
	io_init;
    int N, Q;
    cin >> N;
    cin >> Q;

    vector< int > A(N);
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    vector< int > f(1000001);
    Mo mo(N, Q);
    for (int i = 0; i < Q; i++) {
        int l, r;
        cin >> l >> r;
        mo.add(--l, r);
    }
    int ret = 0;
    auto add = [&](int idx) { if (f[A[idx]]++ == 0) ret++; };
    auto del = [&](int idx) { if (f[A[idx]]-- == 1) --ret; };
    vector< int > ans(Q);
    auto rem = [&](int idx) { ans[idx] = ret; };
    mo.run(add, del, rem);
    for (int i = 0; i < Q; i++) {
        cout << ans[i] << "\n";
    }
}


Measured memory_bytes: 17688
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <iomanip>
#include <utility>
#include <tuple>
#include <functional>
#include <bitset>
#include <cassert>
#include <complex>
#include <stdio.h>
#include <time.h>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#define all(a) a.begin(),a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef long double ld;
typedef complex<ld> com;
constexpr int inf = 1000000000;
constexpr ll INF = 1000000000000000000;
constexpr ld EPS = 1e-12;
constexpr ld PI = 3.141592653589793238;
template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; }
template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; }

vector<int> topological_sort(int n, vector<vector<int>> graph, vector<int> indegree) {
	vector<int> res;
	queue<int> que;
	for (int i = 0; i < n; i++) if (indegree[i] == 0) que.push(i);
	while (!que.empty()) {
		int ver = que.front(); que.pop();
		res.push_back(ver);
		for (int i : graph[ver]) {
			indegree[i]--;
			if (indegree[i] == 0) que.push(i);
		}
	}
	return res;
}

signed main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(20);

	int n, m;
	cin >> n >> m;
	vector<vector<int>> graph(n, vector<int>());
	vector<int> deg(n);
	rep(_, m) {
		int u, v;
		cin >> u >> v;
		u--; v--;
		graph[u].pb(v);
		deg[v]++;
	}
	vector<int> order = topological_sort(n, graph, deg);
	vector<int> dis(n);
	rep(i, n) {
		for (int nxt : graph[order[i]]) {
			chmax(dis[nxt], dis[order[i]] + 1);
		}
	}
	int ans = 0;
	rep(i, n) chmax(ans, dis[i]);
	cout << ans << '\n';
}

Measured memory_bytes: 12672
import itertools
N, C = map(int, input().split())
D = [[int(a) for a in input().split()] for i in range(C)]
X = [[int(a)-1 for a in input().split()] for i in range(N)]
T = [[0] * C for _ in range(3)]
for i in range(N):
    for j in range(N):
        T[(i+j)%3][X[i][j]] += 1

P = [[0] * C for _ in range(3)]
for i in range(3):
    for j in range(C):
        P[i][j] = sum([D[k][j] * T[i][k] for k in range(C)])
    
ans = 1<<100
for a, b, c in itertools.permutations(range(C), 3):
    ans = min(ans, P[0][a] + P[1][b] + P[2][c])
print(ans)

Measured memory_bytes: 45276
/* include c++ libraries */
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <unordered_map>
#include <map>
#include <queue>
#include <set>

/* include c-lang libraries */
#include <climits>
#include <cmath>

/* include my libraries */
//#include <show_vec.hh>

using namespace std;

#define LINE DEBUG{ printf("LINE : %d\n", __LINE__); }
#define DEBUG if(true)

using ll = long long;
template<class T> using vec = vector<T>;

const int MOD = 1000000007;

int main(){
  int N;
  cin >> N;
  //vec<int> A(N);
  int A = 0;
  //vec<int> count(100000+2, 0);
  int count[100000+2] = {};
  ll sum = 0;
  for(int i = 0; i < N; i++){
    cin >> A;
    count[A]++;
    sum += A;
  }
  int Q; cin >> Q;
  vec<ll> Ans;
  for(int i = 0; i < Q; i++){
    int b, c;
    cin >> b >> c;
    sum += ((ll)(c-b))*count[b];
    count[c] += count[b];
    count[b] = 0;
    Ans.push_back(sum);
  }

  for(int q = 0; q < Q; q++){
    //printf("Ans[%d] : ", q);
    cout << Ans[q] << endl;
  }
  return 0;
}


/*
10
1 2 3 4 5 6 7 8 9 0
10
1 2
2 3
3 4
4 5
5 0
1 2
2 3
3 4
4 5
5 0

*/

/*
7
0 0 0 0 0 0 0
5
1 2
100 2
0 3
3 2
2 0
*/


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

int main() {
  int n, l;
  cin >> n >> l;
  vector<string> v(n);
  for(int i = 0; i < n; i++)
    cin >> v.at(i);
  sort(v.begin(), v.end());
  for(int i = 0; i < n; i++)
    cout << v.at(i);
  cout << endl;
}


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

using namespace std;

typedef long long ll;

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

ll mod_pow(ll a, int n)
{
  ll ret = 1;
  while (n != 0) {
    if (n & 1) {
      ret = (ret * a) % MOD;
    }
    a = (a * a) % MOD;
    n >>= 1;
  }
  return ret;
}

int main()
{
  int N; scanf("%d", &N);
  vector<ll> A(N + 1);
  vector<int> occ(N + 5, -1);
  int l = -1, r = -1;
  for (int i = 0; i < N + 1; ++i) {
    scanf("%lld", &A[i]);
    if (occ[A[i]] != -1) {
      l = occ[A[i]];
      r = i;
    }
    else {
      occ[A[i]] = i;
    }
  }

  vector<ll> modfact(N + 5, 1), inv_modfact(N + 5, 1);
  for (int i = 2; i <= N + 1; ++i) {
    modfact[i] = (modfact[i - 1] * i) % MOD;
    inv_modfact[i] = mod_pow(modfact[i], MOD - 2);
  }

  int total = N + 1, inner = r - l - 1;
  for (int k = 1; k <= total; ++k) {
    ll ans = (modfact[total] * inv_modfact[k]) % MOD;
    ans = (ans * inv_modfact[total - k]) % MOD;
    int n = total - inner - 2, r = k - 1;
    if (n >= r) {
      ll dup = (modfact[n] * inv_modfact[r]) % MOD;
      dup = (dup * inv_modfact[n - r]) % MOD;
      ans = (ans - dup + MOD) % MOD;
    }
    printf("%lld\n", ans);
  }
  return 0;
}


Measured memory_bytes: 3968
N = int(input())
S = input()
memo = {"R":0,"G":0,"B":0}
ans = 0
if len(set(S))==3:
  for i,s in enumerate(S):
    a = 1
    for k,v in memo.items(): a *= 1 if k==s else v
    b = sum([len(set(s+S[:i+1][::-(j+1)][:3]))//3 for j in range(i//2)])
    ans += a-b
    memo[s] += 1
print(ans)

Measured memory_bytes: 99756
#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define FL "a"
using namespace std;
typedef long long ll;
typedef long double dd;
const int N=2e5+10;
const int inf=2147483647;
const dd pi=acos(-1);
const ll INF=1ll<<60;
const int mod=998244353;
inline ll read(){
  ll data=0,w=1;char ch=getchar();
  while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
  if(ch=='-')w=-1,ch=getchar();
  while(ch<='9'&&ch>='0')data=data*10+ch-48,ch=getchar();
  return data*w;
}
inline void file(){
  freopen(FL".in","r",stdin);
  freopen(FL".out","w",stdout);
}

int n,k,len,b[N],L[N],R[N],o[N];ll a[N],dp[N],ans=INF;
inline int get(ll x,int p){return (x%p+p)%p;}
#define mid ((l+r)>>1)
#define ls (i<<1)
#define rs (i<<1|1)
int cov[N<<2];
inline void pushdown(int i){if(cov[i])cov[ls]=cov[rs]=cov[i],cov[i]=0;}
void modify(int i,int l,int r,int x,int y,int v){
  if(x<=l&&r<=y){cov[i]=v;return;}pushdown(i);
  if(x<=mid)modify(ls,l,mid,x,y,v);if(mid<y)modify(rs,mid+1,r,x,y,v);
}
int query(int i,int l,int r,int p){
  if(l==r)return cov[i];pushdown(i);
  return p<=mid?query(ls,l,mid,p):query(rs,mid+1,r,p);
}
int main()
{
  n=read();k=read();for(int i=1;i<=n;i++)a[i]=read(),b[i]=read();
  for(int i=1;i<=n;i++)if(b[i]==1&&a[i]*2>k)return puts("-1"),0;
  for(int i=1;i<=n;i++)a[i]+=a[i-1];
  for(int i=1;i<=n;i++)
    if(b[i]==1)L[i]=get(-a[i-1]*2,k),R[i]=get(-a[i]*2,k);
    else L[i]=0,R[i]=k-1;
  for(int i=1;i<=n;i++)o[++len]=L[i],o[++len]=R[i];
  sort(o+1,o+len+1);len=unique(o+1,o+len+1)-o-1;
  for(int i=1;i<=n;i++){
    L[i]=lower_bound(o+1,o+len+1,L[i])-o;
    R[i]=lower_bound(o+1,o+len+1,R[i])-o;
  }
  
  //for(int i=1;i<=n;i++)printf("[%d,%d]\n",L[i],R[i]);
  //for(int i=1;i<=len;i++)printf("%d ",o[i]);puts("");
  
  for(int i=n,p;i;i--){
    p=query(1,1,len,L[i]);
    if(p)dp[i]=dp[p]+get(o[L[p]]-o[L[i]],k);
    if(L[i]<=R[i]){
      if(L[i]!=1)modify(1,1,len,1,L[i]-1,i);
      if(R[i]!=len)modify(1,1,len,R[i]+1,len,i);
    }
    else if(L[i]-R[i]>1)modify(1,1,len,R[i]+1,L[i]-1,i);
    //printf("dp[%d]=%lld\n",i,dp[i]);
  }
  //for(int i=1;i<=len;i++)printf("%d ",query(1,1,len,i));puts("");
  for(int i=1,p;i<=len;i++)
    p=query(1,1,len,i),ans=min(ans,p?dp[p]+get(o[L[p]]-o[i],k):0);
  printf("%lld\n",ans+a[n]*2);return 0;
}


Measured memory_bytes: 7808
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import permutations
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7


#############
# Main Code #
#############

"""
# 飛ばし累積和
N = 10
num = [i for i in range(1, N + 1)]
D = 2
lista = [0] * N
for i in range(D):
    for j in range(i, N, D):
        if j == i:
            lista[j] = num[j]
        else:
            lista[j] = num[j] + lista[j - D]
# [1, 2, 4, 6, 9, 12, 16, 20, 25, 30]
print(lista)
# 9番目までの奇数の数字の合計 - 1番目までの奇数の数字の合計
# 3 + 5 + 7 + 9
print(lista[8] - lista[0])
"""

# Dかそれぞれのqueryで固定なのでこの問題は解ける
H, W, D = getNM()
maze = []
for i in range(H):
    a = getList()
    maze.append(a)
Q = getN()
# piece[0]からpiece[1]まで
# 4 → 6　→ 8
piece = []
for i in range(Q):
    l, r = getNM()
    piece.append([l, r])

place_list = [[-1, -1] for i in range(H * W)]

for y in range(H):
    for x in range(W):
        place_list[maze[y][x] - 1] = [x, y]

# 飛ばし累積和
x_plus = [0] * (H * W)
y_plus = [0] * (H * W)
for i in range(D):
    for j in range(i, H * W, D):
        if j == i:
            opt_x = 0
            opt_y = 0
        else:
            opt_x = abs(place_list[j][0] - place_list[j - D][0])
            opt_y = abs(place_list[j][1] - place_list[j - D][1])
            x_plus[j] = opt_x + x_plus[j - D]
            y_plus[j] = opt_y + y_plus[j - D]

def past_exam(piece_query):
    start = piece_query[0]
    goal = piece_query[1]

    x_point = x_plus[goal - 1] - x_plus[start - 1]
    y_point = y_plus[goal - 1] - y_plus[start - 1]
    return x_point + y_point

for i in range(Q):
    print(past_exam(piece[i]))

Measured memory_bytes: 102744
mod = 10**9+7


def main():
    n, k = map(int, input().split())

    baisuu = [[] for _ in [0]*(k+1)]
    tree = [0]*(k+1)

    for i in range(1, k+1):
        j = 2*i
        while j <= k:
            baisuu[i] += [j]
            j += i
        tree[i] = pow(len(baisuu[i])+1, n, mod)
    for i in range(k, 0, -1):
        for j in baisuu[i]:
            tree[i] -= tree[j]
        tree[i] %= mod
    ans = 0
    for i, j in enumerate(tree):
        ans = (ans+i*j) % mod
    print(ans)


main()

Measured memory_bytes: 85616
// I SELL YOU...! 
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<queue>
#include<chrono>
#include<iomanip>
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
void init_io(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(10);
}
signed main(){
  init_io();
  ll a,b;
  string ap,tp;
  cin >> a >> b;
  for(int i=0;i<b;i++){
    ap += to_string(a);
  }
  for(int i=0;i<a;i++){
    tp += to_string(b);
  }
  cout << min(ap,tp)<<endl;
}


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

using namespace std;

#define FOR(i, n) for(int i = 0; i < (int) n; i++)
#define PB push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define F first
#define S second

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;

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

	int l, r, d; cin >> l >> r >> d;

	int ans = 0;
	for(int i = l; i <= r; i++)
		ans += (i%d == 0);

	cout << ans << '\n';

	return 0;
}


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


Measured memory_bytes: 3720
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
using namespace std;

ll n,a[300010],b[300010][70],var,ans,mod=7+1e9,uno,cero,c[300010][70];

int main()
{

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

    cin >> n;
    for(ll i=0; i<n; i++){
        cin >> a[i];
        for(ll j=0; j<60; j++){
            var = a[i]&(1LL<<j);
            if( var ) b[i][j] = c[i][j] = 1;
        }
    }

    for(ll i=n-2; i>=0; i--){
        for(ll j=0; j<60; j++) b[i][j]+=b[i+1][j];
    }

    for(ll i=0; i+1<n; i++){
        for(ll j=0; j<60; j++){
            uno = b[i+1][j];
            cero = n-i-1-uno;
            if( c[i][j] ){
                ans = ( ans + (((1LL<<j)%mod)*cero)%mod )%mod;
            }else{
                ans = ( ans + (((1LL<<j)%mod)*uno)%mod )%mod;
            }
        }
    }

    cout << ans;

    return 0;
}


Measured memory_bytes: 330752
#include<bits/stdc++.h>
using namespace std;
long long int inf=1e18+5;
int main()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt","r",stdin);
	freopen("output.txt","w",stdout);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int n,i;
	cin>>n;
	long long int a[n];
	for(i=0;i<n;i++)
		cin>>a[i];
	long long int dp[n][n];
	memset(dp,0,sizeof(dp));
	int j,l,k;
	for(i=0;i<n;i++)
		dp[i][i]=0;
	long long int sum[n];
	sum[0]=a[0];
	for(i=1;i<n;i++)
		sum[i]=sum[i-1]+a[i];
	long long int ans=0;
	for(l=2;l<=n;l++)
	{
		for(i=0;i<n-l+1;i++)
		{
			j=i+l-1;
			dp[i][j]=inf;
			for(k=i;k<j;k++)
			{
				dp[i][j]=min(dp[i][j],dp[i][k]+dp[k+1][j]);
			}
			dp[i][j]+=sum[j];
			if(i)
				dp[i][j]-=sum[i-1];
		}
	}
	cout<<dp[0][n-1]<<endl;
	return 0;
}

Measured memory_bytes: 4844
#include <bits/stdc++.h>
using namespace std;
int a[100000+10],t[100000+10]={1<<29};
int main()
{
	int n,k,q,kkk=0;
	cin>>n>>k>>q;
	for(int i=0;i<n;i++)
		t[i]=k;
	for(int i=0;i<q;i++)
		cin>>a[i];
	for(int i=0;i<q;i++)
	{
		if(t[kkk]!=1<<29)
			t[a[kkk]-1]++;
		else
			break;
		kkk++;
	}
	for(int i=0;i<n;i++)
		t[i]-=q;
	for(int i=0;i<n;i++)
	{
		if(t[i]<=0)
			cout<<"No"<<endl;
		else
			cout<<"Yes"<<endl;
	}

	return 0;
}

Measured memory_bytes: 1408
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

void main()
{
    auto s = readln.chomp.to!(char[]);
    char[] S;
    for (size_t i; i < s.length; ++i) {
        if (i < s.length-1 && s[i] == 'B' && s[i+1] == 'C') {
            S ~= 'X';
            ++i;
        } else {
            S ~= s[i];
        }
    }
    long[] C;
    C.length = S.length;
    long cnt;
    foreach (i, c; S) {
        if (c == 'A') {
            ++cnt;
        } else if (c != 'X') {
            cnt = 0;
        }
        C[i] = cnt;
    }
    long r;
    foreach (i, c; S) if (c == 'X') r += C[i];
    writeln(r);
}

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

using namespace std;

#define    watch(x); cout << "\n" <<  #x << " is: " << x << "\n";
#define    all(a) (a).begin(), (a).end()
#define    fi first
#define    se second

template<class A, class B>
ostream& operator << (ostream& output, const pair<A, B>& p) {
    output << "(" << p.first << ", " << p.second << ")";
    return output;
}

template<class A>
ostream& operator << (ostream& output, const vector<A>& a) {
    output << "[";
    for(auto it = a.begin(); it != a.end(); ++it) {
        if(it != a.begin()) {
            output << ", ";
        }
        output << *it;
    }
    output << "]";
    return output;
}

using lld = long long;
using pii = pair<int, int>;
using pll = pair<lld, lld>;

//const int inf = 1e9;
//const int mod = 1e9 + 7;
//const double PI = acos(-1.0);
//const int mx = 1e5 + 5;



int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
#ifndef ONLINE_JUDGE
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
#endif

    string s, t;
    cin >> s >> t;
    for(int i = 0; i < (int) s.size(); ++i) {
        if(s == t) {
            cout << "Yes" << endl;
            return 0;
        }
        char ch = s.back();
        s.pop_back();
        string temp;
        temp = ch + s;
        swap(s, temp);
    }
    cout << "No" << endl; 
    return 0;
}


Measured memory_bytes: 3432