akhauriyash/Code-Regression huggingface dataset:
#[allow(unused_imports)]
use proconio::{
    fastout, input,
    marker::{Bytes, Chars, Isize1, Usize1},
};
use std::cmp::{min, Reverse};

#[allow(unused_macros)]
macro_rules! echo {
    ($($e:expr),+) => ( { $(println!("{}", $e))+ } );
}

fn main() {
    input! {
        n: usize,
        m: usize,
        s: usize,
        cs: [(Usize1, Usize1, usize, u64); m],
        exchanges: [(usize, u64); n],
    }

    let mut cities = vec![vec![]; n];
    for (u, v, a, b) in cs {
        cities[u].push((v, a, b));
        cities[v].push((u, a, b));
    }
    // n * a
    let max_money = 50 * 50;
    // dp[city][money] = time
    let mut dp = vec![vec![std::u64::MAX; max_money + 1]; n];
    // queue for (time, city, money) tuple
    let mut q = std::collections::BinaryHeap::new();

    dp[0][min(s, max_money)] = 0;
    q.push(Reverse((0, 0, min(s, max_money))));

    while let Some(Reverse((time, city, money))) = q.pop() {
        if dp[city][money] < time {
            continue;
        }

        // step forward to neighbor cities
        for &(another, cost_m, cost_t) in cities[city].iter() {
            // need more money!!
            if money < cost_m {
                continue;
            }

            let time = time + cost_t;
            let money = money - cost_m;

            if dp[another][money] > time {
                dp[another][money] = time;
                q.push(Reverse((time, another, money)));
            }
        }

        // step forward to money exchange
        let money = min(money + exchanges[city].0, max_money);
        let time = time + exchanges[city].1;
        if dp[city][money] > time {
            dp[city][money] = time;
            q.push(Reverse((time, city, money)));
        }
    }

    dp.iter()
        .skip(1)
        .map(|i| *i.iter().min().unwrap())
        .for_each(|e| echo!(e));
}


Measured memory_bytes: 4696
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
#define MuhammedAly ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define all(x) (x).begin(),(x).end()
#define allr(x) (x).rbegin(),(x).rend()
#define pb push_back
#define eb emplace_back
#define popc(x) __builtin_popcount(x)
#define LLMX 1e18
#define NotinRange(n,m) i<0||i>=(n)||j<0||j>=(m)
#define mem(x,y) memset((x),(y),sizeof (x));
#define sz(a) (int)(a).size()
#define inFile(s) freopen((s),"r",stdin)
#define	outFile(s) freopen((s),"w",stdout)
#define biEdge(v, a, b) (v)[(a)].pb((b)), (v)[(b)].pb((a))
#define pi (2 * acos(0))
#define X real()
#define Y imag()
#define cross(a, b) ((conj(a) * (b)).Y)
#define dot(a, b) ((conj(a) * (b)).X)
#define vec(a,b) ((b) - (a))
#define ll long long
typedef long double ld;
typedef complex<double>point;
typedef tuple<int, int, int> line;
typedef pair<point, point>segment;
typedef vector<point>polygon;
const int N = 1e5 + 5, M = 50 + 5, OO = 0x3f3f3f3f, mod = 1e9 + 7, base = 131, mod2 = 1000136437, mod3 = 998244353;
const double EPS = 1e-9;
int n, w[N], v[N], W;
ll memo[100][N];
ll solve(int i = 0, int rem = W)
{
    if(i == n)
        return 0;
    ll &ret = memo[i][rem];
    if(~ret)
        return ret;
    ret = solve(i + 1, rem);
    if(rem - w[i] >= 0)
        ret = max(ret, solve(i + 1, rem - w[i]) + v[i]);
    return ret;
}
int main()
{
    MuhammedAly
    mem(memo, -1);
    cin >> n >> W;
    for(int i = 0 ; i < n ; i++)
        cin >> w[i] >> v[i];
    cout << solve();
    return 0;
}

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

using namespace std;
typedef long long ll;
const int modd=1e9+7;
int n,k;
ll a[200005],pre[200005],pos[200005];
ll ans=0;
bool cmp(const ll &a,const ll &b) {
	return a>b;
}
int main() {
	ios::sync_with_stdio(false);
	cin>>n>>k;
	for(int i=1; i<=n+1; i++)
		a[i]=i-1;
	sort(a+1,a+n+2);
	for(int i=1; i<=n+1; i++)
		pre[i]=pre[i-1]+a[i];
	sort(a+1,a+n+2,cmp);
	for(int i=1; i<=n+1; i++)
		pos[i]=pos[i-1]+a[i];
	for(int j=k; j<=n+1; j++) {
		ans+=pos[j]-pre[j]+1;
		ans%=modd;
	}
	cout<<ans;
	return 0;
}

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

#define rep(i, n) for (int i = 0; i < int(n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define reps(i, n) for (int i = 1; i <= int(n); i++)
#define rreps(i, n) for (int i = int(n); i >= 1; i--)
#define repc(i, n) for (int i = 0; i <= int(n); i++)
#define rrepc(i, n) for (int i = int(n); i >= 0; i--)
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define repic(i, a, b) for (int i = int(a); i <= int(b); i++)
#define each(x, y) for (auto &x : y)
#define all(a) (a).begin(), (a).end()
#define bit32(x) (1 << (x))
#define bit64(x) (1ll << (x))
#define sz(v) ((int) v.size())

using namespace std;

using i64 = long long;
using f80 = long double;
using vi32 = vector<int>;
using vi64 = vector<i64>;
using vf80 = vector<f80>;
using vstr = vector<string>;

inline void yes() { cout << "Yes" << endl; exit(0); }
inline void no() { cout << "No" << endl; exit(0); }
inline i64 gcd(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); if (a % b == 0) return b; return gcd(b, a % b); }
inline i64 lcm(i64 a, i64 b) { if (min(a, b) == 0) return max(a, b); return a / gcd(a, b) * b; }
template <typename T> class pqasc : public priority_queue<T, vector<T>, greater<T>> {};
template <typename T> class pqdesc : public priority_queue<T, vector<T>, less<T>> {};
template <typename T> inline void amax(T &x, T y) { x = max(x, y); }
template <typename T> inline void amin(T &x, T y) { x = min(x, y); }
template <typename T> inline T exp(T x, i64 n, T e = 1) { T r = e; while (n > 0) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; }
template <typename T> istream& operator>>(istream &is, vector<T> &v) { each(x, v) is >> x; return is; }
template <typename T> ostream& operator<<(ostream &os, vector<T> &v) { rep(i, v.size()) { if (i) os << ' '; os << v[i]; } return os; }
void solve(); int main() { ios::sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(16); solve(); return 0; }

void solve() {
  int n, m;
  cin >> n >> m;
  for (int l = 1, r = m; l < r; l++, r--) {
    cout << l << " " << r << endl;
  }
  for (int l = m + 1, r = m * 2 + 1; l < r; l++, r--) {
    cout << l << " " << r << endl;
  }
}

