akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

#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)-1; i>=(b); --i)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

const int MOD = 1000000007;

int main() {
  // ios_base::sync_with_stdio(0);

  int N;
  scanf("%d", &N);
  if (N == 3) {
    printf("2 5 63\n");
    return 0;
  }

  set<int> res;
  int su = 0;
  FOR(i,7,30001) {
    if (res.size() == N-2) break;
    if (i%2 == 0 || i % 3 == 0) {
      res.insert(i);
      su += i;
    }
  }

  if (su % 6 == 0) {
    res.insert(2);
    res.insert(4);
  } else if (su % 6 == 1) {
    res.insert(2);
    res.insert(3);
  } else if (su % 6 == 2) {
    res.insert(4);
    res.insert(6);
  } else if (su % 6 == 3) {
    res.insert(3);
    res.insert(6);
  } else if (su % 6 == 4) {
    res.insert(2);
    res.insert(6);
  } else if (su % 6 == 5) {
    res.insert(3);
    res.insert(4);
  }

  if (N == 19999) {
    res.erase(6);
    res.insert(2);
    res.insert(4);
  } else if (N == 20000) {
    res.insert(2);
    res.insert(4);
  }

  for (int u: res) printf("%d ", u);
  printf("\n");
}


Measured memory_bytes: 1280
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main()
{
    int N;
    cin >> N;
    vector<int> d(N);
    rep(i, N) cin >> d[i];
    sort(d.begin(), d.end());
    int res = 0;
    rep(i, N)
    {
        if (i == 0)
        {
            res++;
            continue;
        }
        if (d[i] - d[i - 1] != 0)
            res++;
    }
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 3604
#include "bits/stdc++.h"

#define REP(i,num) for(int i=0;i<(num);++i)
#define ALL(c) c.begin(),c.end()
#define PRINTALL(c) for(auto& x:c){cout<<x<<' ';}cout<<endl;
#define PAIRCOMP(c,comp) [](const pair<ll,ll>& lhs,const pair<ll,ll>& rhs){return lhs.c comp rhs.c;}

using namespace std;
using ll = long long;

constexpr ll atcoder_mod = 1e9+7;

template<typename T=int>
T in(){T x; cin >> x; return (x);}
template<typename T=int,typename C=vector<T>>
C vecin(int N){C x(N);REP(i,N){x[i]=in<T>();}return move(x);}

void vout(){cout << endl;}
template<typename Head,typename... Tail>
void vout(Head&& h,Tail&&... t){cout << ' ' << h;vout(forward<Tail>(t)...);}
void out(){cout << endl;}
template<typename Head,typename... Tail>
void out(Head&& h,Tail&&... t){cout << h;vout(forward<Tail>(t)...);}

void maxHeapify(vector<int>& A,int index){
	int H = A.size();
	int left = 2*index;
	int right = 2*index+1;
	int largest = (left<=H && A[left-1]>A[index-1]) ? left : index;
	if(right<=H && A[right-1]>A[largest-1]) largest = right;

	if(largest!=index){
		swap(A[index-1],A[largest-1]);
		maxHeapify(A,largest);
	}
}

void buildMaxHeap(vector<int>& A){
	int H=A.size();
	for(int i=H/2;i>=1;i--){
		maxHeapify(A,i);
	}
}

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

	int H=in();
	auto A=vecin(H);
	buildMaxHeap(A);
	REP(i,H){
		cout << ' ' << A[i];
	}
	cout << endl;
	
	return 0;
}



Measured memory_bytes: 4476
import math

K = int(input())

ans = 0
'''
for i in range(1,K+1):
    for j in range(1,K+1):
        for l in range(1,K+1):
           ab = math.gcd(i,j)
           abc = math.gcd(ab,l)
           print(i,j,l)
           ans += abc
'''
           
for i in range(1,K+1):
    for j in range(i,K+1):
        for l in range(j,K+1):
           ab = math.gcd(i,j)
           abc = math.gcd(ab,l)
           #print(i,j,l)
           if i == j and j == l:#３つすべて同じ数字のとき
               abc = abc
               #print('abc',abc)
           elif i == j or j == l or i == l:#２つの数字が同じ時
               abc = 3*abc
               #print('abc',abc)
           elif i != j and i != l and j != l:#３つの数字がすべて違うとき
               abc = 6*abc
               #print('abc',abc)
           
           ans += abc
           #print(ans,abc)


print(ans)

Measured memory_bytes: 9100
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <queue>
#include <list>
#define rep(i,n) for(int i=0;i<(n);i++)
#define crep(i) for(char i='a';i<='z';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()
int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}
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(){
    char S[107];
    ll  K;
    cin >> S >> K;
    ll j=strlen(S);
    j=min(j,K);
    rep(i,j){
        if(S[i]!='1'){
            cout << S[i] << endl;
            return 0;
        }
    }
    cout << 1 << endl;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int main() {
    long long x, k, d;
    cin >> x >> k >> d;
    x = abs(x);
    if (k <= x/d) {
        cout << x - k * d << endl;
        return 0;
    }
    k -= x / d;
    x %= d;

    if (k % 2) {
        cout << abs(x - d) << endl;
    } else {
        cout << x << endl;
    }
}

Measured memory_bytes: 3664
#include<iostream>
#include <cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
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;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
  int N, K; cin >> N >> K; ll a[N]; ll sum = 0;
  rep(i, N){
    cin >> a[i]; sum+=a[i];
  }
  sort(a, a+N, greater<ll>()); int H = min(K, N);
  rep(i, H){
    sum-=a[i];
  }
  cout << sum << endl;
}

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

typedef long long ll;

string s;
ll seq[555555];

int main(void)
{
	ll n, i;

	cin >> s;

	n = s.size();

	ll cnt = 1, idx = 0;
    for (i = 1; i < n; i++)
		if (s[i] == s[i - 1])
			++cnt;
		else
		{
            seq[++idx] = cnt;
            cnt = 1;
		}
	seq[++idx] = cnt;

    ll ans = 0, st, ed;

    for (i = 1; i <= idx; i++)
		ans += seq[i] * (seq[i] - 1) / 2;

	if (s[0] == '<' && s[n - 1] == '>')
		st = 1, ed = idx - 1;
	else if (s[0] == '<')
		st = 1, ed = idx + 1;
	else if (s[n - 1] == '>')
        st = 0, ed = idx - 1;
	else
		st = 0, ed = idx;

    for (i = st; i <= ed; i += 2)
		ans += max(seq[i], seq[i + 1]);

	cout << ans;
	return 0;
}

Measured memory_bytes: 2820
import java.util.*;
 
