akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
const int inf = numeric_limits<int>::max()/3;

int main(void){
  int n,m;
  int num[500];
  bool rest[500];
  vector<vector< int >> mat;
  cin >> n >> m;
  mat.resize(n);
  for(int i = 0;i < n;++i){
    mat[i].resize(m);
    for(int j = 0;j < m;++j){
      cin >> mat[i][j];
      --mat[i][j];
    }
  }
  int res = inf;
  for(int i = 0;i < 500;++i) rest[i] = true;
  for(int k = 0;k < m;++k){
    fill(num,num+500,0);
    for(int i = 0;i < n;++i){
      for(int j = 0;j < m;++j){
        if(rest[mat[i][j]]){
          ++num[mat[i][j]];
          break;
        }
      }
    }
    int max_idx = 0;
    for(int i = 0;i < m;++i){
      if(num[i] > num[max_idx]){
        max_idx = i;
      }
    }
    res = min(res,num[max_idx]);
    rest[max_idx] = false;
  }
  cout << res << endl;
  return 0;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int64_t i = 0; i < (int64_t)(n); i++)
using vi = vector<int64_t>;
using vvi = vector<vi>;
using ll = int64_t;
using P = pair<ll,ll>;
ll INF = 1000000007;
// ll INF = 9223372036854775807;
// ll INF = 998244353;

int main() {
    // fasten cin
    cin.tie(0);
    ios::sync_with_stdio(false);
    // implement
    ll n;
    cin >> n;
    vi nums(n+1,1);
    for(ll i=2; i<=n; i++){
        for(ll j=i; j<=n; j+=i){
            nums.at(j)++;
        }
    }
    ll res = 0;
    for(ll i=1; i<=n; i++){
        res += nums.at(i)*i;
    }
    cout << res << endl;
}

Measured memory_bytes: 81292
#define _USE_MATH_DEFINES
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <string>
#include <vector>
#include <utility>
#include <complex>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <deque>
#include <tuple>
#include <bitset>
#include <limits>
#include <algorithm>
#include <array>
#include <random>
#include <complex>
#include <regex>
#include <iomanip>
using namespace std;
// typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define fcout cout << fixed << setprecision(10)
#define rep(i,n)    for(int i=0; i<(int)n; i++)
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define all(x) x.begin(), x.end()
const ll inf = 1e18;
const ll mod = 1e9 + 7;

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

// mod. m での a の逆元 a^(-1) を計算する。
ll modinv(ll a, ll m) {
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}

int main(){
    int x;
    cin >> x;
    rep(a, 1000){
        for(int b=-1000; b<1000; b++){
            if(pow(a,5) - pow(b,5) == x){
                cout << a << " " << b << endl;
                return 0;
            }
        }
    }
}



Measured memory_bytes: 4056
#include <bits/stdc++.h>
using namespace std;
//型に気を付けよう

#define rep(i,n) for(int i=0;i<n;i++)
typedef long long  ll;
typedef long double  ld;
const double pi=3.1415926535;
const ll  mod=1000000007;
//桁数指定　cout << fixed << setprecision(2)
int main() {
  int a,b,k;
  string ans="NG";
  
  cin>>k>>a>>b;
  for(int i=a;i<=b;i++){
    if(i%k==0){ans="OK";break;}
  }
  cout<<ans<<endl;
}

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

using ll = long long;

// Segment-Tree
struct SegmentTree {
  int n;
  vector<int> dat, lazy;
  
  // Constructor
  SegmentTree(int _n) {
    init(_n);
  }
  
  // 初期化
  void init(int _n) {
    n = 1;
    while (n < _n) n *= 2;
    dat.resize(2 * n - 1, 0);
    lazy.resize(2 * n - 1, 0);
  }
  
  // i番目の値を1加算
  void update(int i, int x) {
    i += n - 1; // 葉のノード
    dat[i]++;
    while (i > 0) { // 更新
      i = (i - 1) / 2;
      dat[i] = dat[2 * i + 1] + dat[2 * i + 2];
    }
  }
  
  // k番目の値を更新(RUQ)
  void eval(int k) {
    if (lazy[k] == 0) return;
    if (k < n - 1) {
      lazy[2 * k + 1] = lazy[k];
      lazy[2 * k + 2] = lazy[k];
    }
    dat[k] = lazy[k];
    lazy[k] = 0;
  }
  
  // 区間[a, b)の値をxに変更(RUQ)
  void update(int a, int b, int x, int k = 0, int l = 0, int r = -1) {
    if (r == -1) r = n;
    eval(k);
    if (a <= l && r <= b) {
      lazy[k]++;
      eval(k);
    }
    else if (a < r && l < b) {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 1, (l + r) / 2, r);
      dat[k] = dat[2 * k + 1] + dat[2 * k + 2];
    }
  }
  
  // [a, b)の最小値
  // k:ノードの番号 [l, r)がkに対応づいている
  // query(a, b, 0, 0, n)と呼ぶ
  int query(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r == -1) r = n;
    if (r <= a || b <= l) return 0;
    eval(k); // RUQ
    if (a <= l && r <= b) return dat[k];
    else {
      int vl = query(a, b, 2 * k + 1, l, (l + r) / 2);
      int vr = query(a, b, 2 * k + 2, (l + r) / 2, r);
      return vl + vr;
    }
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  
  ll n, k;
  cin >> n >> k;
  
  vector<ll> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
    a.at(i) -= k;
  }
  
  vector<ll> x(n + 1);
  for (int i = 1; i <= n; i++) {
    x.at(i) = a.at(i - 1);
    x.at(i) += x.at(i - 1);
  }
  
  set<ll> s;
  for (int i = 0; i <= n; i++) s.insert(x.at(i));
  
  vector<ll> y;
  for (auto i : s) y.emplace_back(i);
  
  SegmentTree st(s.size());
  ll ans = 0;
  for (int i = 0; i <= n; i++) {
    auto it = lower_bound(y.begin(), y.end(), x.at(i)) - y.begin();
    ans += st.query(0, it + 1);
    st.update(it, 1);
  }
  
  cout << ans << '\n';
}

Measured memory_bytes: 18544
const
  MD=1000000007;
var
  i,n:longint;
  a,ys,xg,f:array[0..200010] of int64;
begin
  readln(n);
  for i:=1 to n do read(a[i]);
  for i:=1 to n do
    begin
      if ys[a[i]]<>0 then xg[ys[a[i]]]:=i;
      ys[a[i]]:=i;
    end;
  f[1]:=1;
  for i:=1 to n-1 do
    begin
      f[i+1]:=(f[i+1]+f[i]) mod MD;
      if xg[i]>i+1 then f[xg[i]]:=(f[xg[i]]+f[i]) mod MD;
    end;
  writeln(f[n]);
end.

Measured memory_bytes: 6400
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

