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

#include <ext/pb_ds/assoc_container.hpp> //required
#include <ext/pb_ds/tree_policy.hpp> //required

using namespace __gnu_pbds; 
using namespace std;

template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; 
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
typedef long long  ll;
typedef vector<ll> VL;
typedef vector<int> VI;
typedef pair<ll,ll> PLL;
typedef pair<int,int> PII;
#define pb push_back

#define F first
#define S second
#define SZ(a) int((a).size())
#define ALL(a) a.begin(),a.end()
#define fr(i,x,y) for(int i=x;i<y;i++)
#define frr(i,x,y) for(int i=x-1;i>=y;i--)
#define inf 1e18+1
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
const int mod=1000000007;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll power(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;}a=a*a;b>>=1;}return ans;}
ll powerm(ll a,ll b){ll ans=1;while(b!=0){if(b&1){ans*=a;ans%=mod;}a=a*a;a%=mod;b>>=1;}return ans%mod;}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

void solve()
{
    ll n;
    cin>>n;
    string str;
    cin>>str;
    ll ct=0;
    ll i=0,j=n-1;
    while(j>=0&&str[j]=='W')
    {
        j--;
    }
    while(i<n&&str[i]=='R')
    {
        i++;
    }
    while(i<j)
    {
        if(str[i]=='W'&&str[j]=='R')
        {
            swap(str[i],str[j]);
            ct++;
        }
        while(j>=0&&str[j]=='W')
        {
            j--;
        }
        while(i<n&&str[i]=='R')
        {
            i++;
        }
    }
    cout<<ct<<endl;
    return;
}
int main()
{
    IOS;
    ll t=1;
    //cin>>t;
    while(t--)
    {
        //cout<<"Case #"<<pp-t<<":\n";
        solve();
    }
    return 0;
}
/* stuff you should look for
        * int overflow, array bounds
        * special cases (n=1?)
        * do smth instead of nothing and stay organized
        * WRITE STUFF DOWN
*/
/*
recursion - matrix exponential

*/
// BITMASK:
// 1)When some constrall is of the order of 15-20, think of bitmask DP.
// 2)When some constrall is around 40, try out meet in the middle
// 3) See Strings,palindromes,prefix,suffix etc -> KMP,Z algorithm

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

template <typename T>
bool chmax(T &a, const T& b) {
  if (a < b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}

template <typename T>
bool chmin(T &a, const T& b) {
  if (a > b) {
    a = b;  // aをbで更新
    return true;
  }
  return false;
}

typedef long long ll;
const ll INF = 1e16;
int a, b, c, x, y;
ll ans = INF;

int main(){
    cin >> a >> b >> c >> x >> y;
    int n = max(x, y);
    for(int i=0; i<=n; i++){
        ll sum = 2*c*i +  a*max(0, x-i) + b*max(0, y-i);
        if(sum < ans) ans = sum;
    }
    cout << ans << endl;
    return 0;
}

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

int main() {
  char c[3][3];
  for (int i = 0; i < 3; ++i) {
    for (int j = 0; j < 3; ++j) {
      cin >> c[i][j];
    }
  }

  for (int i = 0; i < 3; ++i) {
    cout << c[i][i];
  }
  cout << endl;
}

Measured memory_bytes: 256
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,K = MI()
    H=LI()
    
    dp = [[0]+[inf]*(N) for _ in range(N)] #dp[right][size]: 最も右の項right, いじってないやつの数size
    for i in range(N):
        dp[i][1] = H[i]
    dp[0][1]=H[0]
    for right in range(N):
        for size in range(2,N+1):

            for i in range(right):
                dp[right][size] = min(dp[right][size], dp[i][size-1]+max(0, H[right]-H[i]))
    ans = inf 
    for i in range(N):
        ans = min(ans,dp[i][N-K])
    # print(*dp,sep="\n")
    print(ans)
if __name__ == '__main__':
    main()

Measured memory_bytes: 47708
#include "bits/stdc++.h"
using namespace std;

#define ASC(vec) vec.begin(), vec.end()    // 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend() // 降順ソート 例：sort(DESC(vec));
#define rep(i, n) for (int i = 0; i < (n); i++)
#define Rep(i, n) for (int i = 1; i < n; i++)
#define REP(i, vec) for (auto i = vec.begin(); i != vec.end(); ++i)

const int       mod = 1000000007;
const int       inf = (1 << 21);
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;
using vll = vector<pair<ll, ll>>;

template<class T>
inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T>
inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T>
T GCD(T a, T b) {
	if (b == 0) return a;
	else return GCD(b, a % b);
}
template<class T>
T LCM(T a, T b) {
	return a / GCD(a, b) * b;
}
template<class T>
inline T round_int(T a, T b) {return (a + (b - 1)) / b; }

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };

// ──────────────────────────────────────────────────────────────────

int main() {
	ll n, k;
	cin >> n >> k;
	
	vector<pair<ll, ll>> p(n);
	rep(i, n) {
		ll a, b;
		cin >> a >> b;
		p[i] = make_pair(a, b);
	}

	ll now = 0;
	sort(ASC(p));

	for (ll i = 0; i < n; ++i) {
		if (k <= now + p[i].second) {
			cout << p[i].first << endl;
			return 0;
		}
		else {
			now += p[i].second;
		}
	}
}


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

using namespace std;

typedef long long ll;
typedef long double ld;
#define gap ' '
#define L(i) ((i) - (&i))

const int MAXN = 1e5+50;
const int MAX = 1e6 + 1;
const int MOD = 1e9+7;
const int INF = 1e9 + 5;
ll f[MAXN];

int main(){	
	int n, m;cin >> n >> m;
	f[0] = 1;
	for(int i = 1; i <= 1e5; ++i)
		f[i] = f[i-1]*i%MOD;
	int cnt = 0;
	if(abs(n-m)==1)
		cnt = 1;
	if(abs(n-m)==0)
		cnt = 2;
	cout << (cnt*f[n]*f[m])%MOD << endl;
	return 0;
}

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