public class Main {
	public static void main (String[] args) {
      Scanner sc = new Scanner(System.in);
      
      int t = sc.nextInt();
      int k = sc.nextInt();
      
      int[] arr = new int[t];
      for(int i =0;i<t;i++){
        arr[i]= sc.nextInt();
      }
      int[] dp = new int[t];
      dp[0] = 0;

      for(int i =1;i<t;i++){
        int min = Integer.MAX_VALUE;
        for(int j =1;j<=k;j++){
          if(i-j>=0){
            min = Math.min(dp[i-j]+ Math.abs(arr[i]-arr[i-j]),min);
          }
        }
        dp[i]= min;
      }
      System.out.println(dp[dp.length-1]);
    }
  
}


Measured memory_bytes: 59764
import java.util.*;
public class Main{
	public static void main(String args[]){
    
    	Scanner sc=new Scanner(System.in);
      	int n=sc.nextInt();
      	int W=sc.nextInt();
      	long w[]=new long[n+1];
      	long v[]=new long[n+1];
      	int sum_values = 0;
      	for(int i=1;i<=n;i++){
        	w[i]=sc.nextInt();
          	v[i]=sc.nextInt();
          	sum_values+= v[i];
        }
      	long dp[][]=new long[n+1][sum_values+1];
      	
      	for(int i=0;i<=n;i++)
      	for(int j=0;j<=sum_values;j++)
      	dp[i][j]=Integer.MAX_VALUE;
      	//Logic
        long answer=0;
      for(int i=1;i<=n;i++){
      	for(int j=0;j<=sum_values;j++){
        
        	if(i == 0 || j == 0){
            	dp[i][j] = 0;
              	continue;
            }
            if(i == 1){
                if(j == v[i]){
                    dp[i][j]=w[i];
                }
            }
        	
          	if(v[i]>j){
            	dp[i][j]=Math.min(dp[i][j],dp[i-1][j]);
              	continue;
            }
          
          	dp[i][j] = Math.min(Math.min(dp[i][j],dp[i-1][j]),w[i]+dp[i-1][(int)(j-v[i])]);
        
        }
      }
      for(int i=sum_values;i>=0;i--){
          if(dp[n][i] <= W)
          {
              answer = i;
              break;
          }
      }
      System.out.println(answer);  
    
    
    }
}

Measured memory_bytes: 142024
#include<cstdio>
#include<vector>
#include<set>
#include<algorithm>
#include<iostream>
using namespace std;
int preorder[100005], visited[100005]; int  lowest[100005], p[100005];
int n, e; int t = 1;
vector<int> G[100005];
void dfs(int current, int pre)
{
  visited[current] = 1; preorder[current] = lowest[current] = t++;
  for(int i=0; i<G[current].size(); i++)
  {
    int next = G[current][i];
    if(visited[next] == 0)
    {
      p[next] = current; //visited[next] = 1;
      dfs(next, current);
      lowest[current] = min(lowest[current], lowest[next]);
    }
    else if(next != pre) // undirected graph.
    {
      lowest[current] = min(preorder[next], lowest[current]);
    }
  }
}

void solve()
{
  for(int i=0; i<n; i++)  visited[i] = 0;
  dfs(0, -1);
  set<int> S; int cnt = 0;
  for(int i=1; i<n; i++)
  {
    int parent = p[i];
    if(parent == 0) cnt++;
    else if(preorder[parent] <= lowest[i]) S.insert(parent); // set is perfect, the node will be in order and will not repeat.
  }
  if(cnt >= 2)  S.insert(0);
  for(set<int>::iterator it = S.begin(); it!=S.end(); it++)
  {
    printf("%d\n", *it);
  }
}
int main()
{
  scanf("%d%d", &n, &e);
  int s, t;
  for(int i=0; i<e; i++)
  {
    scanf("%d%d", &s, &t);
    G[s].push_back(t);
    G[t].push_back(s);
  }
  solve();

  return 0;
}



Measured memory_bytes: 7112
#include <stdio.h>

int main(void)
{
	char n,i,a[102],b[102],c[102];
	int ans=0;
	scanf("%hhd",&n);
	scanf("%s",a);
	scanf("%s",b);
	scanf("%s",c);

	for (i=0;i<n;i++) {
		if (a[i]!=b[i]) {
			ans++;
			if (a[i]!=c[i] && b[i]!=c[i]) ans++;
		} else if (a[i]!=c[i]) ans++;
	}

	printf("%d\n",ans);
	return 0;
}


Measured memory_bytes: 256
w, h, n = gets.split.map(&:to_i)
line_x, line_y = [*1..w], [*1..h]
n.times do
  x, y, a = gets.split.map(&:to_i)
  case
    when a == 1 then line_x.delete_if { |l| l <= x }
    when a == 2 then line_x.delete_if { |l| l > x }
    when a == 3 then line_y.delete_if { |l| l <= y }
    when a == 4 then line_y.delete_if { |l| l > y }
  end
end
puts line_x.size * line_y.size

Measured memory_bytes: 1788
#include <iostream>
#include <cstdio>
#include <cmath>
#include <climits>
using namespace std;

int main(){
  int m,f,r;
  while(1){
    cin>>m>>f>>r;
    if(m==-1&&f==-1&&r==-1) break;
    if((m==-1||f==-1)|| m+f<30){
      cout<<'F'<<endl;
    }else if(m+f<50){
      if(r>=50){cout<<'C'<<endl;}
      else{cout<<'D'<<endl;}
    }else if(m+f<65){
      cout<<'C'<<endl;
    }else if(m+f<80){
      cout<<'B'<<endl;
    }else{
      cout<<'A'<<endl;
    }
  }

}

Measured memory_bytes: 3040
#include <bits/stdc++.h>
using namespace std;
#define SORT(a) sort((a).begin(), (a).end())
#define RSORT(a) reverse((a).begin(), (a).end())
#define rep(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
const long long INF = 1LL << 60;

struct UnionFind {
  vector<int> par;  // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

  UnionFind(int N) : par(N) {  //最初は全てが根であるとして初期化
    for (int i = 0; i < N; i++) par[i] = i;
  }

  int root(int x) {  // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
    if (par[x] == x) return x;
    return par[x] = root(par[x]);
  }

  void unite(int x, int y) {  // xとyの木を併合
    int rx = root(x);         // xの根をrx
    int ry = root(y);         // yの根をry
    if (rx == ry) return;  // xとyの根が同じ(=同じ木にある)時はそのまま
    par[rx] =
        ry;  // xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
  }

