akhauriyash/Code-Regression huggingface dataset:
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] s = sc.next().split("");
        String[] t = sc.next().split("");
        boolean flg = true;
        for (int i = 0; i < s.length; i++) {
            if (s[i].equals(t[i])) {continue;}
            else flg = false;
        }
        if (flg) {System.out.println("Yes");}
        else System.out.println("No");
    }
}

Measured memory_bytes: 35744
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>

using namespace std;

const int mod = (int) 1e9 + 7;

int main() {
  int n, m;
  cin >> n >> m;
  set <int> s;
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    s.insert(x);
  }
  int dp[n + 1];
  memset(dp, 0, sizeof dp);
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    if (!s.empty() && i == *s.begin()) {
      dp[i] = 0;
      s.erase(s.begin());
      continue;
    }
    if (i > 0) {
      dp[i] = (dp[i] + dp[i - 1]) % mod;
    }
    if (i > 1) {
      dp[i] = (dp[i] + dp[i - 2]) % mod;
    }
  }
  cout << dp[n] << endl;
  return 0;
}


Measured memory_bytes: 5376
#include<stdio.h>

int main(void){
    int H,W;
    int i,j,k;
    int count=0;
        
    //入力の読み込み
    scanf("%d %d",&H,&W);    
    
    char matrix[H][W+1];

    //入力の格納
    for(i=0;i<H;i++){
            scanf("%s",matrix[i]);
    }
    
    for(i=0;i<H;i++){
        for(j=0;j<W;j++){
            if(matrix[i][j]=='.'){
                //下位行の検証
                for(k=0;k<=2;k++){
                    if(matrix[i-1][j-1+k]=='#'){
                    count++;
                    }
                }
                //同行の検証
                if(matrix[i][j-1]=='#'){
                    count++;
                }
                if(matrix[i][j+1]=='#'){
                    count++;
                }
                //上位行の検証
                for(k=0;k<=2;k++){
                    if(matrix[i+1][j-1+k]=='#'){
                    count++;
                    }
                }
                printf("%d",count);
                count=0;
            }
            else{
                printf("#");    
            }
        }
        printf("\n");
    }
    return 0;
}

Measured memory_bytes: 128
n=int(input())
ans = ['a']
for i in range(1,n):
    ans_ = []
    for j in ans:
        for k in range(ord(max(j))-95):
            ans_.append(j+chr(97+k))
    ans = ans_.copy()
    
ans = sorted(ans)
for i in ans:
    print(i)

Measured memory_bytes: 13896
from fractions import gcd

a, b = map(int, input().split())
ab_gcd = gcd(a, b)

factors = []
while ab_gcd % 2 == 0:
  factors.append(2)
  ab_gcd //= 2
f = 3
while f * f <= ab_gcd:
  if ab_gcd % f == 0:
    factors.append(f)
    ab_gcd //= f
  else:
    f += 2
if ab_gcd != 1:
  factors.append(ab_gcd)
  
print(len(set(factors)) + 1)

Measured memory_bytes: 5300
#include <iostream>
#include <string>
#include <algorithm>
#define rep(i,n) for(int i=0; i<n; i++)
#define inf 2e9
using namespace std;

int main(int argc, char **argv){
    int a,b,n;
    cin>>n>>a>>b;
    a = a*n;
    if(a<b){
        cout<<a<<endl;
        return 0;
    }else{
        cout<<b<<endl;
        return 0;
    }

    return 0;
}

Measured memory_bytes: 256
import sys

