akhauriyash/Code-Regression huggingface dataset:
#include <cassert>
#include <algorithm>
#include <array>
#include <bitset>
#include <cctype>
#include <cstdint>
#include <cmath>
#include <complex>
#include <chrono>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
#include <random>
#include <memory>
#include <utility>
#include <limits>
 
#define rep(i, a, b) for (long long i = (a); (i) < (b); (i)++)
#define all(i) i.begin(), i.end()
#define debug(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

void debug_out(){std::cerr<<std::endl;}

template<typename Head,typename... Tail>
void debug_out(Head h,Tail... t){
  std::cerr<<" "<<h;
  debug_out(t...);
}
 
template <typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, std::pair<T1, T2> pa) {
  return os << pa.first << " " << pa.second;
}
 
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> vec) {
  for (std::size_t  i = 0; i < vec.size(); i++)os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  return os;
}
 
template<typename T1,typename T2>
inline bool chmax(T1& a,T2 b){return a<b && (a=b,true);}
 
template<typename T1,typename T2>
inline bool chmin(T1& a,T2 b){return a>b && (a=b,true);}
 
long long pow_mod(long long a, long long b, long long mod=-1) {
  if(b==0)return 1;
  if ((a == 0)||(mod!=-1&&(a+mod)%mod==0))return 0;
  long long x = 1;
  while (b > 0) {
    if (b & 1)x = (mod!=-1)?(x * a) % mod:x*a;
    a = (mod!=-1)?(a * a) % mod:a*a;
    b >>= 1;
  }
  return x;
}

// const long long MOD = 998244353;
const long long MOD = 1e9 + 7;

using ll = long long;
using P=std::pair<long long,long long>;

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

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

  ll h,w;
  std::cin>>h>>w;

  std::vector<std::string> map(h);
  rep(i,0,h)std::cin>>map[i];

  std::vector<std::vector<ll>> d(h,std::vector<ll>(w,MOD));

  for(ll i=h-1;i>=0;i--){
    for(ll j=w-1;j>=0;j--){
      if(i==h-1&&j==w-1){
        d[i][j]=(map[i][j]=='#');
        continue;
      }

      ll ret=MOD;
      if(i!=h-1){
        if(map[i][j]=='#'){
          if(map[i+1][j]=='#'){
            chmin(ret,d[i+1][j]);
          }else{
            chmin(ret,d[i+1][j]+1);
          }
        }else{
          chmin(ret,d[i+1][j]);
        }
      }
      if(j!=w-1){
        if(map[i][j]=='#'){
          if(map[i][j+1]=='#'){
            chmin(ret,d[i][j+1]);
          }else{
            chmin(ret,d[i][j+1]+1);
          }
        }else{
          chmin(ret,d[i][j+1]);
        }
      }
      d[i][j]=ret;
    }
  }

  std::cout<<d[0][0]<<"\n";

  return 0;
}



Measured memory_bytes: 384
#include<bits/stdc++.h>
  using namespace std;
  typedef long long ll;
   
  ll an[100010],am[100010];
  int main()
  {
      int a,b,c;
     cin>>a>>b>>c;
     if(b-a==c-b)
     {
         cout<<"YES"<<endl;
     }
     else
     {
         cout<<"NO"<<endl;
     }
     return 0;
 }

Measured memory_bytes: 256
import java.math.*

fun main(args: Array<String>) {
    val (a, b, x) = readInputLine().split(" ").map { it.toDouble() }
    
    if (x >= 0.5 * a * a * b) {
        // 0.5 * a^3 * tan(sheta) = a^2 * b - x
        // tan(sheta) = 2 * (a^2 * b - x) / (a^3)
        println(Math.atan(2 * (a * a * b - x) / (a * a * a)) / Math.PI * 180.0)
    } else {
        // ans = 90deg - theta
        // 0.5 * a * b^2 * tan(theta) = x
        // tan(theta) = 2 * x / a / b^2
        println(90.0 - Math.atan(2 * x / a / b / b) / Math.PI * 180.0)
    }
}

fun readInputLine(): String {
    return readLine()!!
}


Measured memory_bytes: 37788
#include <stdio.h>

typedef struct {
    char team[21];
    int w, l, d;
    int s;
} TEAM;

void bubble_sort(TEAM *p, int n)
{
    int i, j;
    TEAM t;
    
    for (i = 0; i < n - 1; i++){
        for (j = 0; j < n - i - 1; j++){
            if (p[j].s < p[j + 1].s){
                t = p[j];
                p[j] = p[j + 1];
                p[j + 1] = t;
            }
        }
    }
}

int main(void)
{
    int i;
    int n;
    int c;
    TEAM pck[10];
    
    c = 0;
    while (1){
        scanf("%d", &n);
        
        if (n == 0){
            break;
        }
        
        for (i = 0; i < n; i++){
            scanf("%s%d%d%d", pck[i].team, &pck[i].w, &pck[i].l, &pck[i].d);
            pck[i].s = pck[i].w * 3 + pck[i].d;
        }
        if (c != 0){
            printf("\n");
        }
        bubble_sort(pck, n);
        
        for (i = 0; i < n; i++){
            printf("%s,%d\n", pck[i].team, pck[i].s);
        }
        c = 1;
    }
    
    return (0);
}

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

dp1 = [[0 for i in range(10)] for _ in range(n+1)]
dp2 = [[0 for i in range(100)] for _ in range(n+1)]
dp3 = [[0 for i in range(1000)] for _ in range(n+1)]


for i in range(n):
    dp1[i+1][int(s[i])] = 1
    for j in range(10):
        if dp1[i][j]:
            dp2[i+1][j*10+int(s[i])] = 1
            dp1[i+1][j] = 1
    for j in range(100):
        if dp2[i][j]:
            dp3[i+1][j*10+int(s[i])] = 1
            dp2[i+1][j] = 1
    for j in range(1000):
        if dp3[i][j]:
            dp3[i+1][j] = 1



ans = 0
for i in range(1000):
    if dp3[n][i]:
        ans += 1

print(ans)