int main() {
  string a, b, c;
  cin >> a >> b >> c;
  if (a.back() == b[0] && b.back() == c[0]) cout << "YES";
  else cout << "NO";
  cout << endl;
  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>

#define N 100

/** テァツつケテ」ツ?ョテ・ツ?凝ヲツ閉ー */
static int n;
/** テァツつケテ」ツ?ョx テ・ツコツァテヲツィツ?*/
static double x[ N ];
/** テァツつケテ」ツ?ョy テ・ツコツァテヲツィツ?*/
static double y[ N ];
/** テ・ツ?クテ・ツ個?ゥツ?づァツつケテ」ツδ陛」ツδゥテ」ツつー */
static double v[ N ];

/** テヲツ鳴ケテ・ツ青妥、ツサツ佚」ツ?催ァツ崢エテァツキツ嗔1->p2 テ」ツ?ォテ・ツッツセテ」ツ?療」ツ?ヲテァツつケp テ」ツ?古・ツキツヲテ・ツ?エテ」ツ?ォテ・ツュツ佚・ツ慊ィテ」ツ?凖」ツつ凝」ツ?凝」ツつ津・ツ按、テ・ツョツ堙」ツ?凖」ツつ?*/
static int
isLeft (
  int p1,
  int p2,
  int p
  )
{
  /** テヲツ鳴ケテ・ツ青妥、ツサツ佚」ツ?催ァツ崢エテァツキツ嗔1->p2 テ」ツ?ィテァツつケp テ」ツ?ョテ・ツ、ツ姪ァツゥツ?*/
  const double op = ( x[ p2 ] - x[ p1 ] ) * ( y[ p ] - y[ p1 ] )
                  - ( y[ p2 ] - y[ p1 ] ) * ( x[ p ] - x[ p1 ] );

  /* テ・ツ、ツ姪ァツゥツ催」ツ?古ィツイツ?」ツ?ェテ」ツつ嘉・ツキツヲテ・ツ?エテ」ツ?ォテ・ツュツ佚・ツ慊ィ */
  return ( !!( op < 0 ) );
}

/** テヲツ慊?・ツキツヲテァツつケテ」ツつ津ヲツ篠「テァツエツ「テ」ツ?療ッツシツ古」ツ?敕」ツ?ョテ」ツつ、テ」ツδウテ」ツδ?」ツδε」ツつッテ」ツつケテ」ツつ津ィツソツ氾」ツ??*/
static int
findIndexOfLeftmost (
  void
  )
{
  int res = 0;
  int i;

  for ( i = 1; i < n; ++i )
  {
    if ( x[ i ] < x[ res ] )
      res = i;
  }

  return ( res );
}

int
main (
  int   argc,
  char *argv[ ]
  )
{
  int i;

  for ( ; ; )
  {
    int res = 0;
    int c;

    scanf ( " %d", &n );
    if ( n == 0 ) break ;
    for ( i = 0; i < n; ++i )
    {
      scanf ( " %lf,%lf", &x[ i ], &y[ i ] );
      v[ i ] = 1;
    }

    /* テヲツ慊?・ツキツヲテァツつケテ」ツ?凝」ツつ嘉・ツ?クテ・ツ個?ゥツ?づァツつケテ」ツつ津・ツ渉ウテ・ツ崢榲」ツつ甘」ツ?ァテゥツ??」ツ?ォテァツ卍コティツヲツ凝」ツ?凖」ツつ?*/
    c = findIndexOfLeftmost ( );
    for ( ; ; )
    {
      int nc = !( c ); /*< ( c == 0 ? 1 : 0 ) */

      v[ c ] = 0;

      /* ティツ?ェティツコツォテ」ツ?凝」ツつ嘉ィツヲツ凝」ツ?ヲテヲツ慊?・ツキツヲテァツつケテ」ツつ津ヲツ篠「テァツエツ「テ」ツ?凖」ツつ?*/
      for ( i = 0; i < n; ++i )
      {
        /* ティツ?ェテ・ツ按?ィツ?ェティツコツォテ」ツ?ッテヲツ篠「テァツエツ「テ・ツッツセティツアツ。テ」ツ?凝」ツつ嘉ゥツ卍、テ・ツ、ツ?*/
        if ( i == c ) continue ;

        if ( isLeft ( c, nc, i ) )
          nc = i;
      }
      /* テ、ツクツ?・ツ堕ィテ」ツ?療」ツ?淌」ツつ嘉ァツオツづ、ツコツ?*/
      if ( v[ nc ] == 0 ) break ;
      c = nc;
    }

    for ( i = 0; i < n; ++i )
      res += !!( v[ i ] );
    printf ( "%d\n", res );
  }

  return ( EXIT_SUCCESS );
}

Measured memory_bytes: 640
//   _/                _/                 _/_/_/   _/
//_/_/_/_/   _/_/   _/_/_/_/   _/_/    _/       _/_/
// _/     _/    _/   _/     _/    _/  _/_/_/     _/
//_/     _/    _/   _/     _/    _/  _/    _/   _/
// _/_/   _/_/       _/_/   _/_/      _/_/     _/
#include<iostream>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<map>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const double pi=3.14159265358979323846;
const int inf=2e9;
const ll INF=9e18;
typedef pair<int,int> P;
ll power(ll a,ll b){
	int sum=1;
	for (; b; b>>=1){
		if (b&1)	sum=1ll*sum*a%MOD;
		a=1ll*a*a%MOD;
	}
	return sum;
}
ll calc(ll n,ll x){
	ll sum=1;
	for (int i=n; i>=n-x+1; i--)	
		sum=sum*i%MOD;
	for (int i=1; i<=x; i++)
        //MODの世界では、Xで割ることとX^(MOD-2)で掛けることが等価
		sum=sum*power(i,MOD-2)%MOD;
	return sum;
}
int main() {
  cin.tie(0),cout.tie(0);
  ios::sync_with_stdio(false);
  ll x,y,n,m,ans;
  cin >> x >> y;
  if((x+y)%3!=0) {
    cout << 0 << endl;
    return 0;
  }
  n=(2*y-x)/3,m=(2*x-y)/3;
  if(n<0||m<0) {
    cout << 0 << endl;
    return 0;
  }
  ans=calc(n+m,m);
  cout << ans << endl;
}

Measured memory_bytes: 256
use std::io;
use std::str::FromStr;
use std::cmp::min;

fn input_tuple() -> (i32, i32, i32) {
    let mut line: String = String::new();
    io::stdin().read_line(&mut line);
    let vec: Vec<i32> = line.split_whitespace().map(|s| s.parse().unwrap()).collect();
    (vec[0], vec[1], vec[2])
}

fn calc_min(i: i32, j: i32, x: i32, y: i32) -> i32 {
    min(j - i, (x - i).abs() + 1 + (y - j).abs())
}

fn main() {
    let (n, x, y) = input_tuple();
    let mut ans = vec![0; n as usize];
    for i in 1..n {
        for j in (i+1)..(n+1) {
            // println!("{}, {}, {}", i, j, calc_min(i, j, x, y));            
            ans[calc_min(i, j, x, y) as usize] += 1;
        }
    }
    for i in 1..n {
        println!("{}", ans[i as usize]);
    }
}


Measured memory_bytes: 4352
package main

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

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	
	n := ScanInt(scanner)
	m := ScanInt(scanner)
	
	fmt.Println((n - 1) * (m - 1))
}

func ScanInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	i, e := strconv.Atoi(scanner.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func ScanFloat64(scanner *bufio.Scanner) float64 {
	scanner.Scan()
	f, e := strconv.ParseFloat(scanner.Text(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func ScanText(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func Max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func Min(a, b int) int {
	if a > b {
		return b
	}
	return a
}

func Abs(a int) int {
	if a < 0 {
		a *= -1
	}
	return a
}
 

Measured memory_bytes: 640
n = int(input())
a = list(map(int,input().split()))
b = list(map(int,input().split()))
c = list(map(int,input().split()))
mae=-1
count = 0
for start in range(0,n):
  if mae>-1 and start<n and a[mae] + 1 == a[start]:
    count += b[a[start] - 1] + c[a[start] - 2]
  else:
    count+=b[a[start] - 1]
  mae+=1
print(count)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

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

const long long INF = 1e18;
//const ll mod = 1000000007;
ll N, M, X;
ll ans = INF;
ll A[100][100];

int main() {
    cin >> N >> M >> X;
    vector<ll> C(N);
    for(int i = 0; i < N; i++) {
        cin >> C[i];
        for(int j = 0; j < M; j++) {
            cin >> A[i][j];
        }
    }
    for(int bits = 0; bits < (1 << N); bits++) {
        ll sum = 0;
        vector<ll> B(M);
        for(int j = 0; j < N; j++) {
            if(!(bits & (1 << j))) continue;
            sum += C[j];
            for(int k = 0; k < M; k++) {
                B[k] += A[j][k];
            }
        }
        bool ok = true;
        for(int i = 0; i < M; i++) {
            if(B[i] < X) ok = false;
        }
        if(ok) chmin(ans, sum);
    }
    if(ans == INF) ans = -1;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3644
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);    
    int a,b,c;
    cin>>a>>b>>c;
    int n=0;
    if(a==b){
        n++;
    }
    if(c==b){
        n++;
    }
    if(a==c){
        n++;
    }
    if(n==1){
        cout<<"Yes";
    }else{
        cout<<"No";
    }
}

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

using namespace std;
typedef long long LL;
const int MOD = 1e9 + 7;
LL Power_mod(LL a, LL b, LL p = MOD)
{
	LL res = 1;
	while(b!=0)
	{
		if(b&1) res = (res*a)%p;
		a = (a*a)%p;
		b >>= 1;
	}
	return res;
}
LL Comb(LL a,LL b, LL p = MOD)
{
	if(a < b) return 0;
	if(a == b) return 1;
	if(b > a-b) b = a-b;
	LL ans = 1, ca = 1, cb = 1;
	for(LL i=0; i<b; ++i)
	{
		ca = (ca*(a-i))%p;
		cb = (cb*(b-i))%p;
	}
	ans = (ca*Power_mod(cb, p-2, p))%p;
	return ans;
}
LL Lucas(int n, int m, int p = MOD)
{
	LL ans = 1;
	while(n && m && ans)
	{
		ans = (ans * Comb(n%p, m%p, p))%p;
		n /= p;
		m /= p;
	}
	return ans;
}
int main()
{
    LL n, a, b, res;
    cin >> n >> a >> b;
    res = (Lucas(n, a) + Lucas(n, b)) % MOD;
    LL ans = Power_mod(2, n);
    cout << (ans - res - 1 + MOD) % MOD << endl;
	return 0;
}


Measured memory_bytes: 256
// Etavioxy
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#include<cmath>
#define il inline
#define ll long long
#define rep(i,s,t) for(register int i=(s);i<=(t);i++)
#define rev_rep(i,s,t) for(register int i=(s);i>=(t);i--)
#define each(i,u) for(int i=head[u];i;i=bow[i].nxt)
#define file(s) freopen(s".in" ,"r",stdin),freopen(s".out","w",stdout)
#define pt(x) putchar(x)
using namespace std;
il int ci(){
	register char ch;int f=1;
	while(!isdigit(ch=getchar()))f=ch=='-'?-1:1;
	register int x=ch^'0';
	while(isdigit(ch=getchar()))x=(x*10)+(ch^'0');
	return f*x;
}

enum{N=200023};

struct Edge{ int nxt,to; } bow[N*2];
int head[N],tot_e;
il void add(int x,int y){
	tot_e++;
	bow[tot_e<<1]=(Edge){head[x],y};
	bow[tot_e<<1|1]=(Edge){head[y],x};
	head[y]=(head[x]=tot_e<<1)|1;
}

struct sth{
	int a,b;
	il bool operator<(const sth&e)const{
		return a<e.a;
	}
}d[N];
int a[N],b[N];

bool vis[N];
int seq[N],tot_seq;
void dfs(int u){
	vis[u] = 1;
	each(i,u){
		int v = bow[i].to;
		if( vis[v]==1 ) continue;
		dfs(v);
		if( b[v]==1 ){
			seq[++tot_seq] = i>>1;
			b[v]^=1;
			b[u]^=1;
		}
	}
}

int main(){
	int n=ci(), m=ci();
	rep(i,1,n) d[i].a=ci(), d[i].b=ci();
//	rep(i,1,n+1) printf("%d ",d[i].b); pt('\n');
	sort(d+1,d+n+1);
	rep(i,1,n) a[i]=d[i].a;
	rev_rep(i,n+1,1) b[i]=d[i-1].b^d[i].b;
//	rep(i,1,n+1) printf("%d ",b[i]); pt('\n');
	rep(i,1,m){
		int l=ci(), r=ci()+1;
		l=lower_bound(a+1,a+n+1,l)-a;
		r=lower_bound(a+1,a+n+1,r)-a;
		add(l,r);
	}
	rep(i,1,n+1) if( vis[i]==0 ) dfs(i);
	bool fail = 0;
	rep(i,1,n+1){
		if( b[i] ) fail=1;
	}
	if( fail ) puts("-1");
	else{
		printf("%d\n",tot_seq);
		sort(seq+1,seq+tot_seq+1);
		rep(i,1,tot_seq) printf("%d ",seq[i]);
		pt('\n');
	}
	return 0;
}


Measured memory_bytes: 10240
fn main() {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).unwrap();

    let mut ws = s.trim().split_whitespace();
    let a: i64 = ws.next().unwrap().parse().unwrap();
    let b: i64 = ws.next().unwrap().parse().unwrap();

    if a<10 && b<10 {
        println!("{}", a*b);
    } else {
        println!("-1");
    }

}

Measured memory_bytes: 4352
#include <stdio.h>
int main() {

  int N,i,c=0,flag=1,b;
  int A[100];

  scanf("%d",&N);
  for(i=0;i<N;i++) {
    scanf("%d",&A[i]);
  }

  while(flag) {
    flag=0;

    for(i=N-1;i>=1;i--) {
      if(A[i]<A[i-1]) {
	b = A[i];
	A[i] = A[i-1];
	A[i-1] = b;
	c++;
	flag=1;
      }
    }
  }

  for(i=0;i<N;i++) {
    if(i>0) printf(" "); 
    printf("%d",A[i]);
  }
  printf("\n");
  printf("%d\n",c);

  return 0;
}

Measured memory_bytes: 604
import java.util.*;
import java.io.*;
import java.math.*;


import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;


public class Main{ 


    static long mod=1000000007;
    //  static int dx[]={1,-1,0,0};
    //  static int dy[]={0,0,1,-1};
    // static int dx[]={1,-1,0,0,1,1,-1,-1};
    // static int dy[]={0,0,1,-1,1,-1,1,-1}; 

public  static void main(String[] args)   throws Exception, IOException{

    
    Reader sc = new Reader(System.in);
    PrintWriter out=new PrintWriter(System.out);

//  int n=sc.nextInt();  
    long n=sc.nextLong();
    long x=sc.nextLong();

    out.println(3*(n-gcd(n,x)));
    out.flush();
}

 static long gcd(long a, long b){
                 if(min(a,b) == 0) return max(a,b);
                 return gcd(max(a, b) % min(a,b), min(a,b));
         }

static void db(Object... os){

    System.err.println(Arrays.deepToString(os));

}

static boolean validpos(int x,int y,int r, int c){
    
    return x<r && 0<=x && y<c && 0<=y;
    
}
 
static boolean bit(long x,int k){
    // weather k-th bit (from right) be one or zero
    return  ( 0 < ( (x>>k) & 1 )  )  ? true:false;
}

}







class Reader
{ 
    private BufferedReader x;
    private StringTokenizer st;
    
    public Reader(InputStream in)
    {
        x = new BufferedReader(new InputStreamReader(in));
        st = null;
    }
    public String nextString() throws IOException
    {
        while( st==null || !st.hasMoreTokens() )
            st = new StringTokenizer(x.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException
    {
        return Integer.parseInt(nextString());
    }
    public long nextLong() throws IOException
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}



Measured memory_bytes: 7764
N = int(input())
zorome = list(range(N))
judgement = 1
for i in range(1,N+1):
    a,b = map(int,input().split())
    if a==b:
        zorome[i-1]=0
    else:
        zorome[i-1]=1
    if i > 2:
        if zorome[i-1]==zorome[i-2] and zorome[i-2]==zorome[i-3]:
            if zorome[i-1] == 0:
                judgement = 0
if judgement == 0:
    print("Yes")
else:
    print("No")

Measured memory_bytes: 62484
#include<bits/stdc++.h>
#define fi        first
#define se        second
#define PI        acos(-1)
#define LC(a)     ((a<<1))
#define RC(a)     ((a<<1)+1)
#define MID(a,b)  ((a+b)>>1)
#define mem(a, b) memset(a, b, sizeof(a))
#define IOS()     std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<ll,ll>   PLL;
typedef pair<ll,int>  PLI;
const int INF = 0X3F3F3F3F;
const int MIN = -(1<<30);
const ll N = 1e5+7;
int main()
{
	IOS();
	string s,t;
	int a,b;
	cin>>s>>t>>a>>b;
	map<string,int> mp;
	mp[s] = a;
	mp[t] = b;
	string u;
	cin>>u;
	mp[u]--;
	cout<<mp[s]<<" "<<mp[t]<<endl;	
	
	
	
	
	
	
	
	
	
	
	

	
	
	return 0;
}



Measured memory_bytes: 256
N = int(input())
mod = 10**9 + 7

a = [(int(input()), 0) for _ in range(N)]
b = [(int(input()), 1) for _ in range(N)]
ab = sorted(a+b, key = lambda x: x[0])
ans = 1
stack = []
for i, j in ab:
    if stack and stack[-1] != j:
        ans = ans * len(stack) %mod
        stack.pop()
    else:
        stack.append(j)
print(ans)

Measured memory_bytes: 30264
class Solve:
    def __init__(self):
        self.M, self.N = map(int, input().split())
        self.bs = [int(input()) for i in range(self.N)]

        self.S = min(sum(self.bs), 1500000)
        self.l, self.r = 0, self.S        

    def check(self, W):
        w = W
        i = 1
        for b in self.bs:
            if(w >= b):
                w -= b
            elif(W >= b):
                if(i >= self.M):
                    return False
                i += 1
                w = W - b
            else:
                return False
        return True

    def solve(self):
        while self.r - self.l > 1:
            m = (self.l + self.r) // 2
            if self.check(m):
                self.r = m
            else:
                self.l = m
        return self.r

while True:
    s = Solve()
    if s.M == 0 and s.N == 0:
        break
    print(s.solve())

Measured memory_bytes: 7760
// STRIVER_79 
// Lets goto the next level 
// AIM 6* at CC and CM at CF *__* 

#include <algorithm>
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define mod (int)(1e9 + 21)
#define all(a) a.begin(),a.end()
#define for0(i, n) for (int i = 0; i < n; i++)
#define for1(i, n) for (int i = 1; i <= n; i++)
#define loop(i,a,b) for (int i = a; i < b; i++)
#define bloop(i,a,b) for (int i = a ; i>=b;i--)
#define tc(t) int t; cin >> t; while (t--)
#define int long long
#define ll long long 
#define pb emplace_back
#define fio ios_base::sync_with_stdio(false), cin.tie(NULL)
#define in(x) scanf("%intd", &x)
#define rr return 0
#define prec(n) fixed<<setprecision(n)
#define maxheap priority_queue<int>
#define minheap priority_queue<int, vector<int>, greater<int> >
#define inf (int)(1e18)
#define ini(a, i) memset(a, i, sizeof(a))
#define fi first
#define se second
#define kitna se
#define endl "\n"
#define pi pair<int, int>
#define LL long long 
const int MAXN = (int)((1e5) + 10);
int gcd(int a,int b) { if (a == 0) return b; return gcd(b%a, a);}
int max(int a,int b){if(a>b) return a; else return b;}
int min(int a,int b){if(a<b) return a; else return b;}  
bool isPrime(int N){ for(int i=2;i*i<=N;++i){if(N%i==0) return false;}return true;}
int cbrt(int x){ int lo=1,hi=min(2000000ll,x);while(hi-lo>1){int mid=(lo+hi)/2;if(mid*mid*mid<x){lo=mid;}else hi=mid;}if(hi*hi*hi<=x) return hi;else return lo;} 
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const int nax =  (int)(1*1e5+10);

// int SPF[N+5];

// void SOF()
// {
//     SPF[1] = 1;
//     for (int i=2; i<N; i++)
//         SPF[i] = i;
//     for (int i=4; i<N; i+=2)
//         SPF[i] = 2;
 
//     for (int i=3; i*i<N; i++)
//         {
//         if (SPF[i] == i)
//             {
//             for (int kon=i*i; kon<N; kon+=i)
//                 if (SPF[kon]==kon)
//                    SPF[kon] = i;
//             }
//         }
// }
int n,m;
string s1,s2;
int dp[3005][3005];







signed main(){

int t=1; 
//cin >> t;
// string s; 
// getline(cin,s);

while(t--)
{
	ini(dp,0); 
	cin>> s1 >> s2;
	n = s1.size();
	m = s2.size(); 
	for0(i,n+1)
	{
		for0(j,m+1)
		{
			if (i == 0 || j == 0)  dp[i][j] = 0; 
            else if (s1[i-1] == s2[j-1])  dp[i][j] = dp[i-1][j-1] + 1; 
            else dp[i][j] = max(dp[i-1][j], dp[i][j-1]); 
		}
	}

	//cout << dp[n][m];  
	vector<char>vec;
	int index = dp[n][m];

	 int i = n, j = m; 
     while (i > 0 && j > 0) 
     {      
       if (s1[i-1] == s2[j-1]) 
       { 
          vec.push_back(s1[i-1]); 
          i--; j--; index--;  
       }   
       else if (dp[i-1][j] > dp[i][j-1]) 
          i--; 
       else
          j--; 
     }

  bloop(i,vec.size()-1,0) cout << vec[i];






}

rr;
}

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

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

int main(){
  int X;
  cin >> X;
  for(;;X++){
    if(isPrime(X)){
      cout << X << "\n";
      return 0;
    }
  }
  return 0;
}

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

int main(){
  int N;
  int A[100000];
  cin >> N;
  
  int s=100001,l=0;
  for(int i=0;i<N;i++){
    cin >> A[i];
    if(A[i]>l) l=A[i];
    if(A[i]<s) s=A[i];
  }
  if(l-s==0){
    if(l+1==N || l*2<=N)
      cout << "Yes" << endl;
    else cout << "No";
    return 0;
  }else if(l-s==1){
    int C=l;
    int uni=0;
    for(int i=0;i<N;i++){
      if(A[i]==s) uni++;
    }
    if(C>uni && (C-uni)*2<=N-uni)
      cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
  }else{
    cout << "No" << endl;
  }
  
  return 0;
}

Measured memory_bytes: 640
//保留所有权利。
#include<iostream>
#include<stdio.h>//printするよ
#include<vector>
#include<algorithm>//辞書順はnext_permutationだよ
#include<set>
#include<string>
#include<sstream>
#include<string.h>
#include<time.h>
#include<random>
#include<cmath>
#include<math.h>
#include<queue>
#include<time.h>
#include<iomanip>
#include<utility>//swapで数値交換するよ
//END OF LIBRARIES.
#define rt "\n"//改行を最速化させるよ
#define rep(i,n) for(int i=0;i<n;i++)
#define rop(i,n) for(int i=1;i<=n;i++)
#define drep(i,n) for(int i=n-1;0<=i;i--)
#define yes(ans) if(ans)cout<<"yes"<<rt;else cout<<"no"<<rt;
#define Yes(ans) if(ans)cout<<"Yes"<<rt;else cout<<"No"<<rt;
#define YES(ans) if(ans)cout<<"YES"<<rt;else cout<<"NO"<<rt;
#define sec(a,b,ans) if(ans)cout<<a<<rt;else cout<<b<<rt;
#define vcin(v) for (int i = 0; i < (v).size(); i++) { cin >> (v)[i]; }
#define sort(s) sort(s.begin(),s.end())
#define reve(s) reverse(s.begin(),s.end())
#define asas int ans=0
#define llcncn llint cnt=0
#define llasas llint ans=0
#define cncn int cnt=0
#define smsm int sum=0
#define str srting
#define v vector
#define please return
#define AC 0//おまじないだよ
#define Rapid cin.tie(0);ios::sync_with_stdio(false)
//END OF DEFINE.
using namespace std;
typedef vector<int> vint;
typedef vector<string> vstr;
typedef vector<char> vcha;
typedef vector<double> vdou;
typedef long long int llint;
typedef pair<int, int> pint;
typedef pair<llint, llint> pllint;
typedef vector<llint> vllint;
typedef vector<pint> vpint;
typedef vector<pair<llint, llint>> vpllint;
typedef vector<vector<int>> vvint;
typedef vector<vector<llint>> vvllint;
typedef vector<vector<string>> vvstr;
typedef vector<vector<pint>> vvpint;
typedef vector<bool> vbool;
//printf("%02d:%02d:%02d\n", h, m, s);・・・2文字は埋める・h、m、ｓ
//END OF TYPEDEF.

//最大公約数（GCD）を求めるよ
//最小公倍数（LCM）は＜＜　A × B ÷ GCD　 ＞＞で求まるよ

long long GCD(long long a, long long b) {
	if (b == 0) return a;
	else return GCD(b, a % b);
}
long long LCM(long long a, long long b) {
	return a * b / GCD(a, b);
}

//for A以上B未満でI探索 for (int I = A; i <= B; i++)
//string s = to_string(i);
int a = 0, b = 0, x = 0, y = 0, z = 0, n = 0;
string s = ""; asas; cncn;

int main(void) {
	cin >> a >> b;
	ans = max((a + b), (a - b));
	z = max(ans, (a * b));
	cout << z << rt;
	please AC;
}

Measured memory_bytes: 256
import queue
S = list(input())

stack = []

for i in S:

    #Sの今の要素がBの時
    if i=="B":
        if len(stack)==0:
            continue
        else:
            stack.pop()
    else:
        #Sの今の要素が文字の時
        stack.append(i)
str = ""
for i in range(len(stack)):
    str = str+stack[i]
print(str)

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

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

using namespace std;

static const ll INF = 1e15;

template<typename T>
static inline void chmin(T & ref, const T  value) {
    if (ref > value) ref = value;
}

template<typename T>
static inline void chmax(T & ref, const T value) {
    if (ref < value) ref = value;
}

int main() {
    vector<ll> ans;
    while (true) {
        int n;
        cin >> n;
        if (n == 0) break;
        vector<ll> ps(n);
        rep(i, n) cin >> ps[i];

        vector<vector<ll>> dp(n, vector<ll>(n, 0));
        for (int i = 0; i < n - 1; i++) {
            if (abs(ps[i] - ps[i + 1]) < 2) {
                dp[i][i + 1] = 2;
            }
        }
        for (int w = 4; w <= n; w += 2) {
            for (int i = 0; i + w - 1 < n; i++) {
                for (int split = 2; split < w; split += 2) {
                    if (dp[i][i + split - 1] == split && dp[i + split][i + w - 1] == w-split) {
                        dp[i][i + w - 1] = w;
                    }
                    if (dp[i][i + w - split - 1] == w - split && dp[i + w - split][i + w - 1] == split) {
                        dp[i][i + w - 1] = w;
                    }
                }
                if (dp[i + 1][i + w - 2] == w - 2) {
                    if (abs(ps[i] - ps[i + w - 1]) < 2) {
                        dp[i][i + w - 1] = w;
                    }
                }
            }
        }
        vector<vector<ll>> dp2 = dp;
        for (int length = 1; length < n; length++) {
            for (int i = 0; i + length < n; i++) {
                for (int j = i; j +1<= i+length; j++) {
                    chmax(dp2[i][i + length], dp2[i][j] + dp2[j + 1][i + length]);
                }
            }
        }
        ans.push_back(dp2[0][n - 1]);
    }
    for (int index = 0; index < ans.size();index++) {
        cout << ans[index] << endl;
    }
    return 0;
}


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


bool isContained(map<string, int> mp, string key){
    for(map<string,int>::iterator itr = mp.begin(); itr != mp.end(); ++itr){
        if( itr->first == key ){
            return true;
        }            
    }
    return false;
}

int main(void){
    int N,M;
    int max=0;
    vector<string> N_card;
    vector<string> M_card;
    string a;
    map<string,int> mp;

    cin >> N;
    for (int i=0; i<N; i++){
        cin >> a;
        N_card.push_back(a);
    }
   cin >> M;
    for (int i=0; i<M; i++){
        cin >> a;
        M_card.push_back(a);
    }


    for (int i=0; i < N; i++){
        if (isContained(mp,N_card[i])){
            mp[N_card[i]]++;
        }
        else{
            mp[N_card[i]] = 1;
        }
    }
    for(int i=0; i < M; i++){
        if (isContained(mp,M_card[i])){
            mp[M_card[i]]--;
        }
        else{
            mp[M_card[i]] = 0;
        }
    }
    for (map<string,int>::iterator itr = mp.begin(); itr!=mp.end(); itr++){
        if(itr ->second > max){
            max = itr -> second;
        }
    }
    
    cout << max;
   
return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
#include <stdio.h>
using namespace std;

#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 FILL(Itr,n) fill((Itr).begin(),(Itr).end(),n)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
#define LBOUND(Itr,val) lower_bound((Itr).begin(),(Itr).end(),(val))
#define UBOUND(Itr,val) upper_bound((Itr).begin(),(Itr).end(),(val))
typedef long long ll;
#define MOD 1000000007

vector<int> G[100010];
int dep[100010];

bool cmp(int a, int  b){
    return dep[a] > dep[b];
}

int dfs(int v){
    if(G[v].size()==0)return dep[v]=0;
    REP(i,G[v].size())dfs(G[v][i]);
    sort(G[v].begin(),G[v].end(),cmp);
    REP(i,G[v].size())dep[v] = max(dep[v],dep[G[v][i]]+i+1);
    return dep[v];
}

int main(){
    
    int N; cin>>N;
    REP(i,N-1){
        int a; cin>>a;
        G[a].push_back(i+2);
    }
    
    cout<<dfs(1)<<endl;
    
    return 0;
}


Measured memory_bytes: 10752
#include<stdio.h>
#include<string>
#include<math.h>
#include<stdlib.h>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<ctime>
#include<complex>
#include<list>
using namespace std;

const int N=1e5+5;
const int INF=123456789;
int n,k;
int ver[N],L[N],R[N];
bool ok=true;
vector<int> nei[N];

void check(int x,int p){
	if(ver[x]!=-INF) L[x]=R[x]=ver[x];
	for(int i=0;i<nei[x].size();i++){
		int to=nei[x][i];
		if(to==p) continue;
		check(to,x);
		if(L[to]!=-INF){
			
			if(L[x]==-INF){
				L[x]=L[to]-1;
				R[x]=R[to]+1;
			}else{
				if(abs(L[x])%2==abs(L[to])%2) ok=false;
				L[x]=max(L[x],L[to]-1);
				R[x]=min(R[x],R[to]+1);
			}
		}
	}
	if(L[x]>R[x]) ok=false;
}

void make(int x,int p){
	if(p==-1){
		if(ver[x]==-INF) ver[x]=L[x];
	}
	for(int i=0;i<nei[x].size();i++){
		int to=nei[x][i];
		if(to==p) continue;
		if(ver[to]==-INF){
			if(L[to]<=ver[x]-1&&ver[x]-1<=R[to]) ver[to]=ver[x]-1;
			else ver[to]=ver[x]+1;
		}
		make(to,x);
	}
}

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++) ver[i]=L[i]=-INF,R[i]=INF;
	for(int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		nei[x].push_back(y);
		nei[y].push_back(x);
	}
	scanf("%d",&k);
	while(k--){
		int x,val;
		scanf("%d%d",&x,&val);
		ver[x]=val;
	}
	check(1,-1);
	if(ok){
		puts("Yes");
		make(1,-1);
		for(int i=1;i<=n;i++) printf("%d\n",ver[i]);
	}else{
		puts("No");
	}
	return 0;
}

Measured memory_bytes: 15360
def f(n5)
  r = 0
  loop do
    r += 1
    r5 = r ** 5
    if r5 == n5
      return r
    elsif r5 > n5
      return nil
    end
  end
end

X = $stdin.gets.to_i
a = 0
b = 0
loop do
  b5 = a ** 5 - X
  if b5 == 0
    b = 0
    break
  elsif b5 > 0
    if b = f(b5)
      break
    end
  else # b5 < 0
    if mb = f(-b5)
      b = -mb
      break
    end
  end
  a += 1
end
puts "#{a} #{b}"


Measured memory_bytes: 14296
s = gets.chomp.split('')

first = []
second = []

if s.count%2 == 0
	(0..s.count-1).each do |i|
		if i < s.count/2
			first << s[i]
		else
			second << s[i]
		end
	end
else
	(0..s.count-1).each do |i|
		if i < s.count/2
			first << s[i]
		elsif i == s.count/2
			first << s[i]
			second << s[i]
		else
			second << s[i]
		end
	end
end

i = 0
count = 0

second.reverse!

while i < first.count
	if first[i] != second[i]
		count += 1
	end
	i += 1
end

puts count

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

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  
  long long A, B, K;
  cin >> A >> B >> K;
  A = A - K;
  if (A < 0) {
    B = B + A;
  }
  if (A < 0) A = 0;
  if (B < 0) B = 0;
  cout << A << " " << B << '\n';
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
struct Fast {
    Fast() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} fast;
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
long long mod = 1000000007;
#define REP(i, n) for (long long i = 0; i < (n); i++)
#define rep(i, a, n) for (long long i = a; i < (n); i++)
long long modpow(long long m, long long n) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        long long t = modpow(m, n / 2);
        return (t * t) % mod;
    } else {
        return (modpow(m, n - 1) * m) % mod;
    }
}
bool bitcheck(ll x, ll y) { return (x >> y) & 1; }

template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T& val) {
    std::fill((T*)array, (T*)(array + N), val);
}
// long long mod = 998244353;
long long power(long long m, long long n) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        long long t = modpow(m, n / 2);
        return t * t;
    } else {
        return modpow(m, n - 1) * m;
    }
}