n = int(input())
for i in range(3500):
    for j in range(3500):
        temp = 4 * (i + 1) * (j + 1) - n * (i + j + 2)
        if temp != 0:
            if (n * (i + 1) * (j + 1)) % temp == 0:
                if (n * (i + 1) * (j + 1)) // temp > 0:
                    print(i + 1, j + 1, (n * (i + 1) * (j + 1)) // temp)
                    sys.exit()

Measured memory_bytes: 66772
from collections import deque
S=deque([c for c in input()])
Q=int(input())
flg=True
for i in range(Q):
    l=[a for a in input().split()]
    if l[0]=="1": 
        flg=not flg
    else:
        if l[1]=="1":
            if flg:
                S.appendleft(l[2])
            else:
                S.append(l[2])
        else:
            if flg:
                S.append(l[2])
            else:
                S.appendleft(l[2])
print("".join(S) if flg else "".join(list(reversed(S))))

Measured memory_bytes: 8420
#include<bits/stdc++.h>
using namespace std;
int main()
{int a,b,c,d,e=0,f,v;
cin>>a>>b>>c>>d;
if(a%d==0)e=a/d;
else e=1+a/d;
if(c%b==0)f=c/b;
else f=1+c/b;
if(f>e)cout<<"No\n";
else cout<<"Yes\n";
}

Measured memory_bytes: 3636
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define mod 1000000007
#define MAX 1000000000000000
#define all(v) v.begin(),v.end()
#define rep(i,a,b) for(i=(ll)a;i<(ll)b;i++)
#define revrep(i,a,b) for(i=(ll)a;i>=(ll)b;i--)
#define ii pair<ll,ll>
#define MP make_pair
#define pb push_back
#define f first
#define se second
#define ll int
#define vi vector<ll>
ll modexp(ll a,ll b){ ll res = 1; while(b > 0){  if(b & 1) res = (res * a)%mod;  a = (a * a)%mod;  b/=2;  }  return res; }
#define rs resize
typedef tree< ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update > OST;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
    template <typename Arg1>
    void __f(const char* name, Arg1&& arg1){
        cout << name << " : " << arg1 << endl;
    }
    template <typename Arg1, typename... Args>
    void __f(const char* names, Arg1&& arg1, Args&&... args){
        const char* comma = strchr(names + 1, ','); cout.write(names, comma - names) << " : " << arg1<<" | ";__f(comma+1, args...);
    }
#else
#define trace(...)
#endif

const ll N = 21;
ll i,j,n,a;
vector<vi> v(N);
ll dp[N][1 << N];
ll go(ll i,ll mask)
{
    if(i < 0) return (mask == 0 ? 1 : 0);
    
    if(dp[i][mask] != -1) return dp[i][mask];
    
    ll res = 0;
    for(auto j: v[i]) if(mask & (1 << j))
        res = (res + go(i - 1,mask ^ (1 << j))) % mod;
    
    return dp[i][mask] = res;
}
int main()
{
    std::ios_base::sync_with_stdio(false); cin.tie(NULL);
    memset(dp,-1,sizeof(dp));
    cin>>n;
    rep(i,0,n) rep(j,0,n){
        cin>>a;
        if(a)
            v[i].pb(j);
    }
    cout<<go(n - 1,(1 << n) - 1);
    return 0;
}

Measured memory_bytes: 172288
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;

bitset<705> adj[705];
vector<int> G[705];
int col[705];

ii dfs(int u, int c) {
	col[u] = c;
	ii ret(1, 0);
	for (auto& v : G[u]) {
		if (col[v] == c) return ii(-1, -1);
		if (col[v] == -1) {
			ii tmp = dfs(v, !c);
			if (tmp == ii(-1, -1)) return ii(-1, -1);
			ret.first += tmp.second;
			ret.second += tmp.first;
		}
	}
	return ret;
}

int main() {
	int N, M;
	cin >> N >> M;
	while (M--) {
		int a, b;
		cin >> a >> b;
		adj[a][b] = adj[b][a] = 1;
	}

	for (int u = 1; u <= N; ++u) {
		for (int v = 1; v < u; ++v) {
			if (adj[u][v] == 0) {
				G[u].push_back(v);
				G[v].push_back(u);
			}
		}
	}

	memset(col, -1, sizeof(col));
	vector<ii> v;
	for (int i = 1; i <= N; ++i) {
		if (col[i] == -1) {
			ii tmp = dfs(i, 0);
			if (tmp == ii(-1, -1)) {
				cout << -1 << '\n';
				return 0;
			}
			v.push_back(tmp);
		}
	}

	bitset<705> dp[705];
	dp[0][0] = 1;
	for (int i = 0; i < v.size(); ++i) {
		for (int j = 0; j <= N; ++j) {
			if (dp[i][j]) {
				dp[i + 1][j + v[i].first] = dp[i + 1][j + v[i].second] = 1; 
			}
		}
	}
	int ans = 1e9;
	for (int i = 0; i <= N; ++i) {
		if (dp[v.size()][i]) ans = min(i * (i - 1) / 2 + (N - i) * (N - i - 1) / 2, ans);
	}
	cout << ans << '\n';
	return 0;
}

Measured memory_bytes: 3072
import java.io.*;
import java.util.*;
import java.lang.*;

public class Main {
	public static ArrayList<ArrayList<Integer>> adj;
	public static int[] visited;
	public static int dfs(int parent,int vertex) {
		visited[vertex] = 1;
		int cnt = 0;
		boolean flag = true;
		for(int i=0;i<adj.get(vertex).size();i++) {
			if(adj.get(vertex).get(i)==parent) continue;
//			flag = false;
			if(visited[adj.get(vertex).get(i)]==0) {
				flag = false;
				cnt+=dfs(vertex,adj.get(vertex).get(i));
			}
		}
		return cnt+1;
	}
	public static void main(String[] args) {
		FastReader in = new FastReader();
		PrintWriter out = new PrintWriter(System.out);
		int n = in.nextInt(), m = in.nextInt();
		adj = new ArrayList<>();
		visited = new int[n];
		for(int i=0;i<n;i++) adj.add(new ArrayList<>());
		for(int i=0;i<m;i++) {
			int u = in.nextInt()-1,v = in.nextInt()-1;
			adj.get(u).add(v);
			adj.get(v).add(u);
		}
		long ans = 0;
		for(int i=0;i<n;i++) {
			if(visited[i]==0) ans = Math.max(ans, dfs(-1,i));
		}
		out.println(ans);
		out.close();
	}

	static final Random random=new Random();
	//	static void ruffleSort(Pair[] a) {
	//		int n=a.length;//shuffle, then sort 
	//		for (int i=0; i<n; i++) {
	//			int oi=random.nextInt(n);
	//			Pair temp=a[oi];
	//			a[oi]=a[i]; a[i]=temp;
	//		}
	//		Arrays.sort(a);
	//	}
	static void ruffleSort(int[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n), temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	static void ruffleSort(char[] a) {
		int n=a.length;//shuffle, then sort 
		for (int i=0; i<n; i++) {
			int oi=random.nextInt(n);
			char temp=a[oi];
			a[oi]=a[i]; a[i]=temp;
		}
		Arrays.sort(a);
	}
	static class FastReader 
	{ 
		BufferedReader br; 
		StringTokenizer st; 

		public FastReader() 
		{ 
			br = new BufferedReader(new
					InputStreamReader(System.in)); 
		} 

		String next() 
		{ 
			while (st == null || !st.hasMoreElements()) 
			{ 
				try
				{ 
					st = new StringTokenizer(br.readLine()); 
				} 
				catch (IOException  e) 
				{ 
					e.printStackTrace(); 
				} 
			} 
			return st.nextToken(); 
		} 

		int nextInt() 
		{ 
			return Integer.parseInt(next()); 
		} 

		long nextLong() 
		{ 
			return Long.parseLong(next()); 
		} 

		double nextDouble() 
		{ 
			return Double.parseDouble(next()); 
		} 

		String nextLine() 
		{ 
			String str = ""; 
			try
			{ 
				str = br.readLine(); 
			} 
			catch (IOException e) 
			{ 
				e.printStackTrace(); 
			} 
			return str; 
		} 

		int[] readArray(int n) {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
	}
}

//class Pair implements Comparable<Pair>{
//    int a;
//    int b;
//    public Pair(int a, int b) {
//        this.a = a;
//        this.b = b;
//    }
//    public int compareTo(Pair o) {
//        if(this.a==o.a)
//            return this.b - o.b;
//        return this.a - o.a;
//    }   
//}

Measured memory_bytes: 99016
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
#define MP make_pair
#define INF (sizeof(int) == 4 ? 1e9:1e18)
#define EPS 0.0000000001
using namespace std;
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vectors<decltype(inner)>(x, inner); }
template<class T> void show_with_brank(T a){ int cnt = 0; for(auto itr :a){ if(cnt++ != 0) cout << " "; cout << itr;} }
using ll  = long long;
using ld  = long double;
using pii = pair<int,int>;
using piii = pair<int,pii>;
int W,H;
int dx[]={0,0,1,-1}, dy[]={1,-1,0,0};
bool valid(int x,int y){return (0<=x&&x<W)&&(0<=y&&y<H);}
#define int ll
signed main(){
    int n;
    while(cin >> n,n){
        vector<string>s(n);
        vector<string>ans;
        rep(i,n) cin >> s[i];
        rep(i,n)rep(j,s[i].size())
        if(s[i][j] == '.')s[i][j] = ' ';
        rep(i,n){
            rep(j,(int)s[i].size()-1)
            if(s[i][j] == ' ' && s[i][j+1] != ' ')s[i][j] = '+';
            ans.push_back(s[i]);
        }
        int maxi = 0;
        rep(i,n)maxi = max(maxi,(int)s[i].size());
        rep(i,n){
            for(int j = s[i].size(); j< maxi;j++)
                ans[i].push_back(' ');
        }
        rep(i,ans.size())rep(j,ans[i].size()){
            if(ans[i][j] == '+'){
                int y = i;
                int cnt = 1;
                while(true){
                    if(y+cnt >= n){cnt = INF;break;}
                    if(ans[y+cnt][j] == '+')break;
                    if(ans[y+cnt][j] != ' '){cnt = INF;break;}
                    cnt++;
                }
                if(cnt == INF)continue;
                for(int y = i+1; y < i+cnt; y++){
                    ans[y][j] = '|';
                }
            }
        }
        rep(i,n){
            rep(j,s[i].size())
            cout << ans[i][j];
            cout << endl;
        }
    }
}







Measured memory_bytes: 5228
#include <iostream>
#include <string>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <iomanip>
#include <fstream>


using namespace std;

#define REP(i, n) for(ll i = 0;i < n;i++)
#define REPR(i, n) for(ll i = n;i >= 0;i--)
#define FOR(i, m, n) for(ll i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define REPO(i, n) for(ll i = 1;i <= n;i++)
#define ll long long
#define INF (ll)1 << 60
#define MINF (-1 * INF)
#define ALL(n) n.begin(),n.end()
#define MOD (ll)1000000007
#define P pair<ll, ll>

int main(){
    ll a;
    cin >> a;
    cout <<a * a <<endl;
}


Measured memory_bytes: 256
#include<iostream>

using namespace std;

int main() {
    int A, B, C;

    cin >> A >> B >> C;

    if (A == B && A == C) {
        cout << "No" << endl;
    } else if (A != B && A != C && B != C) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }

    return 0;
}

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

struct node{int p,c,l,r;};
node no[100001];
long long cost[100001];
bool al[100001];
/*hobo Lapinpon kun no pakuri*/
void dfs(int n,long long c){
  cost[n]=c;
  al[n]=true;
  if(no[n].p&&!al[no[n].p])dfs(no[n].p,c+no[n].c);
  if(no[n].l&&!al[no[n].l])dfs(no[n].l,c+no[no[n].l].c);
  int t=no[n].l;
  while(no[t].r){
    t=no[t].r;
    if(!al[t])dfs(t,c+no[t].c);
  }
}

int main(){
  int n,a,b,c,q,k,x,y;
  cin>>n;
  for(int i=0;i<n-1;i++){
    cin>>a>>b>>c;
    if(no[b].p)swap(a,b);
    no[b].p=a;
    if(no[a].l)no[b].r=no[a].l;
    no[a].l=b;
    no[b].c=c;
  }
  cin>>q>>k;
  dfs(k,0);
  for(int i=0;i<q;i++){
    cin>>x>>y;
    cout<<cost[x]+cost[y]<<endl;
  }
  return 0;
}



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

#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define ABS(x, y) ((x) > (y) ? ((x) - (y)) : ((y) - (x)))

#define DIVUP(x, y) (((x) + ((y) - 1)) / (y))
#define DIVOFF(x, y) (((x) + ((y) / 2)) / (y))
#define REMAINDER(x, y) ((x) % (y))

int main() {
  int N, A, B;
  scanf("%d", &N);
  scanf("%d%d", &A, &B);

  int count[3]{};
  for (int i{}; i < N; ++i) {
    int p;
    scanf("%d", &p);
    if (p <= A) {
      ++count[0];
    } else if (p <= B) {
      ++count[1];
    } else {
      ++count[2];
    }
  }

  printf("%d\n", MIN(MIN(count[0], count[1]), count[2]));
  return 0;
}


Measured memory_bytes: 256
if __name__ == '__main__':
    from sys import stdin
    from collections import defaultdict, deque
    # from collections import deque
    # from collections import Counter
    # import numpy as np
    # import math
    # import random
    input = stdin.readline

    n, m = map(int, input().split())
    roads = defaultdict(deque)
    for _ in range(m):
        a, b = map(int, input().split())
        roads[a].append((b, a))
        roads[b].append((a, b))
    visited = [False]*(n+1)
    visited[1] = True
    ans = [0]*(n+1)
    neighbor = deque(roads[1])
    while neighbor:
        now, base = neighbor.popleft()
        if visited[now]:
            continue
        visited[now] = True
        ans[now] = base
        neighbor.extend(roads[now])
    print('Yes')
    for p_ans in ans[2:]:
        print(p_ans)



Measured memory_bytes: 118692
open System


[<EntryPoint>]
let main argv =
    let N=stdin.ReadLine()
    let L = stdin.ReadLine().Split()|>Array.map int64
    L
    |>Array.sum
    |>fun c -> (c- 2L*(Array.max L))
    |>fun c -> if c>0L then "Yes" else "No"
    |>stdout.WriteLine
    0


Measured memory_bytes: 13268
#include <bits/stdc++.h>
using ll = long long;
#define FOR(i, k, n) for(ll i = (k); i < (n); i++)
#define FORe(i, k, n) for(ll i = (k); i <= (n); i++)
#define FORr(i, k, n) for(ll i = (k)-1; i > (n); i--)
#define FORre(i, k, n) for(ll i = (k)-1; i >= (n); i--)
#define REP(i, n) FOR(i, 0, n)
#define REPr(i, n) FORre(i, n, 0)
#define ALL(x) (x).begin(), (x).end()
#define ALLr(x) (x).rbegin(), (x).rend()
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
using namespace std;

const int INF = 1001001001;

int main(void){
  int n, a, b;
  cin >> n >> a >> b;
  cout << min(n*a, b) << endl;
  return 0;
}

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

c.sort(reverse = True)
ans = 0
f = 1
g = 1
for i in range(n-1):
    f *= 2
    f %= 10**9+7

for i in range(n-2):
    g *= 2
    g %= 10**9+7



for i in range(n):
    ans += f*c[i]
    f += g
    f %= 10**9+7
    ans %= 10**9+7


for i in range(n):
    ans *= 2
    ans %= 10**9+7

print(ans)


Measured memory_bytes: 87628
import itertools
N=int(input())
P=tuple(map(int,input().split()))
Q=tuple(map(int,input().split()))
nList=[]
for i in range(N):
  nList.append(i+1)

nAllList=[]
for i in itertools.permutations(nList):
  nAllList.append(i)
nAllList.sort()

pNum=0
qNum=0

for i in range(len(nAllList)):
  if(nAllList[i]==P):
    pNum=i+1
  if(nAllList[i]==Q):
    qNum=i+1

print(abs(pNum-qNum))  

Measured memory_bytes: 45168
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstdlib>

using namespace std;

int main()
{
  int a;
  int b;
  cin >> a >> b;
  string astr = to_string(a);
  string bstr = to_string(b);

  string str;
  if(a<b)
    {
      for(int i=0; i<b; i++)
	str += astr;
      cout << str << endl;
    }
  else
    {
      for(int i=0; i<a; i++)
	str += bstr;
      cout << str << endl;
    }

  return 0;
}



Measured memory_bytes: 256
#include <stdio.h>

int main(void){
	int a, b;
	scanf("%d %d", &a, &b);
	if(a > b){
		int tmp = a;
		a = b;
		b = tmp;
	}
	if((b - a) % 2){
		printf("IMPOSSIBLE\n");
		return 0;
	}
	printf("%d\n", (b + a) / 2);
	return 0;
}


Measured memory_bytes: 128
N = int(input())
a = [int(input()) for i in range(N)]

re = [0 for i in range(N)]
re[0] = 1
i = 0
count = 0

while re[a[i]-1] == 0:
    i = a[i] - 1
    count += 1
    re[i] += 1
    if i == 1:
        print(count)
        exit()

print(-1)

Measured memory_bytes: 7852
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ll          long long
#define fio         ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb          push_back
#define vi          vector<ll>
#define pp          push_pop
#define pll         pair<ll,ll>
#define in          insert
#define ini(a,n)    memset(a,n,sizeof(a))
#define ff          first
#define ss          second
#define inf         10000000000000000
#define all(p)      p.begin(), p.end()
#define que_max     priority_queue <int>
#define que_min     priority_queue <int, vi, greater<int>>
#define print(a)    for(auto i:a){cout<<i<<" ";}
#define print1(a)   for(auto i:a){cout<<i.ff<<" "<<i.ss<<endl;}

typedef tree<pll, null_type, less<pll>, rb_tree_tag, tree_order_statistics_node_update> Set;
unordered_map<ll,set<pll,greater<pll>>> g;
unordered_map<ll,ll> vis;

const ll mod = 1e9+7;

ll power(ll x,ll y,ll p){ll r=1;x=x%p;while(y>0){if(y&1)r=(r*x)%p;y=y>>1;x=(x*x)%p;}return r;}
const ll N = 2e5+10;
bool imp = false;
ll n,m,k;
ll xx1,yy1,x1,x2;
vector<vector<char>> a;
vector<vi> v,dp;
ll dx[4]={-1, +1, 0, 0};
ll dy[4]={0, 0, +1, -1};

void bfs(ll x,ll y)
{
    queue<pll> q;
    q.push({x,y});
    v[x][y] = 1;
    dp[x][y] = 0;

    while(!q.empty()){
        x = q.front().ff;
        y = q.front().ss;
        ll d = dp[x][y];
        q.pop();

        for(ll i=0;i<4;i++){

            for(ll j=1;j<=k;j++){

                ll xp = x + dx[i]*j;
                ll yp = y  + dy[i]*j;

                if(xp < 0 or xp >= n or yp < 0 or yp >= m) break;

                if(a[xp][yp] == '@')    break;
//                if() break;
                if(v[xp][yp] == 1 and (d + 1) > dp[xp][yp]) break;

                if(v[xp][yp] == 1) continue;

                v[xp][yp] = 1;
                dp[xp][yp] = d + 1;
                q.push({xp,yp});
            }

        }

    }

}


void solve() {
    cin>>n>>m>>k;
    a.resize(n+1,vector<char>(m+1));
    cin>>xx1>>yy1>>x1>>x2;

    v.resize(n+1,vi (m+1,-1));
    dp.resize(n+1,vi (m+1,INT_MAX));

    for(ll i=0;i<n;i++)
    {
        for(ll j=0;j<m;j++)
            cin>>a[i][j];
    }

    bfs(xx1-1,yy1-1);

    if(v[x1-1][x2-1] == -1){
        cout<<-1<<endl;
    }
    else{
        cout<<dp[x1-1][x2-1]<<endl;
    }





}

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

    fio
    int t=1;
    if(imp) cin>>t;
    while(t--)
    {
        // cout<<"Case #"<<tc<<": ";
        g.clear();
        vis.clear();
        solve();
        // tc++;
    }

}


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

