akhauriyash/Code-Regression huggingface dataset:
/* 実行時間1s制限の場合
  10^6 1,000,000   : 余裕をもって間に合う
  10^7 10,000,000  : おそらく間に合う
  10^8 100,000,000 : 非常にシンプルな処理でないと厳しい
 */
#define rep(i, n) for(int i = 0; i < (int)n; i++)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#include <bits/stdc++.h>
using namespace std;

typedef long long ll; // long longはデータモデル(LLP64, LP64, ...)によらず64bit.
ll gcd(ll a, ll b) {return b ? gcd(b, a%b) : a;}  // Greatest Common Divisor ユークリッドの互除法（aが長辺）
vector<pair<ll,int>> factorize(ll n) {            // a,bの公約数 = GCD(a, b)の約数
    vector<pair<ll,int>> res;
    for (ll i = 2; i*i <= n; i++) { // √nまで探せばよい
        if (n%i) continue;
        res.emplace_back(i, 0);     // 割り切れたら
        while (n%i == 0) {
            n /= i;
            res.back().second++;
        }
    }
    if (n != 1) res.emplace_back(n, 1); // この時点で1でなかったら、残りは素数.
    return res;
}
ll lcm(ll a, ll b) {return a/gcd(a,b)*b;}         // Least Commont Multiple オーバーフローしないように先に割る
ll ceil(ll a, ll b) {return (a+b-1)/b;}

const ll INF = LONG_MAX;
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(int argc, char* argv[])
{
#ifdef LOCAL
    // 以降 cin の入力元が 'xxx.txt' になる
    // std::ifstream in(argv[1]);
    //std::cin.rdbuf(in.rdbuf());
    std::ifstream in("a.txt");
    std::cin.rdbuf(in.rdbuf());
#endif

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

    if (a > b) {
        cout << a + a - 1;
    }
    else if (a < b) {
        cout << b + b - 1;
    }
    else {
        cout << a + b;
    }

    return 0;
}

/*
  - 1e9 : 10^9
  - container一般
    - 合計
      accumulate(a.begin(), a.end(), 0LL)  0LLはオーバーフロー用
  - vector
    vector<int> A(N, 0); // 0で初期化(省略可)
    - loop
      for  (int i = 0; i < A.size(); i++) {}
      for  (int elem : A) {}
    - sort
      std::sort(v.begin(), v.end());  // 昇順
      std::sort(v.begin(), v.end(), std::greater<int>()); //降順
      - vector<vector<int>> cnt(h, vector<int>(w))
    - sort(struct)
        struct st_t {
            string name;
            int p;
            bool operator<(const st_t& another) const {
                if (name == another.name)
                {
                    return p > another.p;
                }
                return name < another.name;
            }
        };
        vector<st_t> st(n);
        sort(st.begin(), st.end());
  - pair
    - pairのソートの定義 : firstが優先的に比較。同じだったらsecond。
    - pair<pair<string,int>,int> p[110];
      std::sort(p,p+a);
        こうすると、first.first -> first.second -> secondの順にソートされる
    - sort
      - sort(a.begin(), cb.end(), greater<pair<int, int>>());
  - map
    - for (auto x : map) {}  // x.first:key  x.second:value.
  - priority_queue<int> q;
    - q.top()に最大値が入っている
  - string
    - loop
      for (char& c : s) {}
 */



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
int N;
//エラトステネスの篩
bool is_prime[ 1000000 ];
vector<int> P;
void Eratosthenes( const int N )
{
    for( int i = 0; i <= N; i++ )
    {
        is_prime[ i ] = true;//初期化
    }    
    for( int i = 2; i <= N; i++ )
    {
        if( is_prime[ i ] )
        {
            for( int j = 2 * i; j <= N; j += i )
            {
                is_prime[ j ] = false;
            }
            P.emplace_back( i );
        }
    }
}
int main()
{
	string a,b;
  	cin>>a>>b;
  	cout<<b<<a<<endl;

  
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
    int h,w;
    cin>>h>>w;
    vector<vector<bool>> dict(h);
    int ch, dh, cw, dw;
    cin >> ch >> cw;
    cin >> dh >> dw;
    ch--;
    cw--;
    dh--;
    dw--;
    string s;
    for(int i = 0; i < h; i++){
        cin >> s;
        for(int j = 0; j < w; j++){
            if(s[j] == '#')
                dict[i].push_back(true);
            else dict[i].push_back(false);
        }
    }
    dict[ch][cw] = true;
    queue<pair<int,int>> q, q2;
    q.push(make_pair(ch, cw));
    int num = 0, th, tw, h1, h2, w1, w2;
    while(!q.empty()||!q2.empty()){
        while(!q.empty()){
            th = q.front().first;
            tw = q.front().second;
            q2.push(q.front());
            q.pop();
            if(th > 0){
                if(dict[th - 1][tw] == false){
                    dict[th - 1][tw] = true;
                    q.push(make_pair(th-1, tw));
                }
            }
            if(tw > 0){
                if(dict[th][tw - 1] == false){
                    dict[th][tw - 1] = true;
                    q.push(make_pair(th, tw - 1));
                }
            }
            if(th < h - 1){
                if(dict[th + 1][tw] == false){
                    dict[th + 1][tw] = true;
                    q.push(make_pair(th + 1, tw));
                }
            }
            if(tw < w - 1){
                if(dict[th][tw + 1] == false){
                    dict[th][tw + 1] = true;
                    q.push(make_pair(th, tw + 1));
                }
            }
        }
        if(dict[dh][dw] == true){
            cout << num << endl;
            return 0;
        }
        while(!q2.empty()){
            th = q2.front().first;
            tw = q2.front().second;
            h1 = th > 2 ? (th - 2) : 0;
            h2 = (th + 3 < h) ? (th + 2) : (h - 1);
            w1 = tw > 2 ? (tw - 2) : 0;
            w2 = (tw + 3 < w) ? (tw + 2) : (w - 1);
            for(int i = h1; i <= h2; i++){
                for(int j = w1; j <= w2; j++){
                    if(dict[i][j] == false){
                        q.push(make_pair(i, j));
                        dict[i][j] = true;
                    } 
                }
            }
            q2.pop();
        }
        num++;
    }
    cout << -1 << endl;
    return 0;
}