long long moddiv(long long a, long long b) { return (a * modpow(b, mod - 2)) % mod; }

const ll INF = 1LL << 60;

void print_binary(long long a) {
    for (int i = 31; i >= 0; i--) {
        cout << (a >> i & 1);
    }
    cout << endl;
}

ll nCkmod(ll n, ll k) {
    ll denomi = 1;
    for (int i = n; i > n - k; i--) {
        denomi *= i;
        denomi %= mod;
    }
    ll numera = 1;
    REP(i, k) {
        numera *= (i + 1);
        numera %= mod;
    }
    return moddiv(denomi, numera);
}
map<ll, ll> cnt;
void primeFactors(long long n) {
    while (n % 2 == 0) {
        cnt[2]++;
        n = n / 2;
    }
    for (int i = 3; i <= sqrt(n); i = i + 2) {
        while (n % i == 0) {
            cnt[i]++;
            n = n / i;
        }
    }
    if (n > 2) {
        cnt[n]++;
    }
}
int main() {
    ll n;
    cin >> n;
    vector<ll> as(n);
    REP(i, n) { cin >> as[i]; }
    ll cur = 1000;
    ll shares = 0;
    REP(i, n - 1) {
        if (as[i + 1] > as[i]) {
            shares += cur / as[i];
            cur = cur % as[i];
        }
        if (as[i + 1] < as[i]) {
            cur += shares * as[i];
            shares = 0;
        }
    }
    cout << cur + shares * as[n - 1] << endl;
}