Measured memory_bytes: 333832
// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>	
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) rep(_,v.size())cout<<" "<<v[_];cout<<endl;
#define show2d(v) rep(_,v.size()){rep(__,v[_].size())cout<<" "<<v[_][__];cout<<endl;}cout<<endl;
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int> tp;
typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
//template<class C>constexpr int size(const C &c){return (int)c.size();}
//template<class T,size_t N> constexpr int size(const T (&xs)[N])noexcept{return (int)N;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 1e18+10 : 1e9+10;
int dx[]={0,1,0,-1,1,1,-1,-1};
int dy[]={1,0,-1,0,1,-1,1,-1};//RDLU
class LCA{
	public:
	int N,root,max_log;
	vvi G,parent;
	vi depth;
	LCA(int size){
		N=size;
		G=vvi(size);
		max_log=20;
		depth=vi(size);
		parent=vvi(max_log,vi(size));
	}
	void add_edge(int a,int b){
		G[a].pb(b);
		G[b].pb(a);
	}
	void dfs(int v,int p,int d){
		parent[0][v]=p;
		depth[v]=d;
		rep(i,G[v].size())if(G[v][i]!=p)dfs(G[v][i],v,d+1);
	}
	void init(){
		root=0;
		dfs(root,-1,0);
		for(int k=0;k+1<max_log;k++){//doubling
			for(int v=0;v<N;v++){
				if(parent[k][v]<0)parent[k+1][v]=-1;
				else parent[k+1][v]=parent[k][parent[k][v]];
			}
		}
	}
	int lca(int u,int v){
		if(depth[u]>depth[v])swap(u,v);
		rep(k,max_log)if((depth[v]-depth[u])>>k&1)v=parent[k][v];
		if(u==v)return u;
		for(int k=max_log-1;k>=0;k--){
			if(parent[k][u]!=parent[k][v]){
				u=parent[k][u];
				v=parent[k][v];
			}
		}
		return parent[0][u];
	}
};
int n;
vvi G;
vi dig;
void dfs(int a,int b){
	dig[b]=a;
	rep(i,G[b].size())dfs(a+1,G[b][i]);
}
signed main(){
	cin>>n;
	G=vvi(n);
	dig=vi(n);
	LCA lca(n);
	rep(i,n-1){
		int a;cin>>a;
		G[a-1].pb(i+1);
		lca.add_edge(a-1,i+1);
	}
	lca.init();
	dfs(0,0);
	rep(i,n)sort(all(G[i]));
	queue<int>que;
	que.push(0);
	vvi in;
	while(!que.empty()){
		int a=que.front();
		que.pop();
		in.pb({0,a});
		rep(i,G[a].size())que.push(G[a][i]);
	}
	
	int out=0;
	rep(i,n-1)out+=dig[in[i][1]]+dig[in[i+1][1]]-2*dig[lca.lca(in[i][1],in[i+1][1])];
	cout<<out<<endl;
}









Measured memory_bytes: 40316
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {


        // Scannerクラスのインスタンスの生成
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a = sc.nextInt();
        int b = sc.nextInt();

        int sum =  n * a;

        if (sum < b) {
        	System.out.println(sum);
        } else if (b < sum) {
        	System.out.println(b);

        } else if (sum == b) {
        	System.out.println(sum);
        }

	}



}

Measured memory_bytes: 21844
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, K = LI()
    cnt = 0
    if K == 0:
        print(N * N)
        return

    for b in range(K+1, N+1):
        shou = N // b
        amari = N % b
        cnt += (shou * (b - K))
        if amari >= K:
            cnt += (amari - K + 1)
    print(cnt)

main()



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

using namespace std;
using ll = long long;

void solve(){
	int n,m,acs=0,pen=0;
	cin >> n >> m;
	vector<pair<int,int>> hist(n+1,{0,0});
	for(int i=0;i<m;++i){
		int p;
		string v;
		cin >> p >> v;
		if(v=="AC"&&hist[p].second==0){
			acs++;
			pen+=hist[p].first;
			hist[p].second=1;
		}
		else{
			hist[p].first++;
		}
	}
	cout << acs << " " << pen << '\n';
}

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

Measured memory_bytes: 3920
x = input()
y = list(x)
z=0
a = y.count("K")
b = y.count("U")
c = y.count("P")
d = y.count("C")
while True:
    if a==0 or b==0 or c==0 or d==0:
        break
    
    else:
        z = z+1
        a = a-1
        b = b-1
        c = c-1
        d = d-1
print(z)    
    


Measured memory_bytes: 5556
#include<bits/stdc++.h>
using namespace std;
const int MAX=1e5+10,MOD=1e9+7;
int F[200],ans;
int f(int a,int b,int c)
{
    return a>min(b,c) && a<max(b,c);
}
int main ()
{
    int n;cin>>n;
    for(int i=1;i<=n;i++)cin>>F[i];
    for(int i=2;i<n;i++)ans+=f(F[i],F[i-1],F[i+1]);
    cout<<ans;
    return 0;
}
/*

*/

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

using namespace std;

#define YES cout << "YES" << endl;
#define NO  cout << "NO"  << endl;
#define Yes cout << "Yes" << endl;
#define No  cout << "No"  << endl;
 
#define INF INT_MAX
#define MOD 1000000007
#define PI  acos(-1)

using ll  = long long;
using ull = unsigned long long;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0; i < (n); i++)
#define rrep(i, n) for (ll i = (n)-1; i >= 0; i--)

