akhauriyash/Code-Regression huggingface dataset:
object Main {
  def main(args: Array[String]): Unit = {
    solve
  }

  def solve(): Unit = {
    val sc = new java.util.Scanner(System.in)
    val n = sc.nextInt
    val ts = new Array[Long](n)
    for (i <- 0 until n) {
      ts(i) = sc.nextLong
    }

    var ans = ts(0)
    for (i <- 1 until n) {
      ans = lcm(ans, ts(i))
    }

    println(ans)
  }

  def lcm(x: Long, y: Long): Long = {
    if (x % y == 0) {
      x
    } else if (y % x == 0) {
      y
    } else {
      x / gcd(x, y) * y
    }
  }

  def gcd(x: Long, y: Long): Long = {
    if (x == 0) y
    else gcd(y % x, x)
  }

}


Measured memory_bytes: 25652
n=int(input())
a=input()
b=input()

ans=0
MOD= 1000000007
cnt=0
ptn=-1
while cnt<n:
    if a[cnt]==b[cnt]:
        if cnt==0:
            ans=3
            ptn=0
        else:
            if ptn==0:
                ans*=2
                ptn=0
            else:
                ans*=1
                ptn=0
        cnt+=1
    else:
        if cnt==0:
            ans=6
            ptn=1
        else:
            if ptn==0:
                ans*=2
                ptn=1
            else:
                ans*=3
                ptn=1
        cnt+=2
    ans%=MOD

print(ans)

Measured memory_bytes: 3064
D, G = map(int,input().split())
p,C = [],[]
for i in range(D):
    p_,c_ = map(int,input().split())
    p.append(p_)
    C.append(c_)
ans = float('inf')
 