Measured memory_bytes: 3632
#include <sstream>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>

typedef long long ll;

#define REP(i,n) for(int i=0;i<n;++i)
#define MAX_L   100010

using namespace std;

ll dp[MAX_L] = {0};

map<ll, ll> same;                       // 自分と同じ大きさの数
set<ll, greater<ll> > nextN;            // 次の数
map<ll, ll> pos;                        // この数字が出てくる一番若い場所

int main()
{
    ll N;
    cin >> N;
    for(ll i=1; i<=N; ++i) {
        ll a;
        cin >> a;
        same[a]++;
        nextN.insert(a);
        pos[a] = (pos[a] <= 0) ? i : min(pos[a], i);
    }
    auto it = nextN.begin();
    while(it != nextN.end()) {
        auto forward = it;
        ++forward;
        if(forward != nextN.end()) {
            dp[pos[*it]] += same[*it] * (*it - *forward);
            same[*forward] += same[*it];
            pos[*forward] = min(pos[*forward], pos[*it]);
        } else {
            dp[pos[*it]] += (same[*it] * (*it));
        }
        ++it;
    }
    for(ll i=1; i<=N; ++i) {
        printf("%lld\n", dp[i]);
    }

    return 0;
}


Measured memory_bytes: 18944
#include<stdio.h>
#include<string.h>
char*p,m[22][22],s[128],B[]="v>^<",c;
int main()
{
	int T,t,h,w,i,j,x,y,a,A[128]={0},dx[]={0,1,0,-1,0};
	A['D']=A['v']=1;
	A['R']=A['>']=2;
	A['^']=A['U']=3;
	A['L']=A['<']=4;
	scanf("%d",&T);
	for(t=0;t<T;++t)
	{
		scanf("%d%d",&h,&w);
		for(i=1;i<=h;++i)if(scanf("%s",m[i]+1),p=strpbrk(m[i]+1,"><^v"))y=i,x=p-m[i],a=A[*p]-1,*p='.';
		for(i=0;i<=h;++i)m[i][0]=m[i][w+1]='#';
		for(j=0;j<=w;++j)m[0][j]=m[h+1][j]='#';
		scanf("%*d%s",s);
		for(p=s;*p;++p)
		{
			if(A[*p])
			{
				a=A[*p]-1;
				if(m[y+dx[a+1]][x+dx[a]]=='.')y+=dx[a+1],x+=dx[a];
			}
			else
			{
				for(i=1;c=m[y+i*dx[a+1]][x+i*dx[a]],c!='#'&&c!='*';++i);
				if(c=='*')m[y+i*dx[a+1]][x+i*dx[a]]='.';
			}
		}
		m[y][x]=B[a];
		if(t)puts("");
		for(i=1;i<=h;puts(""),++i)for(j=1;j<=w;++j)putchar(m[i][j]);
	}
	return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
REP(i,v.size()){if(i)os<<" ";os<<v[i];}return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
REP(i,v.size()){if(i)os<<endl;os<<v[i];}return os;}