  bool same(int x, int y) {  // 2つのデータx, yが属する木が同じならtrueを返す
    int rx = root(x);
    int ry = root(y);
    return rx == ry;
  }
};

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m);
  vector<int> b(m);
  rep(i, m) {
    cin >> a[i] >> b[i];
    a[i]--;
    b[i]--;
  }
  int count = 0;

  rep(i, m) {
    UnionFind tree(n);
    rep(j, m) {
      if (i == j) continue;
      tree.unite(a[j], b[j]);
    }
    if (!tree.same(a[i], b[i])) count++;
  }
  cout << count << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rng(i, a, b) for (int i = int(a); i < int(b); i++)
#define rep(i, b) rng(i, 0, b)
#define gnr(i, a, b) for (int i = int(b) - 1; i >= int(a); i--)
#define per(i, b) gnr(i, 0, b)
#define bg begin()
#define ed end()
#define all(x) x.bg, x.ed
#define vi vector<int>
template <class t, class u>
bool chmax(t &a, u b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class t, class u>
bool chmin(t &a, u b)
{
    if (b < a)
    {
        a = b;
        return true;
    }
    return false;
}

ll mod = 1000000007;

signed main()
{
    string s, t;
    cin >> s >> t;
    set<string> ss;
    rep(i, s.size() - t.size() + 1)
    {
        bool f = true;
        rep(j, t.size())
        {
            if (s[i + j] != t[j] && s[i + j] != '?')
            {
                f = false;
                break;
            }
        }
        if (f)
        {
            string str = s;
            rep(j, s.size())
            {
                if (s[j] == '?')
                {
                    str[j] = 'a';
                }
            }
            rep(j, t.size())
            {
                str[i + j] = t[j];
            }
            ss.insert(str);
        }
    }
    if (ss.size() == 0)
    {
        cout << "UNRESTORABLE" << endl;
    }
    else
    {
        cout << *(ss.begin()) << endl;
    }
}

Measured memory_bytes: 3644
#include <iostream>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <string>
#include <set>
#include <iomanip>
#define REP(i,n) for (int i=0;i<(n);++i)
using namespace std;

int main(){
    int a, b;
    cin >> a >> b;

    if(b < a) cout << a - 1 << endl;
    else cout << a << endl;

    return 0;
}


Measured memory_bytes: 256
//#include <bits/stdc++.h>
#include <iostream>
#include <complex>
#include <sstream>
#include <string>
#include <algorithm>
#include <deque>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <vector>
#include <set>
#include <limits>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <climits>
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, j, k) for(int i = (int)(j); i < (int)(k); ++i)
#define ROF(i, j, k) for(int i = (int)(j); i >= (int)(k); --i)
#define FORLL(i, n, m) for(long long i = n; i < (long long)(m); i++)
#define SORT(v, n) sort(v, v+n)
#define REVERSE(v) reverse((v).begin(), (v).end())

using namespace std;
using ll = long long;
const ll MOD=1000000007LL;
typedef pair<int, int> P;

ll ADD(ll x, ll y) { return (x+y) % MOD; }
ll SUB(ll x, ll y) { return (x-y+MOD) % MOD; }
ll MUL(ll x, ll y) { return x*y % MOD; }
ll POW(ll x, ll e) { ll v=1; for(; e; x=MUL(x,x), e>>=1) if (e&1) v = MUL(v,x); return v; }
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/ return MUL(x, POW(y, MOD-2)); }

int v[100100];
int cnt_even[100100], cnt_odd[100100];

const int INF=1e9;
vector<int> even, odd;

int
main(void){  
  int n;
  cin >> n;
  REP(i, n) {
    cin >> v[i];
    if(i%2) cnt_odd[v[i]]++;
    else cnt_even[v[i]]++;
  }

  vector<P> e, o;
  REP(i, 100100){
    e.push_back(make_pair(cnt_even[i], i));
    o.push_back(make_pair(cnt_odd[i], i));    
  }

  sort(e.begin(), e.end(), greater<P>());
  sort(o.begin(), o.end(), greater<P>());

  if(e[0].second != o[0].second) cout << n-e[0].first-o[0].first << endl;
  else cout << min(n-e[0].first-o[1].first, n-e[1].first-o[0].first) << endl;
  
  return 0;
}


Measured memory_bytes: 3312
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <bitset>
#include <list>
#include <sstream>
#include <set>
#include <functional>
 
using namespace std;
 
#define INT_MAX 1 << 30
#define MAX 100
 
typedef long long ll;
 
int n;
char a[10],b[10];
int ans;
 
 
int change(char c)
{
    int anss;
    if(c == 'm')anss = 1000;
    else if(c == 'c')anss = 100;
    else if(c == 'x')anss = 10;
    else if(c == 'i')anss = 1;
    return anss;
}
 
int c_d(char d[100])
{
    int summ = 0;
    for (int i = 0; i < strlen(d); i += 1)
    {
        if ((d[i] >= 50) && (d[i] <= 57))
        {
            int j = i+1;
            int a = int(d[i]-48);   //cout << a << endl;
            int b = change(d[j]);   //cout << b << endl;
            summ += a*b;            //cout << summ << endl;
            i++;
        }
        else
        {
            summ += change(d[i]);
        }
    }
    return summ;
}
 
 
int main()
{
    scanf("%d",&n);
    while (n--)
    {
        scanf("%s",a);
        scanf("%s",b);
        ans = c_d(a)+c_d(b);            //将a,b转换为十进制数
                                        //以下为将十进制数转换输出
        int j = 0,a1,k = 0;       
        char d[100] = {0};
        a1 = ans;
        while (a1)                      //求整数的位数
        {
            a1 /= 10;
            j++;
        }
        int t1,t2,t,j1=j;
        for (int l = 0; l < j; l += 1)  //求整数中每一位并进行转换
        {
            
            t1 = ans%int(pow(10.0,j1));
            t2 = pow(10.0,j1-1);
            t = t1/t2;              
            if(t == 1)
            {
                if(j1 == 4)      d[k] = 'm';
                else if(j1 == 3) d[k] = 'c';
                else if(j1 == 2) d[k] = 'x';
                else if(j1 == 1) d[k] = 'i';
            }
            else if(t!=0)               //为０时相应mcxi字符不显示
            {
                int k1 = k+1;
                if(j1 == 4)     {d[k] = (t+48);d[k1] = 'm';}
                else if(j1 == 3){d[k] = (t+48);d[k1] = 'c';}
                else if(j1 == 2){d[k] = (t+48);d[k1] = 'x';}
                else if(j1 == 1){d[k] = (t+48);d[k1] = 'i';}
                k++;
            }
            if(t!=0)k++;                //为０时不移位，否则逢d[k]遇０停止输出
            j1--;
        }
        cout << d << endl;          
    }
    return 0;
}


