akhauriyash/Code-Regression huggingface dataset:
# https://atcoder.jp/contests/abc053/tasks/abc053_b

s = input()

l = len(s)
for i in range(len(s)):
    if s[i] == 'A':
        l = i
        break

r = 0
for i in range(len(s))[::-1]:
    if s[i] == 'Z':
        r = i
        break
ans = r - l + 1
print(ans)

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

const int N=100005;
int a[N],b[N];
int main(){
	int n,m,x,y,ans;
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++){
		a[i]=0;
		b[i]=1;
	}
	a[1]=1;
	for (int i=1;i<=m;i++) {
		scanf("%d%d",&x,&y);
		b[x]--;
		b[y]++;
		if (a[x]==1) a[y]=1;
		if (b[x]==0) a[x]=0;
	}
	ans=0;
	for (int i=1;i<=n;i++) if (b[i]&&a[i]) ans++;
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
#define _all(arg) begin(arg),end(arg)

template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0; }
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0; }

using namespace std;
using pii=pair<int,int>;

int main(void){
	int n, m;
	while(cin>>n>>m,n) {
		vector<pii> pd(n);
		long long res = 0;
		rep(i,n) {
			cin >> pd[i].second >> pd[i].first;
			res += pd[i].first * pd[i].second;
		}
		sort(pd.begin(), pd.end());
		reverse(pd.begin(), pd.end());
		for(int i = 0; i < n && m > 0; i++) {
			res -= min(pd[i].second, m) * pd[i].first;
			m -= pd[i].second;
		}
		cout << res << endl;
	}
        
	return 0;
}

Measured memory_bytes: 3176
//Bismillahirrahmanirrahim
//█▀█─█──█──█▀█─█─█
//█▄█─█──█──█▄█─█▄█
//█─█─█▄─█▄─█─█─█─█

#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target ("avx2")

#include <bits/stdc++.h>

using namespace std;

typedef long long lo;
typedef pair< lo,lo > PII;

#define fi first
#define se second
#define int long long
#define mp make_pair
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)

const lo inf = 1000000000000000000;
const lo KOK = 100000;
const lo LOG = 30;
const lo li = 500005;
const lo mod = 1000000007;

int n,m,b[li],a[li],k,flag,t;
int cev;
string s;
vector<int> v;

main(void){
	scanf("%lld %lld",&n,&k);
	while(n--){
		int x,y;
		scanf("%lld %lld",&x,&y);
		double at=x*x;
		double bt=y*y;
		at+=bt;
		at=sqrt(at);
		if(at<=k)cev++;
	}
	printf("%lld\n",cev);
	return 0;
}



Measured memory_bytes: 3776
N=int(input())
A=sorted(map(int,input().split()))[::-1]
from heapq import heappop,heappush
class dictmax():
    def __init__(self):
        self.d={}
        self.h=[]
    def add(self,k,v):
        if k not in self.d:
            heappush(self.h,-k)
            self.d[k]=0
        self.d[k]+=v
        if self.d[k]==0:
            del self.d[k]
    def max(self):
        while -self.h[0] not in self.d:
            heappop(self.h)
        return -self.h[0]#,self.d[-self.h[0]]
d=dictmax()
d.add(A[0],1)
r=0
for a in A[1:]:
    m=d.max()
    r+=m
    d.add(m,-1)
    d.add(a,2)
print(r)


Measured memory_bytes: 31680
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using pint = pair<int,int>;
ll mod = 1000000007,mod2 = 998244353;

void chmin(long long &a, long long b) { if (a > b) a = b; }
void chmax(long long &a, long long b) { if (a < b) a = b; }
const long long INF = 1LL<<60;
ll gcd(ll a,ll b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}

//重みある時
using Edge = pair<int,int>;
using Graph = vector<vector<Edge> >;

vector<bool> beki(1010);

int main() {
    int n,l;
    cin >> n >> l;
    vector<string> s(n);
    for (int i = 0; i < n; ++i) {
        cin >> s[i];
    }
    sort(s.begin(),s.end());
    for (int i = 0; i < n; ++i) {
        cout << s[i];
    }
    return 0;
}


Measured memory_bytes: 256
#include <string>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define N 4
#define N2 16

struct puzzle{
  int x[N2];
  int space,MD,cost;
  bool operator < (const puzzle &p)const{
    for(int i=0;i<N2;i++){
      if(x[i]==p.x[i]) continue;
      return x[i]>p.x[i];
    }
    return false;
  }
};

int dx[4] = {0,-1,0,1};
int dy[4] = {1,0,-1,0};
char dir[4] = {'r','u','l','d'};
int MDT[N2][N2];

struct state{
  puzzle puz;
  int est;
  bool operator < (const state &s)const{
    return est > s.est;
  }
};

int astar(puzzle);
int getallmd(puzzle);

int main(){
  puzzle in;

  for(int i=0;i<N2;i++){
    for(int j=0;j<N2;j++){
      MDT[i][j]=abs(i/N-j/N)+abs(i%N-j%N);
    }
  }
      
  for(int i=0;i<N2;i++){
    cin >> in.x[i];
    if(in.x[i] == 0){
      in.x[i] = N2;
      in.space = i;
    }
  }
  cout << astar(in) << endl;

  return 0;
}

int astar(puzzle p){
  priority_queue<state> PQ;
  map<puzzle,bool> V;
  puzzle m,k;
  state ini;
  p.MD=getallmd(p);
  p.cost = 0;
  ini.puz = p;
  ini.est = getallmd(p);
  PQ.push(ini);

  while(!PQ.empty()){
    state st=PQ.top(); PQ.pop();
    m=st.puz;
    
    if(m.MD == 0) return m.cost;
    V[m]=true;
    
    int px=m.space/N;
    int py=m.space%N;
    for(int r=0;r<4;r++){
      int tx=px+dx[r];
      int ty=py+dy[r];
      if(tx<0 || ty<0 || tx>=N || ty>=N) continue;
      k=m;

      k.MD -= MDT[tx*N+ty][k.x[tx*N+ty]-1];
      k.MD += MDT[px*N+py][k.x[tx*N+ty]-1];
      
      swap(k.x[px*N+py],k.x[tx*N+ty]);
      k.space=tx*N+ty;
      if(!V[k]){
	k.cost++;
	state newp;
	newp.puz=k;
	newp.est=k.cost+k.MD;
	PQ.push(newp);
      }
    }
  }
  return -1;
}