for i in range(2**D):
    s, k = 0, 0
 
    for j in range(D):
        if (i>>j) & 1:
            s += p[j]*(j+1)*100 + C[j] 
            k += p[j]
    if s >= G:
        ans = min(ans, k)
    else:
        for l in range(D-1, -1, -1):
            if (i>>l) & 1 == 0: 
                if s+(l+1)*100*p[l]<G:
                    break
                else:
                    rem = G - s
                    k += -(-rem//((l+1)*100))
                    ans = min(ans, k)
print(ans)


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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP0(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define REP1(i,n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
#define RREP0(i,n) for(int i=n-1; i>=0; --i)
#define RREP1(i,n) for(int i=n; i>=1; --i)

#define SORT(c) sort((c).begin(),(c).end())
#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define CLR(a) memset((a), 0 ,sizeof(a))

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

const int INTINF = 2147483647;
const LL LLINF = 9223372036854775807;
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;
const int MAX = 100;
int V, E;
LL D[MAX][MAX];

struct init{
    init(){
        cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(20);
    };
} init;


void input(){
  std::cin >> V >> E;
  REP0(i,V) {
    REP0(j,V) {
      D[i][j] = ( (i==j) ? 0 : LLINF );
    }
  }
  REP0(i, E){
    int s, t, d;
    std::cin >> s >> t >> d;
    D[s][t] = d;
  }
}


void floyd(){ // minpath of i to j through k(=0..n)
  REP0(k,V){
    REP0(i,V){
      if ( D[i][k] == LLINF ) continue;
      REP0(j,V){
        if ( D[k][j] == LLINF ) continue;
        D[i][j] = min( D[i][j], D[i][k] + D[k][j] );
      }
    }
  }
}


void output(){
  REP0(i,V){
    if ( D[i][i] < 0 ) {
      std::cout << "NEGATIVE CYCLE" << '\n';
      return;
    }
  }

  REP0(i,V) {
    REP0(j,V) {
      if ( j ) std::cout << " ";
      if ( D[i][j] == LLINF ) {
        std::cout << "INF";
      } else {
        std::cout << D[i][j];
      }
    }
    std::cout << '\n';
  }
}


int main(int argc, char const *argv[]) {
  input();
  floyd();
  output();
  return 0;
}



Measured memory_bytes: 3248
#include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

template<typename T> class Weighted_UnionFind
{
public:
    vector<int> par,nrank;
    vector<T> wd;
    int V;
    Weighted_UnionFind(int node_size){
        V = node_size;
        par.resize(V),wd.resize(V,0),nrank.resize(V,0);
        rep(i,V){
            par[i] = i;
        }
    }
    int find(int x){
        if(par[x] == x){
            return x;
        }else{
            int parent = find(par[x]);
            wd[x] += wd[par[x]];
            return par[x] = parent;
        }
    }
    T weight(int x){
        find(x);
        return wd[x];
    }
    void unite(int x,int y,T w){
        w += weight(x), w -= weight(y);
        x = par[x],y = par[y];
        if(x == y) return;
    	if(nrank[x] < nrank[y]){
            swap(x,y);
            w = -w;
        }
        nrank[x] += (nrank[x] == nrank[y]);
        par[y] = x;
        wd[y] = w;
    }
    bool same(int x,int y){
        return find(x)==find(y);
    }
    T diff(int x,int y){
        return weight(y)-weight(x);
    }
};


int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,m;
    cin >> n >> m;
    Weighted_UnionFind<int> wu(n);
    rep(i,m){
        int l,r,d;
        cin >> l >> r >> d;
        --l,--r;
        if(wu.same(l,r)){
            if(wu.diff(l,r) != d){
                cout << "No\n";
                return 0;
            }
        }else{
            wu.unite(l,r,d);
        }
    }
    cout << "Yes\n";
    return 0;
}


Measured memory_bytes: 1408
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100000+10;
int n,m,k;
int f[maxn],sum[maxn],fre[maxn];
void init(){
	for(int i=1;i<=n;i++){
		f[i]=i;
		sum[i]=1;
	}
	return ;
}

int find(int x){
	return f[x]==x?x:f[x]=find(f[x]);	
}

void merge(int x,int y){
	int fx=find(x),fy=find(y);
	if(fx!=fy){
		f[fy]=fx;sum[fx]+=sum[fy];
	}
	return ;
}

int main(){
	cin>>n>>m>>k;
	init();
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
		fre[x]++;fre[y]++;
		merge(x,y);
	}
	for(int i=1;i<=k;i++){
		int x,y;
		cin>>x>>y;
		if(find(x)==find(y)){
			fre[x]++;fre[y]++;
		}
	}
	for(int i=1;i<=n;i++){
		cout<<sum[find(i)]-fre[i]-1<<" ";	
	}
	return 0;
}

Measured memory_bytes: 2560
#include<bits/stdc++.h>
#include<string>
#include<cctype>
#include<cmath> 
#define rep(i,n) for(int i=0;i<(n);i++)
using namespace std;
 
int main(){
 
  long long N,X,Y,i;
  
  cin >> N;
  long long a[N];
  long long ans[N];
  long long ans1,min;
  rep(i,N){
    cin >> a[i];
  }
  rep(i,N-1){
    ans[0]=a[0];
    ans[i+1]=ans[i]+a[i+1];
  }
  min=200000000000000;
  for(i=0;i<N-1;i++){
    X = ans[i];
    Y = ans[N-1]-X;
    ans1 = abs(X-Y);
 
    if(min>ans1){
      min = ans1;
    }
  }
  
  cout << min << endl;
  return 0;
}
 

Measured memory_bytes: 3328
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_map>
#include <ctime>
#include <list>
#include <numeric>
#include <utility>
#include <ctime>
#define INF 1000000000
#define LINF 9000000000000000000
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
 
/*---------------------------------------------------*/

int n, m, r;
int rr[8];
bool used[8];
int d[205][205];

void init(){
    rep(i, n + 1){
        rep(j, n + 1){
            d[i][j] = INF;
        }
    }
}

void wf(){
    rep(k, n + 1){
        rep(i, n + 1){
            rep(j, n + 1){
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
}

int dfs(bool used[], int cnt, int cost, int now){
    // cout << cnt << " " << cost << endl;
    int ret = INF;
    if(cnt == r) return cost;
    rep(i, r){
        if(used[i] == false){
            used[i] = true;
            ret = min(ret, dfs(used, cnt + 1, cost + d[now][rr[i]], rr[i]));
            used[i] = false;    
        }
    }
    return ret;
}

int main(){
    int a, b, c;
    cin >> n >> m >> r;
    init();
    rep(i, r)cin >> rr[i];
    rep(i ,m){
        cin >> a >> b >> c;
        d[a][b] = c;
        d[b][a] = c;
    }
    wf();
    int ans = INF;
    rep(i, r){
        used[i] = true;
        ans = min(ans, dfs(used, 1, 0, rr[i]));
        used[i] = false;
    }
    cout << ans << endl;
    return 0;
}


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

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define FI first
#define SE second
using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
 
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}


int main(int argc, char const* argv[])
{
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  string s;
  cin >> s;
  int n = sz(s);
  int res = inf;
  for(int i = 0; i < 2; i++){
    int tmp = 0;
    for(int j = 0; j < n; j++){
      int c = int(s[j] - '0');
      if(j % 2 == 0){
        if(c != i)tmp++;
      }else{
        if(c == i)tmp++;
      }
    }
    res = min(res, tmp);
  }
  cout << res << endl;
	return 0;
}


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

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-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 pf(s): return print(s, flush=True)


def main():
    k,r,l = LI()
    p = F()
    e = F()
    t = F()

    def f(k,r,l):
        if abs(r-t) <= e and abs(t-l) <= e:
            return 1
        if t-l > e or r-t > e:
            return 0
        if k == 0:
            if abs(t - (r+l) / 2) <= e:
                return 1
            return 0

        h = (r+l) / 2
        if h >= t:
            return f(k-1,r,h) * (1-p) + f(k-1,h,l) * p
        return f(k-1,r,h) * p + f(k-1,h,l) * (1-p)

    tr = f(k,r,l)

    return '{:0.9f}'.format(tr)



print(main())



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

int main() {
	int n;
	cin >> n;
	
	vector<char> v(n);
	for (auto &x : v) cin >> x;
	
	v.resize(unique(v.begin(), v.end()) - v.begin());
	
	cout << v.size() << "\n";
	// for (auto x : v) cout << x;
	
	return 0;
}

Measured memory_bytes: 384
# include <bits/stdc++.h>
# define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
# define reps(i, n) for(int i=1, i##_len=(n); i<=i##_len; ++i)
# define rrep(i, n) for(int i=((int)(n)-1); i>=0; --i)
# define rreps(i, n) for(int i=((int)(n)); i>0; --i)
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define optimize_cin() cin.tie(0); ios::sync_with_stdio(false)
using namespace std;
using lint = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
const long long INF = 1LL << 60;

int main()
{
    char c;
    cin >> c;
    string alp = "abcdefghijklmnopqrstuvwxyz";
    char ans;

    rep(i,25){
        if(alp.at(i) == c) {
            ans = alp.at(i+1);
            break;
        }
    }

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define ufor(i,l,r) for (int i=l;i<=r;i++)
#define MAXN 20000500

int main()
{
	int n;
	scanf("%d",&n);
	int ans=n/500*1000+(n%500)/5*5;
	printf("%d",ans);		
	return 0;
}


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

int main(int argc, char const *argv[]) {
  int n,a,b;
  string s;
  std::cin >> n >> a >> b >>s;
  int pass=0,passb=0;

  for (size_t i = 0; i < n; i++) {
    if (s[i]=='a') {
      if (pass<a+b) {
        std::cout << "Yes" << std::endl;
        pass++;
      }else{
        std::cout << "No" << std::endl;
      }
    }else if (s[i]=='b') {
      if ((pass<a+b)&&(passb<b)) {
        std::cout << "Yes" << std::endl;
        pass++;
        passb++;
      }else{
        std::cout << "No" << std::endl;
      }
    }else{
      std::cout << "No" << std::endl;
    }
  }

  return 0;
}


Measured memory_bytes: 896
#include <iostream>
#include <stdio.h>

typedef unsigned long long ull;
using namespace std;

int main(int argc, char *argv[])
{
    int n;
    scanf("%d", &n);
    ull res = 0;
    while (n != 0)
    {
        if (n - 25 >= 0)
        {
            n = n - 25;
        }
        else if (n - 10 >= 0)
        {
            n = n - 10;
        }
        else if (n - 5 >= 0)
        {
            n = n - 5;
        }
        else
        {
            n = n - 1;
        }
        res++;
    }
    printf("%lld\n", res);
    return 0;
}



Measured memory_bytes: 3212
#include<bits/stdc++.h>
#define ll long long int
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
static const ll MAX = 1000000000000000;
static const int NIL = -1;
using namespace std;
const long double EPS = 0.0000000001;
const long double PI = (acos(-1));
const ll MOD = 1e9 + 7;

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



int main(){
    ll n,m,p; cin >> n >> m;
    ll ans = 0;
    vector<vector<ll>> a(51);
    vector<vector<bool>> b(51,vector<bool>(51,false));
    vector<ll> x(51),y(51);
    vector<bool> v(51,false);

    rep(i,m){
        cin >> x[i] >> y[i];
        a[x[i]].push_back(y[i]);
        a[y[i]].push_back(x[i]);
        b[x[i]][y[i]] = true;
        b[y[i]][x[i]] = true;
    }

    queue<ll> q;

    rep(i,m){
        b[x[i]][y[i]] = false;
        b[y[i]][x[i]] = false;
        q.push(1);

        while (!q.empty()){
            p = q.front();
            q.pop();

            rep(j,a[p].size()){
                if(b[p][a[p][j]] && !v[a[p][j]]){
                    v[a[p][j]] = true;
                    q.push(a[p][j]);
                }
            }
        }

        for(int j = 1; j <= n; j ++){
            if(!v[j]) {
                ans ++;
                break;
            }
        }
        rep(j,n+1) v[j] = false;

        b[x[i]][y[i]] = true;
        b[y[i]][x[i]] = true;
    }

    cout << ans;
}

Measured memory_bytes: 3644
#!/usr/bin/env python3
import sys

MOD = 1000000007  # type: int


def solve(N: int):
    mod = 10**9 + 7
    # NG = {AGC,GAC,ACG,ATGC,AGGC,AGTC}
    # (A,C,G,T) = (0,1,2,3)
    # dp[i][j]: 長さiの条件を満たす文字列のうち、末尾がjとなるものの場合の数
    # 初期条件:dp[1] = [1,1,1,1]
    # 答え: dp[N][0] + dp[N][1] + dp[N][2] + dp[N][3]

    dp = [[0]*4 for _ in range(N+1)]
    dp[1] = [1]*4

    for i in range(2,N+1):
        for j in range(4):
            dp[i][j] += sum(dp[i-1]) ##dp[i-1][0] + dp[i-1][1] + dp[i-1][2] + dp[i-1][3]
            dp[i][j] %= mod

        # ダメなパターンを引いて補正する
        # 後ろ二文字が"AG","GA"の場合はCを付け足せない
        dp[i][1] -= dp[i-2][0]*1 + dp[i-2][2]*1

        # 後ろ二文字が"AC"の場合はGを付け足せない
        # ~AC となる場合の数は dp[i-2][0] (- dp[i-3][2])
        dp[i][2] -= dp[i-2][0]

        if i >= 3:
            # 後ろ三文字が"ATG","AGG","AGT"となる場合はCを付け足せない
            dp[i][1] -= dp[i-3][0]*3
            dp[i][2] += dp[i-3][2]

    ans = sum(dp[N]) % mod
    print(ans)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    solve(N)

if __name__ == '__main__':
    main()


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

int main() {
  char b;
  cin >> b;
  
  if(b == 'A') 
    cout << 'T' << endl;
  if(b == 'C') 
    cout << 'G' << endl;
  if(b == 'G') 
    cout << 'C' << endl;
  if(b == 'T') 
    cout << 'A' << endl;
 
}

Measured memory_bytes: 256
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;

typedef long long  LL;
LL n,m,s,p,mx,a[1000010],b[1000010];

void work()
{
	scanf("%lld",&n),m=n*(n+1)/2;
	for (int i=1; i<=n; i++)
		scanf("%lld",&a[i]),b[i]=a[i]-a[i-1],s+=a[i],p+=s/m,s%=m;
	b[1]=a[1]-a[n];
	if (s!=0)  puts("NO"),exit(0);
	for (int i=1; i<=n; i++)  mx=max(mx,b[i]);
	for (int i=1; i<=n; i++)
		{
			if ((mx-b[i])%n!=0)  puts("NO"),exit(0);
			p-=(mx-b[i])/n;
		}
	if ((p<0)||(p%n))  puts("NO"),exit(0);
	puts("YES");
}

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


Measured memory_bytes: 5120
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define pi 3.141592653589793
using namespace std;


int main() {
//    cout << setprecision(10);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);


    int n, m;
    cin >> n >> m;
    int a[n];
    pair<int, int> bc[m];
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = 0; i < m; i++) cin >> bc[i].second >> bc[i].first;

    sort(bc, bc + m);
    sort(a, a + n);

    ll ans = 0;
    int ind = m - 1;
    for (int i = 0; i < n; i++) {
        if (ind >= 0) {
            ans += max(a[i], bc[ind].first);
            bc[ind].second--;
            if (bc[ind].second == 0) {
                ind--;
            }
        }

        else {
            ans += a[i];
        }

    }


    cout << ans;

}



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

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

int walk(int pos,int a,int b){
    if(pos<=a)return a;
    if(b<=pos)return b;
    return pos;
}
int walk(int pos,ii range){
    return walk(pos,range.first,range.second);
}

int n;
vector<ii>v;
ll f(){
    vector<bool>done(n);
    vector<pair<ii,int> >hi;
    vector<int>L(n),R(n);
    hi.resize(n);
    for(int i=0;i<n;i++){
        hi[i]=make_pair(v[i],i);
    }
    sort(hi.begin(),hi.end());
    for(int i=0;i<n;i++){
        L[i]=hi[i].second;
    }
    for(int i=0;i<n;i++){
        swap(hi[i].first.first,hi[i].first.second);
    }
    sort(hi.begin(),hi.end());
    for(int i=0;i<n;i++){
        R[i]=hi[i].second;
    }
    ll ans=0;
    int pos=0;
    int Lpos=n-1;
    int Rpos=0;
    bool goingleft=true;
    while(true){
        int index;
        if(goingleft){
            while(Rpos<n && done[R[Rpos]]){
                Rpos++;
            }
            if(Rpos==n)break;
            index=R[Rpos];
        }
        else{
            while(Lpos>=0 && done[L[Lpos]]){
                Lpos--;
            }
            if(Lpos==-1)break;
            index=L[Lpos];
        }
        done[index]=true;
        //printf("%d\n",goingleft);
        //printf("to %d %d\n",v[index].first,v[index].second);
        int newpos=walk(pos,v[index]);
        ans+=abs(pos-newpos);
        pos=newpos;
        goingleft=!goingleft;
    }
    ans+=abs(pos);
    return ans;
}

int main() {
    scanf("%d",&n);
    int a,b;
    v.resize(n);
    for(int i=0;i<n;i++){
        scanf("%d%d",&a,&b);
        v[i]=ii(a,b);
    }
    ll maxi=f();
    for(int i=0;i<n;i++){
        a=v[i].first;
        b=v[i].second;
        v[i]=ii(-b,-a);
    }
    maxi=max(maxi,f());
    printf("%lld\n",maxi);
}


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

#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define intll int long long
const int INF = 1e9;
const int MOD = 1e9 + 7;

int main() {
  intll n , k ; 
  cin >> n >> k ;
  vector<intll> v(n) ;
  rep(i,n){
    intll a ; 
    cin >> a ;
    v.at(i) = a-1 ;
  }
  
  vector<intll>  Cnt(n) ;
  intll now = INF ;
  intll Key  ;
  bool finish = false ;
  while(!finish){
    if(now == INF) now = 0 ;
    Cnt.at(now) ++ ;
    now = v.at(now) ;
    if(Cnt.at(now) == 1){
      Key = now ;
      finish = true ;
    }
  }
  
  vector<intll> cnt(n) ;
  now = INF ;
  finish = false ;
  intll c = 0;
  intll first = 0; 
  intll second = 0 ;
  
  while(!finish){
    if(now == INF) {
      now = 0 ;
      cnt.at(now) ++ ;
    }
    
     now = v.at(now) ;
     cnt.at(now) ++ ;
     c++ ;
    
    if(now == Key && cnt.at(Key) == 2) {
     second = c - first ;
     finish = true ;
     break;
    }
    
     if(now == Key ){
      first = c ;
    }
   
  }
  
  intll last =  (k-first) % second ; 
  
  now = 0 ;
  
  rep(i,min(first+last,k)){
    now = v.at(now) ;
  }

    

 cout << now + 1  ;
 
}


Measured memory_bytes: 7944
#include <iostream>
using namespace std;
long long f(long long n)
{
	if(n%4==0)
		return n;
	if(n%4==1)
		return 1;
	if(n%4==2)
		return (n+1);
	if(n%4==3)
		return 0;
}
int main()
{
	long long n,m,a,b,c;
	cin>>n>>m;
	a=f(n);
	b=f(m);
	c=a^b^n;
	cout<<c<<endl;
	return 0;
}


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

typedef long long ll;

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

    int n; cin >> n;
    vector<int> a(n);
    double sum = 0.0;
    for(int i=0;i<n;i++){
        cin >> a[i];
        sum += a[i];
    }
    double ave = sum / n;
    double dist,minn = 1000000.0;
    int ans;
    for(int i=0;i<n;i++){
        dist = abs(a[i]-ave);
        if(minn>dist){
            minn = dist;
            ans = i;
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <cstdint>
#include <iostream>

namespace null_null {

using i32 = std::int_fast32_t;
using i64 = std::int_fast64_t;
using u32 = std::uint_fast32_t;
using u64 = std::uint_fast64_t;
using usize = std::size_t;

} // namespace null_null

#include <algorithm>
#include <utility>
#include <vector>

namespace null_null {

void main_() {
  usize N;
  std::cin >> N;
  std::vector<std::pair<usize, usize>> A;

  for (usize i = 1; i <= N; i++) {
    usize a;
    std::cin >> a;
    A.push_back(std::make_pair(a, i));
  }

  std::sort(A.begin(), A.end());

  for (usize i = 0; i < N; i++) {
    if (i != 0)
      std::cout << " ";
    std::cout << A[i].second;
  }
  std::cout << std::endl;
}

} // namespace null_null

int main() {
  std::cin.tie(nullptr);
  std::ios::sync_with_stdio(false);
  null_null::main_();
  return 0;
}

Measured memory_bytes: 2544
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll,ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll,ll>>>
#define PL pair<ll,ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main(){
    ll a;
    cin >> a;
    ll ans = a + a * a + a * a * a;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3596
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
#pragma GCC optimize ("-O3")
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
/*---------------------------------------------------------------------------------------------------
　　　　　　　　　　　 ∧＿∧  
　　　　　 ∧＿∧ 　（´<_｀ ）　 Welcome to My Coding Space!
　　　　 （ ´_ゝ`）　/　 ⌒i     
　　　　／　　　＼　 　  |　|     
　　　 /　　 /￣￣￣￣/　　|  
　 ＿_(__ﾆつ/　    ＿/ .| .|＿＿＿＿  
　 　　　＼/＿＿＿＿/　（u　⊃  
---------------------------------------------------------------------------------------------------*/






int N, A[101010];
//---------------------------------------------------------------------------------------------------
void _main() {
    cin >> N;
    rep(i, 0, N) cin >> A[i];
    sort(A, A + N);

    int ai = A[N - 1];
    int aj = A[0];
    rep(i, 0, N - 1) if (abs(A[i] - ai / 2) <= abs(aj - ai / 2)) aj = A[i];
    printf("%d %d\n", ai, aj);
}

Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <array>
#include <set>
#include <queue>

std::array<int, 2> mex(std::set<int> s) {
    std::array<int, 2> ret;
    int idx = 0;
    for (int x = 0;; ++x) {
        if (s.count(x)) continue;
        ret[idx] = x;
        if (++idx == 2) break;
    }
    return ret;
}

bool solve(const std::vector<int>& par, int choice) {
    int n = par.size();

    std::vector<int> outdeg(n, 0);
    for (auto p : par) ++outdeg[p];

    std::queue<int> que;
    std::vector<int> assign(n, -1);
    for (int v = 0; v < n; ++v) {
        if (outdeg[v] == 0) {
            assign[v] = 0;
            que.push(v);
        }
    }

    std::vector<std::set<int>> ch(n);
    while (true) {
        while (!que.empty()) {
            int v = que.front();
            que.pop();

            int u = par[v];
            ch[u].insert(assign[v]);
            if (--outdeg[u] == 0 && assign[u] < 0) {
                assign[u] = mex(ch[u])[0];
                que.push(u);
            }
        }

        bool finish = true;
        for (int v = 0; v < n; ++v) {
            if (assign[v] < 0) {
                finish = false;
                assign[v] = mex(ch[v])[choice];
                que.push(v);
                break;
            }
        }
        if (finish) break;
    }

    // Judge
    for (auto& c : ch) c.clear();
    for (int v = 0; v < n; ++v) {
        int u = par[v];
        ch[u].insert(assign[v]);
    }
    for (int v = 0; v < n; ++v) {
        if (assign[v] != mex(ch[v])[0]) {
            return false;
        }
    }

    return true;
}

int main() {
    int n;
    std::cin >> n;

    std::vector<int> par(n);
    for (auto& p : par) {
        std::cin >> p;
        --p;
    }

    for (int choice = 0; choice < 2; ++choice) {
        if (solve(par, choice)) {
            std::cout << "POSSIBLE" << std::endl;
            return 0;
        }
    }
    std::cout << "IMPOSSIBLE" << std::endl;
    return 0;
}


Measured memory_bytes: 20224
#include<bits/stdc++.h>
#define int long long

using namespace std;

const int N = 1e5;

int readInt ()
{
    bool minus = false;
    int result = 0;
    char ch;
    ch = getchar();
    while (true)
    {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus = true;
    else result = ch-'0';
    while (true)
    {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus)
        return -result;
    else
        return result;
}
main() {
    int k = readInt(), x = readInt();
    for (int i = x - k + 1; i <= x + k - 1; i++) {
        cout << i << " ";
    }
}


Measured memory_bytes: 256
def main():
    from collections import Counter
    import sys
    sys.setrecursionlimit(10 ** 7)
    readline = sys.stdin.readline

    def prob(c1, c2, c3):
        if memo[c1][c2][c3] != -1.0:
            return

        ret = n
        if c1 > 0:
            ret += c1 * memo[c1 - 1][c2][c3]
        if c2 > 0:
            ret += c2 * memo[c1 + 1][c2 - 1][c3]
        if c3 > 0:
            ret += c3 * memo[c1][c2 + 1][c3 - 1]
        ret /= c1 + c2 + c3

        memo[c1][c2][c3] = ret
        return

    n = int(readline())
    *a, = map(int, readline().split())

    ctr = Counter(a)

    memo = [[[-1.0] * (n + 1) for _ in range(n + 1)] for _ in range(n + 1)]  # 初期値floatにして型変換をさせないようにしてみた
    memo[0][0][0] = 0.0

    for c3 in range(n + 1):
        for c2 in range(n - c3 + 1):
            for c1 in range(n - c2 - c3 + 1):
                prob(c1, c2, c3)

    print(memo[ctr[1]][ctr[2]][ctr[3]])


if __name__ == '__main__':
    main()


Measured memory_bytes: 254344
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
constexpr int mod=1000000007;
constexpr int mod1=998244353;
vector<int> dx={0,1,0,-1},dy={-1,0,1,0};
bool inside(int y,int x,int h,int w){
	if(y<h && y>=0 && x<w && x>=0) return true;
	return false;
}





int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,t,a;cin >> n >> t >> a;
	vector<double> h(n);
	rep(i,n) cin >> h.at(i);
	int ans = 0;
	double mi = 1e18;
	rep(i,n){
		if(abs(a - (t - h.at(i) * 0.006)) < mi){
			ans = i;
			mi = abs(a - (t - h.at(i) * 0.006));
		}
	}
	cout << ans + 1 << endl;
}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main() {
  int n, x;
  cin >> n;
  cin >> x;
  int pos = 0;
  int flag = 0;
  for (int i = 0; i < n; ++i) {
    int a;
    cin >> a;
    pos += a;
    if (pos > x) {
      cout << i + 1 << endl;
      flag = 1;
      break;
    }
  }
  if (flag == 0) {
    cout << n + 1 << endl;
  }
  return 0;
}

Measured memory_bytes: 256
from itertools import groupby

n,k = map(int,input().split())
s = [0] + list(input())
ans = 0
for i in range(1,n):#調べる対象はs[2]からs[n-1]まで
    if s[i] == s[i+1]:
        ans += 1

print(min(ans+ k*2,n-1))


Measured memory_bytes: 4664
#include<bits/stdc++.h>
using namespace std;
#define arep(i,x,n) for(int i=int(x);i<(int)(n);i++)
#define rep(i,n) for(long long i = 0;i < n;++i)
#define rrep(i,n) for(int i=int(n-1);i>=0;i--)
#define cy cout<<"Yes"
#define cn cout<<"No"
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).begin(), (x).end()
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9+7
#define LINF 1e18+10  
using ll = long long; 

using P=pair<int,int>;
using lP=pair<ll,ll>;
using fP=pair<double,double>;
using PPI=pair<P,int>;
using Ps=pair<int,string>;

using vi=vector<int>;
using vl=vector<ll>;
using vc=vector<char>;
using vd=vector<double>;
using vs=vector<string>;
using vp=vector<P>;
using vb=vector<bool>;
using vvi =vector<vector<int>>;
using vvl =vector<vector<ll>>;
using vvd=vector<vector<double>>;
using vvc=vector<vector<char>>;
using vvp =vector<vector<P>>;
using vvb=vector<vector<bool>>;
template <typename T>
bool chmax(T &a, const T b){if(a < b){a = b; return true;} return false;}
template <typename T>
bool chmin(T &a, const T b){if(a > b){a = b; return true;} return false;}

//const ll mod=998244353;
const ll mod=1e9+7;
const ll MAX=300000;

//////////////////////////////////////

int main(){
    int n;
    cin>>n;
    string s;
    cin>>s;
    int rc=0,wc=0;
    rep(i,n){
        if(s[i]=='W')wc++;
        else rc++;
    }
    int ra=0,wa=0;
    rep(i,rc){
        if(s[i]=='W')wa++;
    }
    arep(i,rc,n){
        if(s[i]=='R')ra++;
    }
    cout<<max(ra,wa)<<endl;
    return 0;

}

Measured memory_bytes: 3736
N,D = [int(a) for a in input().split()]
X = []
Y = []
for i in range(N):
  x,y = [int(a) for a in input().split()]
  X.append(x)
  Y.append(y)
c = 0
for i in range(N):
  if (X[i]**2+Y[i]**2)**(1/2) <= D:
    c += 1
print(c)

Measured memory_bytes: 24972
<?php
[$X] = ints();
$A = 100;
for($i = 0; $i < 1000000; $i++){
    $A+=intdiv($A, 100);
    if($A>=$X){
        echo $i+1;
        exit;
    }
}

function str(){return trim(fgets(STDIN));}
function ints(){return array_map('intval',explode(' ',trim(fgets(STDIN))));}
function int(){return intval(trim(fgets(STDIN)));}
function chmax(&$a,$b){if($a<$b){$a=$b;return 1;}return 0;}
function chmin(&$a,$b){if($a>$b){$a=$b;return 1;}return 0;}
function popcount($x){$c=0;while($x){$x&=$x-1;++$c;}return$c;}
function o(...$val){
    if(count($val)==1)$val=array_shift($val);
    echo debug_backtrace()[0]['line'].")";
    if(is_array($val)){
        if(count($val) == 0)echo "empty array";
        elseif(!is_array(current($val)))echo "array: ",implode(" ", addIndex($val)),"\n";
        else{
            echo "array:array\n";
            if(isCleanArray($val))foreach($val as $row)echo implode(" ", addIndex($row)),"\n";
            else foreach($val as $i => $row)echo "[".$i."] ".implode(" ", addIndex($row)),"\n";
        }
    }else echo $val."\n";
}
function addIndex($val){if(!isCleanArray($val)){$val = array_map(function($k, $v){return $k.":".$v;}, array_keys($val), $val);}return $val;}
function isCleanArray($array){$clean=true;$i = 0;foreach($array as $k=>$v){if($k != $i++)$clean = false;}return $clean;}
// 座圧対象の配列 -> [圧縮された配列,復元用のMap,圧縮用のMap]
function atsu($array){$a = array_flip($array);$fuku=array_flip($a);sort($fuku);$atsu = array_flip($fuku);foreach($array as $i=>$val)$array[$i]=$atsu[$val];return [$array, $fuku, $atsu];}
function gcdAll($array){$gcd=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$gcd=gcd($gcd,$array[$i]);}return $gcd;}
function gcd($m, $n){if(!$n)return $m;return gcd($n, $m % $n);}
function lcmAll($array){$lcm=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$lcm=lcm($lcm,$array[$i]);}return $lcm;}
function lcm($a, $b) {return $a / gcd($a, $b) * $b;}

function dfs($current, $from = -1, $d = 0){
  global $G, $distance;
  $distance[$current] = $d;
  foreach($G[$current] as $to){
    if($from != $to){
      dfs($to, $current, $d + 1);
    }
  }
}

function loadTree($N = false){
  if($N === false)$N = $GLOBALS['N'];
  return loadGraph($N, $N-1);
}
function loadGraph($N = false, $M = false, $both = true){
  if($N === false)$N = $GLOBALS['N'];
  if($M === false)$M = $GLOBALS['M'];
  $G = array_fill(1, $N, []);
  for($i = 0; $i < $M; $i++){
    $values = ints();
    if(count($values) == 2){
      list($a, $b) = $values;
      $G[$a][] = $b;
      if($both)$G[$b][] = $a;
    }else{
      list($a, $b, $d) = $values;
      $G[$a][] = [$b, $d];
      if($both)$G[$b][] = [$a, $d];
    }
  }
  return $G;
}


Measured memory_bytes: 16124
n=int(input())
t1=int(input())
t2=int(input())

if t1 >= t2:
    m1, i1 = t1, 1
    m2, i2 = t2, 2
else:
    m1, i1 = t2, 2
    m2, i2 = t1, 1

for i in range(2, n):
    idx = i + 1
    v = int(input())
    if m1 < v:
        m2, i2 = m1, i1
        m1, i1 = v, idx
    elif m2 < v:
        m2, i2 = v, i1
        i2 = idx

for i in range(0, n):
    if m1 == m2:
        print(m1)
    elif i+1 == i1:
        print(m2)
    else:
        print(m1)


Measured memory_bytes: 4668
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
		int intSize = scan.nextInt();
		int intMin = 0;
		int intMax = 0;
		long longSum = 0;
		int intTemp = 0;
		for(int i = 0; i < intSize; i++){
			intTemp = scan.nextInt();
			longSum += intTemp;
			if(intMin == 0){
				intMin = intTemp;
				intMax = intTemp;
			}
			if(intMax < intTemp){
				intMax = intTemp;
			}
			if(intTemp < intMin){
				intMin = intTemp;
			}
		}
		scan.close();
		System.out.println(intMin + " " + intMax + " " + longSum);
	}
}

