akhauriyash/Code-Regression huggingface dataset:
K = int(input())
total = 0

def gcd2(a,b): #互除法
  while True:
    mod = a % b
    if mod == 0:
      return b
    a = b
    b = mod

memo = {}
def gcd(a,b,c):
  return gcd2(gcd2(a,b),c)
#  a,b,c = sorted([a,b,c])
#  if (a,b,c) not in memo:
#    val = gcd2(gcd2(a,b), c)
#    memo[(a,b,c)] = val
#    return val
#  return memo[(a,b,c)]

for a in range(1, K+1):
  for b in range(1, K+1):
    for c in range(1, K+1):
      g = gcd(a, b, c)
#      print(a,b,c, g)
      total += g

print(total)

Measured memory_bytes: 74420
#include<bits/stdc++.h>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<iostream>
#define ios   ios_base::sync_with_stdio(false);  cin.tie(0);    cout.tie(0);
typedef  long long int ll;
using namespace std;
const int M=1e9+7;
const int N=1e5+5;
#define fi first
#define se second
#define pb push_back
int main()
{
    ll ans=0;
    ll n;cin>>n;
    ll a[n];
    ll mx=0;
    for(ll i=0;i<n;++i)
    {
        cin>>a[i];
        ans+=max(0LL,mx-a[i]);
        if(a[i]>mx)
        {
            mx=a[i];
        }
    }
    cout<<ans<<endl;

}

Measured memory_bytes: 5128
import sys, bisect, math, itertools, heapq, collections
from operator import itemgetter
# a.sort(key=itemgetter(i)) # i番目要素でsort
from functools import lru_cache
# @lru_cache(maxsize=None)
sys.setrecursionlimit(10**8)
input = sys.stdin.readline
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp():
    '''
    一つの整数
    '''
    return int(input())
def inpl():
    '''
    一行に複数の整数
    '''
    return list(map(int, input().split()))

def NO():
    print("No")
    exit()