int main() {
    uint64_t N, A, B;
    cin >> N >> A >> B;

    if(A == 0) {
        cout << 0 << endl;
        return 0;
    }

    uint64_t a = N / (A + B);
    uint64_t b = N % (A + B);
    uint64_t counter = 0;

    for(uint64_t i = 0; i < a; i++) {
        counter += A;
    } 
    counter += b > A ? A : b;

    cout << counter << endl;

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main() {
  string S;
  long long K;
	cin >> S >> K;
	for (int i = 0; i < min((long long)S.size(), K); i++) {
		if (S[i] >= '2') { cout << S[i] << endl; return 0; }
	}
	cout << '1' << endl;
	return 0;
}


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

using namespace std;

long long calc(long long h, long long w) {
    if (h == 1 || w == 1) {
        return 1;
    }
    if (h % 2 == 0 || w % 2 == 0) {
        return h * w / 2;
    }
    return h * w / 2 + 1;
}

int main() {
    long long h, w;
    cin >> h >> w;
    cout << calc(h, w) << endl;
    return 0;
}

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

int n, cntMinus;
int a[1000010];
long long ans;

int main()
{
    std::ios::sync_with_stdio(0);
    std::cin.tie(0);
    std::cin >> n;
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i], cntMinus += a[i] <= 0;
    if (cntMinus % 2)
    {
        int _min = 0x7f7f7f7f;
        for (int i = 1; i <= n; ++i)
            ans += a[i] > 0 ? a[i] : -a[i], _min = std::min(_min, a[i] > 0 ? a[i] : -a[i]);
        ans -= 2 * 1ll * _min;
    }
    else
        for (int i = 1; i <= n; ++i)
            ans += a[i] > 0 ? a[i] : -a[i];
    std::cout << ans << std::endl;
    return 0;
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define all(v) (v).begin(),(v).end()
 
#define PII pair<int,int>
#define st first
#define nd second
#define pb push_back
#define lint long long int
#define VI vector<int>

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}

template<class T> ostream &operator<<(ostream &os, vector<T> V){
  os<<"[";for(auto vv:V)os<<vv<<",";return os<<"]";
} 

template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
  return os << "(" << P.st << "," << P.nd << ")";
}


#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
 
#define make( x) int (x); scanf("%d",&(x));
#define make2( x, y) int (x), (y); scanf("%d%d",&(x),&(y));
#define make3(x, y, z) int (x), (y), (z); scanf("%d%d%d",&(x),&(y),&(z));
#define make4(x, y, z, t) int (x), (y), (z), (t); scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define makev(v,n) VI (v); FOR(i,0,(n)) { make(a); (v).pb(a);} 
#define IOS ios_base::sync_with_stdio(0)
#define HEAP priority_queue
 
#define read( x) scanf("%d",&(x));
#define read2( x, y) scanf("%d%d",&(x),&(y));
#define read3(x, y, z) scanf("%d%d%d",&(x),&(y),&(z));
#define read4(x, y, z, t) scanf("%d%d%d%d",&(x),&(y),&(z),&(t));
#define readv(v,n) FOR(i,0,(n)) { make(a); (v).pb(a);}
#define jeb fflush(stdout)


int main () {
	make(n);
	makev(v, n);
	sort(all(v));
	map<int, int> mapka;
	FOR(i,0, v.size()) mapka[v[i]]++;
	VI x;
	for(auto i = mapka.begin(); i!=mapka.end();i++) {

		if (i->nd >= 4) {
			x.pb(i->st);
			x.pb(i->st);
		} else if (i->nd >= 2) {
			x.pb(i->st);
		}
	}
	sort(all(x));
	if (x.size() < 2) {
		printf("0\n");
	} else {
		printf("%lld\n", x.back()*1LL*x[x.size()-2]);
	}
}


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

#define pb push_back
#define mk make_pair
#define rep(i, a, b) for(int i = int(a); i < int(b); i++)
#define all(S) (S).begin(), (S).end()
#define debug(a) cerr << #a << " = " << a << endl;
#define debug2(a,b) cerr << #a << " = " << a << " --- " << #b << " = " << b << endl;
#define debugM(M, l, c ) rep( i, 0, l ){ rep( j, 0, c ){ cerr << M[i][j] << " "; } cerr << endl; }
#define debugP(p) cerr << #p << " = " << p.x << " --- " << #p << " = " << p.y << endl;
#define MAX 20005

using namespace std;

typedef long long int ll;
typedef pair<int, int> ii;
const int INF = 0x3f3f3f3f;

int v[105], custo[105];
ll pd[105][1000005];

int main(void) {
	int n, k;
	
	cin >> n >> k;
	rep(i, 0, n)
		cin >> custo[i] >> v[i];
		
	for(int i = 0; i <= n; i++) {
		for(int w = 0; w <= k; w++) {
			if(i == 0 || w == 0)
				pd[i][w] = 0;
			else if(custo[i-1] <= w)
				pd[i][w] = max(pd[i-1][w-custo[i-1]] + v[i-1], pd[i-1][w]);
			else
				pd[i][w] = pd[i-1][w];
		}
	}
	
	cout << pd[n][k] << endl;	
	return 0;
}

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

using namespace std;

const int N = 2e5;
int n, m;
long long k, a[N + 1], b[N + 1];

int main() {
    cin.tie(0);
    cout.tie(0);
    ios_base::sync_with_stdio(0);

    cin >> n >> m >> k;

    for (int i = 0; i < n; ++i) {
        cin >> a[i + 1];
        a[i + 1] += a[i];
    }
    for (int j = 0; j < m; ++j) {
        cin >> b[j + 1];
        b[j + 1] += b[j];
    }
    int res = 0;
    for (int i = 0, j = m; i <= n; ++i) {
        if (a[i] > k)
            break;
        while (a[i] + b[j] > k)
              --j;
        res = max(i + j, res);

    }
    cout << res << endl;
}


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