Measured memory_bytes: 31756
//██████╗ ██╗   ██╗██╗      ██████╗ ███████╗
//██╔══██╗██║   ██║██║      ██╔══██╗██╔════╝
//██████╔╝██║   ██║██║█████╗██║  ██║█████╗
//██╔══██╗██║   ██║██║╚════╝██║  ██║██╔══╝
//██║  ██║╚██████╔╝██║      ██████╔╝███████╗
//╚═╝  ╚═╝ ╚═════╝ ╚═╝      ╚═════╝ ╚══════╝
#include <bits/stdc++.h>

//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
using namespace std;

#define fst first
#define snd second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back

#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))

#define ADD_OPERATORS_IN(T, COMP) \
    bool operator < (const T& ot) const { return COMP(ot) == -1; } \
    bool operator > (const T& ot) const { return COMP(ot) == 1; } \
    bool operator == (const T& ot) const { return COMP(ot) == 0; } \
    bool operator != (const T& ot) const { return COMP(ot) != 0; }

#define ADD_OPERATORS_OUT(T, COMP) \
    bool operator < (const T& a, const T& b) const { return COMP(a, b) == -1; } \
    bool operator > (const T& a, const T& b) const { return COMP(a, b) == 1; } \
    bool operator == (const T& a, const T&b) const { return COMP(a, b) == 0; } \
    bool operator != (const T& a, const T&b) const { return COMP(a, b) != 0; }


typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector<int> vi;

mt19937_64 mt_rand(chrono::system_clock::now().time_since_epoch().count());

template<typename T1, typename T2> inline bool upmax(T1& a, T2 b) { return (a < b ? (a = b, true) : false); }
template<typename T1, typename T2> inline bool upmin(T1& a, T2 b) { return (b < a ? (a = b, true) : false); }

const int maxn = (int) 1e6+10;
const int maxlog = 21;
const int base = 998'244'353;
const ld eps = (ld) 1e-9;
const ld PI = acos(-1.);
const int pp = 41;
const int INF = (int) 2e9;
const ll llinf = (ll) 1e18;


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

//  freopen("input.txt", "r", stdin);
  ll a, b;
  cin >> a >> b;
  ll res = max({a + b, a - b, a * b});
  cout << res;

  return 0;
}

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

using namespace std;
#define INF 1000000001


int solve(const int &i,const vector<int> &costs,vector<int> &dp,const int &K){
    if(i==0) dp[i] = 0;
    else{
        for(int k = 1;k <= K;k++){
            if(i>=k){
                if(dp[i-k] == INF) dp[i-k] = solve(i-k,costs,dp,K);
                dp[i] = min(abs(costs[i]-costs[i-k]) + dp[i-k],dp[i]);
            }
        }
    }
    return dp[i];
}