Measured memory_bytes: 3188
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int A = sc.nextInt();
        int B = sc.nextInt();
        int k0,k1,k2,k3,k4,k5,ksum,sum = 0;

        for (int i = 0; i <= N; i++) {
            k5 = (i / 100000) % 10;
            k4 = (i / 10000) % 10;
            k3 = (i / 1000) % 10;
            k2 = (i / 100) % 10;
            k1 = (i / 10) % 10;
            k0 = i % 10;
            ksum = k0 + k1 + k2 + k3 + k4 + k5;
            if (A <= ksum && ksum <= B) {
                sum += i;
            }
        }
        System.out.println(sum);
    }
}


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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rep2(i, l, r) for (int i = (l); i < (r); i++)
#define rep2r(i, l, r) for (int i = (r) - 1; i >= (l); i--)
#define range(a) a.begin(), a.end()

using namespace std;
using ll = long long;

void chmin(ll &x, ll y) {
  x = min(x, y);
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  int N, M; ll L; cin >> N >> M >> L;
  constexpr ll INF = 1e18;
  vector<vector<ll>> dist(N, vector<ll>(N, INF));
  rep(i, N) dist[i][i] = 0;
  rep(i, M) {
    int a, b; ll c; cin >> a >> b >> c; a--; b--;
    chmin(dist[a][b], c);
    chmin(dist[b][a], c);
  }
  rep(k, N) rep(i, N) rep(j, N) chmin(dist[i][j], dist[i][k] + dist[k][j]);
  vector<vector<ll>> ans(N, vector<ll>(N, INF));
  rep(i, N) ans[i][i] = 0;
  rep(i, N) rep(j, N) if (dist[i][j] <= L) ans[i][j] = 1;
  rep(k, N) rep(i, N) rep(j, N) chmin(ans[i][j], ans[i][k] + ans[k][j]);
  int Q; cin >> Q;
  while (Q--) {
    int s, t; cin >> s >> t; s--; t--;
    if (ans[s][t] == INF) {
      cout << -1 << '\n';
    } else {
      cout << ans[s][t] - 1 << '\n';
    }
  }
}


Measured memory_bytes: 2048
/** Bismillahir Rahmanir Rahim **/
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define endl '\n'
#define fi first
#define se second
#define Fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define debug(x) cerr << #x << " "<< x << endl