int getallmd(puzzle pz){
  int sum=0;
  for(int i=0;i<N2;i++){
    if(pz.x[i]==N2)continue;
    sum+=MDT[i][pz.x[i]-1];
  }
  return sum;
}



Measured memory_bytes: 257768
#include <stdio.h>

int main() {
  int N=0;
  int a[100]={0};
  int sum=0,ans=100000000;

  scanf("%d",&N );
  for(int i=0;i<N;i++) scanf("%d",&a[i]);
  for(int i=-100;i<=100;i++){
    sum=0;
    for(int j=0;j<N;j++){
      sum += (a[j]-i)*(a[j]-i);
    }
    if(ans>sum) ans=sum;
  }
  printf("%d\n",ans);
  return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
vector<int64_t> f_memo(90,0);
int64_t f(int x){
  if (x ==0) return 2;
  else if(x==1) return 1;
  else if(f_memo.at(x) != 0){
    return f_memo.at(x);
  }else{
    return f_memo.at(x) = f(x-1)+f(x-2);
  }
}

int main() {
  int n;
  scanf("%d" ,&n);
  int64_t ans;
  ans = f(n);
  printf("%lld",ans);
}
  




Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
typedef long long ll;


string s;
int k;

int toA[100010];

void f()
{
    int n = s.length();
    
    for (int i = 0; i < n; i++) {
        if (s[i] != 'a')
            toA[i] = 'z' - s[i] + 1;
        else
            toA[i] = 0;
        
    }
    cout << endl;
    
    for (int i = 0; i < n; i++) {
        
        if (toA[i] <= k) {
            s[i] = 'a';
            //cout << s[i] << endl;
            k -= toA[i];
        }
    }
    //cout << "k: " << k << endl;
    if (k != 0) {
        s[n-1] = (s[n-1] - 'a' + k) % 26 + 'a';
    }
    
    
    cout << s;
    
}

int main() {
    
    cin >> s;
    cin >> k;
    
    f();
 
    return 0;
}


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

#define rep(i, n) for (int i = (int)(0); i < (int)(n); ++i)
#define reps(i, n) for (int i = (int)(1); i <= (int)(n); ++i)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define rreps(i, n) for (int i = ((int)(n)); i > 0; i--)
#define irep(i, m, n) for (int i = (int)(m); i < (int)(n); ++i)
#define ireps(i, m, n) for (int i = (int)(m); i <= (int)(n); ++i)
#define FOR(e, c) for (auto &e : c)
#define SORT(v, n) sort(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define rvisort(v) sort(v.begin(), v.end(), greater<int>());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout<<d<<endl;
#define coutd(d) cout<<std::setprecision(10)<<d<<endl;
#define cinline(n) getline(cin,n);
#define replace_all(s, b, A) replace(s.begin(),s.end(), b, A);
// #define int long long
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) int(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using ul = unsigned long;

template<class T> inline bool chmax(T& A, T b) { if (A < b) { A = b; return 1; } return 0; }
template<class T> inline bool chmin(T& A, T b) { if (A > b) { A = b; return 1; } return 0; }

ll GCD(ll A, ll b) { return b ? GCD(b, A%b) : A; }
ll LCM(ll A, ll b) { return A / GCD(A, b)* b; }

const int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};

inline bool inside(int y, int x, int H, int W) {
    return (y >= 0 && x >= 0 && y < H && x < W);
}

const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;