int main(){
    int count=0;
    int A[100],n;
    cin >> n;
    for (int i=0;i<n;i++){
        cin >> A[i];
    }
    int flag = 1;
    while (flag){
     flag = 0;
     for (int j=n-1;j>=1;j--){
        if (A[j] < A[j-1]){
         swap(A[j],A[j-1]);
         count++;
         flag = 1;
        }
        }
    }
    for (int i=0;i<n;i++){
        if (i!=0){cout << " ";};
        cout << A[i];
    }
    cout << endl;
    cout << count << endl;
}



Measured memory_bytes: 3092
#require 'prime'
#require 'openssl'
MOD=10**9+7;  cnt=0; sum=0; prev=nil; can=true; h=Hash.new(0)
def gs() gets.chomp end
def gi() gets.chomp.to_i end
def gsmi() gets.chomp.split.map(&:to_i) end

def ar2(s1,s2,de=nil) Array.new(s1){Array.new(s2,de)} end
def ar3(s1,s2,s3,de=nil) Array.new(s1){Array.new(s2){Array.new(s3,de)}} end

def desc(ar) ar.sort!{|x,y| y<=>x} end
def min(a,b) a<=b ? a : b end
def max(a,b) a>=b ? a : b end
def sum(ar) ar.inject(:+).to_i end
def ceil(a,b) (a.to_f/b).ceil end

def C(a,b) b==0||a==b ? 1 : (b=a-b if a/2<b;(a-b+1..a).inject(:*)/(1..b).inject(:*)) end
def rui(ar) s=[0]; ar.size.times{|i| s<<s[-1]+ar[i]}; s end
def rev_rui(ar) s=[0]; ar.size.times.reverse_each{|i| s<<s[-1]+ar[i]}; s end

def puts_yesno(b) puts(b ? 'Yes' : 'No') end
def putsend(s) puts s; exit end
def pu(h) begin; require './../nemi_lib/debug.rb'; puts_val(h); rescue LoadError; end; end
########### ( ˘ω˘ )ｽﾔｧ… ###########
x=gsmi
puts x.index(0)+1

Measured memory_bytes: 14328
package main

import (
	"fmt"
	"os"
)

var (
	s string
)

func main() {
	fmt.Scan(&s)
	for i := len(s) - 1; i > 0; i-- {
		if yes(s[0:i]) {
			fmt.Println(i)
			os.Exit(0)
		}
	}
}

func yes(s string) bool {
	if len(s)%2 == 1 {
		return false
	}

	if s[0:len(s)/2] == s[len(s)/2:] {
		return true
	}

	return false
}

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

int main()
{
    int n,w;
    cin>>n>>w;
    vector<pair<long int ,long int>> arr(n);
    for(int i=0;i<n;i++)
        cin>>arr[i].first>>arr[i].second;
    int v_max=0;
    for(int i=0;i<n;i++)
    {
        v_max+=arr[i].second;
    }
    vector<vector<long int>> dp(n+1,vector<long int>(v_max+1,INT_MAX));
    for(int i=0;i<=n;i++)
    dp[i][0]=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=v_max;j++)
        {
            if(j>=arr[i-1].second)
            dp[i][j]=min(dp[i-1][j],dp[i-1][j-arr[i-1].second]+arr[i-1].first);
            else
            dp[i][j]=dp[i-1][j];
        }
    }
    int res=0;
    for(int i=v_max;i>=0;i--)
    {
        if(dp[n][i]<=w)
        {
            res=i;
            break;
        }
    }
    cout<<res;
    return 0;
}

Measured memory_bytes: 80256
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>

using namespace std;

int min(int a, int b){
    return a <= b ? a : b;
}

int main(){
    int n, a[150000], b[150000];
    cin >> n;
    for(int i=0; i<n-1; i++){
        cin >> b[i];
    }
    a[0] = b[0];
    for(int i=1; i<n-1; i++){
        a[i] = min(b[i], b[i-1]);
    }
    a[n-1] = b[n-2];
    int sum = 0;
    for(int i=0; i<n; i++){
        sum += a[i];
    }
    cout << sum << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define all(n) begin(n),end(n)
struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}star;
const long long INF = numeric_limits<long long>::max();
typedef long long ll;
typedef vector<int> vint;
typedef vector<char> vchar;
typedef vector<vector<int>> vvint;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;

int main()
{
    int N;
    ll ans=0;
    cin>>N;
    vint d(N);
    rep(i,N)
    {
        cin>>d[i];
    }
    sort(all(d));
    rep(i,100001)
    {
        auto itr = upper_bound(all(d),i);
        int idx = distance(d.begin(),itr);
        if(idx == N/2)
        {
            ans++;
        }
        if(idx > N/2)
        {
            break;
        }
    }
    cout<<ans;
    return 0;
}

Measured memory_bytes: 640
#include <set>
#include <map>
#include <list>
#include <stack>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;