int main()
{
	int a, b;
	cin >> a >> b;
	int sum = 0;
	vector<int> v(a);
	for (int i = 0; i < a; i++) {
		cin >> v[i];
		sum += v[i];
	}
	int cnt = 0;
	for (int i = 0; i < a; i++) {
		if (ceil(sum / (4 * b * 1.0)) <= v[i]) cnt++;
	}
	if (cnt >= b) cout << "Yes\n";
	else cout << "No\n";

	 return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {

    public static int solve(int i,int dp[],ArrayList<ArrayList<Integer>> arr){
        if(dp[i] != -1) return dp[i];

        int max = 0;
        int leaf = 1;

        for(Integer j : arr.get(i)){
            leaf = 0;
            max = Math.max(max,solve(j,dp,arr));
        }

        return dp[i] = (leaf == 1 ? 0 : 1 + max);
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        int n,e;
        n = sc.nextInt();
        e = sc.nextInt();

       int dp[] = new int[n+1];
        ArrayList<ArrayList<Integer>> arr = new ArrayList<ArrayList<Integer>>();
        for(int i=0;i<=n;i++) arr.add(new ArrayList<Integer>());

        for(int i=0;i<e;i++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            arr.get(a).add(b);
        }


        for(int i=0;i<=n;i++) dp[i] = -1;
        int ans = 0;
        for(int i=1;i<=n;i++){
            ans = Math.max(ans,solve(i,dp,arr));
        }
        System.out.print(ans);

    }
}

Measured memory_bytes: 106716
#include<bits/stdc++.h>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
//g++ -std=c++11 

using namespace std;

long long N, M;
long long A[100005];

long long gcd(long long a, long long b){
  if (a%b==0){
    return(b);
  }
  else{
    return(gcd(b,a%b));
  }
}

long long lcm(long long m, long long n ){
	if ( ( 0 == m ) || ( 0 == n ) )
		return 0;
	return ((m / gcd(m, n)) * n); 
}

int main(){
	cin >> N >> M;
	rep(i, N) cin >> A[i];

	long long twocount = -1;

	for(long long i = 0; i < N; i++) {
		long long two = 0;
		long long num = A[i]/2;
		while(num%2 == 0) {
			two++;
			num/=2;
		}

		if(twocount != -1 && twocount != two) {
			cout << 0 << endl;
			return 0;
		}
		twocount = two;
	}

	long long L = 1;
	for(long long i = 0; i < N; i++) {
		L = lcm(L, A[i]/2);
	if(L > M) {
		cout << 0 << endl;
		return 0;
	}
	}

	if(L > M) {
		cout << 0 << endl;
		return 0;
	}

	long long num = (M - L)/(2*L) + 1;
	cout << num << endl;



}


Measured memory_bytes: 4396
#include<iostream>
#include<algorithm>
#include<cmath>
using lint=int64_t;
using namespace std;

int main()
{
	int M,K;

	cin >> M >> K;

	if(M==1)
	{
		if(K==0)
			cout << "0 0 1 1" << endl;
		else
			cout << -1 << endl;
		return 0;
	}

	if(K>=pow(2,M))
	{
		cout << -1 << endl;
		return 0;
	}

	for(int i=0;i<pow(2,M);i++)
	{
		if(i==K)continue;
		cout << i << " ";
	}
	cout << K << " ";
	for(int i=pow(2,M)-1;i>=0;i--)
	{
		if(i==K)continue;
		cout << i << " ";
	}
	cout << K << endl;
	return 0;
}



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

typedef long long ll;
#define REP(i,n) for(int i=0,_n=(int)(n);i<_n;++i)
#define ALL(v) (v).begin(),(v).end()
#define CLR(t,v) memset(t,(v),sizeof(t))
template<class T1,class T2>ostream& operator<<(ostream& os,const pair<T1,T2>&a){return os<<"("<<a.first<<","<<a.second<< ")";}
template<class T>void pv(T a,T b){for(T i=a;i!=b;++i)cout<<(*i)<<" ";cout<<endl;}
template<class T>void chmin(T&a,const T&b){if(a>b)a=b;}
template<class T>void chmax(T&a,const T&b){if(a<b)a=b;}


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

const int MAX_N = 212345;
const ll MOD = 998244353;
const ll INF = 1LL << 61;

struct PointUpdateRangeMax {
  using Val = ll;
  int n;
  vector<Val> seg;
  void clear(int N) {
    for (n = 1; n < N; n <<= 1);
    seg.assign(n * 2, -INF); // 初期値
  }
  // [l, r)
  Val getMax(int l, int r) {
    if (r - l <= 0) return -INF;
    return getMax(0, n, 1, l, r);
  }
  Val getMax(int nl, int nr, int k, int l, int r) {
    if (r <= nl || nr <= l) return -INF;
    if (l <= nl && nr <= r) return seg[k];
    Val x = getMax(nl, (nl+nr)/2, 2*k  , l, r);
    Val y = getMax((nl+nr)/2, nr, 2*k+1, l, r);
    return max(x, y);
  }
  void update(int pos, ll val) {
    int k = n + pos;
    seg[k] = val;
    for (;;) {
      k /= 2;
      if (k <= 0) break;
      seg[k] = max(seg[2*k], seg[2*k+1]);
    }
  }
};

int main2() {
  int N = nextInt();
  vector< pair<ll, ll> > p(N);
  REP(i, N) {
    ll x = nextInt();
    ll d = nextInt();
    p[i] = {x, d};
  }
  sort(ALL(p));

  PointUpdateRangeMax seg;
  seg.clear(N+5);
  for (int i = N-1; i >= 0; i--) {
    seg.update(i, i);
    pair<ll, ll> t = {p[i].first + p[i].second, -1};
    int right = distance(p.begin(), lower_bound(ALL(p), t));
    ll val = seg.getMax(i, right);
    seg.update(i, val);
  }

  vector<ll> reach(N);
  REP(i, N) reach[i] = seg.getMax(i, i+1);
  // pv(ALL(reach));
  vector<ll> dp(N+1);
  dp[N] = 1;
  for (int i = N-1; i >= 0; i--) {
    dp[i] = 0;
    dp[i] += dp[i+1];
    dp[i] += dp[ reach[i]+1 ];
    dp[i] %= MOD;
  }
  // pv(ALL(dp));

  ll ans = dp[0];
  cout << ans << endl;
  return 0;
}

int main() {

#ifdef LOCAL
  for (;!cin.eof();cin>>ws)
#endif
    main2();
  return 0;
}

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

using namespace std;
using ll = long long;

int main() {
    ll n = 0;
    cin >> n;
    ll ac = 0;
    ll wa = 0;
    ll tle = 0;
    ll re = 0;
    for(ll i = 0;i < n;i++){
        string tmp = "";
        cin >> tmp;
        if(tmp == "AC"){
            ac++;
        }else if(tmp == "WA"){
            wa++;
        }else if(tmp == "TLE"){
            tle++;
        }else if(tmp == "RE"){
            re++;
        }
    }

    cout << "AC x " << ac << endl;
    cout << "WA x " << wa << endl;
    cout << "TLE x " << tle << endl;
    cout << "RE x " << re << endl;
    return 0;
}

Measured memory_bytes: 3628
import java.io.*;
import java.util.*;

public class Main {
  private static void execute(ContestReader reader, PrintWriter out) {
    int r = reader.nextInt();
    int g = reader.nextInt();
    out.println(2 * g - r);
  }
  
  public static void main(String[] args) {
    ContestReader reader = new ContestReader(System.in);
    PrintWriter out = new PrintWriter(System.out);
    execute(reader, out);
    out.flush();
  }
}

class ContestReader {
  private BufferedReader reader;
  private StringTokenizer tokenizer;
  
  ContestReader(InputStream in) {
    reader = new BufferedReader(new InputStreamReader(in));
  }
  
  public String next() {
    while (tokenizer == null || !tokenizer.hasMoreTokens()) {
      try {
        tokenizer = new java.util.StringTokenizer(reader.readLine());
      } catch (Exception e) {
        throw new RuntimeException(e);
      }
    }
    return tokenizer.nextToken();
  }
  
  public int nextInt() {
    return Integer.parseInt(next());
  }
  
  public long nextLong() {
    return Long.parseLong(next());
  }
  
  public double nextDouble() {
    return Double.parseDouble(next());
  }
  
  public String[] next(int n) {
    String[] array = new String[n];
    for (int i = 0; i < n; i++) {
      array[i] = next();
    }
    return array;
  }
  
  public int[] nextInt(int n) {
    int[] array = new int[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextInt();
    }
    return array;
  }
  
  public long[] nextLong(int n) {
    long[] array = new long[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextLong();
    }
    return array;
  }
  
  public double[] nextDouble(int n) {
    double[] array = new double[n];
    for (int i = 0; i < n; i++) {
      array[i] = nextDouble();
    }
    return array;
  }
  
  public char[] nextCharArray() {
    return next().toCharArray();
  }
  
  public int[][] nextInt(int n, int m) {
    int[][] matrix = new int[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextInt();
      }
    }
    return matrix;
  }
  
  public long[][] nextLong(int n, int m) {
    long[][] matrix = new long[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextLong();
      }
    }
    return matrix;
  }
  
  public double[][] nextDouble(int n, int m) {
    double[][] matrix = new double[n][m];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        matrix[i][j] = nextDouble();
      }
    }
    return matrix;
  }
  
  public char[][] nextCharArray(int n) {
    char[][] matrix = new char[n][];
    for (int i = 0; i < n; i++) {
      matrix[i] = next().toCharArray();
    }
    return matrix;
  }
}


Measured memory_bytes: 21204
#include <bits/stdc++.h>
using namespace std;
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define REP(i,n) for(ll i=0;i<n;i++)
#define FOR(i,n1,n2) for(ll i=n1;i<n2;i++)
#define bFOR(i,n1,n2) for(ll i=n1;i>=n2;i--)
#define speed_up    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

typedef long long int ll;
typedef pair<ll,ll> Pi;
typedef tuple<ll,ll,ll> Tu;
const int INF=(ll)(1LL<<30)-1;
const double INFd=100000000000.0;
const double PI=3.14151926535;
const ll INFl=(ll)9223372036854775807/2;
const int MAX=10000;
const ll MOD=(ll)1e9+7;
const ll tMOD=(ll)998244353;
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;}
//int dx[4]={0,-1,0,1},dy[4]={-1,0,1,0};
int mdx[8]={0,1,0,-1,1,1,-1,-1},mdy[8]={-1,0,1,0,1,-1,1,-1};


template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