Measured memory_bytes: 3656
#include<iostream>

using namespace std;

int main () {

    char c;
    int num;
    int cnt[26] = {0};

    while (cin >> c) {

        // if (c == '0') break;

        if ('a' <= c && c <= 'z') {
            num = c - 'a';
            cnt[num]++;
        } else if ('A' <= c && c <= 'Z') {
            num = c - 'A';
            cnt[num]++;
        }
    }

    for (int i = 0; i < 26; i ++) {
        char a = i + 'a';
        cout << a << " : " << cnt[i] << endl;
    }

    return 0;

}


Measured memory_bytes: 3100
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <utility>
#include <climits>
#include <bitset>
#include <cmath>

#define BP 1000000007

using namespace std;

typedef unsigned long long ull;
typedef unsigned long ul;
typedef long long ll;
typedef unsigned int ui;

typedef vector<ull> vull;
typedef vector<ll> vll;
typedef vector<ui> vui;
typedef vector<long> vl;
typedef vector<int> vi;
typedef vector<vl> vvl;
typedef vector<vi> vvi;

typedef pair<ull, ull> pull;
typedef pair<ll, ll> pll;
typedef pair<ui, ui> pui;
typedef pair<long, long> pl;
typedef pair<int, int> pi;

template <class T>
void printv(vector<T> vc) {
    for (auto e: vc) cout << e << " ";
    cout << endl;
}

template <class T>
void printvv(vector<T> vvc) { 
    for (auto vc: vvc) {
        for (auto e: vc) cout << e << " ";
        cout << endl;
    }
}

int main() {
    int n, a, b; cin >> n >> a >> b;
    string str; cin >> str;

    int pass = 0;
    int bc = 0;
    for (int i = 0; i < n; ++i) {
        if (str[i] == 'a') {
            if (pass < a+b) {
                cout << "Yes" << endl;
                ++pass;
            } else {
                cout << "No" << endl;
            }

        } else if (str[i] == 'b') {
            if (pass < a+b && bc < b) {
                ++pass, ++bc;
                cout << "Yes" << endl;

            } else {
                cout << "No" << endl;
            }

        } else {
            cout << "No" << endl;
        }
    }

    return 0;
}

Measured memory_bytes: 832
// http://www.igaris.com/math/c.pdf

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define NEXTP(x) next_permutation(x.begin(),x.end())
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define FORR(I,A,B) for(ll I = (B-1); I >= (A); --I)
const ll INF=1e18+7;
const ll MOD=1e9+7;

class comb{
	public:
	vector<ll> f,fr;
	ll MOD;
	//a^(p-1) = 1 (mod p)(p->Prime numbers)
	//a^(p-2) = a^(-1)
	ll calc(ll a,ll b,ll p){//a^(b) mod p   
		if(b==0)return 1;
		ll y = calc(a,b/2,p);y=(y*y)%p;
		if(b & 1) y = (y * a) % p;
		return y;
	}
	void init(ll n,ll mod){//input max_n
		MOD = mod;
		f.resize(n+1);
		fr.resize(n+1);
		f[0]=fr[0]=1;
		for(ll i=1;i<n+1;i++){
			f[i] = (f[i-1] * i) % mod;
			fr[i] = calc(f[i],mod-2,mod);
		}
	}
	ll nCr(ll n,ll r){
		return f[n] * fr[r] % MOD * fr[n-r] % MOD;
	}
};


int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,k;
	cin >> n >> k;
	if(n>k){
		cout << 0 << endl;
		return 0;
	}
	comb co;
	co.init(n+k,MOD);
	cout << co.nCr(k,n) << endl;
}


Measured memory_bytes: 3192
import sys
sys.setrecursionlimit(1000000000)
import math
from fractions import gcd
from itertools import count, permutations
from functools import lru_cache
from collections import deque, defaultdict
from pprint import pprint
ii = lambda: int(input())
mis = lambda: map(int, input().split())
lmis = lambda: list(mis())
INF = float('inf')
N1097 = 10**9 + 7

def meg(f, ok, ng):
    while abs(ok-ng)>1:
        mid = (ok+ng)//2
        if f(mid):
            ok=mid
        else:
            ng=mid
    return ok
#

def main():
    H,N=mis()
    AB = tuple(tuple(mis()) for _ in range(N))
    DP = [INF] * (H+1)
    DP[0] = 0
    for h in range(1, H+1):
        for i in range(N):
            DP[h] = min(DP[h], (DP[h-AB[i][0]]+AB[i][1] if h-AB[i][0]>0 else AB[i][1]))

    print(DP[-1])

main()


Measured memory_bytes: 68328
N = int(input())
X = [-1] * N
A = [int(a)-1 for a in input().split()]
a, b, c = 0, 0, 0
for i in range(N+1):
    if X[A[i]] >= 0:
        a = X[A[i]]
        b = i - X[A[i]] - 1
        c = N - i
        break
    else:
        X[A[i]] = i

P = 10**9+7
fa = [1]
for i in range(1, N + 10):
    fa.append(fa[-1] * i % P)
fainv = [pow(fa[-1], P-2, P)]
for i in range(1, N + 10)[::-1]:
    fainv.append(fainv[-1] * i % P)
fainv = fainv[::-1]

def C(i, j):
    if j < 0 or j > i: return 0
    return fa[i] * fainv[j] % P * fainv[i-j] % P

for K in range(1, N+2):
    ans = C(N-1, K)
    ans += C(a+c, K-1) + 2 * (C(a+b+c, K-1) - C(a+c, K-1))
    ans += C(a+b+c, K-2)
    ans %= P
    print(ans)

Measured memory_bytes: 21216
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = int(A); I < int(B); ++I)
#define FORR(I,A,B) for(ll I = int((B)-1); I >= int(A); --I)
#define TO(x,t,f) ((x)?(t):(f))
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define REV(x) (reverse(x.begin(),x.end())) //reverse
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define NEXTP(x) next_permutation(x.begin(),x.end())
const ll INF=1e18+7;
const ll MOD=1e9+7;

template<typename T>
class Seg_Tree{
public:
    vector<T> dat;
    int n;
    void initsize(int n0){
        int k=1;
        while(1){
            if(n0<=k){
                n=k;
                dat.resize(2*n-1);
                for(int i=0;i<2*n-1;i++)dat[i]=INF;
                break;
            }
            k*=2;
        }
    }
 
    //i banme wo x nisuru
    void update(int i,T x){
        i += n-1;
        dat[i] = x;
        while(i>0){
            i = (i-1) / 2;
            dat[i] = min(dat[i*2+1],dat[i*2+2]);
        }
    }
 