int main(int argc, char *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll N;
    cin >> N;

    bool flag = false;
    rep (i, N) {
        ll a;
        cin >> a;
        if (a%2) {
            flag = true;
        }
    }

    if (flag) {
        cout << "first" << endl;
    } else {
        cout << "second" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <map>

#define rep(i,n) for(int i=0; i<(n); ++i)
#define rep2(i,a,b) for(int i=(a); i<(b); ++i)

int main() {
    int n; std::cin >> n;
    int L[100], tmp[3];
    int cnt=0;
    rep(i,n) std::cin >> L[i];

    rep2(i,0,n){
        rep2(j,i+1,n){
            rep2(k,j+1,n){
                if(L[i]==L[j] || L[j]==L[k] || L[i]==L[k])
                    continue;
                tmp[0]=L[i]; tmp[1]=L[j]; tmp[2]=L[k];
                std::sort(tmp, tmp+3);
                if(tmp[0]+tmp[1]>tmp[2]) ++cnt;
            }
        }
    }
    std::cout << cnt << std::endl;
    return 0;
}

Measured memory_bytes: 3628
#include <bits/stdc++.h>
using namespace std;
 
typedef long long ll;
#define mod 1000000007
#define ALL(a) a.begin(),a.end()

ll gcd(ll a, ll b) { return b ? gcd(b, a%b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }

int main(){
	ll n, x;
	cin >> n >> x;
	ll c[n];
	for(int i = 0; i < n; i++) cin >> c[i];

	ll dis[n];
	for (int i = 0; i < n; i++){
		dis[i] = abs(c[i] - x);
	}

	ll ans = dis[0];
	for (int i = 1; i < n; i++){
		ans = gcd(ans, dis[i]);
	}
	cout << ans << endl;
	return 0;
}

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

typedef long long ll;

#define CPP_STR(x) CPP_STR_I(x)
#define CPP_CAT(x,y) CPP_CAT_I(x,y)
#define CPP_STR_I(args...) #args
#define CPP_CAT_I(x,y) x ## y

#define FOR(i, start, end) for(ll i = (start), CPP_CAT(i,xxxx_end)=(end); i <= CPP_CAT(i,xxxx_end); ++i)
#define REP(i, n) FOR(i, 0, n)

int main(void) {
    int N;
    cin >> N;

    FOR(i, 1, 9) {
        FOR(j, 1, 9) {
            if (i * j == N) {
                cout << "Yes" << endl;
                return 0;
            }
        }
    }

    cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
#include <algorithm>
#include <array>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
using Line = array<char, 5>;
using Puzzle = vector<Line>;

void newline() {cout << endl;}
template <typename T>
void put(T input){cout << input;}
template <typename T>
void putl(T input){put(input); newline();}

int n;
int point;
Puzzle puzzle;

void swap_rec(int column, int row){
  if(0<column){
    swap(puzzle[column][row], puzzle[column-1][row]);
    swap_rec(column-1, row);
  }
}

void drop(){
  Puzzle prev_puzzle = puzzle;
  for(int column=puzzle.size()-1; 0<=column; column--){
    for(int row=puzzle[column].size()-1; 0<=row; row--){
      if(puzzle[column][row] == '#')
        swap_rec(column, row);
    }
  }
  if(puzzle != prev_puzzle) drop();
}

int clear(Line &line, int n, char c){
  if(line[n] == c){
    line[n]='#';
    if(1 <= n && n < line.size()-1){
      return int(c)-int('0')+clear(line, n+1, c)+clear(line, n-1, c);
    }else{
      return int(c)-int('0');
    }
  } else {
    return 0; 
  }
}


int solve(){
  Puzzle prev_puzzle = puzzle;
  for(Line &line: puzzle)
    for(int k=1; k<line.size()-1; k++)
      if(line[k] != '#' && line[k-1] == line[k] && line[k] == line[k+1])
        point+=clear(line, k, line[k]);
  drop();
  if(prev_puzzle != puzzle) solve();
  return point;
}

int main() {
  while(cin>>n && n){
    point=0; puzzle={}; puzzle.reserve(n);
    for(int ic=0; ic<n; ic++){
      Line line;
      for(int lc=0; lc<5; lc++){
        char s; cin >> s; line[lc]=s;
      }
      puzzle.push_back(line);
    }
    putl(solve());
  }
}

Measured memory_bytes: 3152
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
#define mp make_pair
typedef long long ll;
typedef pair<int,int> pint;

ll a[200001],b[200001];
vector<ll> v;

struct BIT{
  int size;
  vector<ll> dat;
  void init(int n){
    size=n;
    dat.clear();
    dat.resize(n+1);
  }
  void add(int i,ll x){
    while(i<=size){
      dat[i]+=x;
      i +=i&-i;
    }
  }
  ll sum(int i){
    ll s=0;
    while(0<i){
      s +=dat[i];
      i -=i&-i;
    }
    return s;
  }
};

int main(){
    int n,k;
    cin>>n>>k;
    rep(i,n) cin>>a[i];
    rep(i,n){
        b[i+1]=b[i]+a[i]-k;
        v.pb(b[i+1]);
    }
    sort(v.begin(),v.end());
    v.erase(unique(v.begin(),v.end()),v.end());
    ll cnt=0;
    BIT bit;
    bit.init(n+1);
    FOR(i,1,n+1){
        if(b[i]>=0) ++cnt;
        int cur=lower_bound(v.begin(),v.end(),b[i])-v.begin();
        ++cur;
        cnt+=bit.sum(cur);
        bit.add(cur,1);
    }
    cout<<cnt<<endl;
    return 0;
}

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

using namespace std;

bool sv[205];

int gcd(int p, int q)
{
  while (q != 0)
  {
    int r = p % q;
    p = q;
    q = r;
  }
  return p;
}

void solve()
{
  long long int k, ans = 0;
  cin >> k;
  for (int i = 1; i <= k; i++)
  {
    for (int j = 1; j <= k; j++)
    {
      for (int r = 1; r <= k; r++)
      {
        if (i == 1 || j == 1 || r == 1)
        {
          ans += 1;
        }
        else
        {
          int p = gcd(i, gcd(j, r));
          // printf("GCD of %d, %d, %d, is %d\n", i, j, r, p);
          ans += p;
        }
      }
    }
  }
  cout << ans << endl;
}

int main()
{
  memset(sv, true, sizeof(sv));
  for (int i = 4; i < 205; i += 2)
  {
    sv[i] = false;
  }
  for (int i = 3; i < 205; i++)
  {
    if (sv[i] == true)
    {
      for (int j = i * i; j < 205; j += i)
      {
        sv[j] = false;
      }
    }
  }
  solve();
  return 0;
}


Measured memory_bytes: 3624
#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;


int main(){
  int R,G,B,N;
  int cnt =0;
  cin >> R;
  cin >> G;
  cin >> B;
  cin >> N;
  for(int i=0;i*R<N+1;i++){
     for(int j=0;i*R+j*G<N+1;j++){
         if((N-i*R-j*G)%B==0) cnt++;
     }
  }
  printf("%d",cnt);
   return 0;

}


Measured memory_bytes: 256
#include <iostream>
#include <cstring>
#include <cstdio>

#define Maxn 3010
#define MOD 998244353
#define LL long long

using namespace std;

inline int read() {
    int x = 0, f = 1;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while('0' <= c && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return x * f;
}

int a, b, c, d;
LL f[Maxn][Maxn];

int main() {
    a = read(); b = read(); c = read(); d = read();
    for(int i = a; i <= c; ++i) {
        for(int j = b; j <= d; ++j) {
            if(i == a && j == b) {
                f[i][j] = 1;
                continue;
            }
            f[i][j] += f[i][j - 1] * i;
            f[i][j] += f[i - 1][j] * j;
            f[i][j] -= f[i - 1][j - 1] * (i - 1) % MOD * (j - 1) % MOD;
            f[i][j] %= MOD;
        }
    }
    cout << f[c][d] << endl;
    return 0;
}

Measured memory_bytes: 74176
def main():
    N = int(input())
    a1 = list(map(int, input().split()))
    a2 = list(map(int, input().split()))

    for i in range(1, N):
        a1[i] += a1[i - 1]
        a2[i] += a2[i - 1]

    max_ame = 0
    for i in range(0, N):
        if i == 0:
            ame = a1[0]
            ame += a2[N-1]
            max_ame = ame
            continue

        ame = a1[i] + (a2[N-1] - a2[i - 1])
        max_ame = max(max_ame, ame)

    return print(max_ame)


main()


Measured memory_bytes: 3064
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)(((n)>>(m))&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}
ll pom(ll a,ll n,int m){ll x=1;for(a%=m;n;n/=2)n&1?x=x*a%m:0,a=a*a%m;return x;}
//#define MOD 998244353
#define MOD 1000000007
#define invp(a,p)pom(a,p-2,p)

ll p3[50];
ll f(ll a,ll b,ll c,ll d,int k){
	ll aa=a/p3[k];
	ll bb=b/p3[k];
	ll cc=c/p3[k];
	ll dd=d/p3[k];
//	printf("%lld %lld %lld %lld %d\n",aa,bb,cc,dd,k);
	if(aa==cc&&bb==dd)return f(a,b,c,d,k-1);
	ll ans=llabs(a-c)+llabs(b-d);
	if(aa!=cc&&bb!=dd)return ans;
	
	if(bb==dd){
		ll t;
		t=a;a=b;b=t;
		t=c;c=d;d=t;
		t=aa;aa=bb;bb=t;
		t=cc;cc=dd;dd=t;
	}

	if(aa%3==1&&llabs(bb-dd)>1){
		//はい
		ans+=min(
			min(llabs(aa*p3[k]-1-a),llabs((aa+1)*p3[k]-a)),
			min(llabs(aa*p3[k]-1-c),llabs((aa+1)*p3[k]-c))
		)*2;
		return ans;
	}
	if(k==0)return ans;
	return f(a,b,c,d,k-1);
}

int main(){
	p3[0]=1;
	rep(i,1,31)p3[i]=p3[i-1]*3;
	int q;
	scanf("%d",&q);
	while(q--){
		ll a,b,c,d;
		scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
		a--,b--,c--,d--;
		printf("%lld\n",f(a,b,c,d,29));
	}
}

Measured memory_bytes: 384
// ?????¬???????????¬??????

#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
using namespace std;

#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define repq(i,a,n) for(int (i)=(a); (i)<=(n); (i)++)
#define repr(i,a,n) for(int (i)=(a); (i)>=(n); (i)--)
#define int long long int

template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}

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

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

// ?§?????????¨????????¨???????????????????¨???????????§???????
template <typename T>
struct Edge {
    int from, to;
    T cost;
    Edge(int s, T d) : to(s), cost(d) {}
    Edge(int f, int s, T d) : from(f), to(s), cost(d) {}

    bool operator<(const Edge &e) const {
        return cost < e.cost;
    }
    bool operator>(const Edge &e) const {
        return cost > e.cost;
    }
};

template <typename T>
using Graph = vector< vector< Edge<T> > >;

// Kahn????????????????????????????????¢?????´????????? (Verified: AOJ GRL_4_B: Topological Sort)
template <typename T>
vector<int> tpsort_Kahn(const vector< vector< Edge<T> > > &g, priority_queue<int> &S, int sign) {
    const int V = g.size();
    vector<int> indeg(V, 0);

    rep(i,0,V) rep(j,0,g[i].size())
        indeg[ g[i][j].to ]++;
    rep(i,0,V) if(indeg[i] == 0) S.push(i * sign);

    vector<int> ans;
    while(S.size() > 0) {
        int u = S.top() * sign; S.pop();
        ans.push_back(u);
        rep(i,0,g[u].size()) {
            indeg[ g[u][i].to ]--;
            if(indeg[ g[u][i].to ] ==  0)
                S.push( g[u][i].to * sign );
        }
    }
    return ans;
}

signed main() {
    int N, M; cin >> N >> M;
    Graph<int> G(N);

    rep(i,0,M) {
        int a, b; cin >> a >> b;
        a--; b--;
        G[a].push_back(Edge<int>{b, 1});
    }

    priority_queue<int> p, q;
    vector<int> a = tpsort_Kahn(G, p, 1), b = tpsort_Kahn(G, q, -1);

    rep(i,0,a.size()) cout << a[i]+1 << endl;
    cout << (a != b) << endl;
    return 0;
}

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

int n;
string s;
map<string, int> mp[3];

void dfs(int p, string t) {
  if (t.length() == 3) return;

  int sz = t.length();
  for (int i = p + 1; i < n; i++) {
    if (mp[sz].count(t + s[i])) continue;
    mp[sz][t + s[i]]++;
    dfs(i, t + s[i]);
  }
}

int main() {
  cin >> n >> s;
  dfs(-1, "");
  cout << mp[2].size() << endl;
}


Measured memory_bytes: 384
#include<stdio.h>
#define N 100001
int main(void){
  int n;
  scanf("%d",&n);
  int i;
  int t[N]={0},x[N]={0},y[N]={0};
  int flag = 0;
  for(i=1;i<=n;i++){
    scanf("%d%d%d",&t[i],&x[i],&y[i]);
    //judge
    if((abs(x[i]-x[i-1])+abs(y[i]-y[i-1])<=t[i]-t[i-1])&&((abs(x[i]-x[i-1])+abs(y[i]-y[i-1])-(t[i]-t[i-1]))%2==0))flag=0;
    else flag=1;
  }
  if(flag==1)printf("No");
  else printf("Yes");
  return 0;
}


Measured memory_bytes: 1280
#include <stdio.h>
#include <string>
#include <iostream>
#include <iomanip>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;

int main(void){
    int n;
    cin >> n;
    vector<long long> array(n);
    for(int i=0; i<n; i++){
        cin >> array[i];
    }
    sort(array.begin(),array.end());
    cout << array[n-1]-array[0] << endl;
    return 0;
}

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


void solve(long long N, std::vector<long long> A){
    sort(A.begin(), A.end());
    vector<ll> cs(N);
    cs.at(0) = A.at(0);
    for(ll i = 1; i < N; ++i){
        cs.at(i) = A.at(i) + cs.at(i - 1);
    }

    ll ans = 0;
    REP(i, N){
        ll pos = i;
        while(1){
            ll idx = lower_bound(A.begin(), A.end(), cs.at(pos) * 2) - A.begin();
            if(idx >= N){
                ans++;
                break;
            }
            
            if(A.at(idx) > cs.at(pos) * 2){
                idx--;
            }

            if(idx > pos){
                pos = idx;
            }else{
                break;
            }
        }
    }
    cout << ans << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools
int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, std::move(A));
    return 0;
}


Measured memory_bytes: 1792
import java.util.Scanner;

class Main {
    public static void main(String[] args) throws Exception {

        Scanner sc = new Scanner(System.in);

        int ans = 0;

        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int x = sc.nextInt();

        for (int i = 0; i <= a; i++) {
            for (int j = 0; j <= b; j++) {
                for (int k = 0; k <= c; k++) {
                    if (x == i * 500 + j * 100 + k * 50)
                        ans++;
                }
            }
        }

        System.out.println(ans);
        sc.close();
    }
}

Measured memory_bytes: 21844
#include <bits/stdc++.h>
#define rep(i,n) for(int i=(0);i<(n);i++)

using namespace std;

typedef long long ll;

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

	int a, b;
	cin >> a >> b;

	if(a == 1) a = 14;
	if(b == 1) b = 14;

	if(a > b){
		cout << "Alice" << endl;
	}else if(a == b){
		cout << "Draw" << endl;
	}else{
		cout << "Bob" << endl;
	}
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define int long long
#define _GLIBCXX_DEBUG

signed main() {
  string N; cin >> N;
  //vector<int> vec(N);

  //int ans = 0;

  //cout << ans << endl;
  if(N == "ABC") cout << "ARC" << endl;
  else cout << "ABC" << endl;
}

Measured memory_bytes: 3640
n = int(input())
a = [int(x) for x in input().split()]
a.sort()
key = True
if n%2:
  key = False
ans = True
for i in range(n):
  if key:
    if a[i] != (i//2)*2+1:
      ans = False
      break
  else:
    if a[i] != ((i+1)//2)*2:
      ans = False
      break

if ans:
  print(pow(2, n//2, pow(10, 9)+7))
else:
  print(0)

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

typedef long long ll;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define each(itr,c) for(__typeof(c.begin()) itr=c.begin(); itr!=c.end(); ++itr)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second

int main()
{
    string s;
    int k;
    cin >>s >>k;

    string t=s;

    rep(i,s.size())
    {
        int toa=(26-(t[i]-'a'))%26;

        if(k-toa>=0)
        {
            k-=toa;
            t[i]='a';
        }
    }

    if(k>0)
    {
        int T=t.size();
        int res=(t[T-1]-'a'+k)%26;
        t[T-1]=res+'a';
    }

    cout << t << endl;
    return 0;
}


Measured memory_bytes: 512
n = int(input())
a = [0] * (10**5 + 2)
for i in range(n) :
    mi, ma = map(int, input().split())
    a[mi-1] += 1
    a[ma] -= 1
for i in range(n) :
    a[i+1] += a[i]
    
ans = 0
for i in range(n, 0, -1) :
    if a[i] >= i :
        ans = i
        break
print(ans)


Measured memory_bytes: 9468
def resolve():
    n,a,b=map(int, input().split())
    s=list(input())
    cnt=0
    cnt_b=0
    for i in range(n):
        if s[i]=='b' and cnt<a+b and cnt_b<b:
            print('Yes')
            cnt+=1
            cnt_b+=1
        elif s[i]=='a' and cnt<a+b:
            print('Yes')
            cnt+=1
        else:
            print('No')
resolve()

Measured memory_bytes: 4704
#include <iostream>
#include <math.h>
#include <string>

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

using namespace std;

int main(){
	int K, S;
	cin >> K >> S;

	int count = 0;
	for(int i = 0; i <= K; ++i){
		for(int j = 0; j <= K; ++j){
			if(i + j <= S && i + j + K >= S){
				//cout << "X:" << i << " Y:" << j << " Z:" << K << endl;
				count++;
			}
		}
	}
	
	cout << count << endl;	
}



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

int main() {
  int A,B,C,K;
  cin>>A>>B>>C>>K;
  if(A<B&&B<C){
    cout<<"Yes"<<endl;
  }
  else{
    int count  = 0;
    while(A>=B){
      B = B*2;
      count += 1;
    }
    while(B>=C){
      C = C*2;
      count += 1;
    }
    if(count>K){
      cout<<"No"<<endl;
    }
    else{
      cout<<"Yes"<<endl;
    }
  }
}

Measured memory_bytes: 3632
N = int(input())
num = list(map(int,input().split()))
a=0
for i in range(N):
    b = 0
    if(num[i]%2 == 1):
        a = 0
        break
        
    while(num[i]%2==0):
        b += 1
        num[i] //= 2
        
    if(a==0 or a>b):
        a=b
print(a)


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

int main()
{
  int n,m,c;
  cin>>n>>m>>c;
  
  vector<int> B(m);
  for(int i=0;i<m;i++){
    cin>>B.at(i);
  }

  int cnt=0;
  for(int i=0;i<n;i++){
    int wk=c;
    for(int j=0;j<m;j++){
      int A;
      cin>>A;  
      wk+=A*B.at(j);
    }
    if(wk>0)
      cnt++;
  }
  
  cout<<cnt<<endl;
  return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <cassert>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
// head

const int N=510;
int n,col[N],row[N];
char s[N][N];
int main() {
	scanf("%d",&n);
	rep(i,0,n) {
		scanf("%s",s[i]);
		rep(j,0,n) {
			col[j]+=(s[i][j]=='#');
			row[i]+=(s[i][j]=='#');
		}
	}
	bool emp=1;
	rep(i,0,n) if (row[i]!=0) emp=0;
	if (emp) {
		puts("-1");
		return 0;
	}
	int ret=n*2;
	rep(i,0,n) {
		if (row[i]==n) {
			int cnt=0;
			rep(j,0,n) if (col[j]!=n) cnt++;
			ret=min(ret,cnt);
		} else {
			int cnt=0;
			if (col[i]==0) cnt++;
			rep(j,0,n) if (s[i][j]=='.') cnt++;
			rep(j,0,n) if (col[j]!=n) cnt++;
			ret=min(ret,cnt);
		}
	}
	printf("%d\n",ret);
}


Measured memory_bytes: 384
N, K = map(int, input().split())


def cc(n):
    i = 0
    while True:
        if K <= (n * (2 ** i)):
            return i
        i += 1


A = 0
for n in range(1, N+1):
    A += 1.0 / N * (0.5 ** cc(n))
print(A)

Measured memory_bytes: 39280
#include <bits/stdc++.h>
using namespace std;
int main(){
    int k;
    cin >> k;
    int sum = 0;
    int p;
    for(int i=1;i<k+1;i++){
        for(int j=1;j<k+1;j++){
            for(int q=1;q<k+1;q++){
                p = gcd(i,j);
                sum += gcd(q,p);
            }
        }
    }
    cout << sum << endl;
}

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


#define MOD 1000000007
#define debug(x) cout<<"x :"<<x<<endl;
//#define case(t,ans) cout<<"Case #"<<t<<": "<<ans<<endl;
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define for_it(it,s) for (auto it = s.begin(); it != s.end(); it++)
#define for_each(r,s) for(auto r:s)

#define all(s) s.begin(),s.end()
#define rall(s) s.end(),s.begin()
#define F first
#define S second
#define PB push_back
#define pb pop_back
#define EB emplace_back
#define pf push_front

#define fill0(arr) memset(arr,0,sizeof(arr))
#define filln(arr) memset(arr,-1,sizeof(arr))
#define MP make_pair
#define vi vector<int>
#define vll vector<ll>
#define vf vector<float>
#define vd vector<ld>
#define precision(x) cout<<fixed<<setprecision((x));
 


#define pi pair<int,int> 
#define ll long long 
#define ld long double
#define endl "\n"
//typedef long long ll;
//typedef vector<int> vi;
//typedef pair<int,int> pi;
//sort(a, a+n);
//reverse(a, a+n);
//random_shuffle(a, a+n);

const ll mod=1e9 + 7;




# define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
# define trace1(x)                cerr << #x << ": " << x << "\n";
# define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
# define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
# define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << "\n";


#define case(i) cout<<"Case "<<i<<": ";
using namespace std;


template<typename T>
T gcd(T u, T v)
{
    return u%v==0?v:gcd(v,u%v);
}




void solve();
int main(){
  //  #ifndef ONLINE_JUDGE
   // freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
   // freopen("error.txt", "w", stderr);
   // #endif
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll t=1;
    //cin>>t;
    clock_t launch=clock();
    for(ll i=1;i<=t;i++)
    {
       // case(i);
        solve();
    }
  
  
     clog<<((long double)(clock()-launch)/CLOCKS_PER_SEC)<<"\n";
    // cerr<<"error is good"<<endl;
    return 0;
}

void solve()
{
   //code
	string s;
    cin>>s;
    
    for(int i=0;i<s.length();i++)
    {
        cout<<"x";
    }
    cout<<endl;
}




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

using namespace std;

#define int long long
#define all(v) (v).begin(), (v).end()
#define resz(v, ...) (v).clear(), (v).resize(__VA_ARGS__)
#define reps(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep(i, n) reps(i, 0, n)

template<class T1, class T2> void chmin(T1 &a, T2 b){if(a>b)a=b;}
template<class T1, class T2> void chmax(T1 &a, T2 b){if(a<b)a=b;}

typedef pair<int, int> Pi;
typedef tuple<int, int, int> Ti;
typedef vector<int> vint;

const int inf = 1LL << 55;
const int mod = 1e9 + 7;

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int N, M, K;
  cin >> N >> M >> K;
  bool flag = false;
  for(int n = 0; n <= N; n++) {
    for(int m = 0; m <= M; m++) {
      if(n*M+m*N-2*n*m == K) flag = true;
    }
  }
  cout << (flag ? "Yes" : "No") << endl;

  return 0;
}


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

const int INF = 1001001001;

int main(void){
  ll n, m;
  cin >> n >> m;
  vector<ll> a(n), b(n), c(m), d(m);
  REP(i, n) cin >> a[i] >> b[i];
  REP(i, m) cin >> c[i] >> d[i];

  REP(i, n){
    ll dist = 1ll<<60;
    ll id = 0;
    REP(j, m){
      ll t = abs(a[i]-c[j]) + abs(b[i]-d[j]);
      if(t < dist){
        dist = t;
        id = j+1;
      }
    }
    cout << id << endl;
  }
  return 0;
}

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

typedef long long ll;

using namespace std;

#define swap(a, b)    do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while(0)
#define repd(i, a, b) for(typeof(b) i = a; i < (b); ++i)
#define rep(i, n)     repd(i, 0, n)
#define dump(x)       cerr << #x << " = " << (x) << endl
#define debug(x)      cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl
#define YesNo(x)      cout << ((x) ? "Yes" : "No") << endl
#define YESNO(x)      cout << ((x) ? "YES" : "NO") << endl
#define absi(x)       ( ((x) >= 0) ? (x) : (-(x)) )

int main(int argc, const char* argv[]) {
	ll n;
	cin >> n;
	vector<ll> h(n);
	rep(i, n) cin >> h[i];

	vector<ll> dp(n);
	dp[0] = 0;
	dp[1] = absi(h[0] - h[1]);

	repd(i, 2, n) {
		dp[i] = min(dp[i-2] + absi(h[i] - h[i-2]), dp[i-1] + absi(h[i] - h[i-1]));
	}

	cout << dp.back() << endl;

	return 0;
}


Measured memory_bytes: 1792
import heapq

k = int(input())
ln = [i for i in range(1, 10)]
heapq.heapify(ln)
i = 0
while True:
    i += 1
    x = heapq.heappop(ln)
    if i == k:
        break
    msd = x % 10
    if msd != 0:
        heapq.heappush(ln, x * 10 + msd - 1)
    heapq.heappush(ln, x * 10 + msd)
    if msd != 9:
        heapq.heappush(ln, x * 10 + msd + 1)
print(x)


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

int main() {
  int N;
  cin >> N;
  
  vector<int> L(N);
  for (int i = 0; i < N; i++) {
    cin >> L.at(i);
  }
  
  int max_line = 0;
  int number = 0;
  for (int i = 0; i < N; i++) {
    if (max_line < L.at(i)) {
      max_line = L.at(i);
      number = i;
    }
  }
  
  int sum = 0;
  for (int i = 0; i < N; i++) {
    if (i == number)
      sum = sum;
    else
      sum += L.at(i);
  }
  
  if (max_line < sum)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}

Measured memory_bytes: 256
#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() {
    ll X, K, D; cin >> X >> K >> D;
    ll ans = 0;
    if (X < 0) {
        if (K <= -X/D) ans = abs(X + K * D);
        else {
            ll num = (-X+D-1)/D;
            if ((num&1) == (K&1)) ans = abs(X + num * D);
            else ans = abs(X + (num-1) * D);
        }
    }
    else if (X > 0) {
        if (X/K >= D) ans = abs(X - K * D);
        else {
            ll num = (X+D-1)/D;
            if ((num&1) == (K&1)) ans = abs(X - num * D);
            else ans = abs(X - (num-1) * D);
        }
    }
    else {
        if ((K&1) == 0) ans = 0;
        else ans = D;
    }
    cout << ans << endl;
}


Measured memory_bytes: 3632
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <map>

#define M 605

using namespace std;

int main()
{
	string s;
	long long k, front, beh, beh_num = 0;
	cin >> s;
	cin >> k;

	front = 0, beh = s.size() - 1;

	for (long long i = 1; i < s.size(); i++)
		if (s[i] != s[i - 1])
		{
			front = i;
			break;
		}

	for (long long i = s.size() - 1; i >= 1; i--)
		if (s[i - 1] != s[i])
		{
			beh_num = s.size() - i;
			beh = i;
			break;
		}

	if (front == 0 || beh_num == 0)
	{
		cout << k*s.size() / 2 << endl;
		return 0;
	}

	long long tot = 0;
	tot += front / 2;
	tot += beh_num / 2;

	long long mid = 0;

	for (long long i = front; i < beh; i++)
	{
		long long num = 1;
		while (1)
		{
			if (s[i] == s[i + num])
				num++;
			else
				break;
		}
		mid += num / 2;
		i = i + num - 1;
	}

	if (s[0] == s[s.size() - 1])
		tot += (k - 1)*((front + beh_num) / 2) + k * mid;
	else
		tot = tot * k + k * mid;

	cout << tot << endl;

	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
#include <cstring>

int main() {
  int n;
  std::cin >> n;
  std::vector<int> s(n);
  for (int i = 0; i < n; ++i)
    std::cin >> s[n - 1 - i];
  std::vector<int> dp(n);
  dp[0] = 0;
  dp[1] = std::abs(s[0] - s[1]);
  for (int i = 2; i < n; ++i) {
    dp[i] = std::min(dp[i - 1] + std::abs(s[i] - s[i - 1]),
                     dp[i - 2] + std::abs(s[i] - s[i - 2]));
  }
  std::cout << dp[n - 1] << std::endl;
  return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
#include<time.h> 
#define loop(i,c,n) for(long long i=c;i<n;i++) 
#define iter(v) for(auto x : v)  // to iterate over entire vector/sets/map
using namespace std; 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii; 
typedef set<int> si; 
int max(int a,int b){if(a>b)return a;
return b;} 
//optimised school method
bool isPrime(ll a){ 
if(a<=1)return false ;
if(a<=3)return true ;
if(a%2==0 || a%3==0)return false;
for(ll i=5;i*i<=a;i=i+6){//Instead of checking till n, we can check till √n because a larger factor of n 
//must be a multiple of smaller factor that has been already checked.
if(a%i==0 || a%(i+2)==0)return false;}return true;}
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);  
	cout.tie(0);
	//  to read input from local files  
	 #ifndef ONLINE_JUDGE 
	clock_t clocktime; 
	clocktime= clock(); 
	 freopen("input.txt", "r",stdin);
	 freopen("output.txt", "w",stdout);
	#endif
	// to read entire line with spaces 
	// string s;
	// getline(cin,s);
	
	// when amount of data is unknown
	// while(cin>>x){}    
	
 
ll  n; 
cin>> n ;
 vector<long long> a(n); ll sum=0; 
 vector<long long> cou(100001,0);
 loop(i,0,n){cin>>a[i]; sum+=a[i];cou[a[i]]++;}

 ll q ; 
 cin>>q;
  loop(i,0,q){ll b,c ;cin>>b>>c ; cou[c]+=cou[b];sum= sum- cou[b]*b + cou[b]*c;cou[b]=0;cout<<sum<<'\n';  }
	
	#ifndef ONLINE_JUDGE
	clocktime = clock()-clocktime; 
	double runtime  = (((double)clocktime)/CLOCKS_PER_SEC); 
	cout<<"\n--------\nTimeUsed -- "<<runtime<<"s"; 
	#endif
	return 0;
}

Measured memory_bytes: 4764
# -*- coding: utf-8 -*-


def main():
    import sys
    input = sys.stdin.readline

    w, h = map(int, input().split())
    pq = [None] * (w + h)

    # KeyInsight:
    # 全域最小木: 任意の2点を行き来できる&コストを最小化する問題
    # ◯: コストを昇順に並び替える
    # ×: 各コストが何本該当するか?
    for i in range(w + h):
        if i < w:
            pq[i] = (int(input()), 'x')
        else:
            pq[i] = (int(input()), 'y')

    x_count = w + 1
    y_count = h + 1
    ans = 0

    # 一つ道路を舗装すると、片方の座標の辺の数が一つ減る
    # abst: 連結成分を作ると、幅が一つ減る
    # 図のイメージは、以下のURLを参照
    # https://scrapbox.io/ganariya/AtCoderCODEFESTIVAL2016qualB_C%E5%95%8F%E9%A1%8C500%E7%82%B9_%E3%80%8CC_-_Gr-idian_MST%E3%80%8D
    for cost, flag in sorted(pq):
        if flag == 'x':
            ans += cost * y_count
            x_count -= 1
        else:
            ans += cost * x_count
            y_count -= 1

    print(ans)


if __name__ == '__main__':
    main()


Measured memory_bytes: 26084
/*
本宝的程序外人别想看！！

name: [夏宝宝]
run time：[] ms
compilation time: [] ms
output memory: [] MiB
where: []
state: []
*/
#include<bits/stdc++.h>
using namespace std;

int main()
{
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	
	cout.sync_with_stdio(false);
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	int n, m;
	
	cin>>n>>m;
	
	if(n<10)
	{
		cout<<m+100*(10-n)<<endl;
	}
	else
	{
		cout<<m<<endl;
	}
	
	return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <stack>
#include <vector>
using namespace std;
typedef long long ll;
int main(){
  int a,b,c;
  cin >> a >> b >> c;

  int week_num = c/(a*7+b);
  
  int ans = week_num * 7 + (c - week_num * (a * 7 + b) + a - 1)/a;

  int anss = (c+a*7+b-1)/(a*7+b) * 7;
  cout << min(ans,anss) << endl;
}


Measured memory_bytes: 3112
n=int(input())
a=[0]
a.extend(list(map(int,input().split())))
a.append(0)
s=0
for i in range(n+1):
  s+=abs(a[i+1]-a[i])
for i in range(1,n+1):
  print(s-abs(a[i+1]-a[i])-abs(a[i]-a[i-1])+abs(a[i+1]-a[i-1]))

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

int main(){
	int a,b,i,j,x,y,n;
	int map[16][16];

	while(1){
		scanf("%d %d",&a,&b);
		if(!a && !b){
			break;
		}
		memset(map,0,sizeof(map));

		scanf("%d",&n);

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

		for(i = 0;i < a;i++){
			if(map[0][i] == -1){
				break;
			}
			map[0][i] = 1;
		}

		for(i = 0;i < b;i++){
			if(map[i][0] == -1){
				break;
			}
			map[i][0] = 1;
		}

		for(i = 1;i < b;i++){
			for(j = 1;j < a;j++){
				if(map[i][j] != -1){
					if(map[i-1][j] == -1){
						if(map[i][j-1] == -1){
							map[i][j] = 0;
						}else{
							map[i][j] = map[i][j-1];
						}
					}else{
						if(map[i][j-1] == -1){
							map[i][j] = map[i-1][j];
						}else{
							map[i][j] = map[i-1][j] + map[i][j-1];
						}
					}
				}
			}
		}

		printf("%d\n",map[b-1][a-1]);
	}

	return 0;
}

Measured memory_bytes: 600
import math
while True:
    n=int(input())
    if n == 0:
        break
    a=list(map(int,input().split()))
    m=sum(a)/n
    z=0
    for i in range(n): 
        x=a[i]-m
        y=x**2
        z += y
    b=z/n
    c=math.sqrt(b)
    print('{:.8f}'.format(c))


Measured memory_bytes: 5700
#include <bits/stdc++.h>
#include <stdint.h>
#include <algorithm>

using namespace std;

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

	int ans = 0;
	
	for (int i = 0; i < S.size(); i++) {
	//	cout << "dbg " << i << ", " << S.at(i) << " ";
		if (i % 2) {
			if(S.at(i) == '0') ans++;
	//		cout << ans << endl;
		} else {
			if(S.at(i) == '1') ans++;
	//		cout << ans << endl;
		}
	}
	
	//cout << "result " << ans << endl;
	
	if ( ans > (S.size() / 2)) {
		cout << (S.size() - ans) << endl;
	} else {
		cout << ans << endl;
	}
	
	



	return 0;
}


Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    int a,b,c,e,f;
    cin>>a>>b>>c;
    f=a-b;
    e=c-f;
    if(e<0)
        e=0;
    cout<<e;
}


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

using namespace std;
using ll =long long;
typedef pair<int,int> P;
#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cerr << #x << " = " << (x) << endl;
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; }
//Write From this Line

vector<pair<long long,int>> factorize(long long n){
	vector<pair<long long,int>> res;
	for( long long i = 2; i*i <= n; ++i){
		if(n%i) continue ; 
		res.emplace_back(i,0);
		while(n%i == 0){
			n /= i ;
			res.back().second++;
		}
	}
	if ( n != 1) res.emplace_back(n,1);
	return res;
}
int main()
{
	ll n;
	cin >> n;

	auto p = factorize(n-1);
	ll ans = 1;
	for(auto x : p){
		ans *= 1+x.second;
	}
	for(ll i = 2; i * i <= n; i++){
		ll N = n;
		if(N % i == 0){
			while(N % i == 0){
				N /= i;
			}
			if( N % i == 1){
				ans ++;
			}
		}
	}
	cout << ans << endl;
}


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

using namespace std;

int main()
{
    int n; cin>>n;
    n*=2;
    vector<int> a;
    for(int i=0;i<n;i++)
    {
        int x; cin>>x;
        a.push_back(x);
    }
    sort(a.begin(),a.end());
    int res=0;
    for(int i=0;i<n;i+=2)
    {
        res+=a[i];
    }
    cout<<res;
    return 0;
}


Measured memory_bytes: 256
n = int(input())
x = n
soinsuubunkai = []
for i in range(2,int(x**(1/2))+1):
         if n % i == 0:
                  soinsuu = i
                  zyousuu = 0
                  while n % i == 0:
                           n = n // i
                           zyousuu += 1
                  soinsuubunkai.append([soinsuu,zyousuu])
if n != 1:
         soinsuubunkai.append([n,1])

ans = 0
for i in range(len(soinsuubunkai)):
         for j in range(1,100):
                  soinsuubunkai[i][1] -= j
                  if soinsuubunkai[i][1] < 0:
                           break
                  else:
                           ans += 1

print(ans)


Measured memory_bytes: 9468