akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

#define x first
#define y second
#define pb push_back
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 100010;
const ll oo = 1e18;

int n , m , s , t;


struct edge {
	int v;
	ll yen , snuk;
	edge(int _v , ll _yen , ll _snuk) {
		v = _v;
		yen = _yen;
		snuk = _snuk;
	}
};

vector<edge> g[N];

ll cost[N][2];
bool vis[N];

struct state {
	int u;
	ll cost;
	state(int _u , ll _cost) {
		u = _u;
		cost = _cost;
	}
	bool operator<(const state &rhs) const {
		return cost > rhs.cost;
	}
};

void dijsktra(int source , int type) {
	for (int i = 1 ; i <= n ; ++i) {
		cost[i][type] = oo;
		vis[i] = 0;
	}
	priority_queue<state> q;
	cost[source][type] = 0;
	q.push(state(source, cost[source][type]));
	while (q.size()) {
		state src = q.top();
		q.pop();
		if (vis[src.u])continue;
		vis[src.u] = 1;
		for (int i = 0; i < g[src.u].size(); ++i) {
			ll newCost = (type == 0 ? g[src.u][i].yen : g[src.u][i].snuk) + cost[src.u][type];
			int v = g[src.u][i].v;
			if (newCost < cost[v][type]) {
				cost[v][type] = newCost;
				q.push(state(v, newCost));
			}
		}
	}
}

ll dp[N];

int main() {
	cin >> n >> m >> s >> t;
	for (int i = 0; i < m; ++i) {
		int u, v, a, b;
		scanf("%d%d%d%d", &u, &v, &a, &b);
		g[u].push_back(edge(v, a, b));
		g[v].push_back(edge(u, a, b));
	}
	dijsktra(s, 0);
	dijsktra(t, 1);
	for (int i = 1;  i <= n ; ++i) {
		ll need = cost[i][0] + cost[i][1];
		dp[i] = need;
	}
	for (int i = n - 1; i >= 1 ; --i) {
		dp[i] = min(dp[i],dp[i+1]);
	}
	ll fn = 1000000000000000;
	for(int i = 1; i <= n ; ++i){
		printf("%lld\n", fn-dp[i]);
	}
	return 0;
}

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

vector< int64_t > divisor(int64_t n) {
  vector< int64_t > ret;
  for(int64_t i = 1; i * i <= n; i++) {
    if(n % i == 0) {
      ret.push_back(i);
      if(i * i != n) ret.push_back(n / i);
    }
  }
//   sort(begin(ret), end(ret));
  return (ret);
}

// Generated by 1.1.7.1 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(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<int64_t> vec(n);
    rep(i,n) cin >> vec[i];
    sort(vec.begin(), vec.end());

    int cnt = 0;
    rep(i,n)
    {
        vector<int64_t> div = divisor(vec[i]);
        div.push_back(vec[i]);
        // bool flag = false;
        for (int j=0;j<div.size();j++)
        {
            int d = upper_bound(vec.begin(), vec.end(), div[j]) - lower_bound(vec.begin() ,vec.end(), div[j]);
            // cout << vec[i] << " " << div[j] << " " << d << endl;
            if (d==1 && div[j]!=vec[i])
            {
                cnt++;
                // flag = true;
                break;
            }
            else if (d>1)
            {
                cnt++;
                break;
            }
        }
    }
    cout<< n-cnt << endl;
}


Measured memory_bytes: 4768
root = [[] for i in range(4)]
for _ in range(3):
    a,b = map(int,input().split())
    root[a-1].append(b-1)
    root[b-1].append(a-1)

visited = [False] * 4
visited[0] = True

def dfs(s):
    if all(visited):
        print('YES')
        exit()

    for i in root[s]:
        if visited[i]:
            continue

        visited[i] = True
        dfs(i)
        visited[i] = False

dfs(0)
print('NO')

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

int main(){
	int n;
	cin >> n;
	int sum=0;
	for (int i = 1; i < n+1;i++)sum+=i;
	cout << sum << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main() {
int n;
cin>>n;
n%=10;
if(n==3) {
 cout<<"bon"; 
} else if(n==0 || n==1 || n==6 ||n==8) {
  cout<<"pon";
} else {
  cout<<"hon";
}
return 0;
}

Measured memory_bytes: 3636
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
using namespace std;
int main()
{	
	int a,b,c,d;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	if(a*b>c*d)
	printf("%d",a*b);
	else
	printf("%d",c*d);
	return 0;
}

Measured memory_bytes: 128
while 1:
    n = input()
    if n == 0: break
    p = set([tuple(map(int, raw_input().split())) for i in range(n)])
    m = input()
    q = set([tuple(map(int, raw_input().split())) for i in xrange(m)])
    ret = False
    for px, py in p:
        if ret: break
        for qx, qy in q:
            if ret: break
            dx = qx - px
            dy = qy - py
            for x, y in p:
                mx = x + dx
                my = y + dy
                if (mx, my) not in q:
                    break
            else:
                print dx, dy
                ret = True

Measured memory_bytes: 6436
# your code goes here
N,M = map(int,input().split())

cake = [list(map(int,input().split())) for i in range(N)]

ans = 0
for i in range(2):
	for j in range(2):
		for k in range(2):
			cake_tmp = [[0]*3 for i in range(N)]
			for x in range(N):
				cake_tmp[x][0] = cake[x][0]*((-1)**i)
				cake_tmp[x][1] = cake[x][1]*((-1)**j)
				cake_tmp[x][2] = cake[x][2]*((-1)**k)
			cake_point = [0]*N
			for x in range(N):
				cake_point[x] += cake_tmp[x][0] + cake_tmp[x][1] + cake_tmp[x][2]
			cake_point.sort()
			tmp = 0
			for x in range(M):
				tmp += cake_point[-(x+1)]
			ans = max(ans,tmp)

print(ans)

Measured memory_bytes: 3572
#include <bits/stdc++.h>
#define DUMP(x)  cout << #x << " = " << (x) << endl;
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define IFOR(i, m, n) for(int i = n - 1; i >= m; i-- )
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)
#define FOREACH(x,a) for(auto& (x) : (a) )
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
const ll INF = 1e18;
/* テンプレートここまで */

int main() {
  string o,e ; cin >> o >> e;
  ll m = o.size();
  ll n = e.size();
  for(ll i = 0; ; i++){
    if(i%2 == 0 && i/2 < m){
      cout << o[i/2];
    }else if(i%2 == 1 && i/2 < n){
      cout << e[i/2];
    }else{
      cout << endl;
      break;
    }
  }
}