s = list(input())[:-1]
n=len(s)
for i in range(n // 2):
    if s[i] != s[-i - 1]:
        NO()
end=(n-1)//2
for i in range(end//2):
    if s[i] != s[end - i - 1]:
        NO()
begin = (n + 3) // 2
for i in range(begin, (n - begin) // 2):
    if s[i] != s[-i - 1]:
        NO()
print("Yes")


Measured memory_bytes: 38384
n = int(input())
rs = [[0, 0] for _ in range(n)]
bs = [[0, 0] for _ in range(n)]

for i in range(n):
  a, b = map(int, input().split())
  rs[i][0] = a
  rs[i][1] = b

for i in range(n):
  c, d = map(int, input().split())
  bs[i][0] = c
  bs[i][1] = d


#rs.sort(key=lambda x: x[0]+x[1])
#bs.sort(key=lambda x: x[0]+x[1])

#print(rs)
#print(bs)

rb = [[] for _ in range(n)]
for i in range(n):
  for j in range(n):
    if rs[i][0]<bs[j][0] and rs[i][1]<bs[j][1]:
      rb[i].append(j)
rb.sort(key=lambda x: len(x))
#print(rb)

br = [[] for _ in range(n)]
for i in range(n):
  for j in range(n):
    if rs[j][0]<bs[i][0] and rs[j][1]<bs[i][1]:
      br[i].append(j)
#print(br)

use = [False]*n
ans = 0
for i in range(n):
  min_br = n+1
  min_b = n+1
  for j in rb[i]:
    if use[j]:
      continue
    if len(br[j]) <= min_br:
      min_br = len(br[j])
      min_b = j
  if min_b != n+1:
    use[min_b] = True
    ans += 1

print(ans)
    

Measured memory_bytes: 3188
def solve(n,lst):
    #i行j列はjから連続したi個のうち落とせる個数の最大値
    dp=[[0]*n for _ in range(n+1)]

    #initialize
    for j in range(n-1):
        if abs(lst[j]-lst[j+1])<=1 : dp[2][j]=2
    

    #真ん中2*i個が抜けて上下を落とせる場合と2*(i+1)個を複数の部分に分けて落とせる場合の2通りが考えられる
    for i in range(3,n+1):
        for j in range(n-i+1):
            mx=0
            if i%2==0:
                if abs(lst[j]-lst[j+i-1])<=1:
                    if dp[i-2][j+1]==i-2:
                        dp[i][j]=i
                        continue
                mx=dp[i-2][j+1]                
                for k in range(2,i,2):
                    if mx<dp[k][j]+dp[i-k][j+k]:
                        mx=dp[k][j]+dp[i-k][j+k]
                        if mx==i:break
            else :
                for k in range(1,i,2):
                    if mx<dp[k][j]+dp[i-k][j+k]:
                        mx=dp[k][j]+dp[i-k][j+k]                
            dp[i][j]=mx
    print(dp[n][0])        
    #print(dp) 

                

while 1:
    n=int(input())
    if n==0 : break
    lst=tuple(map(int,input().split()))
    solve(n,lst)  


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

using namespace std;

int main()
{
    string S;
    cin >> S;

    int size = S.length();

    bool flag;
    if (S[0] == '0')
    {
        flag = false;
    }
    else if (S[0] == '1')
    {
        flag = true;
    }

    int ans = 0;

    for (int i = 1; i < size; i++)
    {
        if (flag == false)
        {
            if (S[i] == '0')
            {
                ans++;
                flag = true;
            }
            else if (S[i] == '1')
            {
                flag = true;
            }
        }
        else if (flag == true)
        {
            if (S[i] == '0')
            {
                flag = false;
            }
            else if (S[i] == '1')
            {
                ans++;
                flag = false;
            }
        }
    }

    int ans2 = 0;
    reverse(S.begin(), S.end());
    bool flag2;
    if (S[0] == '0')
    {
        flag2 = false;
    }
    else if (S[0] == '1')
    {
        flag2 = true;
    }

    for (int i = 1; i < size; i++)
    {
        if (flag2 == false)
        {
            if (S[i] == '0')
            {
                ans2++;
                flag2 = true;
            }
            else if (S[i] == '1')
            {
                flag2 = true;
            }
        }
        else if (flag2 == true)
        {
            if (S[i] == '0')
            {
                flag2 = false;
            }
            else if (S[i] == '1')
            {
                ans2++;
                flag2 = false;
            }
        }
    }

    cout << min(ans, ans2) << endl;

    return 0;
}

Measured memory_bytes: 512
N, W = map(int, input().split())
w = []
v = []
max_v = 1000
for _ in range(N):
    a, b = map(int, input().split())
    w.append(a)
    v.append(b)
INF = int(1e9)*(N+1)
dp = [[INF] * (max_v * N+1) for _ in range(N+1)]
max_sum = max_v*N
dp[0][0] = 0

for i in range(N):
    for j in range(max_v * N+1):
        if j+v[i] > max_sum:
            break
        dp[i+1][j+v[i]] = min(dp[i+1][j+v[i]], dp[i][j]+w[i])
        dp[i+1][j] = min(dp[i+1][j], dp[i][j])

ans = 0
for i in range(max_v * N + 1):
    if dp[N][i] <= W:
        ans = max(ans, i)
print(ans)


Measured memory_bytes: 119148
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<n; i++)
#define int long long
#define P pair<long,long>
#define all(a) a.begin(),a.end()

using namespace std;
signed main(){
    string s,t=""; cin>>s;
    if(s.size()==1){
        cout<<s<<endl;
        return 0;
    }
    rep(i,s.size()-1){
        t+="9";
    }
    if(s.substr(1,s.size()-1)==t){
        cout<<(s.at(0)-'0')+9*(s.size()-1)<<endl;
    }else{
        cout<<(s.at(0)-'0')-1+9*(s.size()-1)<<endl;
    }
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
#include <functional>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair((a),(b))
#define pb(a) push_back((a))
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

#define INF 2147483600

int main(){
  int h,w;
  cin>>h>>w;
  vector<vector<int> > vec(h, vector<int>(w));
  rep(i,h) rep(j,w) cin>>vec[i][j];
  vector<vector<int> > v(h+w, vector<int>(max(h,w),-1));
  rep(k,h+w-1){
    int i=k,j=0;
    if(k>=h){ i=h-1; j=k-h+1;}
    int d=j;
    while(i>=0 && j<w){
      v[k][d] = vec[i][j];
      i--;j++;d++;
    }
  }

  int d=max(h,w);
  vector<vector<int>> prev(d,vector<int>(d,-1));
  prev[0][0]=0;
  rep(k, h+w-1){
    vector<vector<int>> nxt(d,vector<int>(d,-1));
    rep(j,d)rep(i,j+1)if(prev[i][j]>=0){
      auto c = [&](int a,int b){
        if(v[k][a]<0 || v[k][b]<0) return -INF;
        else if(a==b) return v[k][a];
        else return v[k][a]+v[k][b];
      };
      nxt[i][j] = max(nxt[i][j], prev[i][j]+c(i,j));
      if(i+1<=j) nxt[i+1][j] = max(nxt[i+1][j], prev[i][j]+c(i+1,j));
      if(j+1<d){
        nxt[i+1][j+1] = max(nxt[i+1][j+1], prev[i][j]+c(i+1,j+1));
        nxt[i][j+1] = max(nxt[i][j+1], prev[i][j]+c(i,j+1));
      }
    }
    swap(prev,nxt);
  }

  cout << prev[d-1][d-1] << endl;
  return 0;
}


Measured memory_bytes: 1120
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <fstream>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef tuple<int, int, int> iii;
typedef vector<iii> viii;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef map<int, int> mii;
typedef map<int, string> mis;
typedef map<string, int> msi;
typedef set<int> si;
typedef set<ll> sl;
typedef map<ll, ll> mll;
typedef queue<int> qi;
typedef queue<ii> qii;
typedef vector<string> vs;
typedef pair<ll, ll> iil;
typedef vector<iil> viil;

#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0 ; i < n ; i++)
#define INF 1000000000

int n;
vi adj[2005];

vi runBFS(int v) {
    qii q;
    vi dst(n);
    bool vis[n];
    memset(vis, false, sizeof vis);
    q.push({v, 0});
    while (q.size() > 0) {
        ii ns = q.front();
        q.pop();
        int vt = ns.first;
        if (vis[vt]) {
            continue;
        }
        int ds = ns.second;
        dst[vt] = ds;
        vis[vt] = true;
        for (int a : adj[vt]) {
            if (vis[a]) {
                continue;
            }
            q.push({a, ds + 1});
        }
    }
    return dst;
}

void solve() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    while (true) {
        c -= b;
        if (c <= 0) {
            cout << "Yes" << endl;
            return;
        }
        a -= d;
        if (a <= 0) {
            cout << "No" << endl;
            return;
        }
    }
}

void querySolve() {
    int n;
    cin >> n;
    while (n--) {
        solve();
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    
    //querySolve();
    solve();
}


Measured memory_bytes: 3696
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        if (s.charAt(2) == s.charAt(3)
                && s.charAt(4) == s.charAt(5)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}


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

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

  string w;
  cin >> w;
  vector<int> alphabet_cnt(26, 0);
  for(int i = 0; i < w.size(); ++i) {
    alphabet_cnt.at(w.at(i) - 'a')++;
  }

  for(int i = 0; i < alphabet_cnt.size(); ++i) {
    if (alphabet_cnt.at(i) % 2 != 0) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  return 0;
}


Measured memory_bytes: 256
def even_or_odd(a):
    ans = a[0] % 2
    for i in range(1, N):
        ans ^= int((N-1) & i == i) & a[i]
    return ans

N = int(input())
a = [x-1 for x in list(map(int, list(input())))]

if even_or_odd([x % 2 for x in a]) == 1:
    print(1)
elif 1 in a:
    print(0)
elif even_or_odd([x//2 for x in a]) == 1:
    print(2)
else:
    print(0)

Measured memory_bytes: 21368
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <tuple>
using namespace std;

#define rep(i,a) for(int i=0; i<a; i++)
#define rrep(i,a) for(int i=a; i>=0; i--)
#define rep1(i,a) for(int i=1; i<=a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a,b) cout << a << " " << b << endl;
#define cout3(a,b,c) cout << a << " " << b << " " << c << endl;
#define cout4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a,n) memset( a, n, sizeof(a))
#define all(a) a.begin(),a.end()

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

int N, from[1009];
ll dp[1009];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    cin>>N;
    ll ma=-1LL<<60;
    int best=-1;
    
    rep1(i,N){
        cin>>dp[i];
        ll a=dp[i];
        for(int x=1;x<i;x++)if((i-x)%2==0){
            if(dp[x]+a>dp[i]){
                from[i]=x;
                dp[i]=dp[x]+a;
            }
        }
        if(dp[i]>ma){
            ma=dp[i];
            best=i;
        }
    }
    cout1(ma);
    vector<int> ans;
    int x;
    for(x=N;x>best;x--) ans.push_back(x);
    while(from[x]>0){
        int y=from[x];
        while(y<x){
            ans.push_back((x+y)/2);
            x-=2;
        }
    }
    rep(i,x-1) ans.push_back(1);
    cout1(ans.size());
    for(auto a:ans){
        cout1(a);
    }
}


Measured memory_bytes: 256
/*input
4 3
.S.
.o.
.o.
.T.
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<double,double> pdd;
#define IOS ios_base::sync_with_stdio(0); cin.tie(0)
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define pb push_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
	out<<'('<<P.F<<','<<P.S<<')';
	return out;
}

//}}}
const ll maxn=305;
const ll maxlg=__lg(maxn)+2;
const ll INF64=8000000000000000000LL;
const int INF=0x3f3f3f3f;
const ll MOD=ll(1e9+7);
const double PI=acos(-1);
//const ll p=880301;
//const ll P=31;

ll mypow(ll a,ll b){
	ll res=1LL;
	while(b){
		if(b&1) res=res*a%MOD;
		a=a*a%MOD;
		
		b>>=1;
	}
	return res;
}

struct edge{
	int to,cap,rev;
};

vector<edge> G[maxn];

void add_edge(int u,int v,int cap){
	G[u].pb({v,cap,SZ(G[v])});
	G[v].pb({u,0,SZ(G[u])-1});
}
void bi_edge(int u,int v,int cap){
	add_edge(u,v,cap);
	add_edge(v,u,cap);
}

int lvl[maxn];
int iter[maxn];

int pt;
vector<int> q;

void bfs(int s){
	memset(lvl,-1,sizeof lvl);

	q.clear();q.pb(s);
	lvl[s]=0;
	pt=0;
	while(pt<SZ(q)){
		int cur=q[pt++];
		for(edge e:G[cur]) if(e.cap>0&&lvl[e.to]==-1){
			lvl[e.to]=lvl[cur]+1;
			q.pb(e.to);
		}
	}
}

int dfs(int s,int t,int ff){
	if(s==t) return ff;
	for(int &i=iter[s];i<SZ(G[s]);i++){
		edge &e=G[s][i];
		if(e.cap>0&&lvl[e.to]==lvl[s]+1){
			int tmp=dfs(e.to,t,min(ff,e.cap));
			if(tmp>0){
				e.cap-=tmp;
				G[e.to][e.rev].cap+=tmp;
				return tmp;
			}
		}
	}
	return 0;
}

int dinic(int s,int t){
	int ret=0;	
	while(1){
		bfs(s);
		if(lvl[t]==-1) return ret;
		memset(iter,0,sizeof iter);
		int f;
		while((f=dfs(s,t,INF))>0) ret+=f;
	}
	return 0;
}

string s[maxn];
int main()
{
	IOS;
	int n,m;
	cin>>n>>m;
	REP(i,n) cin>>s[i];

	REP(i,n) REP(j,m) if(s[i][j]!='.'){
		if(s[i][j]=='S') add_edge(n+m,i,INF),add_edge(n+m,j+n,INF);
		else if(s[i][j]=='T') add_edge(i,n+m+1,INF),add_edge(j+n,n+m+1,INF);
		else bi_edge(i,j+n,1);
	}
	int tmp=dinic(n+m,n+m+1);
	if(tmp>=INF) cout<<"-1\n";
	else cout<<tmp<<'\n';
	return 0;
}


Measured memory_bytes: 896
import sys
def dijkstra(n, a):
	v = [False]*n
	v[0] = True
	w = [sys.maxsize] * n
	w[0] = 0
	while True:
		m = -1
		mini = sys.maxsize
		for i in range(n):
			for j in range(n):
				c = a[i][j]
				if v[i] and not v[j] and c != 0:
					w[j] = min(w[j], w[i]+c)
					if mini > w[j]:
						mini = w[j]
						m = j
		if m == -1: break
		v[m] = True
	
	for i in range(n):
		print(str(i) + ' ' + str(w[i]))

n = int(input())
a = [[0 for x in range(n)] for x in range(n)]
for i in range(n):
	t = list(map(int,input().split()))
	u = t[0]
	k = t[1]
	for j in range(k):
		v = t[(j*2)+2]
		c = t[(j*2)+3]
		a[u][v] = c
dijkstra(n, a)



Measured memory_bytes: 5704
n,m=map(int,input().split())
num=[-1]*n
for i in range(m):
    s,c=map(int,input().split())
    if s==1 and c==0 and n!=1:print(-1);exit()
    if num[s-1]==-1:
        num[s-1]=c
    if num[s-1]!=-1:
        if num[s-1]!=c:print(-1);exit()
if n==1 and num[0]==-1:print(0)
else:
    ans=0
    for i in range(n):
        if num[i]!=-1:
            ans+=num[i]*10**(n-i-1)
        elif i==0:ans+=10**(n-1)
        else:continue
    print(ans)
    

Measured memory_bytes: 3064
const input = require('fs').readFileSync('/dev/stdin', 'utf8');
const lines = input.trim().split('\n');

while (line = lines.shift()) {
    const numbers = line.split(' ').map(Number);

    // Edit from here
    let a = numbers[0];
    let b = numbers[1];

    if ( a === 0 && b === 0 ) { break; }

    if ( a > b ) {
        const tmp = a;
        a = b;
        b = tmp;
    } 

    const output = a + ' ' + b;
    // to here
    
    console.log(output);
    
}


Measured memory_bytes: 19364
#define _USE_NATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <random>
#include <tuple>


using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
#define EPS 1e-9
const int INF = 1050000000;
const long long LINF = 1LL << 60;
const ll MOD = 1e9 + 7;
const int MINF = -1050000000;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }




int main() {
	int n;
	cin >> n;
	vector<int> a(n);
	
	rep(i, n) cin >> a[i];

	int sum = 0;
	for (int i = 0; i < n - 1; ++i) {
		sum += abs(a[i] - a[i + 1]);
	}
	for (int i = 0; i < n; ++i) {
		int ans = sum;
		if (i == 0) {
			ans -= abs(a[i] - a[i + 1]);
			ans += abs(0 - a[i + 1]);//最初
			ans += abs(a[n - 1]);//最後
		}
		else if (i == n - 1) {
			ans -= abs(a[i-1] - a[i]);
			ans += abs(0 - a[0]);// 最初
			ans += abs(a[i - 1]);//最後
		}
		else {
			ans -= abs(a[i] - a[i + 1]);
			ans -= abs(a[i-1] - a[i]);
			ans += abs(0 - a[0]);// 最初
			ans += abs(a[i - 1] - a[i + 1]);
			ans += abs(a[n - 1]);//最後
		}
		cout << ans << endl;
	}
	return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

using u128 = __uint128_t;

#define int long long
#define idxSet tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define atIdx find_by_order
#define idxOf order_of_key
#define MOD 1000000007
#define MAX 200005
#define endl '\n'

typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;

int dp[MAX];

void update(vector<vi> &bit , int idx , char v , int n, int sign){
    for( ; idx < n ; idx = idx | (idx + 1))
        bit[idx][v - 'a'] += sign ;

}

void add(vector<vi> &bit , int idx , int sign , int * count){
    for( ; idx >= 0 ; idx = (idx & (idx + 1)) - 1){
        for(int i = 0 ; i < 26 ; i++){
            count[i] += sign * bit[idx][i];
        }
    }
}

void solve() {
    int n;
    cin>>n;
    string x;
    cin>>x;
    vector<vi> bit(n , vector<int>(26 , 0));
    for(int i = 0 ; i< n ; i++){
        update(bit , i , x[i] , n , 1);
    }
    int q;
    cin>>q;
    while(q--){
        int t;
        cin>>t;
        if(t==1){
            int ith ;
            cin>>ith;
            ith--;
            char c;
            cin>>c;
            update(bit , ith , x[ith] , n , -1);
            update(bit , ith , c , n , 1);
            x[ith] = c;
        }else {
            int count[26] = {0};
            int l,r ;
            cin>>l>>r;
            l--,r--;
            add(bit, r , 1 , count);
            add(bit , l -1 , -1 , count);
            int c = 0;
            for(int i = 0 ; i< 26 ; i++){
                if(count[i] > 0)
                    c++;
            }
            cout<<c<<endl;
        }
    }
}

int32_t main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    //cin>>t;
    while (t--)
        solve();
    return 0;
}

Measured memory_bytes: 121884
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
  ios::sync_with_stdio(false); cin.tie(0);
  set<string>s;
  int n; cin>>n;
  for(int i=1;i<=n;++i){
      string x; cin>>x; s.insert(x);
  }
  cout<<s.size()<<"\n";
}

Measured memory_bytes: 19196
#include<bits/stdc++.h>
using namespace std;
#define FOR(i,l,r) for(long long i=(l);i<(r);++i)
#define REP(i,n) FOR(i,0,n)
#define REPS(i,n) FOR(i,1,n+1)
#define RFOR(i,l,r) for(long long i=(l);i>=(r);--i)
#define RREP(i,n) RFOR(i,N-1,0)
#define RREPS(i,n) RFOR(i,N,1)
#define int long long
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define SZ(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
const int INF=1e18;
const int MOD=1e9+7;
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
typedef pair<int,int> P;

signed main(){
    int H,W;cin>>H>>W;
    char maze[H][W];
    int depth[H][W];
    REP(i,H)REP(j,W)depth[i][j]=INF;
    queue<P> q;
    REP(i,H)REP(j,W){
        cin>>maze[i][j];
        if(maze[i][j]=='#')q.push({i,j});
    }
    int ans=0;
    while(q.size()){
        P p=q.front();q.pop();
        int nx=p.first,ny=p.second;
        if(depth[nx][ny]==INF)depth[nx][ny]=0;
        REP(i,4){
            int x=nx+dx[i];
            int y=ny+dy[i];
            if(x>=0&&x<H&&y>=0&&y<W&&depth[x][y]==INF&&maze[x][y]=='.'){
                depth[x][y]=depth[nx][ny]+1;
                q.push({x,y});
            }
        }
    }
    REP(i,H)REP(j,W)chmax(ans,depth[i][j]);
    cout<<ans<<endl;
}

Measured memory_bytes: 25396
n,k=map(int,input().split())
points=[]
for _ in range(n):
    x,y=map(int,input().split())
    points.append((x,y))
points.sort()
ans=float('inf')
for i in range(n-k+1):
    for j in range(i+k-1,n):
        y_sorted=sorted(points[i:j+1],key=lambda x:x[1])
        height_min=float('inf')
        for l in range(j-i-k+2):
            height_min=min(height_min,y_sorted[l+k-1][1]-y_sorted[l][1])
            ans=min(ans,(points[j][0]-points[i][0])*height_min)
print(ans)

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

#define int long long
#define fast_io(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,a,b) for(int i=a;i<b;i++)
#define all(x) x.begin(),x.end()
#define us unordered_set
#define um unordered_map
#define ms multiset
#define mm multimap
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define _max max_element
#define _min min_element
#define tc(T) int T; cin>>T; while(T--)
#define q(q) int q; cin>>q; while(q--)
#define pi 3.1415926535897932384626433832
const int pinf=((int)2e18);
const int ninf=((int)-2e18);
const int mod=1000000007;

using namespace std;

int power(int x,int n,int M)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)
        return power((x*x)%M,n/2,M);
    else
        return (x*power((x*x)%M,(n-1)/2,M))%M;
}

bool vowel(char c)
{
    return(c=='a' or c=='e' or c=='i' or c=='o' or c=='u' or c=='A' or c=='E' or c=='I' or c=='U' or c=='O');
}

bool cp(int n)
{
    if(n<=1)
        return false;

    for(int i=2;i<=sqrt(n);i++)
    {
        if(n%i==0)
            return false;
    }
    return true;
}

int32_t main()
{
    fast_io();

    //FILE *fptr1=freopen("in.txt","r",stdin);
    //FILE *fptr2=freopen("out.txt","w",stdout);

    int n;
    cin>>n;
    int mn=pinf;
    for(int i=1;i<=sqrt(n);i++)
    {
        if(n%i==0)
        {
            mn=min(mn,i-1+n/i-1);
        }
    }
    cout<<mn;
    return 0;
}


Measured memory_bytes: 3668
import std.algorithm, std.container, std.conv, std.math, std.range, std.typecons, std.stdio, std.string;

auto rdsp(){return readln.splitter;}
void pick(R,T)(ref R r,ref T t){t=r.front.to!T;r.popFront;}
void readV(T...)(ref T t){auto r=rdsp;foreach(ref v;t)pick(r,v);}

void main()
{
  int a, b; readV(a, b);
  if (a+b >= 10) writeln("error");
  else           writeln(a+b);
}


Measured memory_bytes: 256
Dice = Struct.new(:labels) do
  def self.init(labels)
    raise ArgumentError, "length of labels should be 6" unless labels.count == 6

    new(labels)
  end

  def rotate(direction)
    case direction
    when ?N then rotate_northward
    when ?S then rotate_southward
    when ?E then rotate_eastward
    when ?W then rotate_westward
    else raise ArgumentError, "direction should be N, S, E or W, but #{direction} was given."
    end
  end

  def rotate_westward
    a,b,c,d,e,f = labels
    self.class.init([c,b,f,a,e,d])
  end

  def rotate_eastward
    c,b,f,a,e,d = labels
    self.class.init([a,b,c,d,e,f])
  end

  def rotate_southward
    a,b,c,d,e,f = labels
    self.class.init([e,a,c,d,f,b])
  end

  def rotate_northward
    e,a,c,d,f,b = labels
    self.class.init([a,b,c,d,e,f])
  end

  def [](index)
    raise ArgumentError, "index should be in 1 to 6" unless (1..6).include?(index)

    labels[index-1]
  end

  def top
    self[1]
  end
end

labels = gets.split.map(&:to_i)
dice = Dice.init(labels)

dice_last_state = gets.chomp.chars.reduce(dice) {|dice, direction|
  dice.rotate(direction)
}

p dice_last_state.top


Measured memory_bytes: 6876
from sys import stderr
from functools import reduce
from operator import add
def f(): return [int(i) for i in input().split()]
def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(repr(item))
        stderr.write(sep)
    stderr.write(end)

n = int(input())
a = f()
abss = []
for i in range(n):
    for j in range(i,n):
        abss.append(abs(a[i] - a[j]))

print(max(abss))

Measured memory_bytes: 3828
#include <stdio.h>

int main(void)
{

	int h, w;
	int ii, jj;

	for (;;) {
		scanf("%d %d", &h, &w);
		if (h == 0 && w == 0) {
			break;
		}
		for (ii = 0; ii < h; ii++) {
			for (jj = 0; jj < w; jj++) {
				printf("#");
			}
			printf("\n");
		}
		printf("\n");
	}

	return 0;
}

Measured memory_bytes: 572
sx, sy, tx, ty = [int(x) for x in input().split()]

dx = tx -sx
dy = ty -sy

c = ['U', 'D', 'L', 'R']

print(c[0]*dy+c[3]*dx+c[1]*dy+c[2]*dx 
 + c[2] + c[0]*(dy+1) +c[3]*(dx+1) + c[1] 
 + c[3] + c[1]*(dy+1) + c[2]*(dx+1) + c[0])
 

Measured memory_bytes: 3060
#input of int(split by space)
def get_i()
  return gets.chomp.split(" ").map(&:to_i)
end
#input of float(split by space)
def get_f()
  return gets.chomp.split(" ").map(&:to_f)
end
#input of string(split by space)
def get()
  return gets.chomp.split(" ")
end
#input of string(split per one character)
def get_nsp()
  return gets.chomp.split("")
end
#yes or no decision
def yn_judge(bool,y="Yes",n="No")
  return bool ? y : n 
end
#create of array
def array(size1,init=nil,size2=-1)
  if size2==-1
    return Array.new(size1){init}
  else
    return Array.new(size2){Array.new(size1){init}}
  end
end

def max(a,b)
    return a>b ? a : b
end

def min(a,b)
    return a>b ? b : a
end

INF=Float::INFINITY

def cal(x)
    case x
    when 1 then
        return 300000
    when 2 then
        return 200000
    when 3 then
        return 100000
    end
    return 0
end
X,Y=get_i
ans=0
ans+=cal(X)
ans+=cal(Y)
if X==1 and Y==1
    ans+=400000
end
puts ans

Measured memory_bytes: 1788
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def main():
    n=int(input())
    a=list(map(int,input().split()))
    q=int(input())
    total=sum(a)
    memo = defaultdict(int)
    answer = 0
    ans=[]
    for i in range(n):
        memo[a[i]]+=1
    for i in range(q):
        b,c=map(int,input().split())
        temp1=memo[b]*b
        temp2=memo[b]*c
        ded=temp2-temp1
        ans.append(total+ded)
        total+=ded

        memo[c]+=memo[b]
        memo[b] = 0

    for i in range(q):
        print(ans[i])










if __name__ == "__main__":

    main()

Measured memory_bytes: 99080
lines = $stdin.read
array = lines.split("\n")
 
X,Y,Z = array[0].split(" ").map(&:to_i)
 
sum   = 0
times = 1
 
while true
  sum = (Z + Y) * times
  break if sum > X
  #puts "#{sum} < #{X}"
  times += 1
end
 
times-=1
 
if (X - (Z + Y) * times) >= Z
  puts times
else
  puts times-1
end

Measured memory_bytes: 1788
#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

struct Med{
	multiset<int>f,b;
	ll s1=0,s2=0;
	void balance(){
		while(f.size()>=b.size()+2){
			int t=*f.rbegin();
			f.erase(--f.end());
			s1-=t;
			s2+=t;
			b.insert(t);
		}
		while(f.size()<b.size()){
			int t=*b.begin();
			b.erase(b.begin());
			s2-=t;
			s1+=t;
			f.insert(t);
		}
	}
	void insert(int x){
		if(!b.empty()&&*b.begin()<x){
			b.insert(x),s2+=x;
		}
		else f.insert(x),s1+=x;
		balance();
	}
};
Med med;
int main(){
	int q;cin>>q;
	ll t=0;
	rep(i,q){
		int ty;scanf("%d",&ty);
		if(ty==1){
			int a,b;scanf("%d%d",&a,&b);
			t+=b;
			med.insert(a);
		}
		else{
			int x=*med.f.rbegin();
			printf("%d %lld\n",x,t+x*(ll)med.f.size()-med.s1+med.s2-x*(ll)med.b.size());
		}
	}
}

Measured memory_bytes: 9600
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {
  int N, K, X, Y, sum;
  cin >> N >> K >> X >> Y;
  //N = 5;
  //K = 3;
  //X = 10000;
  //Y = 9000;
  sum = 0;
  
  for (int i = 1; i <= N; i++) {
    if(i<=K){
        sum += X;
    }else{
        sum += Y;
    }
  }

  cout << sum << endl;
  
}

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

#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)
#define REP(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define ALL(a)  (a).begin(),(a).end()

template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return true; } return false; }
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
int gcd(int a,int b){return b?gcd(b,a%b):a;}
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

void solve() {
    int N;
    cin >> N;
    vector<int> v(N);
    REP(i,N) cin >> v[i];
    ll ans = INF;
    FOR(i,-100,101) {
        ll sum = 0;
        for(const auto & it: v) {
            sum += (it-i) * (it-i);
        }
        chmin(ans,sum);
    }
    cout << ans << endl;

}

int main() {
    solve();
    return 0;
}


Measured memory_bytes: 256
//2019.8.10 by ljz
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
#define res register int
#define LL long long
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f
#define unl __int128
#define eps 5.6e-8
#define RG register
#define db double
#define pc(x) __builtin_popcount(x)
typedef pair<int,int> Pair;
#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pb push_back
#define gc getchar
//inline char gc() {
//    static char buf[100000],*p1,*p2;
//    return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
//}
//inline int read() {
//    res s=0,ch=gc();
//    while(ch<'0'||ch>'9')ch=gc();
//    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//    return s;
//}
inline int read() {
    res s=0,ch=gc(),w=1;
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=gc();}
    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
    return s*w;
}
inline LL Read() {
    RG LL s=0;
    res ch=gc();
    while(ch<'0'||ch>'9')ch=gc();
    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
    return s;
}
//inline LL Read() {
//    RG LL s=0;
//    res ch=gc(),w=1;
//    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=gc();}
//    while(ch>='0'&&ch<='9')s=s*10+ch-'0',ch=gc();
//    return s*w;
//}
//inline void write(RG unl x){
//    if(x>10)write(x/10);
//    putchar(int(x%10)+'0');
//}
inline void swap(res &x,res &y) {
    x^=y^=x^=y;
}
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//clock_t start=clock();
//inline void ck(){
//    if(1.0*(clock()-start)/CLOCKS_PER_SEC>0.1)exit(0);
//}
namespace MAIN{
    int k,x;
    inline void MAIN(){
        k=read(),x=read();
        for(res i=1;i<k;i++)printf("%d ",x-(k-i));
        printf("%d ",x);
        for(res i=1;i<k;i++)printf("%d ",x+i);
    }
}
int main(){
//    srand(19260817);
//    freopen("signin.in","r",stdin);
//    freopen("signin.out","w",stdout);
    MAIN::MAIN();
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#define rep(i,n) for(int i=0;i<(n);i++)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define psortfirst(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.first<b.first;});
#define All(x) (x).begin(),(x).end()
using namespace std;
using pii  = pair<int,int>;
long long gcd(long long a, long long b){return (b == 0 ? a : gcd(b, a%b));}
long long lcm(long long a, long long b){return a*b/gcd(a,b);}
typedef long long ll;
#define MOD 1000000007
#define EPS 10e-8

int main() {
    ll A[4];
    rep(i,3){
        cin >> A[i];
    }
    //sort(A,A+3);
    cout << (A[0]-A[1])%A[2] << endl;
}

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

// Loops
#define _overload3(_1, _2, _3, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, b) for (int i = int(a); i < int(b); ++i)
#define _rrep(i, n) rrepi(i, n, 0)
#define rrepi(i, a, b) for (int i = int(a) - 1; i >= int(b); --i)

#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define rrep(...) _overload3(__VA_ARGS__, rrepi, _rrep, )(__VA_ARGS__)
#define each(xi, x) for (auto &&xi : x)
// Note: we can use rep(i,N) or rep(i,from,to)

// typedef
template <class T> using vec = vector<T>;
using ll = long long;
using vi = vec<ll>;
using vvi = vec<vi>;
using vvvi = vec<vvi>;
using pii = pair<ll, ll>;

// Constants

// Shorter repr for frequently used terms
#define pb push_back
#define mp make_pair

// Algorithms
#define all(x) (x).begin(), (x).end()
#define uniq(v) v.erase(unique(all(v)), v.end())
#define perm(c)                                                                \
  sort(all(c));                                                                \
  for (bool c##p = 1; c##p; c##p = next_permutation(all(c)))

template <class T> pair<T, size_t> max(vector<T> &x) {
  auto it = max_element(all(x));
  return mp(*it, it - x.begin());
}
template <class T> pair<T, size_t> min(vector<T> &x) {
  auto it = min_element(all(x));
  return mp(*it, it - x.begin());
}

template <class T> inline bool chmax(T &maxval, const T &newval) {
  if (maxval < newval) {
    maxval = newval;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &minval, const T &newval) {
  if (minval > newval) {
    minval = newval;
    return 1;
  }
  return 0;
}

// Utilities
// Grid world utilities
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
#define inside(H, W, y, x) 0 <= (x) && (x) < (W) && 0 <= (y) && (y) < (H)

// Debug
#ifdef LOCAL
#include "dump.hpp"
#define debug(x) cerr << #x << ": " << x << '\n'
#else
#define dump(...)
#define debug(x)
#endif

// Paste snippets here!!

//

void solve(long long N, std::vector<long long> A) {
  bool ok = true;
  rep(i,N){
    if(A[i]%2==0){
      if(A[i]%3!=0 && A[i]%5!=0) ok = false;
    }
  }
  if(!ok) cout << "DENIED" << endl;
  else cout << "APPROVED" << endl;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false); // Magic for faster cin
  cout.precision(12);
  long long N;
  std::cin >> N;
  std::vector<long long> A(N);
  for (int i = 0; i < N; i++) {
    std::cin >> A[i];
  }
  solve(N, std::move(A));
  return 0;
}


Measured memory_bytes: 3660
#include<bits/stdc++.h>
using namespace std;
int main() {
	int n; cin >> n;
	int a[n];
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];
	}
	sort(a, a + n);
	int div[1001001] = {};
	int res;
	if(a[0] < a[1]) res = 1;
	else res = 0;
	for (int i = 1; i * a[0] <= a[n-1]; i++)
	{
		div[i*a[0]] = 1;
	}

	for (int i = 1; i < n; i++)
	{
		if(div[a[i]] == 0) {
			if(i != n-1) {
				if(a[i] < a[i+1]) res++;
				for (int j = 1; j * a[i] <= a[n-1]; j++)
				{
					div[j*a[i]] = 1;
				}
			}
			else res++;
		}
	}
	cout << res << endl;

	return 0;
}


Measured memory_bytes: 8328
#include <stdio.h>
#define INFTY 1000000000

int p[100];
int m[100][100];

int main(){
  int n, i;
  int x, j, k, q;

  scanf("%d", &n);

  for(i=0; i<n; i++){
    scanf("%d %d", &p[i], &p[i+1]);
  }

  for(i=1; i<=n; i++){ //[6][6]まで0
    m[i][i]=0;
  }

  //n=n-1;
  
  for(x=2; x<=n; x++){

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

      j=i+x-1;
      m[i][j]=INFTY;
      //printf("j= %d\n", j);

      for(k=i; k<=j-1; k++){

	q=m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j];
	if(q<m[i][j])
	  {m[i][j]=q;
	    //printf("%d\n", m[i][j]);
	  }
	
      }
    }
  }
  printf("%d\n", m[1][n]);
  return 0;
}



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

int main()
{
    int n;
    cin >> n;
    vector<int> a(n + 1);
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    int x = a[1];
    for (int i = 1; i <= n; i++)
    {
        if (x == 2)
        {
            cout << i << endl;
            return 0;
        }
        x = a[x];
    }
    cout << -1 << endl;
    return 0;
}

Measured memory_bytes: 3736
from scipy.sparse.csgraph import floyd_warshall
N,M,L,*X = map(int, open(0).read().split())
ls = X[:3*M]
Els = [[0]*N for i in range(N)]
Q = X[3*M]
query = X[3*M+1:]
for a,b,c in zip(*[iter(ls)]*3):
  if c<=L:
    Els[a-1][b-1] = c
    Els[b-1][a-1] = c
dist = floyd_warshall(Els)
      
Els2 = [[0]*N for i in range(N)]
for i in range(N):
  for j in range(N):
    if dist[i][j]<=L:
      Els2[i][j] = 1
ans = floyd_warshall(Els2)

for s,t in zip(*[iter(query)]*2):
  if ans[s-1][t-1]==float('inf'):
    print(-1)
    continue
  print(int(ans[s-1][t-1])-1)

Measured memory_bytes: 40832
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,y;
    cin>>n>>y;
    for(int i=0;i<=n;i++)
        for(int j=0;j<=n-i;j++)
            if(i*10000+j*5000+(n-i-j)*1000==y)
                cout<<i<<' '<<j<<' '<<n-i-j<<'\n',exit(0);
    cout<<"-1 -1 -1\n";
    return 0;
}

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

void solve(long long N, std::vector<long long> X){
    vector<long long> cand(101);
    for (ll i=0;i<=100;i++){
        for (auto x:X ){
            cand[i]+= (x-i)*(x-i);
        }
    }
    cout << *std::min_element(cand.begin(),cand.end()) << endl;

}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> X(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&X[i]);
    }
    solve(N, std::move(X));
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define fastcin  ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;

int main(){

	fastcin
	
	int n, m, a, b;
	cin >> n >> m;
	
	int h[n+1];
	int ans[n+1];
	
	for(int i=1;i<=n; i++) ans[i] = 1;
	
	for(int i=1; i<=n; i++) cin >> h[i];
	
	for(int i=0; i<m; i++){
		cin >> a >> b;	
		if(h[a] > h[b]) ans[b] = 0;
		else if(h[a] < h[b]) ans[a] = 0;
		else{ans[a]=0; ans[b]=0;}
	}
	
	int cnt=0;
	for(int i=1; i<=n; i++){
		if(ans[i]) cnt++;
	}
	
	cout << cnt << endl;
	      
	return 0;
}



Measured memory_bytes: 4452
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
const int MOD = 1000000007;
int main() {
    int k,a,b;
    cin>>k>>a>>b;
    bool ok=false;
    for (int i = a; i <= b; i++)
    {
        /* code */
        if (i%k==0)
        {
            /* code */
            ok=true;
            break;
        }
        
    }
    if (ok)
    {
        /* code */
        cout<<"OK"<<endl;
    }
    else
    {
        cout<<"NG"<<endl;
    }
    
    
}

Measured memory_bytes: 3624
MOD = 1000000007
def maxProductSubarrayOfSizeK(A, n, k): 

	A.sort() 

	product = 1

	if (A[n - 1] == 0 and (k & 1)): 
		return 0

	if (A[n - 1] < 0 and (k & 1)): 
	    A.reverse()
	    for i in range(k):
	        product *= A[i]
	        if abs(product) >= MOD:
	            product = product%MOD
	    return product

	i = 0

	j = n - 1

	if (k & 1): 
		product *= A[j]
		product %= MOD
		j-= 1
		k-=1

	k >>= 1

	for itr in range( k) : 

		left_product = A[i] * A[i + 1] 

		right_product = A[j] * A[j - 1] 

		if (left_product > right_product) : 
			product *= left_product 
			product %= MOD
			i += 2
		
		else : 
			product *= right_product
			product %= MOD
			j -= 2

	return product 

n, k = map(int, input().split(' '))
numbers = [int(x) for x in input().split(' ')]
ans = maxProductSubarrayOfSizeK(numbers, n, k)
#print(ans)
print((ans + MOD)%MOD)

Measured memory_bytes: 31800
#ifndef BZ
#pragma GCC optimize "-O3"
#endif
#include <bits/stdc++.h>

#define FASTIO

#ifdef FASTIO
#define scanf abacaba
#define printf abacaba
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;

using namespace std;


/*
ll pw(ll a, ll b) {
	ll ans = 1; while (b) {
		while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
		ans = (ans * a) % MOD, --b;
	} return ans;
}
*/
const int MAXN = 120000;

ll a[MAXN];
vector<pair<int, ll> > eds[MAXN];

int was[MAXN];
int cl[MAXN];
vector<int> vv, ee;
int n, m;

void no() {
	cout << 0 << "\n";
	exit(0);
}

void run(int v, ll x) {
	a[v] = x;
	was[v] = 1;
	for (auto e: eds[v]) {
		int u = e.first;
		if (was[u]) {
			if (a[u] + a[v] != e.second)
				no();
		}
		else {
			run(u, e.second - x);
		}
	}
}

int dfs1(int v, int c) {
	vv.push_back(v);
	was[v] = 1;
	cl[v] = c;
	for (auto e: eds[v]) {
		int u = e.first;
		if (was[u]) {
			if (cl[u] == cl[v]) {
				ee.push_back(e.second);
				reverse(vv.begin(), vv.end());
				reverse(ee.begin(), ee.end());
				while (vv.back() != u)
					vv.pop_back(), ee.pop_back();
				ll sum = 0;
				for (ll x: ee)
					sum += x;
				if (sum % 2 != 0)
					no();
				sum /= 2;
				reverse(vv.begin(), vv.end());
				reverse(ee.begin(), ee.end());
				for (int i = 0; i + 1 < ee.size(); i += 2)
					sum -= ee[i];
				if (sum <= 0)
					no();
				memset(was, 0, sizeof(was));
				run(v, sum);
				for (int i = 0; i < n; ++i)
					if (a[i] <= 0)
						no();
				cout << 1 << "\n";
				return 1;
			}
		}
		else {
			ee.push_back(e.second);
			if (dfs1(u, c ^ 1))
				return 1;
			ee.pop_back();
		}
	}
	vv.pop_back();
	return 0;
}

int main() {
#ifdef FASTIO
	ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
	cin >> n >> m;
	for (int i = 0; i < m; ++i) {
		int u, v;
		ll s;
		cin >> u >> v >> s;
		--u, --v;
		eds[u].emplace_back(v, s);
		eds[v].emplace_back(u, s);
	}
	if (dfs1(0, 0)) {
		return 0;
	}
	else {
		memset(was, 0, sizeof(was));
		run(0, 0);
		ll l = -1e18;
		ll r = 1e18;
		for (int i = 0; i < n; ++i) {
			if (cl[i] == 0) {
				l = max(l, -a[i] + 1);
			}
			else {
				r = min(r, a[i] - 1);
			}
		}
		if (l > r)
			no();
		cout << r - l + 1 << "\n";
	}
	return 0;
}




Measured memory_bytes: 19188
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <map>
typedef long long LL;

int main() {
	int x1, x2, y1, y2;
	std::cin >> x1 >> x2 >> y1 >> y2;
	int x = y1-x1;
	int y = y2-x2;

	std::cout << y1-y << " " << y2+x << " " << x1-y << " " << x2+x << std::endl;
	return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>

using namespace std;

int main(void){
	
	int n;
	
	while(scanf("%d", &n) != EOF){
		for(int i = 0; i < n; i++){
			
			char temp[128];
			char str[128];
			int Mx,Mi;
			
			scanf("%s", &temp);
			
			sort(temp,temp+8);
			for(int i = 8; i >= 0; i--){
				str[i] = temp[i];
			}
			
			reverse(temp,temp+8);
			
			Mx = atoi(temp);
			Mi = atoi(str);
			
			printf("%d\n",Mx-Mi);
		}
	}
	return 0;
}

Measured memory_bytes: 0
#include <iostream>
using namespace std;
using large = long long;
int main() {
    large n, blue, red;
    cin >> n >> blue >> red;
    large repeat = n / (blue + red);
    large remains = n - repeat * (blue + red);
    if (remains > blue)
        remains = blue;

    cout << (repeat * blue + remains);
    return 0;
}


Measured memory_bytes: 256
s = gets.chomp.split("")
if  (s[0] == "A")&& (s[2..-2].count{|t|t == "C"} == 1)
  s.delete("A")
  s.delete("C")
  require 'set'
  al = ("a".."z").to_a
  if  Set.new(al).superset?(Set.new(s))
    puts "AC"
  else
    puts "WA"
  end
else
    puts "WA"
end


Measured memory_bytes: 2044
#include <iostream>
#include <cmath>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)

int solver(double ra, double rb, double d);

int main(void)
{
    double xa,ya,ra,xb,yb,rb,d;
    int n;
    
    cin >> n;
    
    rep(i,n)
    {
        cin >> xa >> ya >> ra >> xb >> yb >> rb;
        d = hypot(xb-xa, yb-ya);
        cout << solver(ra, rb, d) << endl;
    }
    
    return 0;
}

int solver(double ra, double rb, double d)
{
    if(d+rb<ra)
        return 2;
    else if(d+ra<rb)
        return -2;
    else if(ra+rb<d)
        return 0;
    else
        return 1;
}

Measured memory_bytes: 1248
#include<stdio.h>
int main(){
int w,h,x,y,r;
scanf("%d %d %d %d %d",&w,&h,&x,&y,&r);
if(x-r>=0&&x+r<=w&&y-r>=0&&y+r<=h)printf("Yes\n");
else printf("No\n");
}

Measured memory_bytes: 1012
#include<stdio.h>
int main(void){
   int m, f, b;
   scanf("%d %d %d", &m, &f, &b);
   if ( m >= b) printf("0\n");
   else if ( m + f >= b ) printf("%d\n", b - m);
   else printf("NA\n");
   return 0;
}

Measured memory_bytes: 1860
#include<bits/stdc++.h>
using namespace std;
int x,y;
int main()
{
    cin>>x>>y;
  
  for(int i=0;i<=x;i++)
  {
      for(int j=0;j<=x-i;j++)
      {
          if(i*4+j*2==y && i+j==x) 
          {
             cout<<"Yes";
             return 0;  
          }
         
      }
  }
  cout<<"No";
  return 0;
}


Measured memory_bytes: 3636