#define FOR(i, j, k) for(int i = j; i < k; ++i)
#define rep(i, j) FOR(i, 0, j)
#define MAX 101

using namespace std;

int field[MAX][MAX];
bool went[MAX][MAX];
int w, h;
int sx, sy, gx, gy;
int scol;
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};

bool dfs(int x, int y) {
	if(field[y][x] == 0) return false;
	if(x == gx && y == gy) return true;
	int ncol = field[y][x];
	field[y][x] = 0;
	rep(i, 4) {
		int nx = x + dx[i];
		int ny = y + dy[i];
		if(0 < nx && nx <= w && 0 < ny && ny <= h && ncol == field[ny][nx]) {
			/*rep(j, h) {
				rep(k, w) cout << field[j][k] << " ";
				cout << endl;
			}*/
			if(dfs(nx, ny)) return true;
		}
	}
	return false;
}

int main() {
	while(cin >> w >> h, w || h) {
		memset(field, 0, sizeof(field));
		memset(went, false, sizeof(went));
		int n;
		cin >> sx >> sy >> gx >> gy;
		cin >> n;
		rep(i, n) {
			int c, d, x, y;
			cin >> c >> d >> x >> y;
			rep(i, (d ? 4 : 2)) rep(j, (d ? 2 : 4)) field[y + i][x + j] = c;
		}
		/*rep(j, h) {
			rep(k, w) cout << field[j][k] << " ";
			cout << endl;
		}*/
		if(dfs(sx, sy)) cout << "OK" << endl;
		else cout << "NG" << endl;
	}
	return 0;
}