Measured memory_bytes: 256
import collections
n=int(input())
D=[int(i) for i in input().split()]
m=int(input())
T=[int(i) for i in input().split()]
cD,cT=collections.Counter(D),collections.Counter(T)
flag=1
for x in set(T):
  if cT[x]>cD[x]:
    flag=0
print("YES" if flag==1 else "NO")

Measured memory_bytes: 65120
#include <stdio.h>

typedef struct {
	int year;
	int month;
	int day;
} Date;

int is_leap(int year)
{
	return (year % 400 == 0 || year % 100 != 0 && year % 4 == 0);
}

int count_day(int year, int month, int day)
{
	const static int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30};
	int serial = 0;
	int mit;

	if (is_leap(year) && month > 2) serial++;

	for (mit = 1; mit < month; mit++){
		serial += days[mit - 1];
	}
	return (serial + day);
}

int year_to_day(int year)
{
	return (year * 365 + (year / 4) - (year / 100) + (year / 400));
}

int get_serial(Date date)
{
	int serial;

	serial = year_to_day(date.year - 1) + count_day(date.year, date.month, date.day);

	return (serial);
}

int input_date(Date *date)
{
	return (scanf("%d %d %d", &date->year, &date->month, &date->day));
}

int main(void)
{
	Date first, last;

	while (1){
		input_date(&first);
		input_date(&last);
		if (first.year == -1) break;

		printf("%d\n", get_serial(last) - get_serial(first));
	}

	return (0);
}

Measured memory_bytes: 596
#include <stdio.h>

int main(void) {
	int r,c;
	int rsum[100+1] = {};
	scanf("%d%d",&r,&c);
	for(int i = 0; i < r; i++){
		int csum = 0;
		for(int j = 0; j < c; j++){
			int temp;
			scanf("%d",&temp);
			printf("%d ",temp);
			rsum[j] += temp;
			csum += temp;
		}
		printf("%d\n",csum);
		rsum[c] += csum;
	}
	for(int i = 0; i < c; i++){
		printf("%d ",rsum[i]);
	}
	printf("%d\n",rsum[c]);
	return 0;
}



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

typedef long long ll;
typedef unsigned long long ull;

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout.precision(12);
    cout.setf(ios_base::fixed, ios_base::floatfield);
    
    int n, m;
    cin >> n >> m;
    int coin[20];
    for(int i=0;i<m;i++){
        cin >> coin[i];
    }
    int dp[50001];
    for(int i=0;i<=n;i++) dp[i] = INT32_MAX;
    dp[n] = 0;
    for(int i=n;i>=0;i--){
        if(dp[i] == INT32_MAX) continue;
        for(int j=0;j<m;j++){
            if(i-coin[j]<0) continue;
            dp[i-coin[j]] = min(dp[i-coin[j]], dp[i]+1);
        }
    }
    cout << dp[0] << endl;
    
    return 0;
}


Measured memory_bytes: 3344
use proconio::input;
use std::collections::HashMap;

fn main() {
    input! {
        u: usize,
        a: [usize; u],
    } 
    let mut map = HashMap::new();
 
    for &a in a.iter() {
        *map.entry(a).or_insert(0) += 1;
    }
    let key = map.keys().collect::<Vec<_>>();

    // 中身がすべて0
    if map.len() == 1 {
        if let Some(_) = map.get(&0) {
            println!("Yes");
        } else {
            println!("No");
        }

    // 中身の1/3が0と2/3が何かしらの同じ値
    } else if map.len() == 2 {
        if u % 3 == 0 && map.get(&0) == Some(&(u / 3)) && key.len() == 2 {
            println!("Yes");
        } else {
            println!("No");
        }

    // 中身の値がx ^ y ^ z = 0 かつそれぞれ1/3づつ含まれている
    } else if map.len() == 3 {
        if map.len() == 3 {
            if u % 3 == 0 && map.values().all(|&c| c == u / 3) && 
                key[0] ^ key[1] == *key[2] {
                println!("Yes");
            } else {
                println!("No");
            }
        }
    } else {
        println!("No");
    }
}


Measured memory_bytes: 6708
#include <bits/stdc++.h>
using namespace std;
/* typedef */
typedef long long ll;
typedef pair<int, int> pii;
/* constant */
const int INF = 1 << 30;
const ll LINF = 1LL << 50;
const int NIL = -1;
const int MAX = 200000;
const int mod = 1000000007;
const double pi = 3.141592653589;
/* struct for mod Calculation*/
struct mint {
    ll x;
    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;
    }
};
istream& operator>>(istream& is, const mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}
/* global variables */
/* function */
struct combination {
    // vector size : n + 1
    vector<mint> fact, ifact;
    // precaliculation O(n)
    combination(int n):fact(n+1), ifact(n+1) {
        assert(n < mod);
        // 0! = 1;
        fact[0] = 1;
        // 1! = 0! * 1 ... n! = (n-1)! * n
        for (int i = 1; i <= n; i++) fact[i] = fact[i-1] * i;
        // 1/n! = n!^(-1)
        ifact[n] = fact[n].inv();
        // (n-1)!^(-1) = n!^(-1) * n ... 0!^(-1) = 1!^(-1) * 1
        for (int i = n; i >= 1; --i) ifact[i-1] = ifact[i] * i;
    }
    // combination O(1)
    // nCk = n! / (k! * (n-k)!)
    //     = n! * k!^(-1) * (n-k)!^(-1)
    mint operator()(int n, int k) {
        if (k < 0 || k > n) return 0;
        return fact[n] * ifact[k] * ifact[n-k];
    }
};
/* global variables */
/* function */
/* main */
int main(){
    ll N, K;
    cin >> N >> K;
    combination c = combination(MAX * 2);
    mint ans = 0;
    K = min(K, N - 1);
    for (int i = 0; i <= K; i++) {
        ans += c(N, i) * c(N - 1, i);
    }
    cout << ans << '\n';
}