int main(){
    int A,B;
    cin >>A >> B;
    int ans = 0,n = 1;
    while (true)
    {
        ans = 1 + (n - 1)*(A - 1);
        if(ans >= B)break;
        n++;
    }
    cout << n-1 << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <cmath>
#define ll long long
#define LINF 0x3f3f3f3f3f3f3f3f
#define INF 0x3f3f3f3f
using namespace std;
const int MAX = 1e5 + 10;
const int MOD = 1e9 + 7;
int N, M, K, Q;

int main()
{
	int i, j, k;
	ll n, s;
	scanf("%lld%lld", &n, &s);
	ll tmp;
	if (n == s) {
		printf("%lld", n + 1);
		return 0;
	}
	vector<ll> candidates;
	for (ll d = 2; d *d <= n; ++d) {
		tmp = n;
		ll sum = 0;
		while (tmp > 0) {
			sum += tmp % d;
			tmp /= d;
		}
		if (sum == s) {
			candidates.push_back(d);
		}
	}


	tmp = abs(n - s);
	vector<ll> factors;
	for (ll d = 1; d*d <= tmp; ++d) {
		if (tmp %d == 0) {
			factors.push_back(d);
			if (tmp / d != d) factors.push_back(tmp / d);
		}
	}
	for (i = 0; i < factors.size(); ++i) {
		ll d = factors[i];
		if (tmp % d == 0) {
			ll b = tmp / d + 1;
			if (n / b + n % b == s) {
				if(b > sqrt(n))
					candidates.push_back(b);
			}
			
		}
	}
	sort(candidates.begin(), candidates.end());
	if (candidates.empty()) printf("-1");
	else printf("%lld", candidates[0]);
}

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

int main() {
	int N;
	cin >> N;
	int max_val = 0;
	vector<int> h(N);
	for (auto& i : h) {
		cin >> i;
		max_val = max(max_val, i);
	}


	int count = 0;

	for (int i = 1; i <= max_val; i++) {
		for (int j = 0; j < N; j++) {
			if (h[j] >= i) {
				if (j - 1 >= 0 && h[j - 1] < i) count++;
				else if (j == 0) count++;
			}
		}
	}

	cout << count << endl;

	return 0;
}


Measured memory_bytes: 256
#include "bits/stdc++.h"
#define ll long long
#define rep2(i,a,b) for(int i=a;i<=b;++i)
#define rep(i,n) for(int i=0;i<n;i++)
#define pii pair<int,int>
#define ti3 tuple<int,int,int>
#define pq priority_queue<int>
#define pqg priority_queue<int,vector<int>,greater<int>>
ll int MOD=998244353;
#define N 3000000
 
using namespace std;

main(){
    int n;
    cin>>n;
    int a[N];
    rep(i,n){
        cin>>a[i];
    }
    sort(a,a+n);
    if(a[0]+1<a[n-1]){
        cout<<"No"; return 0;
    }
    if(a[0]==a[n-1]){
        if(a[0]==n-1||a[0]<=n/2) {cout<<"Yes"; return 0;}
        cout<<"No"; return 0;
    }
    int s=0,t=0;
    rep(i,n){
        if(a[i]==a[0])s++;
        else t++;
    }
    if(a[0]+1<=s+t/2&&a[0]+1>s){
        cout<<"Yes"; return 0;
    }
    cout<<"No"; return 0;
    
    return 0;
}


Measured memory_bytes: 2560
N, K = map(int, input().split())
x = [int(i) for i in input().split()]
ans = x[K - 1] - x[0] + min(abs(x[0]), abs(x[K - 1]))
for i in range(N - K + 1): 
    ans = min(ans, x[K + i -1] - x[i] + min(abs(x[K + i -1]), abs(x[i])) )
print(ans)

Measured memory_bytes: 14384
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<complex>
#include<stack>
#include<cmath>

using namespace std;

#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)


#define X real()
#define Y imag()
typedef complex<double> P;

int main(){
	
	map<int,int> m;
	int a;
	while(scanf("%d",&a)!=EOF){
		m[a]++;
	}
	
	typedef pair<int,int> pii;
	vector<pii> b;
	
	map<int,int>::iterator it = m.begin();
	while(it!=m.end()){
		b.push_back(pii(-it->second, it->first));
		it++;
	}
	
	sort(b.begin(),b.end());
	
	for(int i=0;i<b.size();i++){
		if(i!=0 && b[i].first!=b[i-1].first){
			break;
		}
		printf("%d\n",b[i].second);
	}
	
	
}

Measured memory_bytes: 1176
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long MOD=1e9+7;

/* WA→解説
余りは1,2,3より0,1,2のほうが方針が立てやすそう
実装力
単にlong long使わないからn/b*(b-k)でオーバーフロー？
*/
int main() {
  long long n,k;
  cin>>n>>k;
  if(k==0){
    cout<<n*n<<endl;
    return 0;
  }
  long long ans=0;
  for(long long b=k+1;b<=n;++b){
    ans+=n/b*(b-k);//bで割ったあまりが0,1,2,~b-1の周期が何回あるか
    if(n%b-k+1>0) ans+=n%b-k+1;
    //cout<<n/b*(b-k)<<" "<<max(0,n%b-k+1)<<endl;
  }
  cout<<ans<<endl;
}


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

int main(){
    int a, b, c, t;
    cin>>a>>b>>c;
    if(a>b){t=a; a=b; b=t;}
    if(b>c){t=b; b=c; c=t;}
    if(a>b){t=a; a=b; b=t;}
    cout<<a<<" "<<b<<" "<<c<<endl;
    return 0;
}


Measured memory_bytes: 3092
#include<iostream>
#include<vector>
#include<queue>
#include<utility>
using namespace std;
int w,h;
string matrix[20];

const int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};
bool valid(int x,int y)
{
    return (x>=0)&&(x<w)&&(y>=0)&&(y<h)&&(matrix[y][x]!='#');
}
int main()
{
    while(true)
    {
        queue<pair<int,int> > q;
        pair<int,int> man;
        int amount=1;
        
        cin>>w>>h;
        if(w==0&&h==0)
            break;
        for(int i=0;i<h;i++)
        {
            cin>>matrix[i];
            //cout<<matrix[i]<<endl;
        }
        
        for(int i=0;i<h;i++)
        {
            for(int j=0;j<w;j++)
            {
                if(matrix[i][j]=='@')
                {
                    man.first=j;
                    man.second=i;
                }
            }
        }
        
        q.push(man);
        while(!q.empty())
        {
            pair<int,int> current;
            current=q.front();
            q.pop();
            
            for(int i=0;i<4;i++)
            {

                if(valid(current.first+dx[i],current.second+dy[i]))
                {
                    q.push(make_pair(current.first+dx[i],current.second+dy[i]));
                    matrix[current.second+dy[i]][current.first+dx[i]]='#';
                    amount++;
                    //cout<<current.first+dx[i]<<","<<current.second+dy[i]<<endl;
                }
            }
            
            matrix[current.second][current.first]='#';
        }
        
        cout<<amount<<endl;
    }
}

Measured memory_bytes: 2984
import numpy as np

s = str(input())
y, m, d = s.split("/")

y, m, d = int(y), int(m), int(d)