int main()
{
    int N,K;
    cin >> N >> K;
    vector<int> costs(N,0);
    vector<int> dp(N,INF);
    for(int i = 0 ;i < N;i++){
        cin >> costs[i];
    }

    int ans = solve(N-1,costs,dp,K);
    cout << ans << "\n";

    return 0;
}



Measured memory_bytes: 8832
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii =  pair<int, int>;
using pll =  pair<long long, long long>;
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007LL;
constexpr long long INF = 1000000009LL;
#define all(x) (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define fo(i,j,n) for (int i=(j); i < (n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }


int main(){ 
    vector<int> A(3); rep(i, 3) cin >> A[i];
    sort(all(A));
    cout << A[2] - A[0] << ln;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

typedef long long ll;

ll gcd(ll x, ll y)
{
    while(y>0) {
        x = x%y;
        x^=y^=x^=y;
    }
    return x;
}

ll lcm(ll x, ll y)
{
    return (x * y)/gcd(x, y); 
}

int main()
{
    ll a,b;
    while(cin >> a >> b) {
        cout << gcd(a,b) << " " << lcm(a,b) << endl;        
    }

}

Measured memory_bytes: 0
#include<iostream>
#include<functional>
#include<utility>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;

int n, m;
string s;

int main() {
	cin >> n >> m >> s;
	s[m - 1] = s[m - 1] + 'a' - 'A';
	cout << s << endl;
}

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

using namespace std;

struct I{I(){ios::sync_with_stdio(false);cin.tie(0);}}cww;

#define rep(i,s,t) for(auto i=s;i<t;i++)

typedef long long LL;
const LL MOD=1e9+7;

template<typename T>
string debug(vector<T> v,string c){
    string res;
    for(int i=0,sz=v,size();i<sz;i++){
        if(i)res+=c;
        res+=v[i];
    }
    return res;
}

int main(){
    int N,M;
    cin>>N>>M;
    vector<int> cnt(N+1,1),exist(N+1,0);
    exist[1]=1;
    cnt[0]=0;
    while(M--){
        int x,y;
        cin>>x>>y;
        cnt[x]--;
        cnt[y]++;
        exist[y]|=exist[x];
        if(cnt[x]==0)exist[x]=0;
    }
    int sum=0;
    for(auto &it:exist)sum+=it;
    cout<<sum<<endl;
    return 0;
}


Measured memory_bytes: 1024
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	k := getNextInt(scanner)
	dp := makeGrid(n+1, k+1)
	dp[0][0] = 1
	for i := 0; i < n; i++ {
		for j := 0; j < k; j++ {
			dp[i+1][j+1].addAs(dp[i][j].mul(mint(k - j)))
			dp[i+1][j+1].addAs(dp[i][j+1].mul(mint(j + 1)))
		}
	}
	fmt.Fprintln(writer, dp[n][k])
}