Measured memory_bytes: 6528
#include<iostream>
using namespace std;
int main()
{
    // スペース区切りの整数の入力
    int a,b,flag=0;
    cin >> a >> b;

    int count = 0;
    for(int i=1;i<=12;i++){
        for(int j=1;j<=31;j++){

            if(i==j){
                count ++;
            }
            if(i==a && j>=b){
                flag=1;
                break;
            }
        }
        if (flag==1){
            break;
        }
    }

    cout << count << endl;

    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#define N 200010
int n, a[N],min=200010,b[N],i,j,ans=0;
int main(void)
{
    scanf("%d", &n);
    for (i = 1; i <= n;i++){
        scanf("%d", &a[i]);
        if(a[i]<min)
            min = a[i];
        b[i] = min;
    }
    for (int i = 1; i <= n;i++)
        if(b[i]>=a[i])
            ans++;
    printf("%d", ans);
    return 0;
}

Measured memory_bytes: 1664
/*input
5 3
10 30 40 50 20
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;cin>>n>>k;
	vector<int> h(n+1);
	for(int i=1;i<=n;i++) cin>>h[i];
	vector<int> dp(n+1,1e9);
	dp[1]=0;
	for(int i=2;i<=n;i++) for(int j=1;j<=k;j++) if(i-j>=1) dp[i]=min(dp[i],dp[i-j]+abs(h[i]-h[i-j]));
	// for(int i=1;i<=n;i++) cout<<dp[i]<<" ";
	cout<<dp[n]<<"\n";
	return 0;
}


Measured memory_bytes: 3916
#include <iostream>
#include <algorithm>
using namespace std;
 
int main()
{
    // int a, b, c, d, e;
    // cin >> a >> b >> c >> d >> e;
    int list[10] = {0};
    int ans = 0, sm = 10000;
    for (int i = 0; i < 5; ++i){
        cin >> list[i];
        if (list[i] % 10 < sm && list[i] % 10 != 0) sm = list[i] % 10;
    }
    for (int i = 0; i < 5; ++i){
        ans += list[i];
        if (list[i] % 10 != 0)
            ans += (10 - list[i] % 10);
    }
    if (sm ==10000) sm = 10;
    cout << ans - (10 - sm) << endl;
    // else {
    //     cout << ans << endl;
    // }
    
    

    return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N+1),B(N+1),C(N+1);
  for(int i = 1; i <= N; i++) cin >> A.at(i);
  for(int i = 1; i <= N; i++) cin >> B.at(i);
  for(int i = 1; i < N; i++) cin >> C.at(i);
  int ans=0,tmp=100;
  for(int i = 1; i <= N; i++){
    ans+= B.at(A.at(i));
    if(A.at(i)==tmp+1) ans+=C.at(A.at(i)-1);
    tmp=A.at(i);
  }
  cout << ans;
}

Measured memory_bytes: 256
#最初に置いておくもの
import sys
sys.setrecursionlimit(500000) 
input=sys.stdin.readline
N=int(input())
A = list(map(int, input().split()))

inf=float('inf')
dp=[[inf]*2 for j in range(N+1)]

def chmax(a,b):
    return max(a,b)

def rec(n,m):
    if n==0 and m==0:
        dp[0][0]=0
        return 0
    if n==0 and m==1:
        dp[0][1]=-inf
        return -inf

    if dp[n][m] < inf:
        return dp[n][m]
    
    if m==0:
        res=chmax(rec(n-1,0)+A[n-1] ,rec(n-1,1)-A[n-1] ) 
    if m==1:
        res=chmax(rec(n-1,0)-A[n-1] ,rec(n-1,1)+A[n-1] )
     
    dp[n][m]=res

    return res

print(rec(N,0))

Measured memory_bytes: 103752
#include<iostream>
#include<deque>
#include<cmath>
#include<cstring>
#include<map>
#include<algorithm>
#include<vector>
#include<ios>
using namespace std;
#define ive(cris) int long long 
#define in(out) long double
#define INF 100000007
int main(){
  int a;
  cin>>a;
  if(a<1200){
    cout<<"ABC"<<'\n';
  }else{
    cout<<"ARC"<<'\n';
  }
  return 0;
}


Measured memory_bytes: 256
#include <iostream>

int main(void){
  std::string s;
  std::cin >> s;
  int length = s.length();
  int half_length = (length % 2 == 0) ? length / 2 : length / 2 + 1;
  int odd_one = 0,even_one = 0;
  for(int i = 0; i < length; i += 2){
    if(s[i] == '1')
      odd_one++;
  }
  for(int i = 1; i < length; i += 2){
    if(s[i] == '1')
      even_one++;
    }

  if(length % 2 == 0){
    if(odd_one + half_length - even_one >= half_length)
      std::cout << length - (odd_one + half_length - even_one) << std::endl;
    else
      std::cout << odd_one + half_length - even_one << std::endl;
  }else{
    if(odd_one + length / 2 - even_one > half_length - odd_one + even_one)
      std::cout << half_length - odd_one + even_one << std::endl;
    else
      std::cout << odd_one + length / 2 - even_one << std::endl;
  }
  return 0;
}

Measured memory_bytes: 512
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

string lcs(string &s, string &t) {
    auto chmax = [](int& a, int b) { if (a < b) { a = b; return 1; } return 0; };
    int n = s.size(), m = t.size();
    vector<vector<int>> dp(n + 1, vector<int>(m + 1));
    vector<vector<int>> prv(n + 1, vector<int>(m + 1)); // ↓: 0, →: 1, ↘︎: 2
    for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) {
        if (s[i] != t[j]) {
            if (chmax(dp[i + 1][j + 1], dp[i][j + 1])) prv[i + 1][j + 1] = 0;
            if (chmax(dp[i + 1][j + 1], dp[i + 1][j])) prv[i + 1][j + 1] = 1;
        } else {
            if (chmax(dp[i + 1][j + 1], dp[i][j] + 1)) prv[i + 1][j + 1] = 2;
        }
    }
    string res = "";
    while (n > 0 && m > 0) {
        if (prv[n][m] == 0) n--;
        if (prv[n][m] == 1) m--;
        if (prv[n][m] == 2) n--, m--, res += s[n];
    }
    reverse(res.begin(), res.end());
    return res;
}

int main() {
    int T; cin >> T;
    while (T--) {
        string s, t; cin >> s >> t;
        cout << lcs(s, t).size() << endl;
    }
    return 0;
}


Measured memory_bytes: 10744
//bismillahir rahmanir rahim            //Author:Fayed Anik
 
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
//using namespace __gnu_pbds;
 
//#define         ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define         ll                          long  long
#define         lf                          double
#define         pb(x)                       push_back(x)
#define         ull                         unsigned long long
#define         sfl(a)                      scanf("%lld",&a)
#define         sf(a)                       scanf("%d",&a)
#define         pf(a)                       printf("%d\n",a)
#define         pfl(a)                      printf("%lld\n",a)
#define         FOR(x,n)                    for(ll x=1;x<=n;++x)
#define         pii                         pair< ll , ll >
#define         mp(a,b)                     make_pair(a,b)
#define         UNIQUE(v)                   v.resize(distance(v.begin(),unique(v.begin(),v.end())))
#define         mod                         1000000007
#define         INF                         2e18
#define         EPS                         1e-15
#define         f1                          first
#define         f2                          second
#define         all(v)                      v.begin(),v.end()
#define         PI                          acos(-1)
#define         printminusone               printf("-1\n")
#define         bug                         printf("bug")
#define         FILEIN                      freopen("in.txt","r",stdin)
#define         FILEOUT                     freopen("out.txt","w",stdout)
 
//ll SET(ll mask,ll pos){ return mask = (mask | (1ll<<pos)); }
//ll RESET(ll mask,ll pos){ return mask = mask & ~(1ll<<pos); }
//bool CHECK(ll mask,ll pos) { return (bool) (mask & (1ll<<pos)); }
//priority_queue <ll, vector<ll>, greater<ll> > pq;
//int dx[]={0,0,1,-1,1,1,-1,-1};
//int dy[]={1,-1,0,0,1,-1,1,-1};

#define lim 10000000+10

ll track[lim];

void sieve(){
    for(ll i=1;i<=1e7;i++) track[i]=i;
    for(ll i=2;i<=1e7;i+=2) track[i]=2;
    for(ll i=3;i<=1e7;i+=2){
        if( track[i]==i ){
            for(ll j=i*i;j<=1e7;j+=2*i){
                track[j]=i;
            }
        }
    }
}

int main(){
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    sieve();
    ll n,tot=0,N,val;
    sfl(n);
    for(ll i=1;i<=n;i++){
        N = i;
        ll x=0,nod=1;
        while(N>1){
            val = track[N];
            x=0;
            while(N%val==0 and N>1){
                N /= val;
                x++;
            }
            nod = nod*(x+1);
        }
        tot += i*nod;
    }
    pfl(tot);
    return 0;

}


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

#define mod 1000000007
typedef int integer;
#define int long long
using namespace std;

const int N = 200005;

int n;

integer main(){
    ios_base::sync_with_stdio(false);

    int a, b;
    cin >> a >> b;
    cout << (int) (ceil(1.0 * a / b)) << endl;
}


Measured memory_bytes: 256
k,a,b=map(int,input().split())
n=1
if b-a<=2:
    n += k
    print(n)

elif k<a-1:
    n += k
    print(n)

else: #b-a>2 k-(a-1)>=0
    #1、n,1からa
    n = a
    k -= a-1  #>0
    m = k // 2
    n += (b-a)*m
    if k - 2*m ==1:
        n+=1
    print(n)


Measured memory_bytes: 3060
for data in range(input()):
    dataset=map(int,raw_input().split(" "))
    B,C=0,0
    B=dataset.pop(0)
    while len(dataset)!=0:
        ball = dataset.pop(0)
        if ball-B<0 or ball-C<0:
            if ball-B<0 and ball-C<0:
                print "NO"
                dataset.append(0)
                break
            else:
                if ball-B<0:
                    C=ball
                else:
                    B=ball
        else:
            if ball-B<ball-C:
                B=ball
            else:
                C=ball
    if(len(dataset)==0):
        print "YES"

Measured memory_bytes: 4224
import sys

def f(l,beg,end):
    if beg==end:
        dp[beg][end]=l[beg]
        return l[beg]
    if beg+1==end:
        dp[beg][end] = max(l[beg],l[end]) 
        return max(l[beg],l[end])
    if dp[beg][end]!=-1:
        return dp[beg][end]
    ans1=l[beg]+min(f(l,beg+2,end),f(l,beg+1,end-1))
    ans2=l[end]+min(f(l,beg,end-2),f(l,beg+1,end-1))
    dp[beg][end]=max(ans1,ans2)
    return max(ans1,ans2)

n = int(input())
l = list(map(int, input().split()))
tl = sum(l)
dp=[]
for _ in range(n):
    dp.append([-1]*n)
a = f(l,0,n-1)
b = tl-a
print(a-b)

Measured memory_bytes: 164488
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  long N,i,j;
  cin >> N;
  i=1;
  j=N;
  for (long k = 2;k*k<=N;k++) {
    if (N%k==0) {
      i=k;
      j=N/k;
    };
  };
  cout << i+j-2 << endl;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int num = 0;
        if (k % 2 == 0) {
            num = (int) Math.pow(k / 2, 2);
        } else {
            num = (k / 2) * (k / 2 + 1);
        }
        System.out.println(num);
    }
}

Measured memory_bytes: 23764
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
	int x,y;
}a[100005];
bool cmp1(node m,node n)
{
	if(m.x!=n.x)
		return m.x>n.x;
	else 
		return m.y<n.y;
}
bool cmp2(node m,node n)
{
	return m.y>n.y;
}
int main()
{
	int n,h,i;
	cin>>n>>h;
	for(i=1;i<=n;i++)
		scanf("%d%d",&a[i].x,&a[i].y);
	sort(a+1,a+n+1,cmp1);
	sort(a+2,a+n+1,cmp2);
	cout<<endl;
/*	for(i=1;i<=n;i++)
		cout<<a[i].x<<" "<<a[i].y<<endl;*/
		
	int maxi;
	if(a[1].y>a[2].y) 
		maxi=1;
	else 
		maxi=2;
	int j=2,ans=0;
	while(h>0)
	{
		if(h-a[maxi].y<=0)
		{
			ans++;
			break;
		}
		//cout<<j<<endl;
		if(a[j].y>a[1].x)
		{
			ans++;
			h-=a[j].y;
			j++;
			if(a[1].y>a[j].y) 
				maxi=1;
			else 
				maxi=j;
			
		}
		else 
		{
			ans++;
			h-=a[1].x;
		}
	//	cout<<h<<" ";
	}
	cout<<ans<<endl;
}
/*
5 500
35 44
28 83
46 62
31 79
40 43
*/