Measured memory_bytes: 3800
#include<bits/stdc++.h>
using namespace std;
int n,ans;
int main(){
	cin>>n;
	vector<int>a(n);
	for(int i=0;i<n;i++)
		cin>>a[i];
	sort(a.begin(),a.end(),greater<int>());
	for(int i=0;i<n;i++){
		int k=n-1;
		for(int j=i+1;j<n;j++){
			while(j<k&&a[i]>=a[j]+a[k])
				k--;
			if(i<j&&j<k)
				ans+=k-j;
		}
	}
	cout<<ans;
	return 0;
}

Measured memory_bytes: 256
// 2011/07/20 Tazoe

#include <iostream>
using namespace std;

int main()
{
	while(true){
		int n;
		cin >> n;
		if(n==0)
			break;

		int max_p = -1;
		int max_d = -1;
		for(int i=0; i<n; i++){
			int p, d1, d2;
			cin >> p >> d1 >> d2;
			if(d1+d2>max_d){
				max_p = p;
				max_d = d1+d2;
			}
		}

		cout << max_p << ' ' << max_d << endl;
	}

	return 0;
}

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

typedef long long ll;


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

    int win1 = ceil((double)c / (double)b);
    int win2 = ceil((double)a / (double)d);

    if (win1 == win2) {
        cout << "Yes" << endl;
    }  else if (win2 < win1) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }

    return 0;
}



Measured memory_bytes: 3632
using System;
using System.Linq;

namespace _2639
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] x = Console.ReadLine().Split().Select(int.Parse).ToArray();
            bool flag = true;
            for (int i = 0; i < 1000; i++)
            {
                if ((60 * i + x[2]) % (x[0] + x[1]) <= x[0])
                {
                    flag = false;
                    Console.WriteLine(60 * i + x[2]);
                    break;
                }
            }
            if (flag) Console.WriteLine(-1);
        }
    }
}


Measured memory_bytes: 9616
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <queue>
#include <cmath>
using namespace std;


int main() {
    int n, k;
    cin >> n >> k;
    int num = 1;
    for (int i = 0; i < n; i++) {
        if (num <= k) {
            num *= 2;
        } else {
            num += k;
        }
    }
    cout << num << endl;
}




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

using namespace std;

//#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
//static const int INF = 1LL<<61;
static const int INF = 1<<30;

bool visit[5005];
vector<int> tree, G[5005];

void dfs(int a) {
  if (visit[a]) return;
  visit[a] = true;
  for (int i = 0; i < G[a].size(); ++i) {
    dfs(G[a][i]);
  }
  tree.PB(a);
}

bool isclosed(int a) {
  if (visit[a]) return true;
  visit[a] = true;

  for (int i = 0; i < G[a].size(); ++i) {
    if (isclosed(G[a][i])) return true;
  }

  return false;
}

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

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int a, b;
    cin >> a >> b;
    G[a].PB(b);
  }

  for (int i = 1; i <= n; ++i) {
    sort(G[i].begin(), G[i].end());
    dfs(i);
  }

  bool other = true;
  for (int i = 0; i < tree.size() - 1; ++i) {
    int a = tree[i];
    other = binary_search(G[tree[i + 1]].begin(), G[tree[i + 1]].end(), a);
    if (!other) break;
  }

  for (int i = tree.size() - 1; 0 <= i; --i) {
    cout << tree[i] << endl;
  }
  cout << !other << endl;

  return 0;
}

Measured memory_bytes: 2060
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include <bitset>
#include <cstdio>
#include <set>
#include <stack>
#include <queue>
#include <cassert>
//#include <bits/stdc++.h>
#define rep(i,n) Rep(i,0,n)
#define Rep(i,k,n) for(int i=k ; i<n ; i++)
#define rep1(i,n) for(int i=1 ; i<=n ; i++)
#define vi vector<int>
#define vii vector<vector<int>>
#define mii map<int,int>
#define Sort(v) sort(v.begin(),v.end())
#define Reverse(v) reverse(v.begin(),v.end())
#define ALL(a)  (a).begin(),(a).end()

typedef long long ll;

//const int MOD = 1000000007;
//const int INF = 1<<30;
using namespace std;

string yn(bool x){
    return x ? "YES" : "NO";
}





int main(){
    string s,t; cin >> s >> t;
    int ss = (int)s.size();
    int ts = (int)t.size();
    
    for(int i=ss-ts ; i>=0 ; i--){
        
        bool ans = true;
        
        for(int j=0 ; j<ts ; j++){
            if(s[i+j] != t[j] and s[i+j] != '?') ans = false;
        }
        
        if(ans){
            rep(k,i) cout << (s[k] == '?' ? 'a' : s[k]);
            cout << t;
            for(int k=i+ts ; k<ss ; k++) cout << (s[k] == '?' ? 'a' : s[k]);
            cout << endl;
            
            return 0;
        }
    }
    
    cout << "UNRESTORABLE" << endl;
}






Measured memory_bytes: 256
import itertools

n = int(input())
x = []
y = []
for i in range(n):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)

ans = list(itertools.permutations(range(n),n))

def sqrt(a,b,c,d):
    return(((c-a)**2+(d-b)**2)**0.5)

tot = 0
for aa in ans:
    for i in range(n-1):
        tot += sqrt(x[aa[i]],y[aa[i]],x[aa[i+1]],y[aa[i+1]])
print(tot/len(ans))