Measured memory_bytes: 3080
#include <iostream>
#include<algorithm>
#include<string>
#include<vector>
#define _USE_MATH_DEFINES
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <numeric>
#include <cmath>
#include <limits>
#include <bitset> 
#include <iterator> 
using namespace std;

int main()
{
	int n, maxNum = 0, cnt = 0;
	string s, s1 = {""}, s2 = {""};
	vector<char>c;
	cin >> n >> s;
	for (int i = 0; i < s.length()-1; i++) {
		s1 = s.substr(0,i+1);
		s2 = s.substr(i+1,s.length()-s1.length());
			for (int j = 97; j <= 122; j++) {
				if (count(s1.begin(), s1.end(), (char)j) != 0 && count(c.begin(),c.end(),(char)j)==0)c.push_back((char)j);
			}
			for (int j = 0; j < c.size(); j++) {
				if(count(s2.begin(), s2.end(), c[j])!=0)cnt++;
			}
			maxNum = max(maxNum, cnt);
			cnt = 0;
	}
	cout << maxNum << endl;
}



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

using namespace std;

#ifdef DEBUG_MODE
	#define DBG(n) n;
#else
	#define DBG(n) ;
#endif
#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define PB push_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {for(int W = 0;W < (n);W++)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {for(int aaa = 0;aaa < i;aaa++){for(int bbb = 0;bbb < j;bbb++)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

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