Measured memory_bytes: 1024
//#include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
//#include "boost/multiprecision/cpp_int.hpp"
//typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2

ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
typedef pair<dd,dd> d_d;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fi first
#define se second
#define endl "\n"
#define SORT(v) sort(v.begin(),v.end())
#define ERASE(v) v.erase(unique(v.begin(),v.end()),v.end())
#define POSL(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define POSU(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
//template<class T>void max(T a,T b){if(a<b)return b;else return a;}
//template<class T>void min(T a,T b){if(a>b)return b;else return a;}
//template<class T>bool Max(T&a, T b){if(a < b){a = b;return 1;}return 0;}
//template<class T>bool Min(T&a, T b){if(a > b){a = b;return 1;}return 0;}

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

struct ITEM{
    ll sum;
    ll xp,yp,zp;
    bool operator<(const ITEM op)const{
        return sum<op.sum;
    }
};

ll N=10000*10000;
ll M=10000;

int main(){fastio
    ll x,y,z,k;cin>>x>>y>>z>>k;
    ll a[x],b[y],c[z];
    rep(i,0,x-1)cin>>a[i];
    rep(i,0,y-1)cin>>b[i];
    rep(i,0,z-1)cin>>c[i];
    sort(a,a+x);sort(b,b+y);sort(c,c+z);
    reverse(a,a+x);reverse(b,b+y);reverse(c,c+z);
    priority_queue<ITEM>q;
    q.push({a[0]+b[0]+c[0],0,0,0});
    set<ll>used;
    while(k>0){
        ITEM r=q.top();q.pop();
        ll pos=r.xp*N+r.yp*M+r.zp;
        if(used.find(pos)!=used.end()){
            continue;
        }
        k--;
        used.insert(pos);
        cout<<r.sum<<endl;//":";
        if(r.xp!=x-1)q.push({a[r.xp+1]+b[r.yp]+c[r.zp],r.xp+1,r.yp,r.zp});
        if(r.yp!=y-1)q.push({a[r.xp]+b[r.yp+1]+c[r.zp],r.xp,r.yp+1,r.zp});
        if(r.zp!=z-1)q.push({a[r.xp]+b[r.yp]+c[r.zp+1],r.xp,r.yp,r.zp+1});
        //cout<<r.xp<<" "<<r.yp<<" "<<r.zp<<" "<<pos<<endl;
    }
    
    return 0;
}