ll kaijyou(ll n){ 
                if(n==0) return 1;
                else{return n*kaijyou(n-1);}
}


int main() {
     ll n,a,b;cin>>n;
     a=b=0;
     set <ll> st;
     vector<ll>v1(n);
     vector<ll>v2(n);
     rep(i,n){
              cin>>v1[i];
     }
     rep(i,n){
              cin>>v2[i];
     }
    rep(i,n){
        st.insert(i+1);
    }
    rep(x,n){
        int id = 0;
        for (auto itr = st.begin(); itr != st.end(); itr++)
        {
            if(*itr==v1[x]){
                break;
            }
            id++;
        
        }
        st.erase(v1[x]);
        a+=id*kaijyou(n-x-1);
    }
    rep(i,n){
        st.insert(i+1);
    }
    rep(x,n){
        int id = 0;
        for (auto itr = st.begin(); itr != st.end(); itr++)
        {
            if(*itr==v2[x]){
                break;
            }
            id++;
        
        }
        st.erase(v2[x]);
        b+=id*kaijyou(n-x-1);
    }
    cout<<abs(a-b)<<endl;
}




Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const static ll INF = 1e15;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
// __uint128_t
const ll MOD = 1e9+7;



int main(){
    ll X, N; cin >> X >> N;
    set<ll> ST; 
    for(int i = 0; i < N; i++) {
        ll a; cin >> a; ST.insert(a);
    }
    ll D = INF, ans = 1;
    for(int i = 0; i <= 101; i++){
        if(ST.find(i) != ST.end()) continue;
        ll d = abs(i - X);
        if(d < D){
            D = d;
            ans = i;
        }
    }
    cout << ans << endl;

}