func makeGrid(h, w int) [][]mint {
	index := make([][]mint, h, h)
	data := make([]mint, h*w, h*w)
	for i := 0; i < h; i++ {
		index[i] = data[i*w : (i+1)*w]
	}
	return index
}

type mint int64

func (mt mint) mod() mint {
	m := mint(1e9 + 7)
	mt %= m
	if mt < 0 {
		return mt + m
	}
	return mt
}
func (mt mint) inv() mint {
	return mt.pow(mint(0).sub(2))
}
func (mt mint) pow(n mint) mint {
	p := mint(1)
	for n > 0 {
		if n%2 == 1 {
			p.mulAs(mt)
		}
		mt.mulAs(mt)
		n >>= 1
	}
	return p
}
func (mt mint) add(x mint) mint {
	return (mt + x).mod()
}
func (mt mint) sub(x mint) mint {
	return (mt - x).mod()
}
func (mt mint) mul(x mint) mint {
	return (mt * x).mod()
}
func (mt mint) div(x mint) mint {
	return mt.mul(x.inv())
}
func (mt *mint) addAs(x mint) *mint {
	*mt = mt.add(x)
	return mt
}
func (mt *mint) subAs(x mint) *mint {
	*mt = mt.sub(x)
	return mt
}
func (mt *mint) mulAs(x mint) *mint {
	*mt = mt.mul(x)
	return mt
}
func (mt *mint) divAs(x mint) *mint {
	*mt = mt.div(x)
	return mt
}