Measured memory_bytes: 768
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <algorithm>
#include <numeric>
#include <utility>
#include <random>
#include <chrono>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>

using namespace std;

using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using PIL = pair<int, LL>;
using PLL = pair<LL, LL>;
const int mod = 1e9 + 7;
// const int mod = 998244353;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

int f[1 << 21];
int g[22][22];
int n;

inline void work() {
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> g[i][j];
        }
    }
    f[0] = 1;
    for (int i = 1; i < (1 << n); ++i) {
        int man = __builtin_popcount(i);
        for (int j = 0; j < n; ++j) {
            if ((i & (1 << j)) && g[man - 1][j]) {
                f[i] += f[i - (1 << j)];
                f[i] %= mod;
            }
        }
    }
    cout << f[(1 << n) - 1] << "\n";
}

int main() {
    // freopen("input.txt", "r", stdin);
    quickread();
    work();
    return 0;
}

Measured memory_bytes: 8448
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<vector<string> > pose(2, vector<string>(n));
    for(int i=0; i<2; ++i){
        for(int j=0; j<n; ++j){
            cin >> pose[i][j];
        }
    }

    vector<int> attack(2, 0);
    string ans = "Hikiwake-kun";
    for(int i=0; i<n; ++i){
        // 反則
        if(pose[0][i] == "kougekida" && attack[0] == 0){
            if(!(pose[1][i] == "kougekida" && attack[1] == 0)){
                ans = "Nakajima-kun";
                break;
            }
        }
        else if(pose[1][i] == "kougekida" && attack[1] == 0){
            ans = "Isono-kun";
            break;
        }

        if(pose[0][i] == "mamoru"){
            if(pose[1][i] == "mamoru"){
                /* nothing */
            }
            else if(pose[1][i] == "tameru"){
                attack[1] = min(attack[1] + 1, 5);
            }
            else{
                if(attack[1] == 5){
                    ans = "Nakajima-kun";
                    break;
                }
                attack[1] = 0;
            }
        }
        else if(pose[0][i] == "tameru"){
            if(pose[1][i] == "mamoru"){
                attack[0] = min(attack[0] + 1, 5);
            }
            else if(pose[1][i] == "tameru"){
                attack[0] = min(attack[0] + 1, 5);
                attack[1] = min(attack[1] + 1, 5);
            }
            else{
                ans = "Nakajima-kun";
                break;
            }
        }
        else{
            if(pose[1][i] == "mamoru"){
                if(attack[0] == 5){
                    ans = "Isono-kun";
                    break;
                }
                attack[0] = 0;
            }
            else if(pose[1][i] == "tameru"){
                ans = "Isono-kun";
                break;
            }
            else{
                if(attack[0] < attack[1]){
                    ans = "Nakajima-kun";
                    break;
                }
                else if(attack[1] < attack[0]){
                    ans = "Isono-kun";
                    break;
                }
                attack[0] = 0;
                attack[1] = 0;
            }
        }
    }
    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 1224
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define ALL(n) begin(n),end(n)
#define IN(a, x, b) (a<=x && x<b)
#define INIT std::ios::sync_with_stdio(false);std::cin.tie(0);
template<class T> inline T CHMAX(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T CHMIN(T & a, const T b) { return a = (a > b) ? b : a; }

const long double EPS = 1e-10;
const long long INF = 1e18;
const long double PI = acos(-1.0L);

int main()
{
    INIT;
    int n, l;
    cin >> n >> l;
    int ans = 0;
    int delicious = 0;
    int mindeli = 1e9;
    bool sei = true;
    for(int i=1; i <= n; i++){
        delicious = l + i - 1;
        if(mindeli > abs(delicious)){
            mindeli = abs(delicious);
            if(delicious <= 0){
                sei = false;
            }
        }
        ans += delicious;
    }
    if(sei) cout << ans - mindeli << endl;
    else cout << ans + mindeli << endl;
    return 0;
}

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

#define ll long long int
#define rep(i, a, n) for (ll i = a; i < n; i++)
#define repm(i, a, n) for (ll i = a; i >= n; i--)
#define INF 1e9
#define LINF 1e17
#define MOD (int)(1e9 + 7)
#define mod (int)(1e9 + 7)
#define pi 3.141592653589
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vll vector<ll>
#define vd vector<double>
#define vb vector<bool>
#define vs vector<string>
#define vii vector<vector<int>>
#define vllll vector<vector<ll>>
#define ALL(a) (a).begin(), (a).end()

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

void print(bool f)
{
    cout << (f ? "Yes" : "No") << endl;
}

void Print(bool f)
{
    cout << (f ? "YES" : "NO") << endl;
}

struct Q
{
    int a, b, c, d;
};

int main(void)
{
    ll n, m, q;
    cin >> n >> m >> q;
    vector<Q> vq(q);
    rep(i, 0, q) cin >> vq[i].a >> vq[i].b >> vq[i].c >> vq[i].d;

    queue<vll> que;
    rep(i, 0, m) que.push({i + 1});
    while (1)
    {
        vll a = que.front();
        if (a.size() == n)
            break;
        que.pop();
        rep(i, a.back(), m + 1)
        {
            vll y = a;
            y.push_back(i), que.push(y);
        }
    }

    ll ans = 0;
    while (!que.empty())
    {
        ll sum = 0;
        vll a = que.front();
        que.pop();
        rep(i, 0, q)
        {
            if (a[vq[i].b - 1] - a[vq[i].a - 1] == vq[i].c)
                sum += vq[i].d;
        }
        ans = max(ans, sum);
    }

    cout << ans << endl;
}

Measured memory_bytes: 14692
#!/usr/local/bin/rdmd
import std.stream;
import std.cstream;
import std.algorithm;

int R,C;
bool[10000][10] MAP;
void main(){
  while(true){
    din.readf(&R,&C);
    if(R==0&&C==0)break;
    for(int i=0;i<R;i++){
      for(int j=0;j<C;j++){
        int t;
        din.readf(&t);
        MAP[i][j]=(t==1);
      }
    }
    int ans=0;
    for(int i=0;i<(1<<R);i++){ // 全ての横の列の反転を試す
      int tmpans=0;
      for(int j=0;j<C;j++){
        int count=0;
        for(int k=0;k<R;k++){
          if(MAP[k][j]^((i>>k)&1))count++;
        }
        tmpans+=max(count,R-count);
      }
      ans=max(ans,tmpans);
    }
    dout.writefln(ans);
  }
}

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

int n;
P x[50];
int dp[2][SIZE];

void solve(){
	rep(i,2) rep(j,SIZE) dp[i][j] = 0;
	rep(i,n) cin >> x[i].first >> x[i].second;
	rep(i,n) x[i].second *= 2;
	sort(x,x+n,greater<P>());
	int ans = 0;
	rep(i,n){
		int dir = 0;
		int ti = 0;
		while(x[i].second){
			int tmpti = ti;
			if(ti == 0) ti = ti+x[i].first;
			else ti = max(ti+x[i].first,dp[dir][ti-1]);
			dp[dir][tmpti] = max(dp[dir][tmpti],ti);
			dir = dir^1;
			x[i].second--;
		}
		rep(j,2) for(int k = 1; k < SIZE; k++) dp[j][k] = max(dp[j][k],dp[j][k-1]);
	}
	rep(i,2) rep(j,SIZE) ans = max(ans,dp[i][j]);
	cout << ans << endl;
}

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

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

using ll = long long;
using namespace std;

#define swap(a, b)    do { typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while(false)
#define repd(i, a, b) for(typeof(b) i = a; i < (b); ++i)
#define rep(i, n)     repd(i, 0, n)
#define dump(x)       cerr << #x << " = " << (x) << endl
#define debug(x)      cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl
#define YN(x)         cout << ((x) ? "YES" : "NO") << endl
#define Yn(x)         cout << ((x) ? "Yes" : "No") << endl
#define yn(x)         cout << ((x) ? "yes" : "no") << endl
#define absi(x)       ( ((x) >= 0) ? (x) : (-(x)) )
#define debugvec(v)   rep(i, v.size()) cerr << "L" << __LINE__ << ": " << #v << "[" << i << "] = " << v[i] << endl;
#define debugvec2(v)  cerr << "L" << __LINE__ << ": " << #v << " = { "; rep(i, v.size()) cerr << v[i] << (i + 1 == v.size() ? "" : ", "); cerr << " }" << endl;

ll gcd(ll a, ll b) { return __gcd(a, b); }
ll lcm(ll a, ll b) { return (a == 0 || b == 0) ? 0 : (a / __gcd(a, b) * b); }

int main(int argc, const char* argv[]) {
	ll m, d;
	cin >> m >> d;

	ll ret = 0;

	repd(i, 1, d + 1) {
		repd(j, 1, m + 1) {
			ll d1  = i % 10;
			ll d10 = i / 10;

			if(d1 < 2 || d10 < 2) {
				continue;
			}

			if(d1 * d10 == j) {
				++ret;
			}
		}
	}

	cout << ret << endl;

	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll p=1000000007;
ll bin(ll val,ll po)
{
	if(po==0)
		return 1;
	ll ans=1;
	if(po%2==1)
	{
		po-=1;
		ans=ans*val%p;
	}
	ll p1=bin(val,po/2);
	ans=ans*p1%p;
	ans=ans*p1%p;
	return ans;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	    ll n,k;
	    cin>>n>>k;
	    ll fact[n+1];
	    fact[0]=1;
	    for(ll i=1;i<=n;i++)
	    	fact[i]=fact[i-1]*i%p;
	    ll finv[n+1];
	    finv[0]=1;
	    for(ll i=1;i<=n;i++)
	    {
	    	finv[i]=finv[i-1]*bin(i,p-2)%p;
	    	//cout<<finv[i]<<endl;
	    }
	    for(ll i=1;i<=k;i++)
	    {
	    	if((n-k+1)>=i)
	    	{
	    		ll ans=fact[k-1]*fact[n-k+1]%p;
	    		//cout<<ans<<endl;
	    		ans=ans*finv[i-1]%p;
	    		//cout<<ans<<endl;
	    		ans=ans*finv[k-i]%p;
	    		//cout<<ans<<endl;
	    		ans=ans*finv[n-k+1-i]%p;
	    		//cout<<ans<<endl;
	    		ans=ans*finv[i]%p;
	    		cout<<ans<<endl;
	    	}
	    	else
	    		cout<<"0\n";
	    }
}

Measured memory_bytes: 256
package main

import (
	"fmt"
	"sort"
)

func main() {
	var n, x int
	fmt.Scan(&n, &x)
	xs := make([]int, n)
	for i := range xs {
		fmt.Scan(&xs[i])
		xs[i] = abs(xs[i] - x)
	}

	fmt.Println(gcd(xs))
}

func abs(n int) int {
	if n < 0 {
		return -n
	}
	return n
}

func gcd(s []int) int {
	sort.Sort(sort.Reverse(sort.IntSlice(s)))
	var a, b, r int
	for i := range s {
		if i == 0 {
			a = s[i]
			continue
		}
		b = s[i]
		r = a % b

		for r != 0 {
			a = b
			b = r
			r = a % b
		}

		a = b
	}
	return a
}


Measured memory_bytes: 5504
#include <bits/stdc++.h>
using namespace std;
template <class T> void inline chmax(T& a,T b){if(a<b)a=b;};


int main(){
    int N;
    cin >> N;
    vector<int> a(N);
    for (int i=0;i<N;i++){
        cin >> a[i];
    }
    sort(a.begin(),a.end());
    int x=a[0];int y=a[N-1];
    int ans;
    for (int i=0;i<N;i++ ){

    }
    for (int i=0;i<N;i++){
   chmax<int>(ans, lower_bound(a.begin(),a.end(),a[i]+3) - upper_bound(a.begin(),a.end(),a[i]-1));
}
cout << ans <<endl;
}

Measured memory_bytes: 640
void main()
{
    long a, b, c, x, y;
    rdVals(a, b, c, x, y);

    if (a + b < 2 * c)
    {
        writeln(a * x + b * y);
        return;
    }

    long z = min(x, y);
    long u = a * (x - z) + b * (y - z) + 2 * c * z;
    long v = 2 * c * max(x, y);

    min(u, v).writeln;
}

enum long mod = 10L^^9 + 7;
enum long inf = 1L << 60;
enum double eps = 1.0e-9;

T rdElem(T = long)()
if (!is(T == struct))
{
    return readln.chomp.to!T;
}

alias rdStr = rdElem!string;
alias rdDchar = rdElem!(dchar[]);

T rdElem(T)()
if (is(T == struct))
{
    T result;
    string[] input = rdRow!string;
    assert(T.tupleof.length == input.length);
    foreach (i, ref x; result.tupleof)
    {
        x = input[i].to!(typeof(x));
    }
    return result;
}

T[] rdRow(T = long)()
{
    return readln.split.to!(T[]);
}

T[] rdCol(T = long)(long col)
{
    return iota(col).map!(x => rdElem!T).array;
}

T[][] rdMat(T = long)(long col)
{
    return iota(col).map!(x => rdRow!T).array;
}

void rdVals(T...)(ref T data)
{
    string[] input = rdRow!string;
    assert(data.length == input.length);
    foreach (i, ref x; data)
    {
        x = input[i].to!(typeof(x));
    }
}

void wrMat(T = long)(T[][] mat)
{
    foreach (row; mat)
    {
        foreach (j, compo; row)
        {
            compo.write;
            if (j == row.length - 1) writeln;
            else " ".write;
        }
    }
}

import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
import std.range;
import std.math;
import std.numeric;
import std.mathspecial;
import std.traits;
import std.container;
import std.functional;
import std.typecons;
import std.ascii;
import std.uni;
import core.bitop;

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

using namespace std;
typedef long double ld;
typedef long long ll;
typedef pair<int,int>P;
map<P,int>mp;
int main(){ 
    int n;
    cin>>n;
    while(n--){ 
        ld s;
        cin>>s;
        ll k=ll(s*1000000000.0+0.5);
        ll cnt2=0,cnt5=0;
        while(k%2==0){ 
            k/=2,cnt2++;
        }
        while(k%5==0){ 
            k/=5;cnt5++;
        }
        mp[make_pair(cnt2,cnt5)]++;
    }
    ll ans=0;
    for(auto&v:mp){ 
        for(auto&u:mp){ 
            if(v.first.first+u.first.first>=18&&v.first.second+u.first.second>=18)
            {
               if(v==u)ans+=1ll*v.second*(v.second-1);
               else ans+=1ll*v.second*u.second;
            }
        }
    }
    cout<<ans/2<<endl;
    return 0;
}


Measured memory_bytes: 3684
#include <bits/stdc++.h>
#define rep(i, a) for(int i = 0; i < a; i++)
#define all(v) v.begin(), v.end()
#define dcout cout << fixed << setprecision(15)
#define arep(i, a) for(auto i : a)
#define F first
#define S second
#define VE vector<int>
#define pb(x) push_back(x)
#define MOD 1000000007
#define INF 10000000000
#define int long long
#define KETA(n) floor(log10(n) + 1)
using namespace std;
signed main() {
    char c;
    cin >> c;
    cout << (char)(c + 1);
    return 0;
}

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

using namespace std;
typedef long long int ll;
typedef pair <ll,ll> P;
#define REP(i, n) for(int i = 0; i < (int)(n); i++)
#define REPR(i, n) for(int i = n-1; i >= 0; i--)
#define FOR(i, n, m) for(int i = n; i < (int)(m); i++)
#define PRINT(x) cout << x << endl
#define ALL(v) v.begin(), v.end()
#define INF (ll)1e18

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

ll MOD = 1000000007;

// union by size + path having
class UnionFind {
public:
    vector <ll> par; // 各元の親を表す配列
    vector <ll> siz; // 素集合のサイズを表す配列(1 で初期化)

    // Constructor
    UnionFind(ll sz_): par(sz_), siz(sz_, 1LL) {
        for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
    }
    void init(ll sz_) {
        par.resize(sz_);
        siz.assign(sz_, 1LL);  // resize だとなぜか初期化されなかった
        for (ll i = 0; i < sz_; ++i) par[i] = i; // 初期では親は自分自身
    }

    // Member Function
    // Find
    ll root(ll x) { // 根の検索
        while (par[x] != x) {
            x = par[x] = par[par[x]]; // x の親の親を x の親とする
        }
        return x;
    }

    // Union(Unite, Merge)
    bool merge(ll x, ll y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        // merge technique（データ構造をマージするテク．小を大にくっつける）
        if (siz[x] < siz[y]) swap(x, y);
        siz[x] += siz[y];
        par[y] = x;
        return true;
    }

    bool issame(ll x, ll y) { // 連結判定
        return root(x) == root(y);
    }

    ll size(ll x) { // 素集合のサイズ
        return siz[root(x)];
    }
};

int main()
{
    string S;
    cin >> S;
    string SR = S;
    reverse(ALL(SR));
    ll l = 0;
    ll r = 0;
    ll ans = 0;
    while(l < S.length() || r < S.length()) {
        if (S[l] != SR[r]) {
            if (S[l] != 'x' && SR[r] != 'x') {
                PRINT(-1);
                return 0;
            } else {
                if(S[l] == 'x') {
                    l++;
                    ans++;
                } else {
                    r++;
                    ans++;
                }
            }
        } else {
            l++;
            r++;
        }
    }
    PRINT(ans/2);
    return 0;
}


Measured memory_bytes: 512
from math import factorial as ft

N = int(input())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]

num = [1 for i in range(1,N+1)]
t1 = 0
for a in range(len(p)):
    t1 += ft(N-a-1)*num[:p[a]].count(1)
    num[p[a]-1] = 0
    # print(N-a-1, t1,num)
# print(t1)

num = [1 for i in range(1,N+1)]
t2 = 0
for a in range(len(q)):
    t2 += ft(N-a-1)*num[:q[a]].count(1)
    num[q[a]-1] = 0
    # print(N-a-1, t2,num)
# print(t2)
print(abs(t1-t2))

Measured memory_bytes: 3064
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int solve(int hi,int cs);
int d,n;
int t[1000];
int ta[1111],tb[1111],c[1111];
int mem[1111][1111]={0};
int main(){
  cin>>d>>n;
  for(int i=0;i<d;i++){
    cin>>t[i];
  }
  for(int i=0;i<n;i++){
    cin>>ta[i]>>tb[i]>>c[i];
  }
  cout<< solve(0,0)<<endl;;
}
int solve(int hi,int yc){
  int maxx=0;
  if(mem[hi][yc]!=0){
    return mem[hi][yc];
  }
  if(hi==d){
    return mem[hi][yc]=0;
  }else{
    for(int i=0;i<n;i++){
      if(ta[i]<=t[hi] && t[hi]<=tb[i]){
	maxx=max(maxx,solve(hi+1,c[i])+(hi!=0?abs(yc-c[i]):0));
      }
    }
    return mem[hi][yc]=maxx;
  }
}
  

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

int main(){
	int n,b,f,r,v,aT[3][10],bT[3][10],cT[3][10],dT[3][10];
	cin>>n;
	for(int i=0;i<3;i++){
		for(int j=0;j<10;j++){
			aT[i][j]=0;
			bT[i][j]=0;
			cT[i][j]=0;
			dT[i][j]=0;
		}
	}
	for(int i=0;i<n;i++){
		cin>>b>>f>>r>>v;
		switch(b){
			case 1:aT[f-1][r-1]+=v; break;
			case 2:bT[f-1][r-1]+=v; break;
			case 3:cT[f-1][r-1]+=v; break;
			case 4:dT[f-1][r-1]+=v; break;
		}
	}
	for(int i=0;i<3;i++){
		for(int j=0;j<10;j++){
			cout<<" "<<aT[i][j];
		}
		cout<<endl;
	}
	cout<<"####################"<<endl;
	for(int i=0;i<3;i++){
		for(int j=0;j<10;j++){
			cout<<" "<<bT[i][j];
		}
		cout<<endl;
	}
	cout<<"####################"<<endl;
	for(int i=0;i<3;i++){
		for(int j=0;j<10;j++){
			cout<<" "<<cT[i][j];
		}
		cout<<endl;
	}
	cout<<"####################"<<endl;
	for(int i=0;i<3;i++){
		for(int j=0;j<10;j++){
			cout<<" "<<dT[i][j];
		}
		cout<<endl;
	}
	return 0;
}

Measured memory_bytes: 1168
// In god we trust
 
#include <bits/stdc++.h>
#include <chrono>
#include <algorithm>
#include <signal.h>
#include <thread>
 
#define pb push_back
#define fi first
#define se second
#define Vi vector<int>
#define Vb vector<bool>
#define Pi pair<int, int>
#define MP make_pair
#define MT make_tuple
#define Q queue
#define Qi Q<int>
#define INF 1000000000 
#define LINF 1000000000000000000
#define int long long
using namespace std;
using namespace std::chrono;



signed main(){
  ios_base::sync_with_stdio(false); 
  cin.tie(NULL);

  int a, b, c, k;
  cin >> a >> b >> c >> k;
  if ( k <= a)
    cout << k << endl;
  else if ( k > a + b)
    cout << a - (k - (a + b)) << endl;
  else
    cout << a << endl;
}


Measured memory_bytes: 3544
from sys import stdin
N = int(stdin.readline().rstrip())
X = [[int(x) for x in stdin.readline().rstrip().split()] for _ in range(N)]
X.sort(key=lambda x: x[2])
a, b, c = X[-1]

for px in range(101):
    for py in range(101):
        H = abs(a - px) + abs(b - py) + c
        for k in range(N):
            if X[k][2] != max(H - abs(X[k][0] - px) - abs(X[k][1] - py), 0):
                break
            if k == N - 1:
                print(px, py, H)

Measured memory_bytes: 3064
import collections
N, M = [int(_) for _ in input().split()]
UV = [[int(_) for _ in input().split()] for _ in range(M)]
s, t = [int(_) for _ in input().split()]
v = [[0] * (N + 1) for _ in range(3)]
v[0][s] = 1
G = collections.defaultdict(set)
for U, V in UV:
    G[U].add(V)
Q = collections.deque([[0, s]])
while Q:
    d, i0 = Q.popleft()
    d += 1
    for i1 in G[i0]:
        if not v[1][i1]:
            v[1][i1] = 1
            for i2 in G[i1]:
                if not v[2][i2]:
                    v[2][i2] = 1
                    for i3 in G[i2]:
                        if i3 == t:
                            print(d)
                            exit()
                        elif not v[0][i3]:
                            v[0][i3] = 1
                            Q += [[d, i3]]
print(-1)


Measured memory_bytes: 103256
#include <iostream>

using namespace std;

int main(void) {
  const int inf = 100000000;

  int n, m;
  cin >> n >> m;

  int a[1000], b[1000], c[1000], dist[100][100];

  for (int i = 0; i < m; ++i) {
    cin >> a[i] >> b[i] >> c[i];
    a[i]--, b[i]--;
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      if (i == j)
        dist[i][j] = 0;
      else
        dist[i][j] = inf;
    }
  }

  for (int i = 0; i < m; ++i) {
    dist[a[i]][b[i]] = min(dist[a[i]][b[i]], c[i]);
    dist[b[i]][a[i]] = min(dist[b[i]][a[i]], c[i]);
  }

  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }

  int ans = m;
  for (int i = 0; i < m; ++i) {
    bool shortest = false;
    for (int j = 0; j < n; ++j)
      if (dist[j][a[i]] + c[i] == dist[j][b[i]])
        shortest = true;
    if (shortest == true) {
      ans = ans - 1;
    }
  }

  cout << ans << endl;
}

Measured memory_bytes: 256