if y < 2019:
  print("Heisei")
elif y==2019 and m<4:
  print("Heisei")
elif y==2019 and m==4 and d <= 30:
  print("Heisei")
else:
  print("TBD")


Measured memory_bytes: 18860
#lucas number
n = int(input())


l = []
l.append(2)
l.append(1)

#2 1 3 4 7 11 18
def get_lucas(l, n):
    l_n = l[n-2] + l[n-1]

    l.append(l_n)

    return l


for i in range(n + 1):
    if i > 1:
        l = get_lucas(l, i)


print(l[n])

Measured memory_bytes: 3060
N, x = map(int, input().split())
A = list(map(int, input().split()))
A = [0] + A
total = sum(A)

for i in range(1, N+1):
    if A[i-1] + A[i] > x:
        tmp = (A[i-1]+A[i]) - x
        A[i] -= tmp
        
print(total - sum(A))

Measured memory_bytes: 14540
#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using ll = long long;
using namespace std;
int main(){
    ll N, ans = 0;
    cin >> N;
    for (ll i = 1; i <= N - 1; i++)
    {
        ans += (N - 1) / i;
    }
    cout << ans << endl;
}


Measured memory_bytes: 3548
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>
#include <vector>
#include <map>
#include <queue>
#include <set>
#include <unordered_set>
#include <bitset>
#include <limits>
#include <algorithm>

#define MOD_BIG 1000000007
#define MOD_BIG2 998244353
using std::string;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
using ll = long long;
using pint = std::pair<int, int>;
using pll = std::pair<ll, ll>;
using std::vector;

#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define IFOR(i, begin, end) for(int i=(end)-1,i##_begin_=(begin);i>=i##_begin_;i--)
#define REP(i, n) FOR(i,0,n)
#define IREP(i, n) IFOR(i,0,n)

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


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

int main() {
    
    int N;
    cin >> N;
    vector<int> P(N);
    FOR(i, 0, N) {
        cin >> P[i];
    }
    int b = P[0];
    int cnt = 0;
    FOR(i, 1, N) {
        if (P[i] > b) {
            cnt++;
        }
        b = std::min(P[i], b);
    }
    cout << N - cnt << endl;
    
    return 0;
}


Measured memory_bytes: 1024
def gcd(a,b):
    if(a==0):
        return b
    return gcd(b%a,a)

a,b=[int(i) for i in input().split()]
val = gcd(a,b)

ct=1

for i in range(2,int(val**0.5)+2):
    if(val%i==0):
        ct+=1
        while(val%i==0):
            val//=i
if(val!=1):
    ct+=1
print(ct)


Measured memory_bytes: 3188
#include <bits/stdc++.h>
#include <unordered_map>
#define lli long long int
#define fl(i, a, b) for(int i = a; i < b; ++i)
#define fl2(i, a, b) for(int i = a; i <= b; ++i)
#define rfl(i, a, b) for(int i = a; i > b; --i)
#define rfl2(i, a, b) for(int i = a; i >= b; ++i)
#define all(v) ((v).begin(), (v).end())
#define all2(v) ((a) + 0, (a) + n);
#define sz(s) (s).size()
#define OO (lli)1e18
#define MIN INT_MIN
#define f first
#define s second		
#define mp make_pair
#define pb puch_back
#define debug(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
using namespace std;

lli power(lli x, lli y) {
	lli i, power = 1;
	if (y == 0)
		return 1;
	for (i = 1; i <= y; i++)
		power = power * x;
	return power;
}

int32_t main() {

	std::ios::sync_with_stdio(0);
	std::cin.tie(0);
	//freopen("overcode.in","r",stdin);
	//freopen("output.txt", "w", stdout);

	clock_t z = clock();

	int x;
	string str;
	cin >> x >> str;
	cout << (x >= 3200 ? str : "red") << '\n';

	debug("Total Time: %.3f\n", (double)(clock() - z) / CLOCKS_PER_SEC);
	return 0;
}

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

using namespace std;

template<class T>
void copy(vector<T> from, vector<T> to) {
  copy(from.begin(), from.end(), back_inserter(to));
}

template<class T>
void sort(vector<T> a) {
  sort(a.begin(), a.end());
}

template<class T>
void input_to(vector<T> a) {
  T a_i;
  cin >> a_i;
  a.push_back(a_i);
}

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

  if (a[a.length() - 1] == b[0] && b[b.length() - 1] == c[0]) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}

Measured memory_bytes: 256
#include<stdio.h>

int main(){
	long long int x[300];
	int limit;
	
	scanf("%d",&limit);
	
	for(int i=1;i<=limit;i++) {
		scanf("%d", &x[i]);
	}

	for(int i=1;i<=limit;i++){
	printf("node %d: key = %d, ",i,x[i]);
	if(i/2>0)
		printf("parent key = %d, ",x[i/2]);
	if(i*2<=limit)
		printf("left key = %d, ",x[i*2]);
	if(i*2+1<=limit)
		printf("right key = %d, ",x[i*2+1]);
	printf("\n");
}

return 0;
}


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

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

using namespace std;
using ll = long long;
int mod = 1000000007;

ll solve(int N, int *A, int s);

int main(void){
    int N;
    cin >> N;
    int A[N];
    rep(i, N) cin >> A[i];
    sort(A, A+N);
    ll ans = 0;
    ll temp = solve(N, A, N/2);
    if(temp > ans) ans = temp;
    if(N % 2 == 0) temp = solve(N, A, N/2 - 1);
    if(temp > ans) ans = temp;
    cout << ans << endl;
    return 0;
}