Measured memory_bytes: 9456
X, N = gets.split.map(&:to_i)

Ary = gets.split.map(&:to_i)

best = 0
gap = Float::INFINITY
(-1..101).each do |i|
  next if Ary.include?(i)
  if (X - i).abs < gap
    gap = (X - i).abs
    best = i
  end
end

puts best


Measured memory_bytes: 14320
#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 A, B;
  cin >> A >> B;
  cout << 6 - A - B << endl;
}

Measured memory_bytes: 256
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


def ZIP(n): return zip(*(MAP() for _ in range(n)))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7


def main():
    N = INT()
    A = LIST()
    ans = 1
    A.sort()
    for x in A:
        ans *= x
        if ans > 10**18:
            print(-1)
            exit()
    print(ans)


if __name__ == '__main__':
    main()


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

const ll MOD = 1000000007;

map<ll,ll> prime_factor(ll n) {
    map<ll,ll> ret;
    for(ll i = 2LL; i * i <= n; ++i) {
        while(n % i == 0LL) {
            ++ret[i];
            n /= i;
        }
    }
    if(n != 1LL) ret[n] = 1LL;
    return ret;
}

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

long long modinv(long long a, long long m) {
    return pow_mod(a,m-2);
}



int main()
{
    int N;
    scanf("%d", &N);
    ll A[N];
    for(int i = 0; i < N; ++i) scanf("%lld", &A[i]);
    map<ll,ll> factor_List;
    for(int i = 0; i < N; ++i) {
        map<ll,ll> A_factor = prime_factor(A[i]);
        map<ll,ll>::iterator it = A_factor.begin();
        for(it; it != A_factor.end(); ++it){
            ll a = (*it).first;
            ll b = (*it).second;
            factor_List[a] = max(factor_List[a],b);
        }
    }
    ll LCM = 1LL;
    for(map<ll,ll>::iterator it = factor_List.begin(); it != factor_List.end(); ++it){
        LCM = LCM*pow_mod((*it).first, (*it).second)%MOD;
    }
    ll res = 0LL;
    for(int i = 0; i < N; ++i) {
        res += 1LL*LCM*modinv(A[i],MOD)%MOD;
        if(res >= MOD) res -= MOD;
    }
    printf("%lld\n", res);

    return 0;
}

Measured memory_bytes: 896
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
int digit(int in) {
    if(in < 10) return 1;
    else return 1 + digit(in / 10);
}

int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        int a, L;
        cin >> a >> L;
        if(L == 0) break;
        vector<int> v;
        v.resize(1005000, -1);
        v[a] = 0;
        int index = 0;
        while(true) {
            index++;
            while(digit(a) < L && a != 0) a *= 10;
            string S = to_string(a);
            string T = S;
            sort(S.begin(), S.end());
            sort(T.begin(), T.end(), greater<char>());
            a = stoi(T) - stoi(S);
            if(v[a] != -1) break;
            v[a] = index;
        }
        cout << v[a] << " " << a << " " << index - v[a] << endl;
    }
    return 0;
}


Measured memory_bytes: 7136