signed main()
{
  cin.tie( 0 ); ios::sync_with_stdio( false );
  int n; cin>>n;
  string s,ss;
  cin>>s>>ss;
  ll ans;
  bool front;
  int i=0;
  if(s[i]==ss[0]){
    front=true;
    ans=3;
    i++;
  }else{
    front=false;
    ans=6;
    i+=2;
  }
  while(i<n){
    if(front){
      if(s[i]==ss[i]){
        (ans*=2)%=MOD;
        i++;
      }else{
        (ans*=2)%=MOD;
        i+=2;
        front=false;
      }
    }else{
      if(s[i]==ss[i]){
        i++;
        front=true;
      }else{
        (ans*=3)%=MOD;
        i+=2;
      }
    }
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 256
n,m = list(map(int, input().split()))
d = {}
ll = []
food = [i for i in range(m)]
food2 = []
for i in range(n):
	l = list(map(int, input().split()))
	ll.append(l)
for i in range(n):
	for j in range(1,ll[i][0]+1):
		if not ll[i][j] in food2:
			food2.append(ll[i][j])
			d.setdefault(ll[i][j],0)
		d[ll[i][j]] += 1
print(len([i for i in d.values() if i == n]))
#print(list(d.values()).count(n))

Measured memory_bytes: 3064
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set> 
#include <bitset>
#include <cmath>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
typedef long long ll;
 
int main () {
    
    int n, T;
    cin >> n >> T;
 
    int ans = 1000000000;
    bool tle = true;
 
    for (int i = 0; i < n; i++) {
        int c, t;
        cin >> c >> t;
        if(t <= T) {
            tle = false;
            if(c <= ans) {
                ans = c;
            } 
        } 
    }
    
    if(tle) {
        cout << "TLE" << endl;
    } else {
        cout << ans << endl;  
    } 
    
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <math.h>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
typedef long long ll;
const ll INF = 101010101000;
ll dp[110][101010]; //N*Vの配列。最低の重さ。

int main(){
  int n, W;
  ll max_v = 0;
  cin >> n >> W;
  vector<ll> w(n), v(n);
  rep(i,n) cin >> w[i] >> v[i];
  rep(i,n) max_v += v[i];
  //初期化
  rep(i,n+1){
    rep(j,max_v+1){
      dp[i][j] = INF;
    }
  }
  dp[0][0] = 0;
  
  rep(i,n){
    ll w_i = w[i];
    ll v_i = v[i];
    rep(j,max_v+1){
      if(j-v_i>=0) dp[i+1][j] = min(dp[i][j],dp[i][j-v_i]+w_i);
      else dp[i+1][j] = dp[i][j];
    }
  }
  /*rep(i,n+1){
    rep(j,max_v+1){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }*/
  ll ans = 0;
  rep(i,max_v+1){
    if(dp[n][i]<=W){
      ans = i;
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 80128
v[2][12];n,j,c,t,z="%d\n";main(i){for(;scanf(z,&n)*n;){for(;j<n;)scanf(z,*v+j++);for(;memcmp(v,v+1,n*4);++c)for(i=n;i--;v[~c&1][i]=t)for(j=t=n;j--;)t-=v[c&1][i]!=v[c&1][j];for(printf(z,c-1);++i<n;)printf("%d%c",v[j=c=0][i],n+~i?32:10);}}

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

#define taskname ""
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define for0(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef complex <ld> cd;
typedef vector <cd> vcd;
typedef vector <int> vi;

template<class T> using v2d = vector <vector <T> >;
template<class T> bool uin(T &a, T b)
{
    return a > b ? (a = b, true) : false;
}
template<class T> bool uax(T &a, T b)
{
    return a < b ? (a = b, true) : false;
}

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());

const int maxN = 1e5 + 1;

int n, res = 0;
char a[maxN];

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> (a + 1);
    fore(i, 2, n)
    {
        if (a[i] != a[i - 1])
        {
            ++res;
        }
    }
    cout << ++res;
    return 0;
}


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

int main(){
    int n;
    while(cin >> n, n){
        int m, g, d;  cin >> m >> g >> d;
        int amida[d][n+1];
        memset(amida, 0, sizeof(amida));

        for(int i = 0; i < d; i++){
            for(int j = 1; j < n; j++){
                char x; cin >> x;
                amida[i][j] = x-'0';
            }
        }

        int now = m;
        for(int i = 0; i < d; i++){
            if(amida[i][now-1]) now--;
            else if(amida[i][now])    now++;
        }

        if(now == g){
            cout << 0 << endl;
            continue;
        }

        now = m;
        int ansa = 0, ansb = 0;
        for(int i = 0; i < d; i++){
            if(amida[i][now-1]){
                now--;
            }else if(amida[i][now]){
                now++;
            }else{
                int tmp = now-1;
                if(tmp != 0 && amida[i][tmp-1] == 0){
                    for(int j = i+1; j < d; j++){
                        if(amida[j][tmp-1]) tmp--;
                        else if(amida[j][tmp])    tmp++;
                    }
                    if(tmp == g){
                        ansa = i+1, ansb = now-1;
                        break;
                    }
                }
                tmp = now;
                if(tmp != n && amida[i][tmp+1] == 0){
                    tmp++;
                    for(int j = i+1; j < d; j++){
                        if(amida[j][tmp-1]) tmp--;
                        else if(amida[j][tmp])    tmp++;
                    }
                    if(tmp == g){
                        ansa = i+1, ansb = now;
                        break;
                    }
                }
            }
        }

        if(ansa == 0){
            cout << 1 << endl;
        }else{
            cout << ansa << " " << ansb << endl;
        }

    }
    return 0;
}


Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
using ll=long long;
const ll MOD=1000000007,MOD2=998244353;


int main() {
    int a;
    string s;
    cin>>a>>s;
    if(a>=3200){cout<<s<<endl;}
    else{cout<<"red"<<endl;}
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
using namespace std;
int main () {
	string s; cin >> s;
	int sum = 0;
	for (int i = 0; i < s.size(); i++) 
		if (s[i] == '1')
			sum+= 1;
	cout << sum << endl;
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); ++i)
#define outl(x) cout<< (x) << '\n'
#define fi first
#define se second
using pii = pair<int,int>;
using ll = long long;
using vi = vector<int>;

int main()
{
    char s[1024];
    int w;cin >>s >>w;
    for (int i=0; i<strlen(s); i+=w) putchar(s[i]);
    puts("");
}



Measured memory_bytes: 256
s = gets.strip

# 10^100はめちゃくちゃ多い回数なので最終的にRLに集まりそう

ans = Array.new(s.length, 0)

s.length.times do |i|
    cur_char = s[i]
    next_char = s[i + 1]
    
    if next_char.nil?
        break
    end

    if cur_char + next_char == "RL"
        j = i
        while j >= 0 && s[j] == 'R' do
            ans[i + (i - j) % 2] += 1
            j -= 1
        end

        k = i + 1
        while k < s.length && s[k] == 'L' do
            ans[i + (k - i) % 2] += 1
            k += 1
        end
    end
end

puts ans.join(" ")

Measured memory_bytes: 3196
// #include {{{
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
// }}}
// #define {{{
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
// }}}

int n , a , xo;

int main(){
  cin >> n;
  rep(i,0,n) cin >> a , xo ^= a;
  puts(xo & 1 ? "NO" : "YES");
  return 0;
}


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

int main(){
    string s;
    cin>>s;
    int count=0;
    for(int i=0;i<(int)s.size()-1;i++){
      	if(i<0)continue;
        if(s.substr(i,2)=="01"||s.substr(i,2)=="10"){
            s.erase(s.begin()+i,s.begin()+i+2);
            count+=2;
          i-=2;
        }
    }
    cout<<count<<endl;
}

Measured memory_bytes: 3660
#include <bits/stdc++.h>
using namespace std;
#define SZ(a) ((int)(a).size())
typedef long long int64;
int a[1111][1111];
char s[1111][1111];
int main(){
#ifdef LOCAL
  freopen(".a.in", "r", stdin);
#endif
  //ios_base::sync_with_stdio(false),cout.tie(0),cin.tie(0);
  int x,y,X,Y,n,m;
  scanf("%d%d%d%d%d%d",&n,&m,&x,&y,&X,&Y);
  for(int i=1;i<=n;++i)scanf("%s",s[i]+1);
  vector<pair<int,int>>Q;
  Q.emplace_back(x,y);
  memset(a,-1,sizeof a);
  int idx=0;
  int dx[4]={0,0,-1,1};
  int dy[4]={1,-1,0,0};
  auto bfs=[&](int p,int q,int z){
    vector<pair<int,int>> que;
    que.emplace_back(p,q);
    a[p][q]=z;
    for(int i=0;i<SZ(que);++i){
      for(int j=0;j<4;++j){
        int nx=que[i].first+dx[j],ny=que[i].second+dy[j];
        if(nx>=1&&ny>=1&&nx<=n&&ny<=m&&s[nx][ny]=='.'&&a[nx][ny]==-1){
          a[nx][ny]=z;
          que.emplace_back(nx,ny);
        }
      }
    }
  };
  for(int i=1;i<=n;++i){
    for(int j=1;j<=m;++j){
      if(s[i][j]!='#'&&a[i][j]==-1){
        bfs(i,j,++idx);
      }
    }
  }
  vector<set<int>> V(idx+2);
  for(int i=1;i<=n;++i){
    for(int j=1;j<=m;++j){
      if(a[i][j]!=-1){
        for(int p=-2;p<=2;++p){
          for(int q=-2;q<=2;++q){
            if(i+p>=1&&i+p<=n&&j+q>=1&&j+q<=m&&a[i+p][j+q]!=-1&&a[i+p][j+q]!=a[i][j]){
              V[a[i][j]].insert(a[i+p][j+q]);
            }
          }
        }
      }
    }
  }

  vector<int> dis(idx+2,-1),qq;
  dis[a[x][y]]=0;
  qq.push_back(a[x][y]);
  for(int i=0;i<SZ(qq);++i){
    for(int j:V[qq[i]]){
      if(dis[j]==-1){
        dis[j]=dis[qq[i]]+1;
        qq.emplace_back(j);
      }
    }
  }
  cout<<dis[a[X][Y]]<<'\n';
}
//2020.08.22 20:00:48


Measured memory_bytes: 34456
#include<stdio.h>

typedef struct {
  int num;
  char ch;
} Data;


void swap(Data* a,Data* b){
  Data tmp=*a;
  *a=*b;
  *b=tmp;
}

int Partition(Data A[],int p,int r){
  Data x = A[r];
  int i=p-1,j;
  for(j=p;j<r;j++){
    if(A[j].num<=x.num){
      i++;
      swap(&A[i],&A[j]);
    }
  }
  swap(&A[i+1],&A[r]);
  return i+1;
}

void Quicksort(Data A[],int p,int r){
  if(p<r){
    int q = Partition(A,p,r);
    Quicksort(A,p,q-1);
    Quicksort(A,q+1,r);
  }
}

void Merge(Data A[],int left,int mid,int right){
  int n1=mid-left;
  int n2=right-mid;
  int i,j,k;
  Data L[n1+1],R[n2+1];
   
  for(i=0;i<n1;i++)L[i]=A[left+i];
  for(i=0;i<n2;i++)R[i]=A[mid+i];
  L[n1].num=1000000001;
  R[n2].num=1000000001;
   
  i=j=0;
  for(k=left;k<right;k++){
    if(L[i].num<=R[j].num){
      A[k]=L[i];
      i++;
    }else{
      A[k]=R[j];
      j++;
    }
  }
  
}
 
void Merge_Sort(Data A[],int left,int right){
  int mid;
  if(left+1<right){
    mid=(left+right)/2;
 
    Merge_Sort(A,left,mid);
    Merge_Sort(A,mid,right);
    Merge(A,left,mid,right);
     
  }
  
}


int main(){
  int n,i,flg;
  char x;
  Data A[100000];
  Data B[100000];
  scanf("%d",&n);
  scanf("%c",&x);
  for(i=0;i<n;i++){
    scanf("%c %d",&A[i].ch,&A[i].num);
    scanf("%c",&x);
    //printf("%c %d\n",A[i].ch,A[i].num);
    B[i]=A[i];
  }
  
  Quicksort(A,0,n-1);
  Merge_Sort(B,0,n);

  flg=1;
  for(i=0;i<n;i++)if(A[i].num!=B[i].num || A[i].ch!=B[i].ch)flg=0;

  if(flg)printf("Stable\n");
  else printf("Not stable\n");
  
  for(i=0;i<n;i++){
    printf("%c %d\n",A[i].ch,A[i].num);
  }
  
  return 0;
}

Measured memory_bytes: 2948
#include <bits/stdc++.h>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <functional>
using namespace std; 
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define FORD(i,a,b) for(int i=(a);i>=(b);i--)
inline bool EQ(double a, double b) { return fabs(a-b) < 1e-9; }
const int INF = 1<<29;
typedef long long int ll;
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
template<class T> void chmax(T & a, const T & b) { a = max(a, b); }
template<class T> void chmin(T & a, const T & b) { a = min(a, b); }
const int MOD=1000000007;

bool isPowerOfTwo (ll x)  
{  
    return x && (!(x&(x-1)));  
}
bool isPrime(int n) 
{ 
    if (n <= 1)  return false; 
    if (n <= 3)  return true; 
    if (n%2 == 0 || n%3 == 0) return false; 
  
    for (int i=5; i*i<=n; i=i+6) 
        if (n%i == 0 || n%(i+2) == 0) 
           return false; 
  
    return true; 
}
int highestPowerof2(unsigned int n) 
{ 
    // Invalid input 
    if (n < 1) 
        return 0; 
  
    int res = 1;
    for (int i=0; i<8*sizeof(unsigned int); i++) 
    { 
        int curr = 1 << i; 
        if (curr > n) 
           break; 
  
        res = curr; 
    } 
  
    return res; 
} 
long long binpow(long long a, long long b) {
    long long res = 1;
    while (b > 0) {
        if (b & 1)
            res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}       
////////////////////////////////////////////////////////////////////

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);  
  cout.tie(NULL);
  int t=1;
  while(t--)
  {
    int k,a,b;
    cin>>k;
    cin>>a>>b;
    if(b<a)swap(a,b);
    for(int i=a;i<=b;i++)
    {
        if((i%k)==0)
        {
            return cout<<"OK\n",0;
        }
    }
    cout<<"NG";
  }  
  return 0;
}

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

using namespace std;

#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define repn(i, n) for(int (i) = 1; (i) <= (n); (i)++)
#define repr(i, n) for(int (i) = (n-1); (i) >= 0; (i)--)
#define all(x) (x).begin(), (x).end()
#define lint long long

int main(){
    int n; string s; cin >> n >> s;
    bool isx[26], isy[26]; int ans = 0;
    rep(i, n){
        memset(isx, false, sizeof(isx));
        memset(isy, false, sizeof(isy));
        string x = s.substr(0, i);
        string y = s.substr(i);
        rep(i, (int)x.size()) isx[x[i]-'a']=true;
        rep(i, (int)y.size()) isy[y[i]-'a']=true;
        int res = 0;
        rep(i, 26) if(isx[i]&&isy[i]) res++;
        ans = max(ans, res);
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const ll INF=1LL<<60;
const int inf=1<<30;
const int mod=1e9+7;
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;cin >> n;
    vector<ll> v;
    for(ll i=1;i*i<=n;i++){
        if(n%i==0){
            v.push_back(i);
            if(i*i!=n){
                v.push_back(n/i);
            }
        }
    }
    ll ans=0;
    ll p=n-1;
    map<ll,bool> mp;
    vector<ll> t;
    for(ll i=1;i*i<=p;i++){
        if(p%i==0){
            if(i!=1){
                ans++;
                t.push_back(i);
            }
            mp[i]=true;
            if(i*i!=p){
                ans++;
                mp[p/i]=true;
                t.push_back(p/i);
            }
        }
    }
    for(int i=0;i<v.size();i++){
        if(mp[v[i]]){
            continue;
        }
        ll num=n;
        if(v[i]==1){
            continue;
        }
        while(num%v[i]==0){
            num/=v[i];
        }
        if((num-1)%v[i]==0){
            ans++;
            t.push_back(v[i]);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 640
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>

using namespace std;

#define square(s) (s)*(s)
#define scast(type,e) static_cast<type>(e)

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

int main() {
	ll l, r;
	cin >> l >> r;
	if (r - l >= 2019) {
		cout << 0;
		return 0;
	}
	l %= 2019;
	r %= 2019;
	ll res = 2018;
	for (ll i = l; i < r; i++) {
		for (ll j = i + 1; j <= r; j++) {
			res = min((j * i) % 2019, res);
		}
	}
	cout << res;
	return 0;
}

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

int main(){
	string s;
	cin >> s;
	vector<int> vc;
	int x;
	for(int i=0; i<s.length()-2; i++){
		x = 100*(s[i]-('0'-0)) + 10*(s[i+1]-('0'-0)) + (s[i+2]-('0'-0));
		vc.push_back(x);
	}
	int dif, mn;
	mn = 1000;
	for(int i=0; i<vc.size(); i++){
		dif = abs(vc[i] - 753);
		mn = min(mn, dif);
	}
	cout << mn << endl;
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <iomanip>
#include <climits>
#include <functional>
#include <cassert>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<string> VS;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<PII> VPI;
typedef vector<PLL> VPL;
#define rep(i,n) for(ll i=0;i<(n);i++)
#define all(a) (a).begin(),(a).end()
#define pf push_front
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ub upper_bound
#define lb lower_bound

#include <atcoder/all>

using namespace atcoder;
typedef modint998244353 mint;

struct S{
  mint a,b,c;
};

struct F{
  mint a;
};

S op(S l,S r){
  return S{l.a*r.b+r.a,l.b*r.b,l.c*r.b+r.c};
};

S e(){
  return S{0,1,1};
};

S mapping(F l,S r){
  if(l.a==0) return r;
  return S{l.a*r.c,r.b,r.c};
};

F composition(F l,F r){
  if(l.a==0) return r;
  return l;
};

F id(){
  return F{0};
};

int main(){
  int N,Q;
  cin>>N>>Q;
  VI L(Q),R(Q),D(Q);
  rep(i,Q) cin>>L[i]>>R[i]>>D[i];
  lazy_segtree<S,op,e,F,mapping,composition,id> seg(vector<S>(N,{1,10,1}));
  rep(i,Q){
    seg.apply(L[i]-1,R[i],F{D[i]});
    cout<<seg.all_prod().a.val()<<endl;
  }
}


Measured memory_bytes: 14736
//コンパイラ最適化用
#pragma GCC optimize("Ofast")
#define _GLIBCXX_DEBUG
//インクルード(アルファベット順)
#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>//可変長配列
#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, int> pii;
typedef pair<ll, ld> pld;
typedef pair<pii, int> ppiii;
typedef pair<pii, ll> ppiill;
typedef pair<pll, ll> ppllll;
typedef pair<pli, int> pplii;
typedef map<int, int> mii;
typedef deque<ll> dll;
typedef queue<ll> qll;
typedef priority_queue<ll> pqll;
typedef priority_queue<ll, vector<ll>, greater<ll>> pqrll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<int>> vvint;
typedef vector<vector<pll>> vvpll;

//マクロ
//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 cin(x) cin >> x

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

    //入力
    ll X=0,N =0, T =0;
    cin >> N >> X >> T;

    //vll X(N);
    //REP(i,N) cin >> X[i];


    //本文
    ll ans = 0;
    if(N%X==0)ans = N/X;
    else ans = N/X +1;
    //出力
    cout << ans*T << endl;
    //REP(i,N) cout << ans.at(i) <<endl;
  return 0;
}

Measured memory_bytes: 3648
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    FORR(x,min(a,b),1){
        if (a % x == 0 && b % x == 0){
            c--;
            if (c == 0){
                cout << x << endl;
                return 0;
            }
        }
    }

    return 0;
}


Measured memory_bytes: 256
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <map>
#include <list>
#include <set>
#include <numeric>
#include <queue>
#include <stack>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <climits>
#include <cfloat>
#include <ctime>
#include <complex>
#include <cassert>
#include <array>
#include <bitset> 
#include <unordered_map>

using namespace std;
typedef long long LL;
typedef pair<int,int> P;

const int L=1e5+1;
int a[L];
bool f(int i,int j){
	while(1){
		if(i+1<a[j+1]){
			i++;
			j++;
			continue;
		}
		if(i+1<a[j]){
			if((a[j]-i)%2==0){
				return false;
			}
		}
		int k=0;
		while(i<a[j]){
			k++;
			j++;
		}
		return k%2==1;
	}
}

int main() {
	int N;
	cin >> N;
	for(int i=0;i<N;i++){
		cin >> a[i];
	}
	sort(a,a+N,greater<int>());
	if(f(0,0)){
		cout << "Second" << endl;
		return 0;
	}

	if(a[0]>1&&f(1,0)){
		cout << "First" << endl;
		return 0;
	}
	if(N>1&&f(0,1)){
		cout << "First" << endl;
		return 0;
	}
	cout << "Second" << endl;

	return 0;
}



Measured memory_bytes: 640
local mmi, mma = math.min, math.max
local bls, brs = bit.lshift, bit.rshift
local n, m, x = io.read("*n", "*n", "*n")
local c = {}
local a = {}
for i = 1, n do
  c[i] = io.read("*n")
  a[i] = {}
  for j = 1, m do
    a[i][j] = io.read("*n")
  end
end
local ret = -1
local tot = bls(1, n)
for it = 0, tot - 1 do
  local ti = it
  local cost = 0
  local z = {}
  for j = 1, m do
    z[j] = 0
  end
  for j = 1, n do
    if ti % 2 == 1 then
      for k = 1, m do
        z[k] = z[k] + a[j][k]
      end
      cost = cost + c[j]
    end
    ti = brs(ti, 1)
  end
  local valid = true
  for j = 1, m do
    if z[j] < x then
      valid = false
    end
  end
  if valid then
    if ret == -1 then
      ret = cost
    else
      ret = mmi(ret, cost)
    end
  end
end
print(ret)


Measured memory_bytes: 2764
import heapq
used = {}


def is_used(a, b, c):
    hash = (a, b, c)
    ret = hash in used
    used[hash] = 1
    return ret


X, Y, Z, K = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
C = list(map(int, input().split()))

A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

la = len(A)
lb = len(B)
lc = len(C)

Q = []
heapq.heappush(Q, (-(A[0] + B[0] + C[0]), 0, 0, 0))

ans = []
while len(ans) < K:
    q = heapq.heappop(Q)
    ans.append(-q[0])
    if q[1] != la - 1:
        ai = q[1] + 1
        bi = q[2]
        ci = q[3]
        if not is_used(ai, bi, ci):
            heapq.heappush(Q, (-(A[ai] + B[bi] + C[ci]), ai, bi, ci))
    if q[2] != lb - 1:
        ai = q[1]
        bi = q[2] + 1
        ci = q[3]
        if not is_used(ai, bi, ci):
            heapq.heappush(Q, (-(A[ai] + B[bi] + C[ci]), ai, bi, ci))
    if q[3] != lc - 1:
        ai = q[1]
        bi = q[2]
        ci = q[3] + 1
        if not is_used(ai, bi, ci):
            heapq.heappush(Q, (-(A[ai] + B[bi] + C[ci]), ai, bi, ci))

for i, a in enumerate(ans):
    print(a)

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

const int MAX = 3000000;
int MonSat[MAX+1];
int isPrime[MAX+1];

int main(void){
    rep(i, MAX) MonSat[i] = (i%7==1 || i%7==6) ? 1 : 0;
    rep(i, MAX) isPrime[i] = (MonSat[i]==1) ? 1 : 0;
    for(int i=6; i<=MAX; ++i) {
        if(isPrime[i]==0) continue;
        for(int j=2; i*j<=MAX; ++j) {
            if(MonSat[j]) isPrime[i*j] = 0;
        }
    }
    int n;
    while(cin >> n && n!=1) {
        cout << n << ":";
        for(int i=2; i <= n; i++) {
            if(isPrime[i]==1 && n%i==0) {
                cout << " " << i;
            }
        }
        cout << endl;
    }
}

Measured memory_bytes: 24596
#include<stdio.h>

int main(){
    int S,h,m,s;
    scanf("%d",&S);
    
    h=S/(60*60);
   m=(S-h*60*60)/60;
    s=S-h*60*60-m*60;
    printf("%d:%d:%d\n",h,m,s);

    return 0;
}


Measured memory_bytes: 2100
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;




int main(){
    int k, q;
    cin >> k >> q;
    vector<int> d(k);
    rep(i, k) {
        cin >> d[i];
    }
    vector<int> result;
    rep(i, q) {
        int n, x, m;
        cin >> n >> x >> m;
        int ans = n-1;
        vector<int> v(k);
        ll sum = 0;
        ll eq = 0;
        rep(j, k) {
            v[j] = d[j] % m;
            sum += v[j];
            if (v[j]==0) eq++;
        }
        ll fin;
        int num = (n-1)/k;
        fin = x+sum*num;
        int re = (n-1)%k;
        rep(j, re) {
            fin += v[j];
            if (v[j]==0) ans--;
        }
        ans -= eq*num;
        ans -= (fin/m-x/m);
        result.push_back(ans);
    }
    rep(i, result.size()) {
        cout << result[i] << endl;
    }
    return 0;
}


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

#define lld long long
#define pi pair<int,int>
#define pl pair<lld,lld>
#define mem0(x) memset(x,0,sizeof(x))
#define fillnum(x,n) fill(begin(x),end(x),n)
#define asort(x) sort(x.begin(),x.end())
#define dsort(x,t) sort(x.begin(),x.end(),greater<t>())

int main() {
    lld N;
    cin >> N;
    vector<pi> G;
    for (int i = 0; i < N; i++) {
        int a;
        cin >> a;
        G.push_back(make_pair(a,i+1));
    }
    asort(G);
    for (int i = 0; i < N; i++) {
        cout << G[i].second;
        if(i == N-1){
            cout << endl;
        } else {
            cout << " ";
        }
    }
}

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

int INF = 100100101;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int a,b,k;
  cin >> a >> b >> k;
  int count = 0;
  for(int i = a; i <= b; i++){
    if(count++ < k) cout << i << endl;
    else if(b - i < k) cout << i << endl; 
  }
  return 0;
}

Measured memory_bytes: 256
// -----------------------------------
// author   : MatsuTaku
// country  : Japan
// created  : 08/22/20 18:13:53
// -----------------------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

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

  int h,w; cin>>h>>w;
  int ch, cw; cin>>ch>>cw; ch--; cw--;
  int dh, dw; cin>>dh>>dw; dh--; dw--;
  vector<string> S(h);
  for (int i = 0; i < h; i++) {
    cin>>S[i];
  }

  auto dijkstra = [&]() {
    struct State {
      int cw;
      int x, y;
      bool operator>(const State& r) const {
        return cw > r.cw;
      }
    };
    priority_queue<State, vector<State>, greater<>> ps;
    ps.push({0,ch,cw});
    vector<vector<int>> ws(h, vector<int>(w, 1e9));
    ws[ch][cw] = 0;
    constexpr int dir[4][2] = {
        {1,0},
        {0,1},
        {-1,0},
        {0,-1}
    };
    while (not ps.empty()) {
      auto [warp,x,y] = ps.top(); ps.pop();
//      cerr << warp << " " << x << " " << y << endl;
      if (x == dh and y == dw)
        return warp;
      for (auto [dx, dy] : dir) {
        auto nx = x+dx;
        auto ny = y+dy;
        if (nx < 0 or ny < 0 or nx >= h or ny >= w) continue;
        if (ws[nx][ny] <= warp or S[nx][ny] == '#') continue;
        ws[nx][ny] = warp;
        ps.push({warp, nx, ny});
      }
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          auto nx = x+i;
          auto ny = y+j;
          if (nx < 0 or ny < 0 or nx >= h or ny >= w) continue;
          if (ws[nx][ny] <= warp+1 or S[nx][ny] == '#') continue;
          ws[nx][ny] = warp+1;
          ps.push({warp+1, nx, ny});
        }
      }
    }
    return -1;
  };
  cout << dijkstra() << endl;

  return 0;
}


Measured memory_bytes: 8488
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0;i<n;i++)
#define rep2(i,a,n) for(int i=a;i<n;i++)
#define been(ix) (ix).begin(),(ix).end()
using namespace std;
typedef long long ll; 
typedef vector<int> vi;
typedef vector<long long> vl;
const ll INFL = 1e18;
const int INF = 1001001001; 
typedef pair<int,int> P;
#define foreach(ix,i) for(auto& (ix) : (i))
typedef long double ld;
const int mod =1000000007;
const double PI = acos(-1);
vector<P> edge[100005];
vl seen(100005,INFL);
void dfs(int n,ll c,int m){
    for(auto x:edge[n]){
        if(x.first==m)continue;
        seen[x.first]=min(seen[x.first],c+x.second);
        dfs(x.first,seen[x.first],n);
    }
}
int main(){
	int n;cin>>n;
    rep(i,n-1){
        int a,b;cin>>a>>b;
        a--;b--;
        int c;cin>>c;
        edge[a].push_back({b,c});
        edge[b].push_back({a,c});
    }
    int q,k;cin>>q>>k;
    k--;
    dfs(k,0,-1);
    rep(i,q){
        int a,b;
        cin>>a>>b;
        a--;b--;
        cout<<seen[a]+seen[b]<<endl;
    }
}

Measured memory_bytes: 14316
#include<stdio.h>
int pair(int ,int *,int *);
int main(void){
  int n;
  int x,y;
  scanf("%d",&n);
  int a[9][9];
      for(int i=0;i<9;i++){
		for(int j=0;j<9;j++){
			a[i][j]=0;
        }
      }
	
      for(int i=1;i<=n;i++){
		pair(i,&x,&y);
  			a[x-1][y-1]++;
      }
  long ans=0;
        for(int i=0;i<9;i++){
          for(int j=0;j<9;j++){
		ans+=a[i][j]*a[j][i];
          }
        }
  printf("%ld",ans);
  return 0;
}
        
int pair(int n,int *x,int *y){
  *x=n%10;
  while(n){
    *y=n;
    n/=10;
  }
}

Measured memory_bytes: 128
module AOJ_Volume0086;

import std.stdio,std.string,std.conv;

int main()
{
	while(true)
	{
		bool flag = false;
		int x[102];
		foreach(i;0..102)
		{
			x[i] = 0;
		}
		string s;
		while((s = readln.chomp).length != 0)
		{
			flag = true;
			string[] _s = s.split(" ");
			int a = _s[0].to!int;
			int b = _s[1].to!int;
			if(a == 0 && b == 0)
			{
				break;
			}
			else
			{
				x[a]++;
				x[b]++;
			}
		}

		if(flag)
		{
			int count = 0;
			foreach(i;0..102)
			{
				if(x[i]%2 == 1) count++;
			}
			
			if(count == 2)
			{
				if(x[1]%2 == 1 && x[2]%2 == 1)
				{
					writeln("OK");
				}
				else
				{
					writeln("NG");
				}
			}
			else
			{
				writeln("NG");
			}
		}
		else
		{
			break;
		}
	}
	return 0;
}

Measured memory_bytes: 1076
import numpy as np
n = int(input())
p = list(map(int, input().split()))

p2 = p.copy()
p2.sort()

p = np.asarray(p)
p2 = np.asarray(p2)
p3 = [p!=p2]
c = np.count_nonzero(p3)
if c == 2 or c == 0:
    print("YES")
else:
    print("NO")


Measured memory_bytes: 21896
import std.stdio, std.algorithm, std.string, std.conv, std.array, std.range, std.math;
int[] readints() { return readln.split.to!(int[]); }


void main() {
    auto a = readints();

    auto q = new Queue!(int[]);
    q.push(a ~ 0);
    int ans = 0;
    while (1) {
        auto t = q.front();
        if (t[0] == t[1] && t[1] == t[2]) {
            ans = t[3];
            break;
        }
        q.pop();
        t[3] += 1;
        // +2
        auto tt = t.dup; // 参照になってしまうから dup が必要
        tt[t[0..3]._minIndex] += 2;
        q.push(tt);
        if (t[0..3]._maxElement - t[0..3]._minElement > 3) continue;
        // +1
        q.push([t[0] + 1, t[1] + 1, t[2] + 0, t[3]]);
        q.push([t[0] + 1, t[1] + 0, t[2] + 1, t[3]]);
        q.push([t[0] + 0, t[1] + 1, t[2] + 1, t[3]]);
    }
    writeln(ans);
}

int _minIndex(T) (T[] a){
    int mini = 1145141919;
    int r = -1;
    foreach (i, v; a) if (v < mini) {
        r = i.to!int;
        mini = v;
    }
    return r;
}

int _maxElement(int[] a) {
    int m = -1;
    foreach(i; a) if(m<i) m = i;
    return m;
}

int _minElement(int[] a) {
    int m = 1145141919;
    foreach(i; a) if(m>i) m = i;
    return m;
}

class Queue(T) {
    T[] queue;
    this() { }
    T front(){
        return queue[0];
    }
    void pop(){
        queue = queue[1..$];
    }
    void push(T x){
        queue ~= x;
    }
    int size(){
        return queue.length.to!int;
    }
}

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) x.begin(),x.end()
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int MOD = 1e9+7;

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

    string s,t;
    cin >> s >> t;

    bool ok = true;
    int n = s.size();
    rep(i,n){
        if(s[i] != t[i]) ok = false;
    }

    cout << ( ok ? "Yes" : "No" ) << endl;

    return 0;
}

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