Measured memory_bytes: 3628
process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});
/*
process.on('SIGINT', function(){
    input_stdin_array = input_stdin.split("\n");
    main();
    process.exit();
});
*/
process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////


function main() {
	var s = readLine();	
    s = s.split(" ");
	var N = parseInt(s[0]);
	var M = parseInt(s[1]);
	var A = []; 
	var B = [];
    
	for (var i=0; i < N; i++){
		s = readLine().substr(0,N);
		A.push(s);
	}
	
	for (var i=0; i < M; i++){
		s = readLine().substr(0,M);
		B.push(s);
	}
	
	var ans = "No";
	for (var i=0; i < N-M+1; i++){
		for (var j=0; j < N-M+1; j++){
			var iguales = 1;
			for (var k=0; k < M; k++){
				if ( A[i+k].substr(j,M).localeCompare(B[k]) != 0 ){
					iguales = 0;
					break;
				}
			}
			if (iguales == 1){
				process.stdout.write("Yes");
				process.exit();
			}
		}		
	}
	
	process.stdout.write("No");
    process.exit();
}

Measured memory_bytes: 10184
#include<bits/stdc++.h>
#define rep(i,n,m) for(int i = (n); i <(m); i++)
#define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--)
using namespace std;
using ll = long long;

bool is_ok(ll n, ll s, ll b)
{
    ll sum = 0;

    while (n > 0)
    {
        sum += n % b;
        n /= b;
        // cout << n << ' ' << b << endl;
    }
    return sum == s;
}