ll gcd(ll a,ll b){
	if(a < b)swap(a,b);
	return b == 0 ? a : gcd(b,a%b);
}

int main(){

	int n;cin >> n;
	vector<int> v(n);
	int oddNum = 0;
	int evenNum = 0;
	REP(i,n){
		cin >> v[i];
		if(v[i] % 2)oddNum++;
		else evenNum++;
	}

	if(evenNum % 2){
		cout << "First" << endl;
	}
	else{
		int type = 0;
		while(oddNum == 1){
			bool flag = false;

			REP(i,n)if(v[i] > 1 && v[i] % 2){
				v[i]--;
				flag = true;
			}
			if(!flag)break;
			oddNum = 0;
			evenNum = 0;
			type++;
			ll gcdNum = v[0];
			REP(i,n)gcdNum = gcd(gcdNum,v[i]);
			REP(i,n){
				v[i] /= gcdNum;
				if(v[i] % 2)oddNum++;
				else evenNum++;
			}
			//SHOW1d(v,n);
		}

		if(evenNum % 2)type++;

		if(type % 2){
			cout << "First" << endl;
		}
		else {
			cout << "Second" << endl;
		}
	}
	return 0;
}


Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <utility>
#include <stack>
#include <functional>
#include <queue>
#include <cmath>
#include <set>
#include <map>

#define rep(i,j,k) for(int i=j;i<k;i++)
#define Sort(x) sort((x).begin(),(x).end())
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define vi vector<int>
#define INF 1000000000
#define MOD 1000000007
#define pb push_back
#define MP make_pair
typedef long long ll;
typedef std::pair<int,int> pii;
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};

using namespace std;

int main() {
    int n;
    cin>>n;
    vector<ll> v(n);
    rep(i,0,n)cin>>v[i];
    Sort(v);
    int ct=0;
    ll a=-1;
    for(int i=v.size()-1;i>=0;i--){
        if(v[i]==v[i-1]&&v[i-1]==v[i-2]&&v[i-2]==v[i-3]){
            if(a!=-1)printf("%lld\n",a*v[i]);
            else printf("%lld\n",v[i]*v[i]);
            return 0;
        }
        if(v[i]==v[i-1]){
            if(!ct){
                a=v[i];
                ct++;
            }else {
                printf("%lld\n",a*v[i]);
                return 0;
            }
            while(v[i]==v[i-1])i--;
        }
    }
    cout<<0<<endl;
    
    return 0;
}


Measured memory_bytes: 1024
#include "bits/stdc++.h"
#include <math.h>
#include <time.h>
using namespace std;
const long long mod = 1000000007;
#define dump(x)                             \
    if (dbg) {                              \
        cout << #x << " = " << (x) << endl; \
    }
bool dbg = false;
long long gcd(long long a, long long b){
   if (a % b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a % b));
   }
}
long long lcm(long long a, long long b){
   return (a / gcd(a, b))* b;
}
//---------------------------------------------------------------------------------------------------
/*ここからいじる*/
//---------------------------------------------------------------------------------------------------

void solve(){
}
int main(){
    int n;
    cin >> n;
    vector<string>data(n);
    for(int i = 0; i < n; i++){
        cin >> data.at(i);
    }
    set<string>st;
    char c;
    c = data.at(0).at(0);
    for(int i = 0; i < n; i++){
        if(data.at(i).at(0) == c){
            st.insert(data.at(i));
            c = data.at(i).at(data.at(i).size() - 1);
        }
        else{
            cout << "No" << endl;
            break;
        }
        if(i == n - 1){
            if(st.size() == n){
                cout << "Yes" << endl;
            }
            else{
                cout << "No" << endl;
            }
        }
    }
    solve();
    return 0;
}



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

namespace Modop {
    ll modAdd(ll a, ll b) {
        return (a + b) % MOD;
    }
    
    ll modSub(ll a, ll b) {
        return (((a - b) % MOD) + MOD) % MOD;
    }
    
    ll modMul(ll a, ll b) {
        return ((a % MOD) * (b % MOD)) % MOD;
    }

    ll modExp(ll base, ll power) {
        if (power == 0) {
            return 1;
        } else {
            ll cur = modExp(base, power / 2); cur = cur * cur; cur = cur % MOD;
            if (power % 2 == 1) cur = cur * base;
            cur = cur % MOD;
            return cur;
        }
    }
    
    ll modInv(ll a) {
        return modExp(a, MOD - 2);
    }
    
    ll modDiv(ll a, ll b) {
        return modMul(a, modInv(b));
    }
}

using namespace Modop;

int n;
ll ans = 1;