Measured memory_bytes: 46320
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main{
    public static int[] dp = new int[50];

    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        for(int i = 0; i <= n; i++) dp[i] = -1;
        System.out.println(fib(n));
    }
    /*                                                                             
    public static int fib(int n){                                                  
        if(n == 0 || n == 1){                                                      
            return dp[n] = 1;                                                      
        }                                                                          
        if(dp[n] != -1) return dp[n];                                              
        return dp[n] = fib(n-1) + fib(n-2);                                        
    }                                                                              
    */
    public static int fib(int n){
        dp[0] = dp[1] = 1;
        for(int i = 2; i <= n; i++){
            dp[i] = dp[i-1] + dp[i-2];
        }
        return dp[n];
    }
}

Measured memory_bytes: 24436
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const int INF=1e9;

int main(){
    int m,d;
    cin>>m>>d;
    int ans=0;
    for(int i=1;i<=d;i++){
        if(2<=i%10&&2<=i/10&&(i%10)*(i/10)<=m){
            ans++;
        }
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using namespace std;
using P = pair<int, int>;
template<class T>
constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

template<class T>
class BIT {
    vector<T> bit;

public:
    BIT(int n): bit(vector<T>(n+1, 0)){}

    T sum(int k){
        T ret = 0;
        for (++k; k > 0; k -= (k & -k)) ret += bit[k];
        return ret;
    }

    void add(int k, T x){
        for (++k; k < bit.size(); k  += (k & -k)) bit[k] += x;
    }
};

int main() {
    int n, m;
    cin >> n >> m;

    vector<P> v(n);
    for (int i = 0; i < n; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        v[i] = make_pair(a, b+1);
    }
    sort(v.begin(), v.end(), [](P a, P b){ return a.second - a.first < b.second - b.first; });
    BIT<int> bit(m+3); int cur = 0;
    for (int k = 1; k <= m; ++k) {
        while (cur != n && v[cur].second-v[cur].first <= k){
            bit.add(v[cur].first, 1); bit.add(v[cur].second, -1);
            cur++;
        }
        int ans = n-cur;
        for (int i = k; i <= m; i += k) {
            ans += bit.sum(i);
        }
        cout << ans << "\n";
    }
    return 0;
}


Measured memory_bytes: 3712
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk) {
    var args = chunk.split(' ');
    var length = parseInt(args[0]);
    var width = parseInt(args[1]);
    var area = length * width;
    var perimeter = 2 * (length + width);
    console.log(area, perimeter);
});

Measured memory_bytes: 7648
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#define ll long long int
using namespace std;

int main() {
	int n, X, Y, Z, num=0;
	cin >> X >> Y >> Z >> n;
	for (int i = 0; i < 7; i++){
		if (X >= Y){
			Y = 2 * Y;
			num++;
		}
	}
	for (int i = 0; i < 7; i++){
		if (Y >= Z){
			Z = 2 * Z;
			num++;
		}
	}
	if (num > n){
	cout << "No" << endl;
	}
	else{
		cout << "Yes" << endl;
	}

	return 0;
}

Measured memory_bytes: 3612
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <complex>
#include <utility>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <bitset>
#include <ctime>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <cassert>
#include <cstddef>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <sstream>
#include <fstream>
#include <chrono>
#include <random>

using namespace std;
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define FOR(i, a, b) for (int (i) = (a); (i) < (b); (i)++)
#define RREP(i, a) for(int (i) = (a) - 1; (i) >= 0; (i)--)
#define FORR(i, a, b) for(int (i) = (a) - 1; (i) >= (b); (i)--)
#define DEBUG(C) cerr << #C << " = " << C << endl;
using LL = long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<LL>;
using VVL = vector<VL>;
using VD = vector<double>;
using VVD = vector<VD>;
using PII = pair<int, int>;
using PDD = pair<double, double>;
using PLL = pair<LL, LL>;
using VPII = vector<PII>;
template<typename T> using VT = vector<T>;
#define ALL(a) begin((a)), end((a))
#define RALL(a) rbegin((a)), rend((a))
#define SORT(a) sort(ALL((a)))
#define RSORT(a) sort(RALL((a)))
#define REVERSE(a) reverse(ALL((a)))
#define MP make_pair
#define FORE(a, b) for (auto &&a : (b))
#define FIND(s, e) ((s).find(e) != (s).end())
#define EB emplace_back

const int INF = 1e9;
const int MOD = INF + 7;
const LL LLINF = 1e18;

int N, M;
VI x;
LL ans = 0;