    //[a,b)
    T query0(int a,int b,int k,int l,int r){
        if(r<=a || b<=l)return INF;
        if(a<=l && r<=b)return dat[k];
        else{
            T vl = query0(a,b,k*2+1,l,(l+r)/2);
            T vr = query0(a,b,k*2+2,(l+r)/2,r);
            return min(vl,vr);
        }
    }
 
    //min [a,b)
    T query(int a,int b){
        return query0(a,b,0,0,n);
    }
};


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

	ll N;
	cin >> N;
	vector<ll> h(N+1),a(N+1),idx(N+1);

	FOR(i,1,N+1){
		cin >> h[i];
		idx[h[i]] = i;
	}
	FOR(i,1,N+1){
		cin >> a[i];
	}

	Seg_Tree<ll> sg; //RMQ
	sg.initsize(N+1);
	FOR(i,1,N+1){
		ll k = sg.query(1,idx[i]);
		if(k==INF)k=0;
		k -= a[idx[i]];
		sg.update(idx[i],k);
	}

	cout << -sg.query(1,N+1) << endl;

}

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

long n;
unsigned long long k,sum;
//bool ada[100005]={0};
int main(){
	n=0;
	cin>>n;
	map<int,bool>m;
	for(int i =0; i<n; i++){
		cin>>k;
		if(m[k]){
			m[k]=false;
			sum--;
		}
		else {
			m[k]=true;
			sum++;
		}
	}
	cout<<sum<<"\n";
	
	
	return 0;
}

Measured memory_bytes: 4992
#include<stdio.h>

int main(void){
    int a, b, c, tmp;
    scanf("%d%d%d", &a, &b, &c);
    while(1){
        if(a > b) {tmp = a; a = b; b = tmp;}
        else if( b > c) {tmp = b; b = c; c = tmp;}
        else break;
    }
    printf("%d %d %d\n", a, b, c);
    return 0;
}


Measured memory_bytes: 2072
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> llP;

ll mod(ll a, ll b){
  ll ret=a%b;
  if(ret<0) ret+=b;
  return ret;
}

ll modpow(ll a,ll b,ll c){
  ll res=1;
  while(b>0){
    if(b&1) res=mod(res*a,c);
    a=mod(a*a,c);
    b>>=1;
  }
  return res;
}

int main() {
  string s,t;
  cin>>s>>t;
  if(t.substr(0,s.size())==s) cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
  return 0;
}


Measured memory_bytes: 3632
#include<stdio.h>

void Exchange(int *a, int *b);

int main(void){
	int A, B, C;
	scanf("%d %d %d", &A, &B, &C);
	
    Exchange(&A, &B);
    Exchange(&A, &C);
	
	printf("%d %d %d", A, B, C);
	return 0;
}

// 2つの数字を入れ替える
void Exchange(int *a, int *b){
	int box;
	box = *a;
	*a = *b;
	*b = box;
}

Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string s;
    cin>>n>>s;
    int cnt=0,ans=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='D')
        {
            ans=max(cnt,ans);
            cnt--;
        }
        else
        {
            cnt++;
        }
    }
    cout<<max(cnt,ans)<<endl;
}


Measured memory_bytes: 384
#include<iostream>
#include<vector>

int main() {
	int N, M;
	std::cin >> N >> M;
	std::vector<int> height(N);
	for (int i = 0; i < N; i++) std::cin >> height[i];
	std::vector<bool> flag(N, true);

	for (int i = 0; i < M; i++) {
		int A, B;
		std::cin >> A >> B;
		if (height[A - 1] < height[B - 1]) flag[A - 1] = false;
		else if (height[A - 1] == height[B - 1]) {
			flag[A - 1] = false;
			flag[B - 1] = false;
		}
		else flag[B - 1] = false;
	}

	int answer = 0;
	for (int i = 0; i < N; i++) {
		if (flag[i]) answer++;
	}
	std::cout << answer << std::endl;
	return 0;

}

Measured memory_bytes: 3760
#include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
using namespace std;
int main(){
	int n,k;
	cin>>n;
	int count=0;
	vector<int> p(n,0);
	for(int i=0;i<n;i++){
		cin>>k;
		p[i]=k;
	}
	for(int i=1;i<n-1;i++){
		if(p[i-1]<=p[i]){
			if(p[i]<p[i+1]){
				count++;
			}
		}else{
			if(p[i]>=p[i+1]){
				count++;
			}
		}
	}
	cout << count <<endl;
	return 0;
}

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

using namespace std;

int n;
int main(){
	cin >> n;
	for(int i = 1; i <= 9; ++i) for(int j = 1; j <= 9; ++j) if(n == i*j){
		cout << "Yes" << endl;
		return 0;
	}
	cout << "No" << endl;
}

Measured memory_bytes: 256
import numpy as np
def main():
    order = [len(el) for el in list(map(str, input().rstrip().split("T")))]
    x, y = map(int, input().split())
    order_x = order[2::2]
    order_y = order[1::2]
    L_x, L_y = len(order_x), len(order_y)
    n = 10000
    dp_x = [np.zeros(2*n, dtype=np.bool) for _ in range(L_x + 1)]
    dp_y = [np.zeros(2*n, dtype=np.bool) for _ in range(L_y + 1)]
    dp_x[0][n + order[0]] = True
    dp_y[0][n] = True
    for i, d in enumerate(order_x):
        dp_x[i+1][d:] |= dp_x[i][:2*n-d]
        dp_x[i+1][:2*n-d] |= dp_x[i][d:]
    for i, d in enumerate(order_y):
        dp_y[i+1][d:] |= dp_y[i][:2*n-d]
        dp_y[i+1][:2*n-d] |= dp_y[i][d:]
    if dp_x[L_x][n + x] and dp_y[L_y][n + y]:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()


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

int N, A, B, C;
int l[10];
int INF = 1e9 + 7;

int dfs(int n, int a, int b, int c)
{
    if (n == N)
    {
        if (a == 0 || b == 0 || c == 0)
            return INF;
        else
            return abs(A - a) + abs(B - b) + abs(C - c);
    }
    int res0 = dfs(n + 1, a, b, c);
    int res1 = dfs(n + 1, a + l[n], b, c) + (a == 0 ? 0 : 10);
    int res2 = dfs(n + 1, a, b + l[n], c) + (b == 0 ? 0 : 10);
    int res3 = dfs(n + 1, a, b, c + l[n]) + (c == 0 ? 0 : 10);
    return min({res0, res1, res2, res3});
}

int main()
{
    cin >> N >> A >> B >> C;
    for (int i = 0; i < N; i++)
    {
        cin >> l[i];
    }
    cout << dfs(0, 0, 0, 0) << endl;
}

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

int main()
{
	char S[100],T[100],U[100];
	int A,B;

	scanf("%s %s %d %d %s", S,T,&A,&B,U);

	if(strcmp(S,U) == 0)
	{
		A--;
	}
	else if(strcmp(T,U) == 0)
	{
		B--;
	}
	printf("%d %d\n",A,B);
	

	return 0;
}