ll solve()
{
    ll n, s;
    cin >> n >> s;

    if (s > n)
        return -1;
    else if (s == n)
        return n + 1;

    for (ll b = 2; b * b <= n + 1; b++)
    if (is_ok(n, s, b))
        return b;

    ll b = -1;
    for (ll p = 1; p * p < n + 1; p++)
    {
        ll b2 = (n - s) / p + 1;
        if (b2 == 1) continue; 
        if(is_ok(n, s, b2))
            b = b2;
    }
    return b;
}

int main()
{
    ll ans = solve();
    cout << ans << endl;
    return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> v(n);
  vector<int> counter1(100000+1, 0);
  vector<int> counter2(100000+1, 0);
  int mode1 = 0;
  int maxCount1 = 0;
  int mode2 = 0;
  int maxCount2 = 0;
  for(int i=0;i<n;i++) {
    cin >> v[i];
    if(i%2==0) {
      counter1[v[i]]++;
      if(maxCount1 < counter1[v[i]]) {
        mode1 = v[i];
        maxCount1 = counter1[v[i]];
      }
    } else {
      counter2[v[i]]++;
      if(maxCount2 < counter2[v[i]]) {
        mode2 = v[i];
        maxCount2 = counter2[v[i]];
      }
    }
  }
  if(mode1 == mode2) {
    sort(counter1.rbegin(), counter1.rend());
    sort(counter2.rbegin(), counter2.rend());
    int ans1 = n - counter1[0] - counter2[1];
    int ans2 = n - counter2[0] - counter1[1];
    if(ans1<ans2) cout << ans1 << endl;
    else cout << ans2 << endl;
  } else {
    cout << n - maxCount1 - maxCount2 << endl;
  }

  return 0;
}

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

// @author: pashka

vector<pair<int, int>> calc(string s, int c) {
    int n = s.size();
    vector<pair<int, int>> res(n + 1);
    for (int i = 0; i < n; i++) {
        if (s[i] == 'x') {
            res[i + 1] = res[i];
        } else {
            if (res[i].first == 0 || res[i].second < i - c) {
                res[i + 1] = {res[i].first + 1, i};
            } else {
                res[i + 1] = res[i];
            }
        }
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);

    int n, k, c;
    cin >> n >> k >> c;
    string s;
    cin >> s;

    auto pref = calc(s, c);
    reverse(s.begin(), s.end());
    auto suf = calc(s, c);

    for (int i = 0; i < n; i++) {
        int k1 = pref[i].first;
        int k2 = suf[n - i - 1].first;
        int s = k1 + k2;
        if (k1 > 0 && k2 > 0) {
            if (pref[i].second >= (n - 1 - suf[n - i - 1].second) - c) {
                s--;
            }
        }
        if (s < k) {
            cout << i + 1 << "\n";
        }
    }

    return 0;
}

Measured memory_bytes: 6820
#include <cstdlib>
#include <iterator>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
#include <functional>
using namespace std;

int main() {
    string s; cin >> s;
    cout << (s == "ARC" ? "ABC" : "ARC") << endl;
}


Measured memory_bytes: 3636
#include <iostream>
#include <vector>
#include <cmath>
#include <math.h>
#include <map>
#include <algorithm>
using namespace std;

int max(vector<int> x, int N) {
    int xMax = x.at(0);
    for (int i = 0; i < N; i++) {
        if (xMax < x.at(i)) xMax = x.at(i);
    }
    
    return xMax;
}

int min(vector<int> y, int M) {
    int yMin = y.at(0);
    for (int i = 0; i < M; i++) {
        if (yMin > y.at(i)) yMin = y.at(i);
    }
    
    return yMin;
}

int main() {
    int N, M, X, Y;
    cin >> N >> M >> X >> Y;
    vector<int> x(N, 0), y(M, 0);
    for (int i = 0; i < N; i++) {
        cin >> x.at(i);
    }
    for (int i = 0; i < M; i++) {
        cin >> y.at(i);
    }
    
    int xx = max(x, N);
    int yn = min(y, M);
    for (int i = xx + 1; i <= yn; i++) {
        if (i > X && i <= Y) {
            cout << "No War" << endl;
            return 0;
        }
    }
    
    cout << "War" << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
const int N=100050;
int a[N];
ll sum[N],pre[N];
int main()
{
	int n,q,i;
	scanf("%i %i",&n,&q);
	for(i=1;i<=n;i++)
	{
		scanf("%i",&a[i]);
		if(i==1) sum[i]=a[i];
		else sum[i]=sum[i-2]+a[i];
		pre[i]=pre[i-1]+a[i];
	}
	int x;
	while(q--)
	{
		scanf("%i",&x);
		int y=upper_bound(a+1,a+1+n,x)-a;
		if(y>=n) printf("%lld\n",sum[n]);
		else
		{
			int top=n,bot=1,mid,ans;
			while(top>=bot)
			{
				mid=top+bot>>1;
				int dist=a[n-mid]-x;
				int f=lower_bound(a+1,a+1+n,x-dist)-a;
				int sz=n-mid-f+1;
				if(sz<=mid) ans=mid,top=mid-1;
				else bot=mid+1;
			}
			ll ret=pre[n]-pre[n-ans]+sum[n-ans*2];
			printf("%lld\n",ret);
		}
	}
	return 0;
}

Measured memory_bytes: 3712