int main(void) {
    scanf("%d%d", &N, &M);
    x.resize(N);
    for_each(ALL(x), [](int &e){scanf("%d", &e);});
    VVI modset(M);
    VI modcnt(M);
    FORE(e, x) {
        modset[e % M].EB(e);
    }
    REP(i, M) {
        modcnt[i] = modset.size();
        SORT(modset[i]);
    }
    FOR(i, 1, ((M - 1) / 2) + 1) {
        if (M % 2 == 0 && i == M / 2) continue;
        int buf = min(modset[i].size(), modset[M - i].size());
        ans += buf;
        modcnt[i] = modcnt[M - i] = buf;
    }
    if (M % 2 == 0) {
        int buf = modset[M / 2].size() / 2;
        modcnt[M / 2] = buf * 2;
        ans += buf;
    }
    int buf = modset[0].size() / 2;
    modcnt[0] = buf * 2;
    ans += buf;
    REP(i, M) {
        int cnt = 0;
        int pre = -1;
        int ok = (int)modset[i].size() - modcnt[i];
        FORE(e, modset[i]) {
            assert(ok >= cnt);
            if (ok - cnt <= 1) break;
            if (pre == e) {
                ans++;
                pre = -1;
                cnt += 2;
            } else {
                pre = e;
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 6528
using System;
using System.Collections.Generic;
using System.Text;

namespace ABC0613
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] inp = Console.ReadLine().Split(' ');
            int ans = int.MaxValue;
            if(inp[1]=="0")
            {
                ans = int.Parse(inp[0]);
            }
            else
            {
                int x = int.Parse(inp[0]);
                int n = int.Parse(inp[1]);
                string[] array = Console.ReadLine().Split(' ');
                List<int> list = new List<int>();

                foreach(string str in array)
                {
                    int hoge = int.Parse(str);
                    list.Add(hoge);
                }

                for(int i=0;i<1000;i++)
                {
                    int temp = x - i;
                    if(!list.Contains(temp))
                    {
                        ans = temp;break;
                    }
                    temp = x + i;
                    if (!list.Contains(temp))
                    {
                        ans = temp; break;
                    }
                }

            }
            
            Console.WriteLine(ans);
        }
    }
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<pair<int,int>> vpii;

#define F first
#define S second
#define PU push
#define PUF push_front
#define PUB push_back
#define PO pop
#define POF pop_front
#define POB pop_back

#define REP(i,a,b) for(int i=a; i<=b; i++)

void solve(int test_case) {
	int k,s,cnt=0;
	cin>>k>>s;
	for(int i=0;i<=k;i++) {
		for(int j=0;j<=k;j++) {
			if(s-i-j<=k&&s-i-j>=0)cnt++;
		}
	}
	cout<<cnt;
}

int main() {
	
	////// FILE BASED IO////
	//freopen("in", "r", stdin);
	//freopen("out", "w", stdout);
	///////////////
	
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t=1;
	//cin>>t;
	REP(i,1,t) {
		solve(i);
	}
}	


Measured memory_bytes: 3580
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = (int)1.01e9;
const double PI = acos(-1.0);

struct pt {
    ll x, y;
    int id;

    pt() {}
    pt(ll _x, ll _y) : x(_x), y(_y) {}

    pt operator- (const pt &p) const {
        return pt(x - p.x, y - p.y);
    }

    ll operator* (const pt &p) const {
        return x * p.y - y * p.x;
    }

    bool operator< (const pt &p) const {
        if (x != p.x) return x < p.x;
        if (y != p.y) return y < p.y;
        return id < p.id;
    }

    pt rotate90() const {
        return pt(-y, x);
    }

    double angle() const {
        return atan2(y, x);
    }
};

void convex(vector<pt> &a) {
    int n = a.size();
    sort(a.begin(), a.end());
    if (n <= 2) return;
    pt p1 = a[0], p2 = a[n - 1];
    vector<pt> up, down;
    for (int i = 0; i < n; i++) {
        if (i == 0 || i == n - 1 || ((a[i] - p1) * (p2 - p1)) > 0) {
            while (down.size() > 1 && ((a[i] - down.back()) * (down.back() - down[(int)down.size() - 2])) >= 0) {
                down.pop_back();
            }
            down.push_back(a[i]);
        }
        if (i == 0 || i == n - 1 || ((a[i] - p1) * (p2 - p1)) < 0) {
            while (up.size() > 1 && ((a[i] - up.back()) * (up.back() - up[(int)up.size() - 2])) <= 0) {
                up.pop_back();
            }
            up.push_back(a[i]);
        }
    }
    a.clear();
    for (int i = 0; i < (int)down.size(); i++) a.push_back(down[i]);
    for (int i = (int)up.size() - 2; i > 0; i--) a.push_back(up[i]);
}

int main() {
#ifdef HOME
    freopen("in", "r", stdin);
#endif

    int n;
    while (cin >> n) {
        vector<pt> a(n);
        for (int i = 0; i < n; i++) {
            cin >> a[i].x >> a[i].y;
            a[i].id = i;
        }
        convex(a);

        vector<double> ans(n);
        if (a.size() == 2) {
            ans[a[0].id] = ans[a[1].id] = 0.5;
        } else {
            int n = a.size();
            for (int i = 0; i < n; i++) {
                pt p1 = (a[(i - 1 + n) % n] - a[i]).rotate90();
                pt p2 = (a[i] - a[(i + 1) % n]).rotate90();
                double a1 = p1.angle();
                double a2 = p2.angle();

                if (a2 < a1) {
                    a2 += 2 * PI;
                }
                ans[a[i].id] = (a2 - a1) / (2 * PI);
            }
        }

        for (int i = 0; i < n; i++) printf("%.15f\n", ans[i]);
    }

    return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
#define MAXN 300010
#define LL long long

const LL P=998244353;

int n;
LL a,b,m;
LL fac[MAXN],invfac[MAXN];

LL getPow(LL x,LL y){
	LL res=1;
	while(y){
		if(y&1) res=res*x%P;
		x=x*x%P;
		y>>=1;
	}
	return res;
}

LL getC(int x,int y){
	return fac[x]*invfac[y]%P*invfac[x-y]%P;
}

int main(){
	scanf("%d%lld%lld%lld",&n,&a,&b,&m);
	fac[0]=1;
	for(int i=1;i<MAXN;i++) fac[i]=fac[i-1]*i%P;
	invfac[MAXN-1]=getPow(fac[MAXN-1],P-2);
	for(int i=MAXN-2;i>=0;i--) invfac[i]=invfac[i+1]*(i+1)%P;
	LL ans=0;
	for(int i=0;i<=n;i++)
		if(a*i<=m && (m-a*i)%b==0 && (m-a*i)/b<=n){
			int j=(m-a*i)/b;
			(ans+=getC(n,i)*getC(n,j))%=P;
		}
	printf("%lld\n",ans);
}


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

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef string str;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;
typedef map <str,int> mapsi;
typedef map <str,int> :: iterator mapsitr;
typedef map <int , int> mint;
typedef map <ll , ll> mll;
typedef set <int> si;
typedef set <ll> sll;
typedef si :: iterator sitr;
typedef si :: reverse_iterator rsitr;
typedef sll :: iterator sltr;
typedef sll :: reverse_iterator rsltr;
#define mset multiset

typedef mset <int> msi;
typedef mset <ll> msll;
typedef msi :: iterator msitr;
typedef msi :: reverse_iterator msritr;
typedef msll :: iterator msltr;
typedef msll :: reverse_iterator mslritr;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
#define fbo find_by_order
#define ook order_of_key

char to_upper (char x){
    if( 97 <= int(x) && int(x) <= 122)return char(x-32);
    else if( 65 <= int(x) && int(x) <= 90)return x;
}
char to_lower (char x){
    if( 97 <= int(x) && int(x) <= 122)return x;
    else if( 65 <= int(x) && int(x) <= 90)return char(x+32);
}
int numerize (char x){
    if(48 <= int(x) && int(x) <= 57)return int(x-'0');
    else if( 97 <= int(x) && int(x) <= 122)return int(x-96);
    else if( 65 <= int(x) && int(x) <= 90)return int(x-64);
}
bool isect (int l1, int r1, int l2, int r2){
    pii p1,p2;
    p1 = mp(l1,r1); p2 = mp(l2,r2);
    if(p1>p2)swap(p1,p2);
    if(p2.fi <= p1.se)return true;
    else return false;
}
ll quickpow (ll num1, ll num2, ll MOD){
    if(num2==0)return 1;
    else if(num2==1)return num1;
    else{
        ll temp = quickpow (num1,num2/2,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
        if(num2%2==1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
    }
}
ll invmod (ll num, ll MOD){return quickpow (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
    if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
    while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
    return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/__gcd(num1,num2))*num2;}
// end of Template
ll h,a,ans;
int main(){

//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    cin>>h>>a;
    ans = h/a;
    if(h%a) ans++;
    cout<<ans<<newl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int pow2(int n){
  int t = 1;
  for(int i=0;i<n;i++){
    t *= 2;
  }
  return t;
}


int main(){
  int n;
  cin >> n;
  int f[n][10];
  long p[n][11];
  long ans = -9999999999;
  int tmp = 0;
  int tmpAns = 0;
  int i,j,k;
  for(i=0;i<n;i++){
    for(j=0;j<10;j++){
      cin >> f[i][j];
    }
  }
  for(i=0;i<n;i++){
    for(j=0;j<=10;j++){
      cin >> p[i][j];
    }
  }
  for(i=1;i<=1023;i++){
    tmpAns = 0;
    for(j=0;j<n;j++){
      tmp = 0;
      for(k=0;k<=9;k++){
        if(i%pow2(k+1)>=pow2(k)) tmp += f[j][k];
      }
      tmpAns += p[j][tmp];
    }
    if(tmpAns>ans) ans = tmpAns;
  }
  cout << ans;


  
}

Measured memory_bytes: 256
#include<iostream>
#include<numeric>
#include<map>
#include<algorithm>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)



#define rotate_swap(x,a,b,c,d) swap(x.a,x.b);swap(x.b,x.c);swap(x.c,x.d);

class Dice{
public:
  int top,front,right,left,back,bottom;
};

void rotate_r(Dice &x){
  rotate_swap(x,top,left,bottom,right);
}

void rotate_f(Dice &x){
  rotate_swap(x,top,back,bottom,front);
}

void rotate_cw(Dice &x){
  rotate_swap(x,back,left,front,right);
}


Dice data[4][24];
void generate_all(Dice *x,int n){
  rep(k,n){
    rep(i,6){
      rep(j,4){
	data[k][i*4+j]=x[k];
	rotate_cw(x[k]);
      }
      if (i%2 == 0)rotate_r(x[k]);
      else rotate_f(x[k]);
    }
  }
}


int ans;

void count_min(int n,Dice *in){
  int color[6];
  rep(i,6)color[i]=(1<<20);
  rep(i,n){
    int tcolor[6]={0};
    rep(j,n){
      if (in[i].front  != in[j].front)tcolor[0]++;
      if (in[i].right  != in[j].right)tcolor[1]++;
      if (in[i].left   != in[j].left) tcolor[2]++;
      if (in[i].bottom != in[j].bottom)tcolor[3]++;
      if (in[i].top    != in[j].top)   tcolor[4]++;
      if (in[i].back   != in[j].back)tcolor[5]++;
    }
    rep(j,6)color[j]=min(color[j],tcolor[j]);
  }
  ans=min(ans,accumulate(color,color+6,0));
}

void search(int n,int now,Dice *in){
  if (n == now){
    count_min(n,in);
    return;
  }
  rep(i,24){
    in[now]=data[now][i];
    search(n,now+1,in);
  }
}

int solve(Dice *in,int n){
  ans=(1<<21);
  generate_all(in,n);
  search(n,0,in);
  return ans;
}

int getname(string &in,map<string,int> & M){
  int index=M.size();
  if (M.find(in) == M.end())M[in]=index;
  return M[in];
}

main(){
  int n;
  Dice in[4];
  while(cin>>n && n){
    map<string,int> M;
    rep(i,n){
      string tmp;
      cin>>tmp;
      in[i].front=getname(tmp,M);
      cin>>tmp;
      in[i].right=getname(tmp,M);
      cin>>tmp;
      in[i].top=getname(tmp,M);
      cin>>tmp;
      in[i].bottom=getname(tmp,M);
      cin>>tmp;
      in[i].left=getname(tmp,M);
      cin>>tmp;
      in[i].back=getname(tmp,M);
    }
    cout << solve(in,n) << endl;
  }
}

Measured memory_bytes: 932
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        @SuppressWarnings("resource")
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        if (x >= 2000) {
            System.out.println(1);
            return;
        }
        int c = x / 100;
        if (x % 100 > c * 5) {
            System.out.println(0);
        } else {
            System.out.println(1);
        }
    }

}

Measured memory_bytes: 22996
#include <bits/stdc++.h> 
using namespace std;
///////////////////////////////////////////
const long long int INF = INT32_MAX;
const long long int Mod = 1000000007;
const long long int mod = 100000;//10^5
using ll = long long int; //long long int は64bit
using ci = const int; using vi = vector<int>; using Vi = vector<long long int>; using pii = pair<int,int>;
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define mt make_tuple
#define all(x) (x).begin(),(x).end()
#define rep(i,N) for(int i=0;i<N;i++)
#define np nullptr
template<class T>bool chmax(T &former, const T &b) { if (former<=b) { former=b; return true; } return false; }
template<class T>bool chmin(T &former, const T &b) { if (b<former) { former=b; return true; } return false; }
template<class T>T sqar(T x){ return x*x; }//sqrt(x)は平方根;
#define Sort(v) std::sort(v.begin(), v.end(), std::greater<decltype(v[0])>()) //降順でVをソート
#define fill(x,num) memset(x,num,sizeof(x));
typedef vector<vector<ll> > matrix;
//       cin.tie(0);ios::sync_with_stdio(false);
void cinit(){cin.tie(0);ios::sync_with_stdio(false);}
//////////////////////////^^ *Emile ^^^//////////////////////////////////////////////////////

ll a,b,c;

int main (){
	cin>>a>>b>>c;
	ll asn = 0;
	asn += min(c,a+b+1);
	asn += b;
	cout<<asn<<endl;
	return 0;
}

Measured memory_bytes: 764
def isPrime(n):
    for i in range(2,N):
        if N%i == 0:
            return False
    return True
    
    
def find_next_prime(n):
    return find_prime_in_range(n, 2*n)

def find_prime_in_range(a, b):
    for p in range(a, b):
        for i in range(2, p):
            if p % i == 0:
                break
        else:
            return p
    return None

N = int(input())
if(isPrime(N)):
    print(str(N))
else:
    print(str(find_next_prime(N+1)))


Measured memory_bytes: 3060
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
#include<functional>
#include<assert.h>
#include<numeric>
using namespace std;
#define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i )
#define rep(i,n) REP(i,0,n)
using ll = long long;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;


ll dp[110][101010];

void solve(long long N, long long W, long long w[], long long v[]){
    rep(i,W+1) dp[0][i]=0;
    rep(i,N) {
        // cout << endl;
        rep(j,W+1) {
            if (j-w[i]>=0) {
                dp[i+1][j] = max(dp[i][j], dp[i][j-w[i]]+v[i]);
            }
            else
            {
                dp[i+1][j] = dp[i][j];
            }
            // cout << dp[i+1][j] << endl;
        }
        // cout << dp[i][W] << endl;
    }
    // cout << endl;
    cout << dp[N][W] << endl;
}

// Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    cin >> N;
    long long W;
    cin >> W;
    long long w[N];
    long long v[N];
    for(int i = 0 ; i < N ; i++){
        cin >> w[i] >> v[i];
    }
    solve(N, W, w, v);
    return 0;
}


Measured memory_bytes: 80128
#define  _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <stack>
#include <deque>
#include <queue>
#include <utility>
#include <map>
#include <set>
#include <functional>
#include <math.h>
#include <time.h>
#include <fstream>
#include <list>
#include <climits>

using namespace std;

typedef long long int lld;
typedef long double ld;
typedef vector<int> vi;
typedef vector<lld> vlld;

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

	int n, sm = 0;

	cin >> n;

	vi vc(n);

	for (int i = 0; i < n; ++i)
		cin >> vc[i];

	sort(vc.begin(), vc.end());

	for (int i = 0; i < n; ++i)
	{
		for (int j = i + 1; j < n; ++j)
		{
			for (int k = j + 1; k < n; ++k)
			{
				if (vc[i] == vc[j] || vc[i] == vc[k] || vc[j] == vc[k])
					continue;

				if (vc[i] + vc[j] > vc[k])
					sm++;
			}
		}
	}

	cout << sm << '\n';

	return 0;
}


Measured memory_bytes: 3620
import java.util.Scanner;


public class Main{

	public static void main(String args[]){
	int i,j,k,n,q;;
	Scanner sc=new Scanner(System.in);
	
	n=Integer.parseInt(sc.next());
	long s[]=new long[n+1];
	for(i=0;i<n;i++){
		s[i]=Integer.parseInt(sc.next());
	}
	
	q=Integer.parseInt(sc.next());
	long T[]=new long[q+1];
	
	for(i=0;i<q;i++){
		T[i]=Integer.parseInt(sc.next());
	}
	
	int count=0;
	
	for(j=0;j<q;j++){
		for(i=0;i<n;i++){
	if(T[j]==s[i]){
			count++;
			break;
			}
		}	
	}
	System.out.println(count);
	}
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
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 ll MOD = 1e9+7;

const int MAX_N = 10000;

ll fact[MAX_N+100];
ll frev[MAX_N+100];

ll rev(ll a, ll p){
    if(p == 0) return 1;
    ll tmp = rev(a, p/2);
    tmp = tmp * tmp % MOD;
    if(p % 2 == 1) tmp = tmp * a % MOD;
    return tmp;
}


void calc_fact(){
    fact[0] = frev[0] = 1;
    for(int i = 1; i <= MAX_N; i++){
        fact[i] = (fact[i-1] * i) % MOD;
        frev[i] = rev(fact[i], MOD-2);
    }
}


ll comb(int n, int k){
    if(n < 0 || k < 0 || n < k) return 0;
    if(n == 0 || k == 0) return 1;
    return fact[n] * frev[k] % MOD * frev[n-k] % MOD;
}

map<ll, ll> sosu(ll X){
    map<ll, ll> ret;
    for(ll i = 2; i*i <= X; i++){
        while(X % i == 0){
            X /= i;
            ret[i]++;
        }
    }

    ret[X]++;
    return ret;
}

ll mod_pow(ll x, ll n){
    ll ret = 1;
    while(n > 0){
        if(n % 2 == 1) ret *= x;
        x *= x;
        n >>= 1;
        ret %= MOD;
        x %= MOD;
    }
    return ret;
}

ll mod_div(int n, int k){
    return (n * mod_pow(k, MOD-2)) % MOD;
}

int main(){
    ll N;
    cin >> N;
    vector<ll> A(N);
    REP(i, N){
        ll a;
        cin >> a;
        A[i] = a;
    }

    map<ll, ll> S;
    vector<map<ll, ll>> tmp(N);
    REP(i,N){
        tmp[i] = sosu(A[i]);
        for(auto&& p : tmp[i]){
            chmax(S[p.first], p.second);
        }
    }

    ll sum = 1;
    for(auto&& p : S){
        sum *= mod_pow(p.first, p.second);
        sum %= MOD;
    }

    ll ans = 0;
    REP(i,N){
        ans += mod_div(sum, A[i]);
        ans %= MOD;
    }
    
    //map<ll, ll> L;
    //REP(i,N){
    //    auto s = S;
    //    for(auto&& p : tmp[i]){
    //        s[p.first] -= p.second;
    //    }

    //    ll x = 1;
    //    for(auto&& p : s){
    //        x *= mod_pow(p.first, p.second);
    //        x %= MOD;
    //    }
    //    ans += x;
    //    ans %= MOD;
    //}

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 3968
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

int main(){
    
    int K, S;
    cin >> K >> S;
    
    int ans = 0;
    for(int x = 0; x <= K; x++){
        for(int y = 0; y <= K; y++){
            int z = S - (x + y);
            if(z <= K && z >= 0) ans++;
        }
    }
    
    cout << ans << endl;
    
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>

using namespace std;
int main() {
	int n, m;
	cin >> n >> m;
	int h[n];
	for (int i = 0; i < n; i++) cin >> h[i];
	int a[m], b[m];
	int maxh[n] = {0};
	for (int i = 0; i < m; i++){
		cin >> a[i] >> b[i];
		maxh[a[i]-1] = max(maxh[a[i]-1], h[b[i]-1]);
		maxh[b[i]-1] = max(maxh[b[i]-1], h[a[i]-1]);
	}

	int cnt = 0;

	for (int i = 0; i < n; i++){
		if (maxh[i] < h[i]) cnt++;
	}

	cout << cnt << endl;
	

}


Measured memory_bytes: 5168
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
INF = float("inf")
import sys
sys.setrecursionlimit(10000)

a = []
memo = []


def dfs(no, i):
    global memo, a
    if memo[no][i] != -1:
        return memo[no][i]

    ans = abs(a[-1] - a[i])
    for j in range(i + 1, len(a) - 1):
        nex = dfs(1 - no, j + 1)
        # X
        if no == 0:
            ans = max(ans, nex)
        # Y
        else:
            ans = min(ans, nex)

    memo[no][i] = ans
    return ans


def solve2(N, Z, W):
    global a, memo
    a = [Z, W] + a
    memo = [[-1] * len(a) for _ in range(2)]
    return dfs(0, 1)


def solve(N, Z, W):
    global a
    ans = abs(a[-1] - W)
    if len(a) > 1:
        ans = max(ans, abs(a[-2] - a[-1]))
    return ans


def main():
    global a
    N, Z, W = map(int, input().split())
    a = list(map(int, input().split()))
    print(solve2(N, Z, W))


if __name__ == '__main__':
    main()


Measured memory_bytes: 46940
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtTest.HCPC2019_GW_Div1
{
    class B
    {
        static void Main(string[] args)
        {
            Method(args);
        }

        static void Method(string[] args)
        {
            List<Seed[]> res = new List<Seed[]>();
            while (true)
            {
                int n = ReadInt();
                if (n == 0) break;

                res.Add(new Seed[n]);
                for(int i = 0; i < n; i++)
                {
                    string[] input = Read().Split();
                    int p = int.Parse(input[1]);
                    int a = int.Parse(input[2]);
                    int b = int.Parse(input[3]);
                    int c = int.Parse(input[4]);
                    int d = int.Parse(input[5]);
                    int e = int.Parse(input[6]);
                    int f = int.Parse(input[7]);
                    int s = int.Parse(input[8]);
                    int m = int.Parse(input[9]);
                    double val = m * f * s - p;
                    val /= a + b + c + (d + e) * m;
                    res[res.Count - 1][i] = new Seed(input[0], val);
                }
            }

            for (int i = 0; i < res.Count; i++)
            {
                Array.Sort(res[i]);
                for (int j = 0; j<res[i].Length; j++)
                {
                    WriteLine(res[i][j].name);
                }
                WriteLine("#");
            }
        }

        class Seed : IComparable<Seed>
        {
            public string name;
            public double value;

            public Seed(string name,double value)
            {
                this.name = name;
                this.value = value;
            }
            
            public int CompareTo(Seed other)
            {
                if (value > other.value) return -1;
                if (value == other.value)
                {
                    return string.Compare(name, other.name);
                }
                else return 1;
            }
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}



Measured memory_bytes: 18852
//#define _GLIBCXX_DEBUG
#include<bits/stdc++.h>
#define PI 3.14159265359
using namespace std;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
const long long INF= 1e+18+1;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll> >vvl;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> T;
const ll MOD=1000000007LL;
string abc="abcdefghijklmnopqrstuvwxyz";
string ABC="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int main(){
  ll a,b,c,d,e,f;cin>>a>>b>>c>>d>>e>>f;
  vector<ll>vec;
  rep(i,3001){
    rep(j,1500){
      vec.push_back(c*i+d*j);
    }
  }
  double ans=-1;
  ll anssugar;
  ll answater;
  sort(vec.begin(),vec.end());
  for(ll i=0;100*i*a<=f;i++){
    for(ll j=0;100*i*a+100*j*b<=f;j++){
      ll remain=min(e*(a*i+b*j),f-100*(a*i+b*j));
      ll water=100*(a*i+b*j);
      ll idx_upper=distance(vec.begin(),upper_bound(vec.begin(),vec.end(),remain));
      idx_upper--;
      double sugar=vec[idx_upper];
      if(sugar+water>0&&ans<sugar/(sugar+water)){
        ans=sugar/(sugar+water);
        anssugar=sugar;
        answater=water;
      }
    }
  }
  cout<<anssugar+answater<<" "<<anssugar<<endl;
}

Measured memory_bytes: 67808
char*p,*q,r['~~'];w;i;main(h){for(;scanf("%d%d",&h,&w)*w++;i=!printf(h?"%d %d\n":"LOOP\n",(p-r)%w,(p-r)/w)){for(p=r;i<h;scanf("%s",r+i++*w));for(h*=w;*p>46&&--h;)p+=*p%5?*p&8?*p<63?1:-w:w:-1;}}

Measured memory_bytes: 0
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <cstring>
#include <queue>
#include <assert.h>
#include <cmath>
#include <stack>
#include <deque>
#include <set>
#include <unordered_map>
#include <complex>
#include <fstream>
#include <map>
#include <numeric>
#include <functional>
using namespace std;
const int mxn = 1e6+10;

long long gcdlike(long long a, long long b) {
	long long ret = a+b;
	if(a<b) swap(a, b);
	while(b) {
		long long q = a/b;
		long long r = a%b;
		ret+=2*q*b;
		if(r==0) ret-=b;
		a = b; b = r;
	}
	return ret;
}


int main() {
	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	long long n,x; cin>>n>>x;
	cout << gcdlike(n-x, x) << '\n';

	return 0;
}
//comparator function in sorting
//swapping arguments
//missing cases in dp
//dividing in modulo
//Beware of overflow and modulo
//Be mindful of overflow
//s is a matrix but forget
//length of ranges
//working with bit using ull

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

// #define fi first
// #define se second
#define rep(i, n) for (ll i=0;i<(n); i++)
#define ALL(v) (v).begin(), (v).end()
#define FORR(i, a, b) for(int i=(b)-1; i>=(a); --i)
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; }

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pi;

const ll M=1000000007ll;
const ll MOD=1000000007ll;
const ll MAX=100000001ll;
//const ll M = 1000000000+7;

const int mod = 1000000007;
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
   return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

mint f(ll n){
  if(n==0) return 1;
  mint ans = f(n/2);
  ans *= ans;
  if(n%2==1) ans*= 2;
  return ans;
}

mint choose(ll n, ll k){
  mint x=1, y=1;
  rep(i,k){
    x *= (n-i);
    y *= (i+1);
  }
  return x / y;
}

struct BIT {
  private:
   vector<ll> bit;
   ll N;
 
  public:
   BIT(ll size) {
     N = size;
     bit.resize(N + 1);
   }
 
   // 一点更新です
   void add(ll a, ll w) {
     for (ll x = a; x <= N; x += x & -x) bit[x] += w;
   }
 
   // 1~Nまでの和を求める。
   int sum(ll a) {
     ll ret = 0;
     for (ll x = a; x > 0; x -= x & -x) ret += bit[x];
     return ret;
   }
 };
 
ll fff(ll n, vector<ll>& v, ll i, ll j, ll rl){
  ll can = 0;
  ll can_i = 0;

  cout << v[0]<<endl;
  
  // right
  if(rl == 0){
    rep(k, n){
      if(v[k]*(j-k)>can){
	can = v[k]*(j-k);
	can_i = k;
      }
      //cout << can<<endl;
    }
    v[can_i] = 0;
    return can;
  }
  // left
  else{
    rep(k, n){
      if(v[k]*(k-i)>can){
	can = v[k]*(k-i);
	can_i = k;
      }
      //cout << can<<endl;

    }
    v[can_i] = 0;
    return can;
  }
  
}

ll ff(ll x){
  return x * (x+1) / 2;
}

int main(){
  ll n, k,x,y;
  cin>>n;

  map<string, ll> m;
  string tmp;
  rep(i, n){
    cin>>tmp;
    m[tmp]++;
  }
  ll ans =0;
  for(auto x:m){
    ans++;
  }
  
  cout<<ans<<endl;
}


Measured memory_bytes: 19068
#include <bits/stdc++.h>
  using namespace std;
#define ll long long
#define fw(p) for(int w=0;w<(p);w++)
#define fx(p) for(int x=0;x<(p);x++)
#define fy(p) for(int y=0;y<(p);y++)
#define fz(p) for(int z=0;z<(p);z++)
#define fyg(p,g) for(int y=(g);y<(p);y++)
#define fzg(p,g) for(int z=(g);z<(p);z++)
#define ce(d) cout<<d<<endl;
#define vecp(p) int aa;cin>>aa;(p).push_back(aa);
#define vecpl(p) long long aa;cin>>aa;(p).push_back(aa);
#define vecps(p) string aa;cin>>aa;(p).push_back(aa);
#define vecp2(p) cin>>aa;(p).push_back(aa);
#define vecpl2(p) long long a	b;cin>>ab;(p).push_back(ab);
#define vecps2(p) string ab;cin>>ab;(p).push_back(ab);
#define set0(k,n) for(int nn=0;nn<(n);nn++){ (k).push_back(0); }
#define sorts(c) sort((c).begin(),(c).end());
#define reverses(c) reverse((c).begin(),(c).end());
#define vec(b) vector<int> (b);
#define vecl(b) vector<long long> (b);
#define vecs(b) vector<string> (b);
#define vecsize(b,size) vector<int> (b)((size));
#define pb(b,a) (b).push_back((a));
#define doublece(a,b) cout<<(a)<<' '<<(b)<<endl;
#define pairs(s) vector<pair<int,int>> (s);
#define pairsl(s) vector<pair<ll,ll>> (s);
#define pairss(s) vector<pair<string,string>> (s);
#define pairsp(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define pairspl(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define pairsps(s) int aa,bb;cin>>aa>>bb;(s).push_back(make_pair(aa,bb));
#define pairsREV(s) (s).push_back(make_pair(bb,aa));
#define pairslREV(s) (s).push_back(make_pair(bb,aa));
#define pairssREV(s) (s).push_back(make_pair(bb,aa));
#define MOD 1000000007

int gcd(int x,int y){
	
	if(x%y==0){
		return y;
	}else{
		return gcd(y,x%y);
	}
	
}


int main() {
	
string S;
cin>>S;
int red=0,blue=0;

fx(S.size()){
	if(S.at(x)=='0'){
		red++;
	}else{
		blue++;
	}
}

ce(min(red,blue)*2)
}

Measured memory_bytes: 512