const int INF = 0x3f3f3f3f;
const int N = 3e3 + 5;

int dp[N][N];
string a, b, ans;

int dpf(int i, int j) {
  if(dp[i][j] != -INF) return dp[i][j];
  if(!i or !j) return dp[i][j] = 0;
  if(a[i-1] == b[j-1]) return dp[i][j] = 1 + dpf(i-1, j-1);
  return dp[i][j] = max(dpf(i-1, j), dpf(i, j-1));
}

void rec(int i, int j) {
  if(!i or !j) return;
  if(a[i-1] == b[j-1]) ans += a[i-1], rec(i-1, j-1);
  else if(dp[i][j] == dp[i-1][j]) rec(i-1, j);
  else rec(i, j-1);
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  cin >> a >> b;
  int n = a.size(), m = b.size();
  for(int i = 0; i < N; i++) 
    for(int j = 0; j < N; j++) dp[i][j] = -INF;
  dpf(n, m);
  rec(n, m);
  reverse(ans.begin(), ans.end());
  cout << ans << "\n";
}


Measured memory_bytes: 35968
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N,K,x,s=0;
  cin >> N >> K;
  for(int i=0;i<N;i++){
    cin >> x;
    if(x <= K-x) s += x*2;
    else s += (K-x)*2;
  }
  cout << s << endl;
}
    
  

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); ++i)
using namespace std;
using Tup = tuple<int, int, int>;