ll solve(int N, int *A, int s){
    ll ret = 0, acc = 0;
    int l = 0, r = N-1;
    rep(i, N-1){
        if(i == 0) ret += A[s] - A[l];
        else ret += A[r] - A[l];
        if(i == 0) continue;
        else if(i % 2 == 0) r--;
        else l++;
        if(l == s) l++;
        if(r == s) r--;
    }
    l = 0, r = N-1;
    rep(i, N-1){
        if(i == 0) acc += A[r] - A[s];
        else acc += A[r] - A[l];
        if(i == 0) continue;
        else if(i % 2 == 0) l++;
        else r--;
        if(l == s) l++;
        if(r == s) r--;
    }
    return max(acc, ret);
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
int main(){
  int s;
  set<int> a;
  cin>>s;
  a.insert(s);
  while(1){
    if(s%2==0) s/=2;
    else s=3*s+1;
    if(a.count(s)) break;
    else a.insert(s);
  }
  cout<<a.size()+1<<endl;
}

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

int main() {
  string S, T;
  cin >> S >> T;
  int lens, lent;
  int ans=100000;
  int tmp;
  lens=S.size();
  lent=T.size();
  for (int i=0; i<lens-lent+1; i++){
    tmp=0;
    for (int j=0; j<lent; j++){
      if (S.at(i+j)!=T.at(j)){
        tmp++;
      }
    }
    if (tmp < ans)
      ans = tmp;
  }
  cout << ans << endl;
}


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

int main(){
   string A,B,C;
   cin>>A>>B>>C;
   if(A.at(A.size()-1)==B.at(0)&&B.at(B.size()-1)==C.at(0)){
       cout<<"YES"<<endl;
   }else{
       cout<<"NO"<<endl;
   }
}

Measured memory_bytes: 3584
#include <bits/stdc++.h>
using namespace std;
#define endl "\n"
using ll = long long;
using P = pair<ll, ll>;

const ll infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
    IoSetup() {
        cin.tie(nullptr);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(10);
        cerr << fixed << setprecision(10);
    }
} iosetup;

template< typename T1, typename T2 >
inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }

template< typename T1, typename T2 >
inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }

void fail() {
    cout << -1 << endl;
    exit(0);
}

int main(){
    ll n;
    cin >> n;
    vector<ll> a(n);
    double mean=0;
    for(ll i=0; i < n; i++){
        cin >> a[i];
        mean += a[i] / double(n);
    }
    double ans_d = 1000000.0;
    ll ans_i = 0;
    for(ll i=0; i < n; i++){
        if(ans_d > abs(mean - a[i])){
            ans_i = i;
            ans_d = abs(mean - a[i]);
        }
    }
    cout << ans_i << endl;
}

Measured memory_bytes: 256
// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };

    ($next:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, bytes) => {
        read_value!($next, String).into_bytes()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        n: usize,
        mut a: [usize; n],
    }
    let t = a.len();
    a.sort();
    a.dedup();
    println!("{}", if t == a.len() { "YES" } else { "NO" });
}


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


#define li          long long int
#define rep(i,to)   for(li i=0;i<((li)(to));i++)
#define repp(i,start,to)    for(li i=(li)(start);i<((li)(to));i++)
#define pb          push_back
#define sz(v)       ((li)(v).size())
#define bgn(v)      ((v).begin())
#define eend(v)     ((v).end())
#define allof(v)    (v).begin(), (v).end()
#define dodp(v,n)       memset(v,(li)n,sizeof(v))
#define bit(n)      (1ll<<(li)(n))
#define mp(a,b)     make_pair(a,b)
#define rin rep(i,n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;

#define INF bit(60)

#define DBGP 1


#define idp if(DBGP)
#define F first
#define S second
#define p2(a,b)     idp cout<<a<<"\t"<<b<<endl
#define p3(a,b,c)       idp cout<<a<<"\t"<<b<<"\t"<<c<<endl
#define p4(a,b,c,d)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<endl
#define p5(a,b,c,d,e)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<endl
#define p6(a,b,c,d,e,f)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<endl
#define p7(a,b,c,d,e,f,g)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<endl
#define p8(a,b,c,d,e,f,g,h)     idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<endl
#define p9(a,b,c,d,e,f,g,h,i)       idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<endl
#define p10(a,b,c,d,e,f,g,h,i,j)        idp cout<<a<<"\t"<<b<<"\t"<<c<<"\t"<<d<<"\t"<<e<<"\t"<<f<<"\t"<<g<<"\t"<<h<<"\t"<<i<<"\t"<<j<<endl
#define foreach(it,v)   for(__typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define p2p(x)      idp p2((x).F, (x).S)
#define dump(x,n)   idp{rep(i,n){cout<<x[i]<<" ";}puts("");}
#define dump2(x,n)  idp{rep(i,n){cout<<"["<<x[i].F<<" , "<<x[i].S<<"] ";}puts("");}
#define dumpi(x)    idp{foreach(it, x){cout<<(*it)<<" ";}puts("");}
#define dumpi2(x)   idp{foreach(it, x){cout<<"["<<(it)->F<<" , "<<(it)->S<<"] ";}puts("");}

#define read2d(a,w,h)   rep(i,h)rep(j,w)cin>>a[i][j]
#define dump2d(a,w,h)   rep(i,h){rep(j,w)cout<<a[i][j]<<" ";puts("");}

typedef pair<li, li> PI;

vector<li> edge[100100];

li n, m;

bool visited[100100][3];

int main() {
    cin >> n >> m;
    rep(i, m) {
        li u, v;
        cin >> u >> v;
        u--;
        v--;
        edge[u].pb(v);
    }

    // run bfs
    li s, t;
    cin >> s >> t;
    s--;
    t--;
    queue<pair<li, PI>> q;
    q.push({0, {s, 0}});
    while (!q.empty()) {
        li now_d = q.front().F;
        li now_pos = q.front().S.F;
        li now_s = q.front().S.S;
        q.pop();
        if (visited[now_pos][now_s]) {
            continue;
        }
        visited[now_pos][now_s] = true;
        if (now_pos == t && now_s == 0) {
            cout << now_d / 3 << endl;
            return 0;
        }
        for (auto next : edge[now_pos]) {
            q.push({now_d + 1, {next, (now_s + 1) % 3}});
        }
    }
    puts("-1");


    return 0;
}