int main() {
    cin.sync_with_stdio(0); cin.tie(0);
    cin >> n;
    vector<ll> a(n), b(n), c(n, 0);
    for (auto& i : a) cin >> i;
    for (auto& i : b) cin >> i;
    c[0] = a[0];
    c[n - 1] = b[n - 1];

    for (int i = 1; i < n; ++i) {
        if (a[i] > a[i - 1]) {
            if (c[i] && a[i] != c[i]) {
                cout << 0;
                return 0;
            } 
            c[i] = a[i];
        }
    }
    
    for (int i = n - 2; i >= 0; --i) {
        if (b[i] > b[i + 1]) {
            if (c[i] && b[i] != c[i]) {
                cout << 0;
                return 0;
            } 
            c[i] = b[i];
        }
    }

    if (b[0] != a[n - 1]) {
        cout << 0;
        return 0;
    }

    for (int i = 0; i < n; ++i) {
        if (a[i] < c[i] || b[i] < c[i]) {
            cout << 0;
            return 0;
        }
    }

    // for (int i = 0; i < n; ++i) cout << c[i] << ' ';
    // cout << '\n';

    for (ll i = 0, range = 0, last = 0; i < n; ++i) {
        if (c[i]) {
            if (range) {
                // cout << min(c[i], last) << ' ' << range << '\n';
                ans = modMul(ans, modExp(min(c[i], last), range));
                range = 0;
            } 
            last = c[i];
        } else ++range;
    }
    cout << ans;
}


Measured memory_bytes: 5572
#include <bits/stdc++.h>
using namespace std;
#define rep(i,N) for(int i=0,i##_max=(N);i<i##_max;++i)
#define repp(i,l,r) for(int i=(l),i##_max=(r);i<i##_max;++i)
#define per(i,N) for(int i=(N)-1;i>=0;--i)
#define perr(i,l,r) for(int i=r-1,i##_min(l);i>=i##_min;--i)
#define all(arr) (arr).begin(), (arr).end()
#define SP << " " <<
#define SPF << " "
#define SPEEDUP cin.tie(0);ios::sync_with_stdio(false);
#define MAX_I INT_MAX //1e9
#define MIN_I INT_MIN //-1e9
#define MAX_UI UINT_MAX //1e9
#define MAX_LL LLONG_MAX //1e18
#define MIN_LL LLONG_MIN //-1e18
#define MAX_ULL ULLONG_MAX //1e19
  typedef long long ll;
  typedef pair<int,int> PII;
  typedef pair<char,char> PCC;
  typedef pair<ll,ll> PLL;
  typedef pair<char,int> PCI;
  typedef pair<int,char> PIC;
  typedef pair<ll,int> PLI;
  typedef pair<int,ll> PIL; 
  typedef pair<ll,char> PLC; 
  typedef pair<char,ll> PCL; 

inline void YesNo(bool b){ cout << (b?"Yes" : "No") << endl;}
inline void YESNO(bool b){ cout << (b?"YES" : "NO") << endl;}
inline void Yay(bool b){ cout << (b?"Yay!" : ":(") << endl;}

int main(void){
  SPEEDUP
  cout << setprecision(15);
  string X;cin >> X;
  int count = 0;
  int sum = 0;
  rep(i,X.length()){
    if(X[i]=='T'){
      if(count==0)++sum;
      else --count;
    }else ++count;
  }
  cout << 2*sum << endl;
  return 0;
}


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

int main(){
  int n;
  cin >> n;
  
  long long s[100] = {0};
  for(int i=0; i<n; i++){
    string v;
    cin >> v;
    s[v[0]]++;
  }

  long long ss[5] = {s['M'], s['A'], s['R'], s['C'], s['H']};
  
  long long ans = 0;
  for(int i=0; i<3; i++){
    for(int j=i+1; j<4; j++){
      for(int k=j+1; k<5; k++){
        ans+=ss[i] * ss[j] * ss[k];
      }
    }
  }

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


Measured memory_bytes: 256
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

S = input()
T = input()

if S == T[:-1]:
    print('Yes')
else:
    print('No')

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

int main(){
  string s,t;
  cin >> s;
  t=s;
  int a=0,b=1;
  for(int i=0;i<s.size()-1;i++){
    if(s[i]=='0'&&s[i+1]=='0'){ 
      s[i+1]='1';
      a++;
    }
    else if(s[i]=='1'&&s[i+1]=='1'){
      s[i+1]='0';
      a++;
    }
  }
  
  if(s[0]=='0') t[0]='1';
  else t[0]='0';
  
  for(int i=0;i<s.size()-1;i++){
    if(t[i]=='0'&&t[i+1]=='0'){ 
      t[i+1]='1';
      b++;
    }
    else if(t[i]=='1'&&t[i+1]=='1'){
      t[i+1]='0';
      b++;
    }
  }
  
  cout << min(a,b) << endl;
  
  
}


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

#pragma region template


// -type-
using ll = long long;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vvl = vector<vl>;
using vs = vector<string>;


// -const-
const ll MOD = 1000000007;


const ll MAX = (ll)0x3f0000003f000000;
const double EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1},
          dy[] = {1, 0, -1, 0, 1, -1, -1, 1};


// -define-
#define F first
#define S second

#define reps(i, k, n) for (int i = (k), i##_len = (n); i < i##_len; ++i)
#define rep(i, n) reps(i, 0, n)
#define all(o) begin(o), end(o)
#define sz(a) ((int)(a).size())
#define debug(x) cerr << x << " (L:" << __LINE__ << ")" << '\n';


// -functions-

inline bool in(string s, string c) {
    return (s.find(c) != string::npos);
}
inline bool in(string s, char c) {
    return (s.find(c) != string::npos);
}

template <class T>
void print(T& x) {
    cout << x << '\n';
}

struct setup_main {
    setup_main() {
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout << fixed << setprecision(15);
    }
} setup_main_;

#pragma endregion

ll ans = 0;

void solve(long long N, long long H, std::vector<long long> a,
           std::vector<long long> b) {
    ll A = 0;
    for (auto&& i : a) {
        A = max(A, i);
    }

    std::sort(all(b), greater<ll>());

    for (auto&& bi : b) {
        if (bi > A) {
            H -= bi;
            ans++;
            if (H <= 0) return;
        }
    }
    ans += (H + A - 1) / A;
    return;
}

signed main() {
    long long N;
    scanf("%lld", &N);
    long long H;
    scanf("%lld", &H);
    std::vector<long long> a(N);
    std::vector<long long> b(N);
    for (int i = 0; i < N; i++) {
        scanf("%lld", &a[i]);
        scanf("%lld", &b[i]);
    }
    solve(N, H, std::move(a), std::move(b));

    print(ans);
    return 0;
}


Measured memory_bytes: 1792
#include <bits/stdc++.h>
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,s,e) for (int i=s;i<(e);i++)
#define All(v) (v).begin(),(v).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
using namespace std;
typedef long long llint;
typedef pair<int, int> P;
const int MOD = (int)1e9 + 7;
const int INF = 999999999;