const int INF = 1e9;
// → ↑ ← ↓
const int dy[4] = {0, 1, 0, -1};
const int dx[4] = {1, 0, -1, 0};

int main() {
    int H, W;
    cin >> H >> W;
    int sy, sx, gy, gx;
    cin >> sy >> sx >> gy >> gx;
    sy--, sx--, gy--, gx--;

    vector<string> graph(H);
    for(auto &e : graph) cin >> e;

    priority_queue<Tup, vector<Tup>, greater<Tup>> que;
    vector<vector<int>> dist(H, vector<int>(W, INF));
    auto push = [&](int y, int x, int cost) {
        if(dist[y][x] <= cost) return;
        que.emplace(cost, y, x);
        dist[y][x] = cost;
    };
    push(sy, sx, 0);
    while(!que.empty()) {
        auto[cost, y, x] = que.top(); que.pop();
        for(int i = 0; i < 4; ++i) {
            int ny = y + dy[i], nx = x + dx[i];
            if(ny < 0 || nx < 0 || ny >= H || nx >= W) continue;
            if(graph[ny][nx] == '.') push(ny, nx, dist[y][x]);
        }
        for(int i = -2; i <= 2; ++i) {
            for(int j = -2; j <= 2; ++j) {
                int ny = y + i, nx = x + j;
                if(ny < 0 || ny >= H || nx < 0 || nx >= W) continue;
                if(graph[ny][nx] == '.') push(ny, nx, dist[y][x] + 1);
            }
        }
    }

    if(dist[gy][gx] == INF) cout << -1 << '\n';
    else cout << dist[gy][gx] << '\n';
    return 0;
}


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