Measured memory_bytes: 128
//10-04-2K20
    //Happy Coding :)

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi 2*acos(0.0)
#define f first
#define s second
#define pb push_back
#define gap ' '
#define fastio {ios_base::sync_with_stdio(false);cin.tie(NULL);}
#define mod 1000000007
#define MAX 1000000000000
#define N 305

int main()
{
    fastio;
    int i;
    string s;
    cin>>s;

    if(s[0]=='7' || s[1]=='7' || s[2]=='7')
        cout<<"Yes";
    else
        cout<<"No";
    cout<<"\n";
    return 0;   //:D
}





Measured memory_bytes: 3612
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FIO ios_base::sync_with_stdio(0);cin.tie(0);
 
const int N = 2e5+5, M = 1e6+6, OO = 0x3f3f3f3f;
int tc;
int h, w, k;
string grid[M];
int dir[] = {-1,1,0,0};
int dic[] = {0,0,-1,1};
int dis[M], directions[M];
 
bool valid(int r, int c)
{
    return r >= 0 && r < h && c >= 0 && c < w && grid[r][c] != '@';
}
 
 
int main()
{
    int x1, x2, y1, y2;
    scanf("%d %d %d ", &h, &w, &k);
    scanf("%d %d %d %d ", &x1, &y1, &x2, &y2);
 
    for(int i = 0 ; i< h ;i++)
    {
        string s;
        getline(cin, s);
        grid[i] = s;
    }
    x1--; y1--; x2--; y2--;
    memset(dis, -1, sizeof dis);
    dis[x1*w + y1] = 0;
    directions[x1*w+y1] = 15;
    queue<pair<int, int> > q;
    q.push({x1, y1});
    while(!q.empty())
    {
        int ur = q.front().first, uc = q.front().second;
        // cout<<ur<<" "<<uc<<endl;
        q.pop();
        if(ur == x2 && uc == y2)
            break;
        for(int i = 0 ; i< 4 ; i++)
        {
            int steps = 1, vr, vc;
            while(steps <=k)
            {
                vr = ur + (dir[i] * steps);
                vc = uc + (dic[i] * steps);
                if(valid(vr, vc))
                {
                    if(!((directions[vr*w+vc]>>i) & 1))
                    {
                        if(!directions[vr*w+vc])
                        {
                            dis[vr*w+vc] = dis[ur*w+uc] + 1;
                            q.push({vr, vc});
                        }
                        directions[vr*w+vc] |= (1 << i);
                    }
                    else if(dis[vr*w + vc] ==dis[ur*w+uc] + 1 )
                    {
                        steps++;
                        continue;
                    }
                    else
                        break;
                    
                }
                else
                    break;
                steps++;
            }
        }
    }
    printf("%d", dis[x2*w+y2]);
    return 0;
}


// Another Submission


Measured memory_bytes: 43196
#include<bits/stdc++.h>
#define rep(i, n) for(int i=0; i<n; i++)
#define repo(i, n) for(int i=1; i<=n; i++)
#define ssort(a) sort(a.begin(), a.end())
#define INF 1001001001
#define INFll 100100100100100
// debug用
#define PrintVec(x) for (auto elementPrintVec: x) { cout << elementPrintVec << " "; } cout << endl;

using namespace std;
using ull = unsigned long long;
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

int main(){
  int n, p;
  cin >> n >> p;
  string s;
  cin >> s;
  ll ans=0;
  if(p==2 || p==5){
    rep(i, n){
      if((int)(s[i]-'0')%p==0){
        ans+=i+1;
      }
    }
    cout << ans << endl;
  } else {
    int ten=1;
    vector<int> d(n+1);
    map<int, int> mp;
    mp[0]++;
    for(int i=n-1; i>=0; i--){
      int a=(int)(s[i]-'0')*ten%p;
      d[i]=(d[i+1]+a)%p;
      mp[d[i]]++;
      ten*=10;
      ten%=p;
    }
    for(auto p:mp){
      ans+=(ll)p.second*(p.second-1)/2;
    }
    cout << ans << endl;
  }
  return 0;
}


Measured memory_bytes: 4648
#include<bits/stdc++.h>
using namespace std;
#define int  long long 
#define speedrap ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int be(int a,int b,int mod) {if(b == 0) return 1; if(b%2 == 1) return (a*be(a,b-1,mod))%mod; else return (be(a,b/2,mod)*be(a,b/2,mod))%mod;}
const int mod = 1e9 + 7;
#define PI 3.14159265359
#define pf push_front
#define pb push_back
#define bitcount __builtin_popcountll
#define setbitcnt __builtin_ctz
#define sc(a) scanf("%d", &a);
#define sz(s) (int)s.size()
#define eb emplace_back
#define pf push_front
#define mkp(x,y) make_pair(x,y)
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define roundup(a,b)  std::cout << std::fixed;std::cout << std::setprecision(b); std::cout << a
const int siz = (1e6 + 10);
const int logi = 21;
const double epsilon = 1e-6;
bool cmp(const int &a,const int &b)
{
  return (abs(a-b)>1);
}



signed main()
{
	#ifndef ONLINE_JUDGE
 	freopen("in.txt", "r", stdin);
 	freopen("out.txt", "w", stdout);
 	#else
// // online submission
 	#endif 
	speedrap
	int t;
	t = 1;
	//cin>>t;
	int val = 0;
	while(t--)
	{
		int  n;
		cin>>n;
		int a[n];
		int pre[n];
		memset(pre,0,sizeof(pre));
		for(int i = 0 ; i < n ; i ++)
		{
			cin>>a[i];
			if(i == 0)
			{
				pre[i] = a[i];
			}
			else
			{
				pre[i] = pre[i-1] + a[i];
			}
		}

		int sum = 0;

		for(int i = 0 ; i < n-1 ; i++)
		{

			int val1 = a[i];
			int val2 = pre[n-1] - pre[i];

			val1 %= mod;
			val2 %= mod;

			int val = (val1*val2)%mod;

			sum = (sum + val)%mod;
		}

		cout<<sum;
	}
}



Measured memory_bytes: 6736
/**
 *  author: MahfuzAhmed
 *  created: 24.08.2019
**/

#include <bits/stdc++.h>
using namespace std;
#define debug(x) cout << '>' << #x << ':' << x << endl;
//~ const double pi = 2*acos(0.0);
//~ const long long mod = 1e9+7;
//~ const int max_n = 1e6+5;
typedef long long ll;


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
    
    int a, b;
    char x;
    
    cin >> a >> x >> b;
    
    if(x == '+')
        cout << a+b;
    else
        cout << a-b;
        
    cout << "\n";
    
    
    return 0;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