Measured memory_bytes: 8576
#include <bits/stdc++.h>
using namespace std;
int gcd(int a,int b){
  if(a<b){
    swap(a,b);
  }
  int r;
  while(b>0){
    r = a%b;
    a = b;
    b = r;
  }
  return a;
}

int main(){
  int a,b,c;
  cin >> a >> b >> c;
  if(c%gcd(a,b) == 0){
    cout << "YES" << endl;
  }else{
    cout << "NO" << endl;
  }
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define REP(i,m) for(int i=0;i<(m);++i)
#define REPN(i,m,in) for(int i=(in);i<(m);++i)
#define ALL(t) (t).begin(),(t).end()
#define CLR(a) memset((a),0,sizeof(a))
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
using namespace std;


#ifdef DEB
#define dump(x)  cerr << #x << " = " << (x) << endl
#define prl cerr<<"called:"<< __LINE__<<endl
template<class T> void debug(T a,T b){ for(;a!=b;++a) cerr<<*a<<' ';cerr<<endl;}
#else
#define dump(x) ;
#define prl ;
template<class T> void debug(T a,T b){ ;}
#endif

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

typedef long long int lint;
typedef pair<int,int> pi;

namespace std{
  template<class S,class T>
  ostream &operator <<(ostream& out,const pair<S,T>& a){
    out<<'('<<a.fr<<','<<a.sc<<')';
    return out;
  }
}

//const int INF=5e8;

int n;

int ar[4][305];

void ng(){
  puts("NO");
  exit(0);
}

int buf[305][305];//0:horizonal 1:vertical
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
//down,right,up,left

int LR[305],UD[305];

int ud[305],perm[305];


bool done[305][305];
bool vis[305][305];
bool start[305][305];

vector<pi> ever;
bool dfs(int y,int x){
  dump(y);dump(x);
  if(vis[y][x]){
    prl;
    int m=ever.size();
    for(int i=m-1;i>=0;--i) if(ever[i]==mp(y,x)){
      dump(ever[i]);
      for(int j=m-1;j>i;--j){
        dump(ever[j]);
        int nxt=j+1;
        if(nxt==m) nxt=i;
        pi p=ever[j],q=ever[nxt];
        swap(buf[p.fr][p.sc],buf[q.fr][q.sc]);
      }
      start[y][x]=1;
      break;
    }
    vis[y][x]=0;
    return true;
  }
  ever.pb(mp(y,x));
  vis[y][x]=1;
  int d=buf[y][x];
  int py=y+dy[d],px=x+dx[d];
  while(!(px<0 || py<0 || px>=n || py>=n)){
    if(!done[py][px]){
      if(dfs(py,px)){
        if(start[y][x]){
          start[y][x]=false;
          ever.pop_back();
          vis[y][x]=0;
          return dfs(y,x);
        }else{
          vis[y][x]=0;
          ever.pop_back();
          return true;
        }
      }
    }
    px+=dx[d],py+=dy[d];
  }
  done[y][x]=1;
  char code[]="DRUL";
  printf("%c%d\n",code[buf[y][x]],buf[y][x]&1?y+1:x+1);
  ever.pop_back();
  return false;
}
int main(){
  cin>>n;
  REP(i,4) REP(j,n) cin>>ar[i][j];

  REP(i,n) if(ar[0][i]+ar[1][i]>n || ar[2][i]+ar[3][i]>n) ng();
  prl;
  REP(i,n){
    LR[i]=ar[2][i]+ar[3][i];
    UD[i]=ar[0][i]+ar[1][i];
  }

  REP(j,n) ud[j]=UD[j];
  REP(i,n){
    REP(j,n) perm[j]=j;
    sort(perm,perm+n,[&](int a,int b){
      return ud[a]<ud[b];
    });

    REP(j,n){
      if(j<LR[i]) buf[i][perm[j]]=0;
      else buf[i][perm[j]]=1,--ud[perm[j]];
    }
  }
  debug(ud,ud+n);
  REP(i,n) if(ud[i]!=0) ng();
  prl;


  REP(i,n){
    REP(j,n){
      if(buf[i][j]==0){
        if(ar[2][i]>0) buf[i][j]=3,--ar[2][i];
        else buf[i][j]=1;
      }else{
        if(ar[0][j]>0) buf[i][j]=2,--ar[0][j];
        else buf[i][j]=0;
      }
    }
  }

  REP(i,n) debug(buf[i],buf[i]+n);
  REP(i,n) REP(j,n) if(!done[i][j]) dfs(i,j);
  REP(i,n) debug(buf[i],buf[i]+n);

  return 0;
}





Measured memory_bytes: 1664
#include <iostream>

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

    // 4x + 7y = N

    bool ans = false;
    for (int i = 0; i <= 25; i++) {
        for(int j = 0; j < 15; j++) {
            int c = 4 * i + 7 * j;
            if (c == N) {
                ans = true;
                break;
            }
            else if (c > N) break;
        }
    }

    std::cout << ((ans)?"Yes":"No") << std::endl;

    return 0;
}

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

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    string s;
    cin >> s;
    
    int ans = 0;
    for (int i = 0; i < 3; i++){
        if (s[i] == 'R'){
            ans += 1;
            i += 1;
            while (i < 3 && s[i] == 'R'){
                i++;
                ans += 1;
            }
            break;
        }
    }

    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 3608
from sys import stdin
N = int(stdin.readline().rstrip())
A = [0] * N
B = [0] * N
for i in range(N):
    a,b = [int(x) for x in stdin.readline().rstrip().split()]
    A[i] = a
    B[i] = b

A = reversed(A)
B = reversed(B)

ans = 0
for j,k in zip(A,B):
    if (j + ans) % k != 0: 
       ans += (k - ((j+ans) % k))
print(ans)