struct edge
{
  int to;
  int cost;
};
typedef vector< vector < edge > > Graph;

template< typename T = int >
bool dijkstra(Graph &G, int st, vector< T > &min_cost)
{
  // 最短経路木Qを構成するノード
  typedef pair< T, int > QNode;

  priority_queue< QNode, vector< QNode >, greater< QNode > > Q;
  Q.emplace(0, st);
  min_cost[st] = 0;
  while(!Q.empty()) {
    auto fixedNode = Q.top();
    Q.pop();
    if(fixedNode.first > min_cost[fixedNode.second]) continue;
    for(auto &e : G[fixedNode.second]) {    // 確定ノードから伸びている辺e
        if(min_cost[e.to] == INF){
            min_cost[e.to] = fixedNode.first + e.cost;
            Q.emplace(min_cost[e.to], e.to);      // 各辺の行き先のノードをQに追加
        }else if(min_cost[e.to] != fixedNode.first + e.cost){
            return true;
        }
    }
  }
  return false;
}

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

    int N, M;
    cin >> N >> M;

    Graph G(N);
    vector< vector<int> > ls;
    REP(i, M){
        vector<int> v(3);
        edge e;
        cin >> v[0] >> v[1] >> v[2];
        v[0]--; v[1]--;
        REP(i, 2){
            e.to = v[1];
            e.cost = v[2];
            G[v[0]].pb(e);
            ls.pb(v);
            swap(v[0], v[1]);
            v[2] = -v[2];
        }
    }
    vector<int> min_cost(G.size(), INF);
    REP(i, N){
        if(min_cost[i] == INF){
            if(dijkstra(G, i, min_cost)){
                puts("No");
                return 0;
            }
        }
    }
    // REP(i, N){
    //     cout << min_cost[i] << "\n";
    // }
    puts("Yes");

    return 0;
}

Measured memory_bytes: 33832
#include <bits/stdc++.h>
const char nl = '\n';
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> pt;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int N = 404;

int n;
int a[N];
ll sum[N];
ll dp[N][N];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    sum[i] = sum[i-1] + a[i-1];
  }
  for (int sz = 0; sz < n; sz++) {
    for (int l = 0; l+sz < n; l++) {
      int r = l+sz;
      if (l == r) {
        dp[l][r] = 0;
        continue;
      } else if (l+1 == r) {
        dp[l][r] = a[l] + a[r];
        continue;
      }
      dp[l][r] = INFLL;
      for (int i = l; i < r; i++) {
        dp[l][r] = min(dp[l][r], dp[l][i] + dp[i+1][r] + sum[r+1] - sum[l]);
      }
    }
  }
  cout << dp[0][n-1] << nl;
  return 0;
}


Measured memory_bytes: 1536
#include <iostream>
#include <cstdio>
#include <vector>

using namespace std;

int n, m, cnt;
int cp[100005];

int fnd(int p)
{
    if(cp[p] == p) return p;
    return cp[p] = fnd(cp[p]);
}

int main()
{
    scanf("%d%d", &n, &m);
    cnt = n;
    for(int i = 1; i <= n; i++) cp[i] = i;
    while(m--)
    {
        int t1, t2;
        scanf("%d%d%*d", &t1, &t2);
        if(fnd(t1) != fnd(t2)) cp[fnd(t1)] = fnd(t2), cnt--;
    }
    printf("%d", cnt);
    return 0;
}


Measured memory_bytes: 640
#include<stdio.h>
int main()
{
    int A,B,i,c,d,e=0;
    long long int K;
    scanf("%d%d",&A,&B);
    if(A>B)
    {
        for(i=1;i<A;i++)
            {
                K=i;
                c=A-K;
                d=B-K;
                if(c<0)
                {
                    c=c*(-1);
                }
                if(d<0)
                {
                    d=d*(-1);
                }
                if(c==d)
                {
                   e++;
                    break;
                }
            }
    }
    else if(A<B)
    {
        for(i=1;i<B;i++)
            {
                K=i;
                c=A-K;
                d=B-K;
                if(c<0)
                {
                    c=c*(-1);
                }
                if(d<0)
                {
                    d=d*(-1);
                }
                if(c==d)
                {
                    e++;
                    break;
                }
            }
    }
    if(e>0)
    {
        printf("%lld\n",K);
    }
    else
    {
        printf("IMPOSSIBLE\n");
    }

}

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

int main()
{	
	int n;
	cin >> n;
	int build[n] = {};

	for(int i=0; i<n;i++){
		cin >> build[i];
	}

	int count =0;
	bool flag = 1;
	while(flag){
		flag =0;
		for(int j=n-1;j>0;j--){
			if(build[j] < build[j-1]){
				swap(build[j],build[j-1]);
				count++;
				flag = 1;
			} 
		}
	}
	

	for(int i=0;i<n;i++) cout << build[i] << " \n"[i==n-1];
	cout << count << endl;


    return 0;
}



Measured memory_bytes: 3104
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Main {

	public static void main(String[] args) throws IOException {
		List<Integer> values = new ArrayList<Integer>();
		String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
		String outString = "";
        String[] sarr = s.split(" ");
        int len = sarr.length;
        
        for(int i =0; i < len; i++){
            values.add(Integer.valueOf(sarr[i]));
        }

		Collections.sort(values);
		Collections.reverse(values);
		
		Iterator SortedList  = values.iterator();
		
		while(SortedList.hasNext()){
			outString = outString + SortedList.next() + " ";
		}

		System.out.println(outString.trim());
	}

}

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

ll K;
ll d;
ll N;

int main()
{
    
    ll ans;
    ll keep;
    cin >> N >> K;
    N %= K;
    ans = min(N,abs(N-K));
    //cout << N%K << endl;
    cout << ans << endl;
}


Measured memory_bytes: 256
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cstring>
#include <iterator>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#include <fstream>
#include <iomanip>
#include <cassert>
//#include <utility>
//#include <memory>
//#include <functional>
//#include <deque>
//#include <cctype>
//#include <ctime>
//#include <numeric>
//#include <list>
//#include <iomanip>

//#if __cplusplus >= 201103L
//#include <array>
//#include <tuple>
//#include <initializer_list>
//#include <forward_list>
//
//#define cauto const auto&
//#else

//#endif

using namespace std;


typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()
#define DEBUG
#ifdef DEBUG
#define dump(x)  cout << #x << " = " << (x) << endl;
#define debug(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#else
#define dump(x) 
#define debug(x) 
#endif
#define LDcout(x,n) fixed<<setprecision(n)<<x

#define MOD 1000000007LL
#define EPS 1e-8
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define maxs(x,y) x=max(x,y)
#define mins(x,y) x=min(x,y)

void mainmain(){
	int n;
	while(cin>>n,n){
		vint v(10);
		rep(i,n){
			int t;
			cin>>t;
			v[t]++;
		}
		rep(i,10){
			if(v[i]==0) cout<<"-"<<endl;
			else{
				rep(j,v[i]){
					cout<<"*";
				}
				cout<<endl;
			}
		}
	}
}


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

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

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

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

void Main()
{
	int a, b, c, d;
	cin >> a >> b >> c >> d;
	cout << min(a,b) + min(c,d) << endl;
	return;
}

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
	return 0;
}

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

a,b = 0,0
for i in s:
    if i == '.':
        a += 1 #白の個数
    else:
        b += 1 #黒の個数

min_c = c = 10**10
counta, countb = 0, 0
for k, i in enumerate(s):
    if s[k] == '.':
        counta += 1
    if s[k] == '#':
        countb += 1
        c = (a - counta) + (countb - 1)
    if min_c > c:
        min_c = c
        
print(min(a,b, min_c))

Measured memory_bytes: 3500
/**
 * 	author: krayr
 * 	created: 18.05.2019
**/

#include <bits/stdc++.h>
using namespace std;
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back
#define mp make_pair
#define forn(i, s, n) for(int i = s; i < int(n); i++)

typedef long long ll;
//~ const double pi = 2*acos(0.0);
//~ const long long mod = 1e9+7;
//~ const int MAX_N = 1e6+5;

int main(int argc, char* argv[]){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
       
    #ifdef _DEBUG
    freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    #endif
    
	double a, b; cin >> a >> b;
	double t; cin >> t;
	t+= 0.05;
	
	int cnt = 1;
	int currTime = a;
	while(currTime < t){
		cnt++;
		currTime = a*cnt;
		
	}
	cnt--;
	if(t > a){
		cout << int(b*cnt);
	}else{
		cout << 0;
	}
	
	cout << "\n";
	
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <map>
using namespace std;
#define rep(i, n) for(int i=0;i<(n);++i)
int main(){
  string S;
  cin >> S;
  int len = S.size();
  map<int, int> mii;
  int mode = 1;// right:0 left:1
  int cnt[2];
  int kiPos = 0, guPos = 0;
  cnt[0] = 0;
  cnt[1] = 0;
  rep(i, len) {
    if (mode == 1) {
      if (S[i] == 'R') {
        mii[guPos] = cnt[0];
        mii[kiPos] = cnt[1];
        cnt[0] = 0;
        cnt[1] = 0;
        mode = 0;
      }
      ++cnt[i % 2];
    } else {
      if (S[i] == 'L') {
        if (i % 2) { // 奇数
          kiPos = i;
          guPos = i - 1;
        } else { // 偶数
          guPos = i;
          kiPos = i - 1;
        }
        mode = 1;
      }
      ++cnt[i % 2];
    }
  }
  mii[guPos] = cnt[0];
  mii[kiPos] = cnt[1];

  rep(i, len) {
    cout << mii[i] << " ";
  }
  cout << "\n";

  return 0;
}


Measured memory_bytes: 5248
#include<bits/stdc++.h>
using namespace std;
int main()
{
  string s;
  cin>>s;
  sort(s.begin(),s.end());
  if(s[0]==s[2])
    cout<<"No";
  else if(s[0]==s[1]&&s[2]==s[3])
  {
    cout<<"Yes";}
  else{
    cout<<"No";}
  return 0;
}

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

int main() {
  int N;
  cin >> N;
  map<string, int> mp;
  for(int i=0;i<N;i++){
    string blue;
    cin >> blue;
    mp[blue] = mp[blue] + 1;
  }
  int M;
  cin >> M;
  for(int i=0;i<M;i++){
    string red;
    cin >> red;
    mp[red] = mp[red] -1;
  }
  int ans = 0;
  map<string, int> ::iterator ite;
  for(ite =mp.begin() ; ite != mp.end() ; ++ite){
    if(ite->second > ans)ans = ite->second;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <cstring>
#include <stdio.h>
#define lowbit(x) x&(-x)

using namespace std;
const int maxn=105;
typedef long long ll;


int main(){
    ios::sync_with_stdio(0);
    ll a,b,c,d;
    cin>>a>>b>>c>>d;
    ll minn=min(a,b);
    ll minn2=min(c,d);
    cout<<minn+minn2<<endl;
   return 0;
}


Measured memory_bytes: 256
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <set>
#include <map>
#define REP(i,n) for(ll i = 0; i < (ll)n; i++)
#define INF 1000000000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;

const ll max_a = 1000000000;

struct BIT{
  ll M;
  std::vector<ll> bit;
  BIT(ll n){
    init(n);
  }
  void init(ll n){
    M = n;
    bit.resize(M+1);
    fill(bit.begin(),bit.end(),0);
  }
  ll sum(ll i){
    ll s = 0;
    while(i>0){
      s += bit[i];
      i -= i & -i;
    }
    return s;
  }
  void add(ll i, ll x){
    while(i<=M){
      bit[i] += x;
      i += i & -i;
    }
  }
};

int main(){
  ll n;
  cin >> n;
  ll a[n];
  ll a_sort[n];
  REP(i,n){
    cin >> a[i];
    a_sort[i] = a[i];
  }
  sort(a_sort,a_sort+n);
  if(n==1){
    cout << a[0] << endl;
    return 0;
  }
  ll lb = 0, ub = n;
  while(ub-lb>1){
    ll x = (lb + ub)/2;
    ll b[n];
    REP(i,n) b[i] = (a[i]<a_sort[x]) ? -1 : 1;
    ll s[n];
    REP(i,n){
      s[i] = (i==0) ? b[i] : s[i-1]+b[i];
    }
    ll cnt = 0;
    BIT bit(2*n+1);
    bit.add(n,1);
    REP(j,n){
      cnt += bit.sum(s[j]+n);
      bit.add(s[j]+n,1);
    }
    if(cnt>=ceil(n*(n+1)/2/2)){
      lb = x;
    }else{
      ub = x;
    }
  }
  cout << a_sort[lb] << endl;
  return 0;
}


Measured memory_bytes: 4968