#define rep(i,n) for (ll i = 0; i < (ll)(n); ++i)
#define rrep(a,b,n) for(ll a = (ll)(b); a < (ll)(n); a++)
#define mrep(i,n) for (ll i = (ll)(n); i > 0; i--)
#define be(v) (v).begin(), (v).end()
#define dcout cout << fixed << setprecision(20) 
ll INF = 1LL << 60;
ll mod = 1e9 + 7;

int main() {
	int n; cin >> n;
	vint p(n);
	rep(i,n) cin >> p[i];
	int ans = 0;
	rep(i,n) {
		if(p[i]==i+1) {
			if(i!=n-1) swap(p[i],p[i+1]);
			else swap(p[i],p[i-1]);
			ans++;
		}
	}
	rep(i,n) {
		if(p[i]==i+1) {
			if(i!=n-1) swap(p[i],p[i+1]);
			else swap(p[i],p[i-1]);
			ans++;
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define REP(i, n) FOR(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define bit(x) (1L << (x))
using ll = long long;
using namespace std;

template<typename T>
vector<T> make_v(size_t a,T b){return vector<T>(a,b);}
 
template<typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v(ts...))>(a,make_v(ts...));
}

using graph = vector<vector<int>>;
using p = pair<int, int>;

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

  int n; cin >> n;
  graph g(n);
  vector<int> d(n, 0);
  REP(i, n-1){
    int s, t; cin >> s >> t;
    --s; --t;

    g[s].push_back(t);
    g[t].push_back(s);

    d[s]++; d[t]++;
  }

  priority_queue<p, vector<p>, greater<p>> pq;
  REP(i, n){
    pq.push(make_pair(d[i], i));
  }

  vector<int> a(n, -1), c(n);
  REP(i, n) cin >> c[i];

  sort(c.begin(), c.end());

  if(pq.top().first == n-1){
    const int v = pq.top().second;
    REP(i, n) a[i] = c[i];
    swap(a[0], a[v]);
  } else {
    int i=0;
    while(!pq.empty()){
      const p top = pq.top(); pq.pop();
      const int v = top.second, dis = top.first;
      if(dis != d[v]) continue;

      a[v] = c[i++];

      for(int u : g[v]){
        if(a[u] == -1){
          d[u]--;
          pq.push(make_pair(d[u], u));
        }
      }
    }
  }


  function<int(int, int)> rec = [&](int v, int bv){
    int ret = 0;

    for(int u : g[v]){
      if(u != bv){
        ret += rec(u, v) + min(a[u], a[v]);
      }
    }

    return ret;
  };

  cout << rec(0, -1) << endl;
  REP(i, n) cout << a[i] << " ";
  cout << endl;

  return 0;
}


Measured memory_bytes: 2048
#include <cstdio>
int main(){
    int n, k;
    scanf("%d %d", &n, &k);
    double ans;
    for (int i = 1; i <= n; i++){
        int cur = i;
        double p = 1;
        if (i >= k){
            ans += p;
            continue;
        }
        while (true){
            cur *= 2;
            p /= 2.0;
            if (cur >= k){
                ans += p;
                break;
            }
        }
    }
    printf("%.18lf\n", ans / double(n));
}

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

int main(){
  int n;
  int count=0;
  int a=0;
  cin>>n;
  int button[n];
  for(int i=0;i<n;i++) cin>>button[i];
  while(1){
    count++;
    if(button[a]==2){
      break;
    }else{
      a=button[a]-1;
    }
    
    if(count==n){
      count=-1;
      break;
    }
  }
  cout<<count<<endl;
}


Measured memory_bytes: 640
//
#include <vector>
#include <set>
#include <map>
#include <array>
#include <iostream>
#include <cstdio>
#include <cstdint>
#include <algorithm>
#include <numeric>
#include <bitset>
#include <iomanip>
//
using ll = long long;
using namespace std;
#define rep(i,n) for(ll i=0;i<(n);++i)
#define outD(fpv) cout << setprecision(20) << fpv << endl;
inline void yes(bool cond) { cond ? puts("Yes") : puts("No"); }
inline void YES(bool cond) { cond ? puts("YES") : puts("NO"); }
template<typename Type> inline void chmin(Type& a, Type b) { if (a > b) a = b; }
template<typename Type> inline void chmax(Type& a, Type b) { if (a < b) a = b; }
template<typename Arr, typename Type> inline void fill(Arr& arr, Type v) { std::fill(arr.begin(), arr.end(), v); }
template<typename Type> inline void sort(Type& arr) { std::sort(arr.begin(), arr.end(), std::less<>()); }
template<typename Type> inline void rsort(Type& arr) { std::sort(arr.begin(), arr.end(), std::greater<>()); }
template<typename Type> inline Type nth(vector<Type>& arr, size_t pos) { std::nth_element(arr.begin(), arr.begin() + pos, arr.end()); return arr[pos]; }
template<typename Type> inline void unique(Type& arr) { sort(arr); arr.erase(unique(arr.begin(), arr.end()), arr.end()); }
constexpr ll INF_I = std::numeric_limits<ll>::max();
constexpr ll BIGP = 1000000007L;
constexpr double INF_F = std::numeric_limits<float>::infinity();
constexpr double PI = 3.14159265358979323846L;
using P = pair<ll, ll>;
//
int main()
{
    cin.tie(0); ios::sync_with_stdio(false);
    ll A, B, T;
    cin >> A >> B >> T;
    cout << (T * 10 + 5) / (A * 10) * B;
    return 0;
}

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

int main()
{
	int A, B;
	string op;
	cin >> A >> op >> B;

	if (op == "+") {
		cout << A + B << endl;
	}
	else if (op == "-") {
		cout << A - B << endl;
	}
}






Measured memory_bytes: 3628
#include <stdio.h>
int main(void)
{
	int a, b, c;

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

	if(a<=b && b<=c)
		printf("%d %d %d\n", a, b, c);
	else if(a<=c && c<=b)
		printf("%d %d %d\n", a, c, b);
	else if(b<=a && a<=c)
		printf("%d %d %d\n", b, a, c);
	else if(b<=c && c<=a)
		printf("%d %d %d\n", b, c, a);
	else if(c<=a && a<=b)
		printf("%d %d %d\n", c, a, b);
	else
		printf("%d %d %d\n", c, b, a);

	return 0;
}

Measured memory_bytes: 596
#include<iostream>
#include<cmath>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

int main(){
	int n;
	cin>>n;
	int t[n],x[n],y[n];
	rep(i,n)cin>>t[i]>>x[i]>>y[i];
	int nowx=0,nowy=0,nowt=0;
	bool check=true;
	rep(i,n){
		int diffs=abs(nowx-x[i])+abs(nowy-y[i]);
		int difft=t[i]-nowt;
		if(diffs>difft||(int)abs(diffs-difft)%2){
			check=false;
			break;
		}
	}
	cout<<(check?"Yes":"No")<<endl;
	return 0;
}

Measured memory_bytes: 1408
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const ll mod = 1e9 + 7;
const ll MAXX = 1e18;

vector<ll> hold[1000001];

//for eTour
pair<ll, ll> tim[1000001];
bool pass[1000001];
ll cnt = 0;

void eTour(ll sta = 1) {
	pass[sta] = true;
	tim[sta].first = ++cnt;
	for (ll i = 0; i < hold[sta].size(); i++) {
		if (!pass[hold[sta][i]]) {
			eTour(hold[sta][i]);
		}
	}
	tim[sta].second = cnt;
}

//for Tree
ll tree[1000001];
ll lazy[1000001];

void propa(ll node, ll left, ll right) {
	if (lazy[node]) {
		tree[node] += (right - left + 1) * lazy[node];
		if (left != right) {
			lazy[node * 2] += lazy[node];
			lazy[node * 2 + 1] += lazy[node];
		}
		lazy[node] = 0;
	}
}

void update(ll node, ll left, ll right, ll uLeft, ll uRight, ll val) {
	propa(node, left, right);
	if (uLeft > right || uRight < left) {
		return;
	}
	if (uLeft <= left && right <= uRight) {
		lazy[node] = val;
		propa(node, left, right);
		return;
	}
	ll mid = (left + right) / 2;
	update(node * 2, left, mid, uLeft, uRight, val);
	update(node * 2 + 1, mid + 1, right, uLeft, uRight, val);
	tree[node] = tree[node * 2] + tree[node * 2 + 1];
}

ll query(ll node, ll left, ll right, ll pos) {
	propa(node, left, right);
	if (pos < left || pos > right) {
		return 0;
	}
	if (left == right && pos == left) {
		return tree[node];
	}
	ll mid = (left + right) / 2;
	return query(node * 2, left, mid, pos) + query(node * 2 + 1, mid + 1, right, pos);
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	//freopen("INP.txt", "r", stdin);
	//freopen("OUT.txt", "w", stdout);
	ll n, m;
	cin >> n >> m;
	for (ll i = 1; i < n; i++) {
		ll a, b;
		cin >> a >> b;
		hold[a].push_back(b);
		hold[b].push_back(a);
	}
	eTour();
	//cerr << 1 << '\n';
	// for (ll i = 1; i <= n; i++) {
		// cerr << tim[i].first << ' ' << tim[i].second << '\n';
	// }
	for (ll i = 0; i < m; i++) {
		ll a, b;
		cin >> a >> b;	
		ll l = tim[a].first, r = tim[a].second;
		update(1, 1, n, l, r, b);
	}
	//cerr << 1 << '\n';
	for (ll i = 1; i <= n; i++) {
		cout << query(1, 1, n, tim[i].first) << ' ';
	}
	return 0;
}

Measured memory_bytes: 61440
import math
class KochCurve(object):
	x = 0.0
	y = 0.0
	def koch(self, n, a, b):
		if n == 0:
			return 
		s = KochCurve()
		t = KochCurve()
		u = KochCurve()
		th = math.pi * 60.0 / 180.0
		s.x = (2.0 * a.x + 1.0 * b.x) / 3.0
		s.y = (2.0 * a.y + 1.0 * b.y) / 3.0
		t.x = (1.0 * a.x + 2.0 * b.x) / 3.0
		t.y = (1.0 * a.y + 2.0 * b.y) / 3.0
		u.x = (t.x - s.x) * math.cos(th) - (t.y - s.y) * math.sin(th) + s.x
		u.y = (t.x - s.x) * math.sin(th) + (t.y - s.y) * math.cos(th) + s.y	
		KochCurve().koch(n - 1, a, s)
		msg = "%.8f %.8f" % (s.x, s.y)
		print(msg)
		KochCurve().koch(n - 1, s, u)
		msg = "%.8f %.8f" % (u.x, u.y)
		print(msg)
		KochCurve().koch(n - 1, u, t)
		msg = "%.8f %.8f" % (t.x, t.y)
		print(msg)
		KochCurve().koch(n - 1, t, b)
a = KochCurve()
b = KochCurve()
n = int(input())
a.x = 0
a.y = 0
b.x = 100
b.y = 0
msg = "%.8f %.8f" % (a.x, a.y)
print(msg)
KochCurve().koch(n, a, b)
msg = "%.8f %.8f" % (b.x, b.y)
print(msg)


Measured memory_bytes: 5760
//インクルード(アルファベット順)
#include<algorithm>//sort,二分探索,など
#include<bitset>//固定長bit集合
#include<chrono>//実行時間計測
#include<cmath>//pow,logなど
#include<complex>//複素数
#include<deque>//両端アクセスのキュー
#include<functional>//sortのgreater
#include<iomanip>//setprecision(浮動小数点の出力の誤差)
#include<iostream>//入出力
#include<iterator>//集合演算(積集合,和集合,差集合など)
#include<map>//map(辞書)
#include<numeric>//iota(整数列の生成),gcdとlcm,accumulate
#include<queue>//キュー
#include<set>//集合
#include<stack>//スタック
#include<string>//文字列
#include<unordered_map>//順序保持しないmap
#include<unordered_set>//順序保持しないset
#include<utility>//pair
#include<vector>//可変長配列

using namespace std;
typedef long long ll;

//マクロ
//forループ
//引数は、(ループ内変数,動く範囲)か(ループ内変数,始めの数,終わりの数)、のどちらか
//Dがついてないものはループ変数は1ずつインクリメントされ、Dがついてるものはループ変数は1ずつデクリメントされる
#define REP(i,n) for(ll i=0;i<ll(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=a;i<=ll(b);i++)
#define FORD(i,a,b) for(ll i=a;i>=ll(b);i--)
//xにはvectorなどのコンテナ
#define ALL(x) x.begin(),x.end() 
#define SIZE(x) ll(x.size()) 
//定数
#define INF 1000000000000 //10^12:∞
#define MOD 1000000007 //10^9+7:合同式の法
#define MAXR 100000 //10^5:配列の最大のrange
//略記
#define PB push_back //挿入
#define MP make_pair //pairのコンストラクタ
#define F first //pairの一つ目の要素
#define S second //pairの二つ目の要素

#define int long long

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 H,W,K;
int I,J;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//ここに関数宣言

int count_black(string blocka[]){

	int counter=0;

	REP(i,H)REP(j,W){
		if (blocka[i][j]=='#')
		{
			counter++;
		}
	}

	return counter;
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
signed main(){
    //入力の高速化用のコード
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int ans=0;

cin>>H>>W>>K;

string block[H];
string block_copy[H];


REP(i,H){cin>>block[i];}
REP(bit_i,1<<H)REP(bit_j,1<<W){

	
	REP(y,H){block_copy[y]=block[y];}

	REP(i,H)
	{
		if ( bit_i & ( 1<< i) ){ REP(j2,W){block_copy[i][j2]='R';} }
	}

	REP(j,W)
	{
		if ( bit_j & ( 1<< j)){ REP(i2,H){block_copy[i2][j]='R';} }
	}

	if(count_black(block_copy) == K){ans++;}


}


cout<<ans<<endl;























return 0;

}

Measured memory_bytes: 3616
#include<bits/stdc++.h>
using namespace std;
 
int main(){
  int N;
  cin >> N;
  vector<int> a(N);
  for(int i = 0; i < N; i++) cin >> a[i];
  
    sort(a.begin(), a.end(), greater<int>());  
    int Alice = 0;
    int Bob = 0;
    for (int i = 0; i < N; i++) {
        if (i % 2 == 0) { 
            Alice += a[i];
        }
        else { 
            Bob += a[i];
        }
    }
    cout << Alice - Bob << endl;
 
}

Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>

#define null INT_MAX

typedef struct{
  int key;
  int parent;
  int right;
  int left;
}Heep;


int main(){
  Heep H[251];
  int n, i, x;

  scanf("%d",&n);
  if( n<1 || 251<n )exit(1);

  for( i=1 ; i<=n ; i++ ){
    H[i].parent = null;
    H[i].right = null;
    H[i].left = null;
  }
  

  for( i=1 ; i<=n ; i++ ){
    scanf("%d",&H[i].key);
    if( H[i].key < -2000000000 || 2000000000 < H[i].key ) exit(2);

    if( i!=1 ){
      H[i].parent = H[i/2].key;
      
      if( i%2 == 0 ){
	H[i/2].left = H[i].key;
      }
      else{
	H[i/2].right = H[i].key;
      }
    }
  }

  for( i=1 ; i<=n ; i++ ){
    printf("node %d: key = %d, ", i , H[i].key);
    
    if( H[i].parent != null )     printf("parent key = %d, ", H[i].parent);
   
    if( H[i].left != null )       printf("left key = %d, ", H[i].left);
    
    if( H[i].right != null )      printf("right key = %d, ", H[i].right);

    printf("\n");
  }  

return 0;
}
							    

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef complex<double> Point;

#define PI acos(-1.0)
#define EPS 1e-10
const ll INF = 1e16;
const ll MOD = 1e9 + 7;

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,N) for(int i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()
#define EQ(a,b) (abs((a)-(b))<EPS)
#define EQV(a,b) ( EQ((a).real(), (b).real()) && EQ((a).imag(), (b).imag()) )
#define fi first
#define se second
#define N_SIZE (1LL << 20)
#define NIL -1

ll sq(ll num) { return num*num; }
ll mod_pow(ll x, ll n) {
	if (n == 0)return 1;
	if (n == 1)return x%MOD;
	ll res = sq(mod_pow(x, n / 2));
	res %= MOD;
	if (n % 2 == 1) {
		res *= x;
		res %= MOD;
	}
	return res;
}

ll cal(ll n){return n*(n+1)*(n+2)/6;}

ll dp[1000010],dp2[1000010];

int main(){
	ll N;
	vector<ll> v;
	for(int i=0;cal(i)<=1000000;i++)v.push_back(cal(i));
	ll M=v.size();
	for(int i=0;i<1000010;i++)dp2[i]=INF;
	dp2[0]=0;
	for(int i=0;i<1000010;i++){
		for(int j=0;j<M;j++){
			if(i-v[j]<0)break;
			dp2[i]=min(dp2[i],dp2[i-v[j]]+1);
		}
	}
	vector<ll> v_k;
	for(int i=0;cal(i)<=1000000;i++){
		ll x=cal(i);
		if(cal(i)%2==0)continue;
		v_k.push_back(cal(i));
	}
	for(int i=0;i<1000010;i++)dp[i]=INF;
	dp[0]=0;
	for(int i=0;i<1000010;i++){
		for(int j=0;j<v_k.size();j++){
			if(i-v_k[j]<0)break;
			dp[i]=min(dp[i],dp[i-v_k[j]]+1);
		}
	}
	while(1){
		cin>>N;
		if(N==0)break;
		cout<<dp2[N]<<" "<<dp[N]<<endl;
	}
}


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

int calc(string s) {
    int _min, _max;
    sort(s.begin(), s.end());
    _min = atoi(s.c_str());
    reverse(s.begin(), s.end());
    _max = atoi(s.c_str());
    return (_max - _min);
}

int main() {
    int n;
    cin >> n;
    string s;
    for (int i = 0; i < n; i++) {
        cin >> s;
        cout << calc(s) << endl;
    }
    return 0;
}

Measured memory_bytes: 0
from math import gcd

def lcm(a,b):
    return a*b//gcd(a,b)

n = int(input())
A = list(map(int,input().split()))

l = lcm(A[0], A[1])
for i in range(2,n):
    l = lcm(l, A[i])

ans = 0
for a in A:
    ans += (l-1)%a
    
print(ans)

Measured memory_bytes: 73844
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (ll i=0; i<(n); ++i)
typedef long long ll;
bool debug=false;
string yes = "Yes";
string no = "No";
ll e=pow(10,9)+7;
ll pnum(ll n, ll i){
  return i*(n-i+1)+1;
}
int main(){
  ll n,k; cin >> n >> k;
  ll ans=0;
  for(ll i=k; i<=n+1;i++){
    ans+=pnum(n,i);

  }
  ans%=e;
  cout << ans;
  return 0;
}


Measured memory_bytes: 3640
#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <iostream>
#include <functional>
#include <bitset>
#include <algorithm>
#include <vector>
#include <forward_list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void)
{
    int a, b;

    cin >> a >> b;

    cout << max({a + b, a - b, a * b}) << endl;
}

Measured memory_bytes: 256
program double_helix
  implicit none
  character(1) :: b
  read(*,*) b
  write(*,'(a)') achar(pair(ichar(b)-65)+65)
contains
  integer function pair(n)
    integer, intent(in) :: n
    pair = (228-12*n)/(11*n+12)
  end
end program double_helix

Measured memory_bytes: 256
//  Created by conan1024hao in 2019.
//  Copyright © 2019 conan1024hao. All rights reserved.
//  専用ライブラリです、自由にコピーして構いません。
//  感谢看我的代码！Wechat:conan1024hao QQ:810396815
#pragma GCC optimize ("O3")
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <unordered_map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
#define IINF 10e8
#define INF 10e16
#define MOD 1000000007
#define mod 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define Endl endl
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define chmax(x,y) x=mmax(x,y)
#define chmin(x,y) x=mmin(x,y)
#define all(x) (x).begin(),(x).end()
#define siz(x) (ll)(x).size()
#define PI acos(-1.0)
using namespace std;
//using Int=int_fast64_t;
typedef long long int ll;
typedef pair<ll,ll>Pll;
typedef pair<int,int>Pin;
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int dx[8]={-1,0,1,0,1,1,-1,-1};
int dy[8]={0,-1,0,1,1,-1,1,-1};
char dir[4]={'u','l','d','r'};
ll cmp1(pair<ll,string>a,pair<ll,string> b){
        if(a.fi!=b.fi)
        return a.fi<b.fi;
        else
        return a.se<b.se;
}
//--------------------------------------------------------------------------

//---------------------------------------------------------------------------
int main(){//問題をちゃんと見ろ!!!!!!!!!!!!!!!!!      llか??????????        memset()!!!!!!!!!!!!       ペナを減らせ!!!!!!!!!!!!!
    cin.tie(0);
    ios::sync_with_stdio(false);
    //-------------------------------
    int n;string s;cin>>n>>s;int ans=0;
    for(int i=0;i<n-2;i++){
        if(s[i]=='A'&&s[i+1]=='B'&&s[i+2]=='C')
            ans++;
    }
    cout<<ans<<Endl;
    //-------------------------------
    return 0;
}
//---------------------------------------------------------------------------







Measured memory_bytes: 256
class UnionFind
    def initialize(n)
        @size = Array.new(n, 1)
        @par = (0..n-1).to_a
    end

    def root(x)
        @par[x] = (x == @par[x] ? x : root(@par[x]))
    end

    def unite(x, y)
        rx = root(x)
        ry = root(y)

        if rx != ry
            rx, ry = ry, rx if @size[rx] < @size[ry]
            @par[ry] = rx
            @size[rx] += @size[ry]
        end
    end

    def same?(x, y)
        root(x) == root(y)
    end

    def size(x)
        @size[root(x)]
    end
end

n,m,k = gets.split.map(&:to_i)

uf = UnionFind.new(n+1)
f = Array.new(n+1, 0)
m.times do 
    a, b = gets.split.map(&:to_i)
    uf.unite(a, b)
    f[a] += 1
    f[b] += 1
end

g = Array.new(n+1, 0)
k.times do 
    c, d = gets.split.map(&:to_i)
    if uf.same?(c, d)
        g[c] += 1
        g[d] += 1
    end
end

ans = Array.new(n)
1.upto(n) do |i|
    ans[i-1] = uf.size(i) - f[i] - g[i] - 1
end

puts ans.join(" ")


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

int main() {
  int n, q, t;
  string name;
  cin >> n >> q;
  queue<pair<string, int> > Q;
  for (int i = 0; i < n; i ++) {
    cin >> name >> t;
    Q.push(make_pair(name, t));
  }

  pair<string, int> u;
  int elaps = 0, a;

  while (!Q.empty()) {
    u = Q.front(); Q.pop();
    a = min(u.second, q);
    u.second -= a;
    elaps += a; //経過時間
    if (u.second > 0) {
      Q.push(u);
    } else {
      cout << u.first << " " << elaps << endl;
    }
  }
}



Measured memory_bytes: 6372
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0

s = readline().rstrip().decode('utf-8')
t = readline().rstrip().decode('utf-8')
ans = 0
for i in range(len(s)):
    if s[i] != t[i]:
        ans += 1

print(ans)

Measured memory_bytes: 68056
import java.util.*;
public class Main {

	public static void main(String[] args) {
		int i1, i2;
		Scanner sc = new Scanner(System.in);
		i1 = sc.nextInt();
		i2 = sc.nextInt();
		System.out.printf("%d %d %.5f\n", (i1 / i2), (i1 % i2), ((double)i1 / (double)i2));
	}
}

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

int main(){
  int n,m;
  cin >> n >> m;
  vector<string> a(n), b(n);
  for(int i = 0; i < n; i++){
    cin >> a[i];
  }
  for(int i = 0; i < m; i++){
    cin >> b[i];
  }

  bool flag1;

  for(int i = 0; i < n-m+1; i++){
    for(int j = 0; j < n-m+1; j++){
      flag1 = true;
      for(int k = 0; k < m; k++){
        for(int l = 0; l < m; l++){
          if(a[i+k][j+l] != b[k][l]){
            flag1 = false;
            break;
          }
        }
        if(flag1 == false) break;
      }
      if(flag1 == true) break;
    }
    if(flag1 == true) break;
  }
  if(flag1 == true) cout << "Yes" << endl;
  else cout << "No" << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#define rep(i, n)   for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int main(){
    string s;   cin >> s;
    rep(i, s.size()){
        cout << 'x';
    }
    cout << endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
using namespace std;
inline int read(){
	int v=0,f=1;
	char c=getchar();
	while (c<'0' || c>'9'){
		if (c=='-') f=-1;
		c=getchar();
	}
	while (c>='0' && c<='9'){
		v=v*10+c-'0';
		c=getchar();
	}
	return v*f;
}
const int Maxn=2000005;
const int mod=998244353;
int fact[Maxn],ivf[Maxn];
int qp(int x,int p){
	int ret=1;
	while (p){
		if (p&1) ret=1ll*ret*x%mod;
		x=1ll*x*x%mod;
		p>>=1; 
	}
	return ret;
}
int inv(int x){
	return qp(x,mod-2);
}
int C(int x,int y){
	return 1ll*fact[x]*ivf[y]%mod*ivf[x-y]%mod;
}
void _init(){
	fact[0]=1;
	for (int i=1;i<Maxn;i++){
		fact[i]=1ll*fact[i-1]*i%mod;
		ivf[i]=inv(fact[i]);
	}
	ivf[0]=1;
}
int main(){
	_init();
	int n,m;
	scanf("%d %d",&n,&m);
	int ans=0;
	for (int i=m;i>=0;i-=2){
		if (i>n) continue;
		int rem=(3*m-i)/2;
		ans+=1ll*C(n,i)*C(rem+n-1,n-1)%mod;
		if (ans>=mod) ans-=mod;
	}
//	cout<<ans<<endl;
	for (int i=m;i>=0;i-=2){
		if (i>n) continue;
		int rem=(3*m-i)/2;
		rem-=m;
		ans-=1ll*C(n,i)*C(rem+n-1,n-1)%mod*i%mod;
		if (ans<0) ans+=mod;
	}
	for (int i=m;i>=0;i-=2){
		if (i>n) continue;
		int rem=(3*m-i)/2;
		rem-=m+1;
		if (rem<0) continue;
		ans-=1ll*C(n,i)*C(rem+n-1,n-1)%mod*(n-i)%mod;
		if (ans<0) ans+=mod;
	}
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 16000
h,w = (int(i) for i in input().split())
a = [input() for _ in range(h)]

tate, yoko = [], []
for i in range(h):
  if any([s == '#' for s in a[i]]):
    tate.append(i)
for i in range(w):
  if any(a[j][i] == '#' for j in range(h)):
    yoko.append(i)

for i in tate:
  for j in yoko:
    print(a[i][j], end='')
  print('')

Measured memory_bytes: 4468