Measured memory_bytes: 12916
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
using namespace std;
#define debug(x) //std::cerr << #x << " = " << (x) << std::endl
typedef long long LL;
const int MAXN = 2e5+17;
int main(int argc ,char const *argv[])
{
	#ifdef noob
	freopen("Input.txt","r",stdin);freopen("Output.txt","w",stdout);
	#endif	
	int n;
	cin>>n;
	int ans = 0;
	vector<int > vec;
	vec.push_back(0);
	for (int i = 0; i < n; ++i)
	{
		int tmp;
		cin>>tmp;
		vec.push_back(tmp);
	}
	vec.push_back(0);
	for (int i = 1; i <= n+1; ++i)
		ans+=abs(vec[i]-vec[i-1]);
	for (int i = 1; i <= n; ++i)
	{
		int tmp = ans;
		tmp -= (abs(vec[i]-vec[i-1])+abs(vec[i]-vec[i+1]));
		tmp += abs(vec[i-1]-vec[i+1]);
		cout<<tmp<<endl;
	}
	return 0;	
}

Measured memory_bytes: 1784
import bisect

A, B, Q = map(int, input().split())
S = [int(input()) for _ in range(A)]
T = [int(input()) for _ in range(B)]
X = [int(input()) for _ in range(Q)]
#print("S:", S)
#print("T:", T)

answer1 = []
answer2 = []

for x in X:
  
  answer = 10 ** 12
  
  i1 = bisect.bisect_right(S, x) - 1
  i2 = (i1 + 1) % A
  S1 = S[i1]
  S2 = S[i2]  
  dist1 = abs(x - S1)
  dist2 = abs(x - S2)
  #print("dist1, dist2:", dist1, dist2)
  
  i11 = bisect.bisect_right(T, S1) - 1
  i12 = (i11 + 1) % B
  T11 = T[i11]
  T12 = T[i12]
  dist11 = abs(S1 - T11)
  dist12 = abs(S1 - T12)
  #print("dist11, dist12:", dist11, dist12)
    
  i21 = bisect.bisect_right(T, S2) - 1
  i22 = (i21 + 1) % B
  T21 = T[i21]
  T22 = T[i22]
  dist21 = abs(S2 - T21)
  dist22 = abs(S2 - T22)
  #print("dist21, dist22:", dist21, dist22)
  
  dist = min(dist1 + min(dist11, dist12), dist2 + min(dist21, dist22))
  answer1.append(dist)
  
S, T = T, S
A, B = B, A

for x in X:
  
  answer = 10 ** 15

  i1 = bisect.bisect_right(S, x) - 1
  i2 = (i1 + 1) % A
  S1 = S[i1]
  S2 = S[i2]  
  dist1 = abs(x - S1)
  dist2 = abs(x - S2)
  #print("dist1, dist2:", dist1, dist2)
  
  i11 = bisect.bisect_right(T, S1) - 1
  i12 = (i11 + 1) % B
  T11 = T[i11]
  T12 = T[i12]
  dist11 = abs(S1 - T11)
  dist12 = abs(S1 - T12)
  #print("dist11, dist12:", dist11, dist12)
    
  i21 = bisect.bisect_right(T, S2) - 1
  i22 = (i21 + 1) % B
  T21 = T[i21]
  T22 = T[i22]
  dist21 = abs(S2 - T21)
  dist22 = abs(S2 - T22)
  #print("dist21, dist22:", dist21, dist22)
  
  dist = min(dist1 + min(dist11, dist12), dist2 + min(dist21, dist22))
  answer2.append(dist)

for i in range(Q):
  print(min(answer1[i], answer2[i]))



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

//typedef
//------------------------------------------
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);
const int MOD = 1000000007;

//MOD
//--------------------------------------------
#define MUL(a,b) (((a) % MOD) * ((b) % MOD)) % MOD

// grid
//--------------------------------------------
VL dx = {0, 1, 0, -1};
VL dy = {1, 0, -1, 0};
VL dx2 = {-1, 0, 1, -1, 1, -1, 0, 1};
VL dy2 = {-1, -1, -1, 0, 0, 1, 1, 1};

//debug
//--------------------------------------------
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

//IO accelerate
//--------------------------------------------
struct InitIO {
    InitIO() {
        cin.tie(nullptr);
        ios_base::sync_with_stdio(false);
        cout << fixed << setprecision(30);
    }
} init_io;

//main code

int main(int argc, char const* argv[])
{
	int n;
	cin >> n;
	VS s(n);
	for (int i = 0; i < n; i++) {
		cin >> s[i];
	}
	LL c_front_b = 0, c_back_a = 0, ans = 0;
	LL fb_ab = 0;
	for (int i = 0; i < n; i++) {
		if (s[i][0] == 'B') {
			c_front_b++;
			if (s[i][s[i].size()-1] == 'A') {
				fb_ab++;
			}
		}
		if (s[i][s[i].size()-1] == 'A') {
			c_back_a++;
		}
		for (int j = 0; j < s[i].size() - 1; j++) {
			if (s[i][j] == 'A' and s[i][j+1] == 'B') {
				ans++;
			}
		}
	}
	ans += min(c_front_b, c_back_a);
	if (c_back_a == c_front_b and fb_ab == c_back_a and c_back_a > 0) {
		ans--;
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 768
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}
bool cmp(int x,int y)
{
    return x>y;
}
const int N=55;
const int mod=1e9+7;
char s[15];
int pos[15];
ll num(int l,int r)
{
    ll ans=0;
    for(int i=l;i<=r;i++){
        ans=ans*10+s[i]-'0';
    }
    return ans;
}
ll solve(int n)
{
    int l=-1,r=0;
    ll ans=0;
    pos[n]=1;
    for(int i=0;i<=n;i++){
        if(pos[i]==0) r++;
        else{
            ans+=num(l+1,i);
            l=i;
        }
    }
    return ans;
}
int main()
{
     while(cin>>s){
        int n=strlen(s);
        ll ans=0;
        int R= (1<<(n-1))-1 ;
        for(int i=0;i<=R;i++){
            mem(pos);
            int tot=0,temp=i;
            while(temp){
                pos[tot++]=temp%2;
                temp=temp/2;
            }
            ans+=solve(n-1);
        }
        printf("%lld\n",ans);
     }
    return 0;
}


Measured memory_bytes: 384