int n;
string ss[100010];
int main(){
	cin>>n;
	cout<<0<<endl;
	string s,t;
	cin>>s;
	if(s=="Vacant"){
		return 0;
	}
	cout<<n/2<<endl;
	cin>>t;
	if(t=="Vacant"){
		return 0;
	}
	ss[0]=s;
	ss[n]=s;
	ss[n/2]=t;
	int l,r;
	if(((s==t)^((n/2)%2==0))){
		l=0;
		r=n/2;
	}else{
		l=n/2;
		r=n;
	}
	for(int zz=0;zz<18;zz++){
		int lr=(l+r)/2;
		cout<<lr<<endl;
		string newt;
		cin>>newt;
		ss[lr]=newt;
		if(newt=="Vacant")return 0;
		if(!((lr-l)%2==0^(newt==ss[l]))){
			l=lr;
		}else{
			r=lr;
		}
	}
	return 0;
}

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

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int n, m;
    cin >> n >> m;
    bool solved[n + 1];
    memset(solved, false, sizeof(solved));
    int penalties[n + 1];
    memset(penalties, 0, sizeof(penalties));
    int correct = 0;
    for (int i = 0; i < m; ++i) {
        int p;
        string verdict;
        cin >> p >> verdict;
        if (verdict == "WA" && solved[p] == false) {
            penalties[p]++;
        } else if (verdict == "AC" && solved[p] == false) {
            correct++;
            solved[p] = true;
        }
    }
    int penal = 0;
    for (int i = 1; i <= n; ++i) {
        if (solved[i]) {
            penal += penalties[i];
        }
    }
    cout << correct << " " << penal << '\n';
    return 0;
}

Measured memory_bytes: 768
#include<iostream>
using namespace std;
int main(){
  int N;
  cin>>N;
  for(int i=0;i<20;i++){
    for(int j=0;j<=20;j++){
      if(7*i+4*j==N){
        cout<<"Yes"<<endl;
        return 0;
      }
    }
  }
  cout<<"No"<<endl;
  return 0;
}


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

int main(){
int arr[4][4]={0};
for(int i=0;i<3;i++){
        int a,b;
    cin>>a;
    cin>>b;
arr[a-1][b-1]=1;
arr[b-1][a-1]=1;
}
int i=0;
for(i=0;i<4;i++){
    int count1=0;
if(i==0||i==3){
    for(int j=0;j<4;j++){
        if(arr[i][j]==1){
            count1++;
        }
    }
    if(count1!=1){
        break;
    }
}
else{
    for(int j=0;j<4;j++){
        if(arr[i][j]==1){
            count1++;
        }
    }
    if(count1!=2){
        break;
    }
}
}
if(i==4){
    cout<<"YES";
}
else{
    cout<<"NO";
}
return 0;}


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

using namespace std;

const int mx = 1e2 + 9;

int h, w;
char g[mx][mx];
int dp[mx][mx][mx];

int solve(int i, int j, int k) {
    int& ans = dp[i][j][k];
    if (ans != -1) return ans;

    ans = 0;
    k += (g[i][j] == '#');

    if (i == h - 1 and j == w - 1) {
        if (k > 0) return ans = 1;
        return ans = 0;
    }

    if (g[i][j] == '.' and k > 0) {
        ans++;
        k = 0;
    }

    int t1 = 2e9, t2 = 2e9;
    if (i != h - 1) t1 = solve(i + 1, j, k);
    if (j != w - 1) t2 = solve(i, j + 1, k);

    ans += min(t1, t2);

    return ans;
}

int main() {
    scanf("%d %d", &h, &w);
    for (int i = 0; i < h; i++) {
        scanf("%s", g[i]);
    }

    memset(dp, -1, sizeof dp);
    int ans = solve(0, 0, 0);

    printf("%d\n", ans);
}

Measured memory_bytes: 8900
#include <stdio.h>
int main(void){
    int cards[128][14]={{0}};
    int i,N;
    
    scanf("%d",&N);
    
    for(i=0;i<N;i++){
        char c;
        int cn;
        scanf("%c",&c);
        scanf("%c %d",&c,&cn);
        cards[c][cn]=1;
    }
    for(i=1;i<=13;i++){
        if(cards['S'][i]==0){
            printf("S %d\n",i);
        }
    }
    for(i=1;i<=13;i++){
        if(cards['H'][i]==0){
            printf("H %d\n",i);
        }
    }
    for(i=1;i<=13;i++){
        if(cards['C'][i]==0){
            printf("C %d\n",i);
        }
    }
    for(i=1;i<=13;i++){
        if(cards['D'][i]==0){
            printf("D %d\n",i);
        }
    }
    return(0);
}





Measured memory_bytes: 2048
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_CHAR_MAX		5005									// 最大文字数

// 内部変数
static FILE *szpFpI;											// 入力
static char sc1Str[D_CHAR_MAX];									// 文字列
static int siSLen;												// 文字列長
static int si2MCnt[D_CHAR_MAX][D_CHAR_MAX];						// 一致数[文字位置１][文字位置２]

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpA;
	static int siTNo;
#endif

// １行出力
int
fOutLine(
	char *pcpLine				// <I> １行
)
{
	char lc1Buf[1024];

#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, pcpLine)) {
		siRes = -1;
	}
#else
	printf("%s", pcpLine);
#endif

	return 0;
}

// 最小値 - 取得
int
fGetMinI(
	int piVal1					// <I> 値１
	, int piVal2				// <I> 値２
)
{
	if (piVal1 < piVal2) {
		return piVal1;
	}
	else {
		return piVal2;
	}
}

// 最大値 - 取得
int
fGetMaxI(
	int piVal1					// <I> 値１
	, int piVal2				// <I> 値２
)
{
	if (piVal1 > piVal2) {
		return piVal1;
	}
	else {
		return piVal2;
	}
}

// 実行メイン
int
fMain(
)
{
	int i, j;
	char lc1Buf[1024];

	// データ - 初期化
	memset(si2MCnt, 0, sizeof(si2MCnt));
	int liMax = 0;

	// 文字列長 - 取得
	fgets(lc1Buf, sizeof(lc1Buf), szpFpI);
	sscanf(lc1Buf, "%d", &siSLen);

	// 文字列 - 取得
	fgets(sc1Str, sizeof(sc1Str), szpFpI);

	// １文字目
	for (i = 1; i < siSLen; i++) {
		if (sc1Str[0] == sc1Str[i]) {
			si2MCnt[0][i] = 1;
			liMax = 1;
		}
	}

	// ２文字目以降
	for (i = 1; i < siSLen; i++) {
		for (j = i + 1; j < siSLen; j++) {
			if (sc1Str[i] == sc1Str[j]) {
				si2MCnt[i][j] = fGetMinI(si2MCnt[i - 1][j - 1] + 1, j - i);
				liMax = fGetMaxI(liMax, si2MCnt[i][j]);
			}
		}
	}

	return liMax;
}