public class Main{

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

		S: while (true) {
			int k = 0, max = 1;
			int n = s.nextInt();
			if (n == 0)
				break;
			String[] AC = new String[n];
			int[] ANS = new int[n];

			String[] c = new String[n];
			for (int i = 0; i < n; i++) {
				c[i] = s.next();
			}
			Arrays.fill(AC, " ");

			if (n == 1) {
				System.out.println(c[0] + " 1");
				continue S;
			}
			for (int i = 0; i < n; i++) {
				for (k = 0;; k++) {
					if (AC[k].equals(c[i])) {
						ANS[k]++;
						if ((k + 1) > max) {
							max = k + 1;
						}
						break;
					} else if (AC[k].equals(" ")) {
						AC[k] = c[i];
						ANS[k]++;
						if ((k + 1) > max) {
							max = k + 1;
						}
						break;
					}
				}
				int[] ANS2 = ANS.clone();
				Arrays.sort(ANS2);

				if (max >= 2) {
					if (ANS2[n - 1] > ANS2[n - 2] + (n - i - 1)) {
						for (int j = 0; j < max; j++) {
							if (ANS[j] == ANS2[n - 1]) {
								System.out.println(AC[j] + " " + (i + 1));
								continue S;
							}
						}

					}
				}
				if (ANS[k] > n / 2) {
					System.out.println(AC[k] + " " + (i + 1));
					continue S;
				}

			}
			System.out.println("TIE");
		}
		s.close();
	}
}



Measured memory_bytes: 61116