// １回実行
int
fOne(
)
{
	int liRet;
	char lc1Buf[1024];

	// 入力 - セット
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", siTNo);
	szpFpI = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", siTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#else
	szpFpI = stdin;
#endif

	// 実行メイン
	liRet = fMain();

	// 出力
	sprintf(lc1Buf, "%d\n", liRet);
	fOutLine(lc1Buf);

	// 残データ有無
#ifdef D_TEST
	lc1Buf[0] = '\0';
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, "")) {
		siRes = -1;
	}
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpI);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", siTNo);
	}
	else {
		printf("NG %d\n", siTNo);
	}
#endif

	return 0;
}

// プログラム開始
int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		siTNo = i;
		fOne();
	}
#else
	fOne();
#endif

	return 0;
}



Measured memory_bytes: 99600
#include <bits/stdc++.h>
 using namespace std;
 int main() {
int A;
int B;
int C;
int K;
cin >> A >> B >> C >> K ;
int P = K - A - B;
if(A >= K){
 cout << K << endl;
}
else if(A + B >= K){
cout << A << endl;
}
else{
cout << A - P << endl;
}
} 


Measured memory_bytes: 3624
#include<iostream>
#include <bits/stdc++.h>
#include<algorithm>
#include <cstring>
#define endl "\n"
#define ll long long
using namespace std;

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    if (n >= 400 && n <= 599)
    {
        cout << "8\n";
    }
    else if (n >= 600 && n <= 799)
    {
        cout << "7\n";
    }
    else if (n >= 800 && n <= 999)
    {
        cout << "6\n";
    }
    else if (n >= 1000 && n <= 1199)
    {
        cout << "5\n";
    }
    else if (n >= 1200 && n <= 1399)
    {
        cout << "4\n";
    }
    else if (n >= 1400 && n <= 1599)
    {
        cout << "3\n";
    }
    else if (n >= 1600 && n <= 1799)
    {
        cout << "2\n";
    }
    else if (n >= 1800 && n <= 1999)
    {
        cout << "1\n";
    }
}


Measured memory_bytes: 3636
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
ans = 0
c = Counter(a)  # aの要素の数と値の辞書を出してくれる
#print(c)
#print(c.keys())
#print(c.values())
for m in c.values():  # Counterで出した辞書の値（values)のリストの中身でfor
    ans += int((m*(m-1))/2)  # mC2をしている


for k in range(n):
    #print(c[a[k]])
    print(ans - (c[a[k]] - 1))
    """
    c[a[k]]-1というのはｋ番目のボールを隠す前と隠した後についての差。
    隠す前、aのリストにA_kがｍ個あったとする。隠した後、aのリストにA_kはｍ－１個
    なので、mC2 - (m-1)C2 = m-1 である。ここでｍとはc[a[k]]なので、上が成り立つ。
    """

Measured memory_bytes: 87756
// template version 1.6
// {{{ define basic macro
using namespace std;
#include <iostream>
#include <bits/stdc++.h>
// varibable settings
#define int long long
#define INF 1000000000000000000LL
// #define INF 2147483647
#define MOD 1000000007LL
// #define MOD 998244353LL
#define infile "../test/sample-1.in"

#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 rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)(b-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ten(n) ((int)1e##n)
template<class T, size_t N> size_t sza(const T (&array)[N]) { return N; }
template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }
template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }
template<class T=int> T in() {T x; cin>>x; return (x);}
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;

// dump macro
#ifdef PCM
  #include "dump.hpp"
#else
  #define dump(...) 42
  #define dump_1d(...) 42
  #define dump_2d(...) 42
#endif

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;
template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
#define fi first
#define se second

// }}}

int solve(){
  int n;cin>>n;
  string s;cin>>s;
  int Q; cin>>Q;

  int D[n+1]={};/*{{{*/
  int M[n+1]={};
  int C[n+1]={};
  int AC[n+1]={};

  rep(i, n){
    if (s[i]=='D')
      D[i+1] = D[i] + 1;
    else
      D[i+1] = D[i];
    if (s[i]=='M')
      M[i+1] = M[i] + 1;
    else
      M[i+1] = M[i];
    if (s[i]=='C'){
      C[i+1] = C[i] + 1;
      AC[i+1] = AC[i] + M[i];
    }
    else{
      C[i+1] = C[i];
      AC[i+1] = AC[i];
    }
  }
  dump_1d(D, n+1);
  dump_1d(M, n+1);
  dump_1d(C, n+1);
  dump_1d(AC, n+1);
  /*}}}*/

  s = "-" + s;
  dump(s);

  rep(q, Q){
    int k;cin>>k;
    int res = 0;

    // count in m
    rep(m, 1, n+1){
      if (s[m]=='M'){
        res += D[m-1]*(C[n]-C[m]);
      }
    }
    dump(q, res);

    // count d>=k && no m
    int dif = 0;
    rep(l, 1, n){
      if (s[l]=='D'){
        if (l+k-1<=n){
          dif += AC[n] - AC[l+k-1];
          dif -= M[l]* (C[n]-C[l+k-1]);
        }
      }
    }
    dump(q, dif);

    cout << res-dif << endl;
  }


  return 0;
}

signed main() { //{{{
#ifdef INPUT_FROM_FILE
  std::ifstream in(infile);
  std::cin.rdbuf(in.rdbuf());
#endif
  solve();
  return 0;
} //}}}


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

typedef pair<int,int>pint;

// Union Find
int par[100010];

void init(int n){
	for(int i=0;i<n;i++){
		par[i]=i;
	}
}

int find(int x){
	if(x==par[x])return x;
	return par[x]=find(par[x]);
}

void unite(int x, int y){
	int parx=find(x);
	int pary=find(y);
	if(parx==pary)return;
	par[parx]=pary;
}

bool same(int x, int y){
	int parx=find(x);
	int pary=find(y);
	return parx==pary;
}

// Edge
vector<pair<int,pint>>E;

int main(){
	int N;
	cin>>N;
	vector<pair<int,int>>X;
	vector<pair<int,int>>Y;
	for(int i=0;i<N;i++){
		int x,y;
		cin>>x>>y;
		X.push_back(make_pair(x,i));
		Y.push_back(make_pair(y,i));
	}
	sort(X.begin(),X.end());
	sort(Y.begin(),Y.end());
	for(int i=1;i<X.size();i++){
		int d=X[i].first-X[i-1].first;
		pint p(X[i].second,X[i-1].second);
		E.push_back(make_pair(d,p));
	}
	for(int i=1;i<Y.size();i++){
		int d=Y[i].first-Y[i-1].first;
		pint p(Y[i].second,Y[i-1].second);
		E.push_back(make_pair(d,p));
	}
	sort(E.begin(),E.end());
	int ans=0;
	init(N);
	for(int i=0;i<E.size();i++){
		if(!same(E[i].second.first,E[i].second.second)){
			unite(E[i].second.first,E[i].second.second);
			ans+=E[i].first;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 7144
fun main(args: Array<String>) {
    val matrix = QueensMatrix(8)
    repeat(readLine()?.toInt() ?: 0) {
        val input = readLine()?.split(" ".toRegex())?.map(String::toInt) ?: listOf()
        matrix.putQueen(input[0], input[1])
    }
    matrix.fillMatrix(0)
}

class QueensMatrix(private val size: Int) {
    private val matrix = Array(size) {
        BooleanArray(size).apply { fill(false) }
    }

    private val row = IntArray(size) {
        -1
    }

    private val col = IntArray(size) {
        -1
    }

    private val dpos = IntArray(size * 2 - 1) {
        -1
    }

    private val dneg = IntArray(size * 2 - 1) {
        -1
    }

    fun putQueen(row: Int, col: Int) {
        matrix[row][col] = true
    }

    fun fillMatrix(i: Int) {
        if (i == size) {
            printMatrix()
            return
        }

        for (j in matrix.indices) {
            if (col[j] == 1
                || dpos[i + j] == 1
                || dneg[i - j + size - 1] == 1) {
                continue
            }
            row[i] = j
            col[j] = 1
            dpos[i + j] = 1
            dneg[i - j + size - 1] = 1
            fillMatrix(i + 1)
            row[i] = -1
            col[j] = -1
            dpos[i + j] = -1
            dneg[i - j + size - 1] = -1
        }
    }

    private fun printMatrix() {
        for (i in matrix.indices) {
            for (j in matrix[i].indices) {
                if (matrix[i][j] && this.row[i] != j) {
                    return
                }
            }
        }
        println(matrix.indices.joinToString("\n") { i ->
            matrix[i].indices.joinToString("") { j ->
                when (row[i]) {
                    j -> "Q"
                    else -> "."
                }
            }
        })
    }
}


Measured memory_bytes: 33548
#include <stdio.h>
int main(){
  int l,a,b,c,d,p,i;
  scanf("%d\n%d\n%d\n%d\n%d",&l,&a,&b,&c,&d);
  for(i = 1 ; a > c || b > d ; i++){
    a = a-c;
    b = b-d;
  }
  p = l - i;
  printf("%d\n",p);
  return 0;
}

Measured memory_bytes: 596
#include <bits/stdc++.h>
using namespace std;
 
//DEFINE and TYPEDEF--------------------------------------------------------------
 
#define FORG(i,a,n) for(lld i=a;i<n;i++) //for - general
#define FORZ(i,n) for(lld i=0;i<n;i++) //for - zero
#define FORGI(i,a,n,inc) for(lld i=a;i<n;i+=inc) //for - general incremental
#define FORZI(i,n,inc) for(lld i=0;i<n;i+=inc) //for - zero incremental
#define DEBUG(x) cerr<<(#x)<<" is "<<(x)<<"\n" //to debug values of variables
#define ZINDA cerr<<"\nIdhar-ich hai apun!!\n"; //to debug if code reached that point
#define SPEEDUP ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
 
#define SORTall(m) sort(m.begin(),m.end());
 
#define st first
#define nd second
#define mkp make_pair
#define pb push_back
#define endl '\n'
#define mod 1000000007
 
typedef long long int lld;
typedef unsigned long long int ulld;
 
lld power(lld b, lld e)
{
    if(e==0)
        return 1;
    if(e%2==1)
        return b*power(b*b,(e-1)/2);
    else
        return power(b*b,e/2);
}
 
lld power(lld b, lld e, lld m)
{
	if(e==0)
		return 1;
	if(e%2==1)
		return b*power(((b%m)*(b%m))%m,(e-1)/2,m)%m;
	else
		return power(((b%m)*(b%m))%m,e/2,m)%m;
}
 
int nDig(lld n)
{
	if(n<0) 
		n*=-1;
	if(n==0)
		return 1;

    return floor(log10(n) + 1);
}

//endl : changed to '\n', to flush comment that definition
//--------------------------------------------------------------------------------

void swagWaalaFunction()
{
	lld n,ans=0;
	cin>>n;

	ans=(n/500)*1000;
	n%=500;
	ans+=(n/5)*5;

	cout<<ans;
	return;
}

int main()
{
    SPEEDUP;
 	lld t=1;
 	//cin>>t;

 	while(t--)
 		swagWaalaFunction();

 	return 0;
}

Measured memory_bytes: 256
import sys
input = sys.stdin.buffer.readline

#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode()

import bisect

def main():
	n,a,b=MI()

	if abs(b-a)%2==0:
		ans=abs(b-a)//2

	else:
		if b<a:
			a,b=b,a
		ans=min(a-1,n-b)+1+(b-a-1)//2

	print(ans)

















if __name__ == "__main__":
	main()


Measured memory_bytes: 9276
#include<bits/stdc++.h>
using namespace std;
#define fr first
#define sc second
typedef long long ll;
typedef double ld;
typedef pair<int,int> p2;
typedef pair<p2,int> p3;
typedef vector<ll> vi;
const ll Mod = 7 + 1e9;
const ll INF32 = 5 + 2e9;
const ll INF64 = 5 + 1e18;
const ll MAX = 5+ 1e3;

int H,W,dp[MAX][MAX];
bool G[MAX][MAX];
bool IsOk(int i,int j)
{
    return i>0 && j>0 && i<=H && j<=W && !G[i][j];
}
int main()
{
    cin >> H >> W;
    for(int i=1; i<=H; i++)
        for(int j = 1 ; j<=W; j++)
        {
            char c;
            cin >> c;
            G[i][j] = (c=='#');
        }
    dp[1][1] = 1;
    for(int i=1; i<=H; i++)
        for(int j=1; j<=W; j++)
            if( !G[i][j] )
            {
                if( IsOk(i-1,j) )
                    dp[i][j] += dp[i-1][j];
                if( IsOk(i,j-1) )
                    dp[i][j] += dp[i][j-1];
                dp[i][j]%=Mod;
            }
    cout << dp[H][W] << endl;
    return 0;
}


Measured memory_bytes: 5120