akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
const int mod=1000000007;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;cin >> n;
	vector<ll> a(n);
	rep(i,n) cin >> a.at(i);
	ll k=10000000000;
	ll sum=0;
	int flg=1;
	rep(i,n){
		if(a.at(i)<0) flg^=1;
		k=min(abs(a.at(i)),k);
		sum+=abs(a.at(i));
	}
	if(flg) cout << sum << endl;
	else cout << sum-k*2 << endl;
}

Measured memory_bytes: 1024
#include<stdio.h>
int main()
{
	int a,b,c;

	scanf("%d%d%d",&a,&b,&c);

	for(int i=1;i<=b;i++)
		if(a*i%b==c){
			printf("YES");
			return 0;
		}
	printf("NO");
	return 0;
}

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

#define rep(i,x,y) for(int i=(x);i<(y);++i)
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define show(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define show(x)
#endif

typedef long long int ll;
typedef pair<int,int> pii;
template<typename T> using vec=std::vector<T>;

const int inf=1<<30;
const long long int infll=1LL<<62;
const double eps=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
    os << "[";
    for (const auto &v : vec) {
    	os << v << ",";
    }
    os << "]";
    return os;
}

ll f(ll a,ll b){
    if(a>b) swap(a,b);
    if(b%a==0) return a*(2*(b/a)-1);
    return 2*a*(b/a)+f(a,b%a);
}

void solve(){
    ll n,x;
    cin >> n >> x;
    cout << n+f(x,n-x) << endl;
}

int main(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(10);
    solve();
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
using namespace std;
typedef  long long int lint;
typedef pair<int, int> P;
typedef pair<lint, lint> LLP;
typedef pair<char, char>CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
/*
vector<char>al(26);
al = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
vector<char>AL(26);
AL = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
*/

int main() {
	int N;
	cin >> N;
	vector<lint>A(N);
	rep(i, N) {
		cin >> A[i];
	}
	vector<lint>n(N+1);
	int cnt = 0;
	rep(i, N) {
		if (A[i] <= N) {
			n[A[i]]++;
		}
		else {
			cnt++;
		}
	}
	rep(i, N + 1) {
		if (n[i] != 0) {
			if (i < n[i]) {
				cnt += (n[i] - i);
			}
			else if (i > n[i]) {
				cnt += n[i];
			}
		}
	}
	cout  << cnt << endl;
}




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

int main() {
  int N, M, count;
  count = 0;
  cin >> N;
  vector<string> p(N);
  vector<int> pcount(N);

  for (int i = 0; i < N; i++){
    cin >> p.at(i);
  }
  cin >> M;
  vector<string> m(M);
  for (int i = 0; i < M; i++){
    cin >> m.at(i);
  }
  for (int i = 0; i < N; i++){
    for (int j = 0; j < N; j++){
      if (p.at(i) == p.at(j)){
        pcount.at(i)++;
      }
    }
    for (int j = 0; j < M; j++){
      if (p.at(i) == m.at(j)){
        pcount.at(i)--;
      }
    }
  }

  for (int i = 0; i < N; i++){
    if (count < pcount.at(i)){
      count = pcount.at(i);
    }
  }
  cout << count << endl;
}


Measured memory_bytes: 256
import std.stdio;
import std.ascii;
import std.conv;
import std.string;
import std.algorithm;
import std.range;
import std.functional;
import std.math;
import core.bitop;

void main()
{
    auto n = readln.chomp.to!long;
    foreach (seven; 0 .. (n / 7 + 1))
    {
        if ((n - (7 * seven)) % 4 == 0)
        {
            "Yes".writeln;
            return;
        }
    }
    "No".writeln;
}


Measured memory_bytes: 256
using System;
using System.Linq;
using System.Collections.Generic;

namespace Contest
{
    partial class Program
    {
        static void Main(string[] args)
        {
            ABC155_B();
        }

        static void ABC155_A()
        {
            var abc = ReadIntN();
            var A = abc[0];
            var B = abc[1];
            var C = abc[2];
            if (A == B && B == C) { Console.WriteLine("No");return; }
            if (A == B || A == C || B == C) { Console.WriteLine("Yes");return; }
            Console.WriteLine("No");
        }
        static void ABC155_B()
        {
            var N = ReadInt();
            var A = ReadIntN();
            for(int i = 0; i < N; ++i)
            { var a = A[i];
                if (a % 2 == 0 && (a % 3 > 0 && a % 5 > 0)) { Console.WriteLine("DENIED");return; }
            }
            Console.WriteLine("APPROVED");
        }
        static void ABC155_C()
        {

        }
        static void ABC155_D()
        {

        }
        static void ABC155_E()
        {

        }
        static void ABC155_F()
        {

        }

        static string ReadStr()
        {
            return Console.ReadLine();
        }
        static string[] ReadStrN()
        {
            return Console.ReadLine().Split(' ');
        }
        static int ReadInt()
        {
            return Convert.ToInt32(Console.ReadLine());
        }
        static int[] ReadIntN()
        {
            return Console.ReadLine().Split(' ').Select(s => Convert.ToInt32(s)).ToArray();
        }
        static long ReadLong()
        {
            return Convert.ToInt64(Console.ReadLine());
        }
        static long[] ReadLongN()
        {
            return Console.ReadLine().Split(' ').Select(s => Convert.ToInt64(s)).ToArray();
        }
        static Tuple<long, long>[]  ReadTupleLong(int n)
        {
            List<Tuple<long, long>> list = new List<Tuple<long, long>>();
            for (int i = 0; i < n; ++i)
            {
                var ab = ReadLongN();
                list.Add(new Tuple<long, long>(ab[0], ab[1]));
            }
            return list.ToArray();
        }
    }
}


Measured memory_bytes: 13268
n = gets.to_i
x = []
y = []
for num in 0..n-1 do
  a = gets.chomp.split(" ")
  a[1] = a[1].to_i
  a[2] = num + 1
  x << a 

  
end
x = x.sort {|l, m| (l[0] <=> m[0]).nonzero? || (m[1] <=> l[1])}

for num in 0..n-1 do
p x[num][2]
end

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

int main()
{
    long long int a,b,c,x,y,m;
    cin>>a>>b>>c;
    m=(c-(a+b));
    x=m*m;
    y=4*a*b;
    if((a+b)<c && x>y)
        cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
const int ma = 6000;
const int m = ma / 2;
const int sz = ma + 10;
main()
{
    int n;
    cin >> n;
    vector< double > dp(sz);
    dp[m] = 1;
    for( int i = 0; i < n; i++ )
    {
        vector< double > e(sz);swap( dp, e );
        double p;
        cin >> p;
        double q = 1 - p;
        for( int j = 1; j <= ma; j++ )
        {
            dp[j - 1] += e[j] * q;
            dp[j + 1] += e[j] * p;
        }
    }
    double ans = 0;
    for( int i = m; i <= ma; i++ )ans += dp[i];
    printf( "%.10lf\n", ans );
    return 0;
}

Measured memory_bytes: 384
"use strict"

function main(input) {
  input = input.split(" ").map(v => ~~v)

  let ans = input[0]
  if (input[0] == input[1]) ans = input[2]
  else if (input[0] == input[2]) ans = input[1]

  console.log(ans) //
}
main(require("fs").readFileSync("/dev/stdin", "utf8"))

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

const int MAXN = 100005;

long long N, x;
long long a[MAXN];

int main() {

    cin >> N >> x;
    for (int i = 0; i < N; i++) {
        cin >> a[i];
        //cout << a[i] << endl;
    }

    long long ans = 0;
    for (int i = 0; i < N-1; i++) {
        if (a[i] + a[i+1] > x) {
            if (a[i] > x) {
                ans += a[i+1];
                a[i+1] = 0;
                ans += a[i] - x;
                a[i] = x;
                //cout << a[0] << endl;
            } else {
                ans += a[i] + a[i+1] - x;
                a[i+1] = x - a[i];
            }
        }
    }

    cout << ans << endl;

    return 0;
}

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

using namespace std;

void solve() {
    string s; cin >> s;
    if (s[2] == s[3] && s[4] == s[5])
        cout << "Yes";
    else
        cout << "No";
}

int main() {
    ios::sync_with_stdio(NULL), cin.tie(0), cout.tie(0);
    cout.setf(ios::fixed), cout.precision(20);
    solve();
}


Measured memory_bytes: 256
mod = 10**9+7
def gcd(c,d):
    #c<d
    if d%c == 0:
        return c
    else:
        return gcd(d%c,c)

def main():
    a,b,c,d=map(int,input().split(' '))
    c,d=sorted([c,d])
    lmc = int(c*d/gcd(c,d))
    e = a-1
    ans = int((b-a+1)-(b//c + b//d - b//lmc -1*(e//c + e//d - e//lmc)))
    print(ans)

main()

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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n; cin >> n;
    vector<ll> a(n); cin >> a;
    vector<vector<ll>> dp(n+1, vector<ll>(2, 0));
    dp[0][0] = 1;
    rep(i, n) rep(j, 2) rep(k, -1, 2) {
        if ( (a[i] + k) % 2 == 0 ) {
            dp[i+1][1] += dp[i][j];
        }
        else {
            dp[i+1][j] += dp[i][j];
        }
    }
    cout << dp[n][1] << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int n,m,c;
    cin>>n>>m>>c;
    int b[m];
    int a[n][m];
    int ans=0;
    int kai=0;
    for(int i=0;i<m;i++){
        cin>>b[i];
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<m;j++){
        cin>>a[i][j];  
      }
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<m;j++){
          kai+=a[i][j]*b[j];
          if(j==m-1){
            if(kai+c>0){
              ans++;  
            }
            kai=0;
          }
      }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vb=vector<bool>;
using vvb=vector<vb>;
using vd=vector<double>;
using vvd=vector<vd>;
using vi=vector<int>;
using vvi=vector<vi>;
using vl=vector<ll>;
using vvl=vector<vl>;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll>;
using tlll=tuple<ll,ll,ll>;
using vs=vector<string>;

#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define rep(i,n) range(i,0,n)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define range(i,a,n) for(int i=(a);i<(n);i++)

#define LINF ((ll)1ll<<60)
#define INF ((int)1<<30)
#define EPS (1e-9)
#define MOD (1000000007ll)
#define fcout(a) cout<<setprecision(a)<<fixed
#define fs first
#define sc second
#define PI 3.1415926535897932384

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

template<class T>bool chmax(T&a,T b){if(a<b){a=b; return true;}return false;}
template<class T>bool chmin(T&a,T b){if(a>b){a=b; return true;}return false;}
template<class S>S acm(vector<S>&a){return accumulate(all(a),S());}
template<class S>S max(vector<S>&a){return *max_element(all(a));}
template<class S>S min(vector<S>&a){return *min_element(all(a));}
void YN(bool b){cout<<(b?"YES":"NO")<<"\n";}
void Yn(bool b){cout<<(b?"Yes":"No")<<"\n";}
void yn(bool b){cout<<(b?"yes":"no")<<"\n";}

ll max(int a,ll b){return max((ll)a,b);} ll max(ll a,int b){return max(a,(ll)b);}
template<class T>void puta(T&&t){cout<<t<<"\n";}
template<class H,class...T>void puta(H&&h,T&&...t){cout<<h<<' ';puta(t...);}
template<class S,class T>ostream&operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool a=1; for(auto s:t){os<<(a?"":" ")<<s;a=0;} return os;}
#define _cTime (chrono::system_clock::now())
#define progress (chrono::duration_cast<chrono::milliseconds>(_cTime-_sTime).count())
#define reset _sTime=_cTime
auto reset;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n,m,c,cnt=0;
	cin>>n>>m>>c;
	vl a(m),b(m);
	rep(i,m)cin>>b[i];
	rep(i,n){
		rep(j,m)cin>>a[j];
		ll tmp=0;
		rep(j,m)tmp+=a[j]*b[j];
		if(tmp>-c)cnt++;
	}
	cout<<cnt<<endl;
}


Measured memory_bytes: 256
import java.io.*;
import java.util.*;

class Main {

	public static void main(String[] args) throws Exception {
		final Scanner sc = new Scanner(System.in);
		long M = 1L << sc.nextInt();
		long K = sc.nextLong();
		if (M == 2 && K == 0) {
			System.out.println("0 0 1 1");
			return;
		}

		if (K >= M || M == 2) {
			System.out.println(-1);
			return;
		}

		StringBuilder sb = new StringBuilder();
		for (long i = 0; i < M; i++) {
			if (i == K) continue;
			sb.append(i).append(" ");
		}
		sb.append(K).append(" ");
		for (long i = M - 1; i >= 0; i--) {
			if (i == K) continue;
			sb.append(i).append(" ");
		}
		sb.append(K);
		System.out.println(sb.toString());
	}
}


Measured memory_bytes: 37572
#include<stdio.h>

int ov_abs(int a){
    return a>0 ? a : -a;
}

int score[1000];
int main(){
    int n;
    scanf("%d",&n);
    while(n!=0){
        int ans = 889464810;
        for(int i=0; i<n; i++){
            scanf("%d",&score[i]);
        }
        for(int i=0; i<n; i++){
            for(int j=i+1; j<n; j++){
                int tmp = ov_abs(score[j]-score[i]);
                ans = ans > tmp ? tmp:ans;
            }
        }
        printf("%d\n",ans);
        scanf("%d",&n);
    }

    return 0;
}

Measured memory_bytes: 2660
//PseudoName

	#pragma GCC optimize("O3")
	#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;
	template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;


	#define FOR(i,n) for(i=0;i<n;i++)
	#define FOR1(i,n) for(i=1;i<=n;i++)
	#define FOR2(I, A, B) for (int I = (A); I <= (B); ++I)
	#define pb push_back
	#define unique_sort(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
	#define print(v) for(auto it:v)cout<<it<<" ";cout<<endl
	#define fast() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
	#define endl "\n"
	#define min3(a,b,c) min(a,min(b,c))
	#define forr(n) for(int i=0;i<n;i++)
	#define frr(n) for(int i=1;i<=n;i++)
	#define rep(i,a,b) for(int i=a;i<=b;i++)



	typedef long long ll;
	typedef long li;
	typedef vector<int> vi;
	typedef vector<ll> vli;
	typedef vector<vi> vvi;
	typedef vector<vli> vvli;
	typedef pair<int,int> pi;
	typedef pair<ll,ll> pli;
	typedef vector<pi> vpi;
	typedef vector<pli> vpli;



	//const ll MOD=1e9+7;

	/*
	ll fast_exp(ll base, ll exp) {
	    ll res=1;
	    while(exp>0) {
	       if(exp%2==1) res=(res*base)%MOD;
	       base=(base*base)%MOD;
	       exp/=2;
	    }
	    return res%MOD;
	}
	ll imod(ll a)
	{
		return fast_exp(a, MOD-2);
	}
	*/

	const double PI=3.141592653589793238;

	int main() {
	    fast();

	    double r;
        cout<<setprecision(12);
	    cin>>r;
	    cout<<2*PI*r<<endl;



	}


Measured memory_bytes: 3800
#include <algorithm>
#include <iostream> //入出力
#include <stdio.h>
#include <map>
#include <string>
#include <math.h> //算術演算子
#include <vector>
#include <cstdlib>
#include <queue>
#include <stack>
#include <list>
#include <iomanip> //小数点以下を表示させる(setprecision())
//#include <bits/stdc++.h>
using namespace std;
typedef long long ll; //64bit型

#define debug(x) cerr << x << " " \
                      << "(L:" << __LINE__ << ")" << '\n'
#define PRINT(V)                                   \
        {                                          \
                for (int i = 0; i < V.size(); i++) \
                {                                  \
                        cout << V[i] << " ";       \
                }                                  \
                cout << endl;                      \
        }

int gcd(int a, int b)
{
        return b ? gcd(b, a % b) : a;
}

int main()
{
        //input->compute->output//
        int N, T, A;
        cin >> N >> T >> A;

        vector<int> h(N);
        for (int i = 0; i < N; i++)
        {

                cin >> h[i];
        }

        int res = 1 << 30;
        int ans;
        for (int i = 0; i < N; i++)
        {
                res = min(res, abs(1000 * A - (1000 * T - 6 * h[i])));
                if (res == abs(1000 * A - (1000 * T - 6 * h[i])))
                {
                        ans = i;
                }
        }

        cout << ans + 1 << endl;

        return 0;
}


Measured memory_bytes: 256
n,m=map(int,input().split())
drk=[list(map(int,input().split())) for _ in range(n)]
drk.sort()
i=0
ans=0

while m>0:
  a,b=drk[i]
  if m>=b:
    ans+=a*b
    m=m-b
  else:
    ans+=a*m
    m=0
  i+=1
  
print(ans) 

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

// probably some shit cancels out 
// Sa0 = a1 ^ a2 ^ a3...
// Sa1 = a0 ^ a2 ^ a3...
// Sa2 = a0 ^ a1 ^ a3...
// Sa3 = a0 ^ a1 ^ a2...

// a0 ^ a1 

// a2 ^ a3


// Sa1 = a0 ^ a2 ^ a3 ^ a4 ...
// Sa2 = a0 ^ a1 ^ a3 ^ a4 ...
// Sa3 = a0 ^ a1 ^ a2 ^ a4 ...


// Sa0 ^ Sa1 = a0 ^ a1
// Sa1 ^ Sa2 = a1 ^ a2
// Sa2 ^ Sa3 = a2 ^ a3


int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int all = 0;
  for (int i = 0; i < n; i += 2)
    all ^= a[i] ^ a[i + 1];
  for (int i = 0; i < n; i++) 
    cout << (all ^ a[i]) << ' ';
  return 0;
}

Measured memory_bytes: 3968
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<s;i++)
#define CC puts("-------ok--------");
#define all(in) in.begin(), in.end()
using namespace std;
typedef pair<int,int> pii;
#define MAX 99999999
signed main(){
while(1){
	int n,m;
	cin>>n>>m;
    if(!(n+m))break;
	vector<vector<pii>>v(m+1,vector<pii>(m+1,pii(INF,INF)));
	rep(i,m+1)v[i][i]=pii(0,0);
	rep(i,n){
		pii temp;
		int s,g;
		cin>>s>>g>>temp.first>>temp.second;
        v[--s][--g]=temp;
	}
    n=m;
    rep(k,n){
        rep(i,n){
            rep(j,n){
                v[i][j].first=min(v[i][k].first+v[k][j].first,v[i][j].first);
                v[i][j].second=min(v[i][k].second+v[k][j].second,v[i][j].second);
                v[j][i]=v[i][j];

            }
        }
    }
    int k; cin>>k;
    rep(i,k){
        int s,g,r; cin>>s>>g>>r;
        if(!r)cout<<v[--s][--g].first<<endl;
        else cout<<v[--s][--g].second<<endl;
    }
    
    }
}

	

Measured memory_bytes: 3028
/*
これを入れて実行
g++ code.cpp
./a.out
 */
 
#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
#include <stack>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <unordered_map>
#include <cmath>
#include <math.h>
#include <tuple>
#include <iomanip>
#include <bitset>
#include <functional>
#include <cassert>

#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
using namespace std;
typedef long long ll;
typedef long double ld;
 
int dy4[4] = {-1, 0, +1, 0};
int dx4[4] = {0, +1, 0, -1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dx8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
 
const long long INF = 1LL << 60;
const ll MOD = 1e9 + 7;
 
bool greaterSecond(const pair<int, int>& f, const pair<int, int>& s){
    return f.second > s.second;
}
 
ll gcd(ll a, ll b){
	if (b == 0)return a;
	return gcd(b, a % b);
}
 
ll lcm(ll a, ll b){
    return a / gcd(a, b) * b;
}

ll conbinationMemo[100][100];

void cmemoInit(){
    rep(i, 100){
        rep(j, 100){
            conbinationMemo[i][j] = -1;
        }
    }
}
 
ll nCr(ll n, ll r){
    if(conbinationMemo[n][r] != -1) return conbinationMemo[n][r];
    if(r == 0 || r == n){
        return 1;
    } else if(r == 1){
        return n;
    }
    return conbinationMemo[n][r] = (nCr(n - 1, r) + nCr(n - 1, r - 1));
}
 
ll nPr(ll n, ll r){
    r = n - r;
    ll ret = 1;
    for (ll i = n; i >= r + 1; i--) ret *= i;
    return ret;
}
//-----------------------ここから-----------
ll d;
vector<vector<ll>> s;
vector<ll> c(26);
vector<ll> t;
ll calcScore(){
    ll score = 0;
    vector<ll> last(26, -1);
    rep(i, d){
        score += s[i][t[i]];
        last[t[i]] = i;
        rep(j, 26){
            score -= (i - last[j]) * c[j];
        }
    }

    return score;
}
int main(void){

    cin >> d;
    s.resize(d, vector<ll>(26));
 
    rep(i, 26) cin >> c[i];
    rep(i, d){
        rep(j, 26){
            cin >> s[i][j];
        }
    }
 
    t.resize(d);
    rep(i, d){
        cin >> t[i];
        t[i]--;
    }

    ll firstScore = calcScore();
    ll m;
    cin >> m;
    vector<pair<ll,ll>> q(m);
    rep(i, m){
        cin >> q[i].first >> q[i].second;
        q[i].first--;
        q[i].second--;
    }

    rep(i, m){
        t[q[i].first] = q[i].second;
        cout << calcScore() << endl;
    }

 
}


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

int main() {
  int N,a,b,c,d;
  cin >>N;
  a=N/1000;
  b=(N-a*1000)/100;
  c=(N-a*1000-b*100)/10;
  d=N-a*1000-b*100-c*10;
  if(a==b){
    if(b==c)cout <<"Yes"<<endl;
    else cout<<"No"<<endl;
  }
  else if(b==c){
    if (c==d)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
  }
  else cout<<"No"<<endl;// ここにプログラムを追記
}

Measured memory_bytes: 256
#include <stdio.h>
int main(){
    while (1) {
        int n;
        scanf("%d",&n);
        if (n==0) {
            return 0;
        }
        int m,count=0,i;
        char s[1000001];
        scanf("%d",&m);
        scanf("%s",s);
        int x=0;
        for (i=0; i<m; i++) {
            if (x%2) {
                if (s[i]=='O') {
                    x++;
                }else{
                    if (x>=2*n+1) {
                        count+=x/2-n+1;
                    }
                    x=1;
                }
            }else{
                if (s[i]=='I') {
                    x++;
                }else{
                    if (x>=2*n+1) {
                        count+=x/2-n;
                    }
                    x=0;
                }
            }
        }
        printf("%d\n",count);
    }
}

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

int main(int argc, char const *argv[])
{
  while(1)
  {
    int m, f, r;
    cin >> m >> f >> r;

    if(m == -1 && f == -1 && r == -1)
      break;

    if(m == -1 || f == -1)
    {
      cout << 'F' << endl;
      continue;
    }

    if(m+f > 79)
      cout << 'A' << endl;
    else if(m+f > 64)
      cout << 'B' << endl;
    else if(m+f > 49)
      cout << 'C' << endl;
    else if(m+f > 29)
      cout << (r > 49 ? 'C' : 'D') << endl;
    else
      cout << 'F' << endl;
  }
  return 0;
}

Measured memory_bytes: 3092
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}


int main(){
  int N;
  cin >> N;
  int L[2*N];
  rep(i,2*N){
    cin >> L[i];
  }
  sort(L,L+2*N,greater<int>());
  int ans = 0;
  rep(i,2*N){
    if(i%2 == 1){
      ans += L[i];
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main { 
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    long N = sc.nextLong();
    StringBuilder sb = new StringBuilder();
    for (; N > 0; N = (N - 1) / 26)
        sb.append((char) ((N - 1) % 26 + 'a'));
    System.out.println(sb.reverse().toString());
  }
}    

Measured memory_bytes: 35796
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define yes "Yes\n"
#define no "No\n"
//int m,n;
//int i,j,k,l;
//int a,b,c,d,e,f,g;
//int sum,total;
//long long int ;
//float ;
//double ;
//char a[101],b[101],s[101],t[101],u[101];
int a[3];
int main(void){
    scanf("%d %d %d",&a[0],&a[1],&a[2]);
    if (a[0]+a[1]+a[2]>=22)
    {
        printf("bust\n");
    } else
    {
        printf("win\n");
    }
    
    
    return 0;
}

Measured memory_bytes: 128
#include <iostream>
#include <string>
using namespace std;
 
struct Dice{
    int num[6];
};
 
int main(){
    struct Dice dice;
    string str;
    int store[6];
 
    for(int i=0;i<6;i++){
        cin>>dice.num[i];
    }
    cin>>str;
 
    for(int i=0;i<str.length();i++){
        for(int j=0;j<6;j++){
            store[j] = dice.num[j];
        }
        switch(str[i]){
        case 'N':
            dice.num[0] = store[1];
            dice.num[1] = store[5];
            dice.num[5] = store[4];
            dice.num[4] = store[0];
            break;
        case 'E':
            dice.num[0] = store[3];
            dice.num[3] = store[5];
            dice.num[5] = store[2];
            dice.num[2] = store[0];
            break;
        case 'S':
            dice.num[0] = store[4];
            dice.num[4] = store[5];
            dice.num[5] = store[1];
            dice.num[1] = store[0];
            break;
        case 'W':
            dice.num[0] = store[2];
            dice.num[2] = store[5];
            dice.num[5] = store[3];
            dice.num[3] = store[0];
            break;
        }
    }
 
    cout<<dice.num[0]<<endl;
    return 0;
}

Measured memory_bytes: 1204
#include<bits/stdc++.h>
#define lg long long int
using namespace std;
lg min(lg a,lg b)
{
    if(a<b)
        return a;
    else
        return b;

}
lg max(lg a,lg b)
{
    if(a>b)
        return a;
    else
        return b;

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


Measured memory_bytes: 3596
package main

import (
	"fmt"
	"strings"
)

type q struct {
	data []point
}

type point struct {
	x, y int
}

func (q *q) q(v point) {
	q.data = append(q.data, v)
}

func (q *q) dq(v *point) {
	if len(q.data) > 0 {
		*v = q.data[0]
		q.data = q.data[1:]
	}
}

var h, w int

// var s, g point
var maze [][]byte
var d [][]int
var dx = [4]int{1, -1, 0, 0}
var dy = [4]int{0, 0, -1, 1}

func bfs(s, g *point) int {
	//fmt.Printf("bfs en: s(%d,%d), g(%d,%d)\n", s.x+1, s.y+1, g.x+1, g.y+1)
	var q q
	d[s.y][s.x] = 0
	q.q(point{s.x, s.y})
	//fmt.Println("len(q), q.data", len(q.data), q.data)
	for len(q.data) > 0 {
		var p point
		q.dq(&p)
		// fmt.Printf("(%d,%d) dq\n", p.x+1, p.y+1)
		for i := 0; i < 4; i++ {
			nx := p.x + dx[i]
			ny := p.y + dy[i]
			if 0 <= ny && ny < h && 0 <= nx && nx < w && d[ny][nx] == -1 && maze[ny][nx] != '#' {
				d[ny][nx] = d[p.y][p.x] + 1
				q.q(point{nx, ny})
			}
		}
	}
	return d[g.y][g.x]
}

func main() {
	fmt.Scan(&h, &w)
	var s, g point
	s.x = 0
	s.y = 0
	g.x = w - 1
	g.y = h - 1
	maze = make([][]byte, h)
	var black int
	d = make([][]int, h)
	for i := range maze {
		maze[i] = make([]byte, w)
		fmt.Scan(&maze[i])
		black += strings.Count(string(maze[i]), "#")
		d[i] = make([]int, w)
		for j := range d[i] {
			d[i][j] = -1
		}
	}
	ans := -1
	d := bfs(&s, &g)
	if d > 0 {
		ans = h*w - (d + 1) - black
	}
	fmt.Println(ans)
}


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

#define NDEBUG
#include <cassert>


typedef long long ll;
typedef long double Double;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef pair<ll,ll> llll;
typedef pair<double,double> dd;

typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ii> vii;
typedef vector<vector<ii>> vvii;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<long double> vD;

#define sz(a)  int((a).size())
#define pb  push_back
#define eb  emplace_back
#define FOR(var,from,to) for(int var=(from);var<=(to);++var)
#define rep(var,n)  for(int var=0;var<(n);++var)
#define rep1(var,n)  for(int var=1;var<=(n);++var)
#define repC2(vari,varj,n)  for(int vari=0;vari<(n)-1;++vari)for(int varj=vari+1;varj<(n);++varj)
#define ALL(c)  (c).begin(),(c).end()
#define RALL(c)  (c).rbegin(),(c).rend()
#define tr(i,c)  for(auto i=(c).begin(); i!=(c).end(); ++i)
#define found(s,e)  ((s).find(e)!=(s).end())
#define mset(arr,val)  memset(arr,val,sizeof(arr))
#define mid(x,y) ((x)+((y)-(x))/2)
#define IN(x,a,b) ((a)<=(x)&&(x)<=(b))
#define cons make_pair

#define PQ(T) priority_queue<T>
#define RPQ(T) priority_queue<T,vector<T>,greater<T>>

template<class T> inline void amin(T & a, T const & b) { a = min(a, b); }
template<class T> inline void amax(T & a, T const & b) { a = max(a, b); }

inline ll square(ll x) { return x * x; }
inline ll gcd(ll a, ll b) { while(a) swap(a, b%=a); return b; }
template <typename T>
inline T mod(T a, T b) { return ((a % b) + b) % b; }


const ll MOD=1000000007LL;

ll ADD(ll x, ll y) { return (x+y) % MOD; }
ll SUB(ll x, ll y) { return (x-y+MOD) % MOD; }
ll MUL(ll x, ll y) { return x*y % MOD; }
ll POW(ll x, ll e) { ll v=1; for(; e; x=MUL(x,x), e>>=1) if (e&1) v = MUL(v,x); return v; }
ll DIV(ll x, ll y) { /*assert(y%MOD!=0);*/ return MUL(x, POW(y, MOD-2)); }

inline bool bad(int a,int b,int c,int d){
    if (        b==1 && c==2 && d==3) return true;
    if (a==1         && c==2 && d==3) return true;
    if (        b==2 && c==1 && d==3) return true;
    if (        b==1 && c==3 && d==2) return true;
    if (a==1 && b==2         && d==3) return true;
    return false;
}

ll solve(int N) {
    ll dp[101][625][2];
    mset(dp, 0);
    dp[0][0][0] = 1;
    rep(i,N){
        rep(a,5) rep(b,5) rep(c,5) rep(d,5) {
            int abcd = ((a*5+b)*5+c)*5+d;
            for (int e=1; e<5; ++e) {
                int bcde = ((b*5+c)*5+d)*5+e;
                bool _bad = bad(b,c,d,e);
                rep(j,2){
                    (dp[i+1][bcde][j || _bad] += dp[i][abcd][j]) %= MOD;
                }
            }
        }
    }
    ll ans = 0;
    rep(i,625) {
        (ans += dp[N][i][1]) %= MOD;
    }
    return SUB(POW(4,N), ans);
}

int main() {
    int N; cin >> N;
    cout << solve(N) << endl;
    return 0;
}


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

int main() {
	int n;
	string s;
	cin >> n >> s;

	int max = 0;
	int num = 0;
	for (int i = 0; i < n; i++) {
		if (s.at(i) == 'I') {
			num++;
		} else if (s.at(i) == 'D') {
			num--;
		}
		if (max < num) {
			max = num;
		}
	}
	cout << max << endl;
	return 0;
}


Measured memory_bytes: 256
#素因数分解、計算量はO(√N)
from collections import defaultdict
def factorize(n):
  b = 2
  dic = defaultdict(lambda: 0)
  while b * b <= n:
    while n % b == 0:
      n //= b
      dic[b] += 1
    b = b + 1
  if n > 1:
    dic[n] += 1
  return dic
N = int(input())
fct = factorize(2*N)
lis = []
for k,v in fct.items():
  lis.append(pow(k,v))
ans = 10**18
from itertools import groupby, accumulate, product, permutations, combinations
for pro in product([0,1],repeat=len(lis)):
  prod1 = 1
  for i,p in enumerate(pro):
    if p==1:
      prod1 *= lis[i]
  if prod1==1:
    continue
  prod2 = (N*2)//prod1
  if prod1<prod2:
    prod1,prod2 = prod2,prod1
  
  for i in range(1,prod2+1):
    if prod1*i>ans:
      break
    if (prod1*i-1)%prod2==0:
      ans = min(ans, prod1*i-1)
      break
    if (prod1*i+1)%prod2==0:
      ans = min(ans, prod1*i)
      break

print(ans)



Measured memory_bytes: 68860
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Practice
{
    class Program
    {
        static void Main(string[] args)
        {
            SolveA();
            //SolveB();
            //SolveC();
            //SolveD();
        }
        private const long MOD = 1000000007;

        static void SolveA()
        {
            var o = ReadAndParseIntArr();
            int a = o[0];
            int b = o[1];
            int c = o[2];
            string ans = a <= c && c <= b ? "Yes" : "No";
            Console.WriteLine(ans);
        }

        #region よく使う便利関数
        private static bool isPrime(long x)
        {
            if (x == 2) { return true; }
            if (x < 2 || x % 2 == 0) { return false; }
            long i = 3;
            while (i * i <= x)
            {
                if (x % i == 0) { return false; }
                i = i + 2;
            }
            return true;
        }
        private static long lcm(long x, long y)
        {
            return x / gcd(x, y) * y;
        }
        private static long gcd(long x, long y)
        {
            return y > 0 ? gcd(y, x % y) : x;
        }
        private static long pow(long x, long n)
        {
            if (n == 0) { return 1; }
            long res = pow(x * x % MOD, n / 2);
            if (n % 2 == 1)
            {
                res = res * x % MOD;
            }
            return res;
        }
        private static int ReadAndParseInt()
        {
            return int.Parse(Console.ReadLine());
        }
        private static int[] ReadAndParseIntArr()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);
        }
        private static long ReadAndParseLong()
        {
            return long.Parse(Console.ReadLine());
        }
        private static long[] ReadAndParseLongArr()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), long.Parse);
        }
        #endregion
    }
}

Measured memory_bytes: 11092
import java.util.Random;
import java.util.Scanner;

public class Main {
	private static final int K = 26;
	private static final int SEARCH = 50;
	private static final int CHECK = 100000;
	private static Random rand = new Random(System.currentTimeMillis());

	public static void main(String[] args) {
		var sc = new Scanner(System.in);
		var d = sc.nextInt();
		var c = new int[K];
		var s = new int[d][K];
		for (var i = 0; i < K; i++) {
			c[i] = sc.nextInt();
		}
		for (var i = 0; i < d; i++) {
			for (var j = 0; j < K; j++) {
				s[i][j] = sc.nextInt();
			}
		}
		sc.close();

		var t = new int[d];
		var max = Integer.MIN_VALUE;
		for (var i = 0; i < SEARCH; i++) {
			var t2 = search(c, s, d);
			var score = score(c, s, t2, d);
			if (score > max) {
				t = t2;
				max = score;
			}
		}

		for (var i = 0; i < d; i++) {
			System.out.println(t[i]);
		}
	}

	private static int[] search(int[] c, int[][] s, int d) {
		var count = 0;
		var t = create(c, s, d);
		for (var i = 0; count < CHECK; i++) {
			for (var j = 1; j < K; j++) {
				count++;
				var mdi = i % d;
				var mti = (t[mdi] + j - 1) % K;
				var check = check(c, s, t, d, mdi, mti);
				if (check > 0) {
					t[mdi] = mti + 1;
					break;
				}
			}
		}

		return t;
	}

	private static int check(int[] c, int[][] s, int[] t, int d, int mdi, int mti) {
		var sum = 0;
		var ti = t[mdi] - 1;
		sum += s[mdi][mti] - s[mdi][ti];

		var last1 = 0;
		var last2 = 0;
		for (var i = mdi - 1; i >= 0; i--) {
			if (t[i] == ti + 1) {
				last1 = i + 1;
				break;
			}
		}
		for (var i = mdi - 1; i >= 0; i--) {
			if (t[i] == mti + 1) {
				last2 = i + 1;
				break;
			}
		}

		var count1 = 1;
		var count2 = 1;
		for (var i = mdi + 1; i < d; i++) {
			if (t[i] == ti + 1) {
				break;
			}
			count1++;
		}
		for (var i = mdi + 1; i < d; i++) {
			if (t[i] == mti + 1) {
				break;
			}
			count2++;
		}
		sum += (mdi - last2 + 1) * c[mti] * count2 - (mdi - last1 + 1) * c[ti] * count1;

		return sum;
	}

	private static int score(int[] c, int[][] s, int[] t, int d) {
		var sum = 0;
		var last = new int[K];
		for (var i = 0; i < d; i++) {
			var ti = t[i] - 1;
			sum += s[i][ti];
			last[ti] = i + 1;
			for (var j = 0; j < K; j++) {
				sum -= ((i + 1) - last[j]) * c[j];
			}
		}

		return sum;
	}

	private static int[] create(int[] c, int[][] s, int d) {
		var t = new int[d];
		var last = new int[K];
		for (var i = 0; i < d; i++) {
			var v = new int[K];
			for (var j = 0; j < K; j++) {
				v[j] = s[i][j] + (((i + 1) - last[j]) * c[j]);
			}
			t[i] = rand(v) + 1;
			last[t[i] - 1] = i + 1;
		}
		return t;
	}

	private static int rand(int[] v) {
		var sum = 0;
		for (var i = 0; i < v.length; i++) {
			sum += v[i];
		}
		var value = rand.nextInt(sum);
		var index = 0;
		sum = 0;
		for (var i = 0; i < v.length; i++) {
			sum += v[i];
			if (value < sum) {
				break;
			}
			index++;
		}
		return index;
	}
}

Measured memory_bytes: 48900
#include<iostream>
#include<cstdio>
#include<cstring>
namespace test
{
	typedef long long ll;
	const int N=100100;
	int n,m;
	template<typename T0,typename T1>void mul(T0 *a,T1 *b)
	{
		static T0 c[N];
		for(int i=1;i<=n;i++)
			c[i]=a[b[i]];
		for(int i=1;i<=n;i++)
			a[i]=c[i];
	}
	int a[N],c[N];
	ll f[N];
	ll ans,b;
	void initialize()
	{
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
			scanf("%lld",f+i),a[i]=c[i]=i;
		ans=f[1];
		for(int i=1;i<n;i++)
			f[i]=f[i+1]-f[i];

		scanf("%d%lld",&m,&b);
		for(int i=1,x;i<=m;i++)
			scanf("%d",&x),std::swap(a[x-1],a[x]);
	}
	void qpow()
	{
		for(;b;b>>=1,mul(a,a))
			if(b&1)mul(c,a);
	}
	void solve()
	{
		initialize();
		qpow();
		mul(f,c);
		printf("%lld\n",ans);
		for(int i=1;i<n;i++)
			printf("%lld\n",ans+=f[i]);
	}
}
int main()
{
//	freopen("in","r",stdin);
//	freopen("out","w",stdout);
	test::solve();
	return 0;
}


Measured memory_bytes: 3968
from collections import defaultdict

def main():
    n = int(input())
    a = list(map(int, input().split()))

    r = [0]
    d = defaultdict(int)
    d[0] = 1
    ans = 0
    for i in range(n):
        r.append(r[i] + a[i])
        ans += d[r[i+1]]
        d[r[i+1]] += 1
    print(ans)


if __name__ == '__main__':
    main()

Measured memory_bytes: 110288
/*
17/10/2017
*/
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
int main()
{
string n;
cin>>n;
int ans=0;
for(int i=0;i<n.length();i++)
{
ans=(ans+n[i]-'0')%9;
}
if(ans==0)
{
cout<<"Yes"<<endl;
}
else
{
cout<<"No";
}
}
/*
 *||||

 */

Measured memory_bytes: 3748
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<vector>
#include <bitset>
using namespace std;
#define MAX 1000000007
typedef long long ll;

ll A[210000];

int main(){
  ll N,K,data,ans=0;
  cin>>N>>K;
  for(int i=0;i<N;i++){
    cin>>data;
    A[data]++;
  }
  sort(A,A+N,greater<ll>());
  for(int i=K;i<=N;i++){
    ans+=A[i];
  }
  cout<<ans<<endl;
  
}


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

using namespace std;

int n, d[(int)1e5];

int main(){
	cin >> n;
	if(n % 2 == 1){
		cout << 0 << endl;
		return 0;
	}
	for(int i = 0; i < n; ++i) cin >> d[i];	
	sort(d, d + n);
	int left = n / 2 - 1, right = n / 2;
	cout << d[right] - d[left] << endl;
}

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

using namespace std;

const int N = 1e5+7, inf = 1e9;

#define pii pair<int,int>
#define fi first
#define se second

vector<int> g[N], ans(N);
vector<pii> range(N, {-inf, inf}), edge;
int n, k;

pii merge(pii x, pii y){
	if(x > y) swap(x, y);
	return {max(x.fi, y.fi), min(x.se, y.se)};
}

void dfs(int p, int u){
	for(auto v: g[u]){
		if(v != p){
			dfs(u, v);
			range[u] = merge(range[u], {range[v].fi - 1, range[v].se + 1});
		}
	}
}

void build(int p, int u){
	for(auto v: g[u]){
		if(v != p){
			if(range[v].fi <= ans[u] - 1 && ans[u] - 1 <= range[v].se) ans[v] = ans[u] - 1;
			else ans[v] = ans[u] + 1;
			build(u, v);
		}
	}
}

signed main(){
	cin >> n;
	for(int i = 0; i < n - 1; ++i){
		int u, v;
		cin >> u >> v;
		g[u].push_back(v), g[v].push_back(u);
		edge.push_back({u,v});
	}
	cin >> k;
	
	for(int i = 0; i < k; ++i){
		int v, x;
		cin >> v >> x;
		range[v] = {x, x};
	}
	dfs(1,1);
	for(int i = 1; i <= n; ++i)
		if(range[i].fi > range[i].se) 
			cout << "No", exit(0);
	
	ans[1] = range[1].fi;
	dfs(1,1);
	build(1,1);
	
	bool ok = true;
	for(int i = 1; i <= n; ++i)
		ok &= (range[i].fi <= ans[i] && ans[i] <= range[i].se);
	
	for(auto e: edge) ok &= (abs(ans[e.fi] - ans[e.se]) == 1);
	
	if(!ok) cout << "No";
	else{
		cout << "Yes" << endl;
		for(int i = 1; i <= n; ++i) cout << ans[i] << endl;
	}
}

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

int m;

int x[20];
int y[20];

int dfs1(vector<bool>& used);

int dfs2(vector<bool>& used, int i1, int i2, int d) {
	int c = count(used.begin(), used.end(), true);
	int res = d;
	if (c == m) return res;
	bool ng = true;
	for (int i = 0; i < m; i++) if (!used[i]) {
		used[i] = true;
		for (int j = i + 1; j < m; j++) if (!used[j]) {
			int dx1 = x[i1] - x[i2], dy1 = y[i1] - y[i2];
			int dx2 = x[i] - x[j], dy2 = y[i] - y[j];
			if (dx1 * dy2 != dx2 * dy1) continue;
			used[j] = true;
			ng = false;
			res = max(res, d + dfs2(used, i, j, d + 1));
			used[j] = false;
		}
		used[i] = false;
	}
	if (ng) res = max(res, d + dfs1(used));
	return res;
}

map<vector<bool>, int> dic;

int dfs1(vector<bool>& used) {
	if (dic.count(used)) return dic[used];
	int res = 0;
	for (int i = 0; i < m; i++) if (!used[i]) {
		used[i] = true;
		for (int j = i + 1; j < m; j++) if (!used[j]) {
			used[j] = true;
			res = max(res, dfs2(used, i, j, 0));
			used[j] = false;
		}
		used[i] = false;
	}
	return dic[used] = res;
}

int main()
{
	cin >> m;
	for (int i = 0; i < m; i++) {
		cin >> x[i] >> y[i];
	}
	vector<bool> used(m);
	cout << dfs1(used) << endl;
	return 0;
}



Measured memory_bytes: 7204
#include <bits/stdc++.h>
#define meow(args...) fprintf(stderr, args)
template<class T1, class T2> inline bool cmin(T1 &a, const T2 &b) {return b<a?(a=b, true):false;}
template<class T1, class T2> inline bool cmax(T1 &a, const T2 &b) {return a<b?(a=b, true):false;}
template<class Type> Type read() {
	Type a;
	bool b;
	unsigned char c;
	while(c=getchar()-48, (c>9)&(c!=253));
	for(a=(b=c==253)?0:c; (c=getchar()-48)<=9; a=a*10+c);
	return b?-a:a;
}
int (*rd)()=read<int>;
typedef unsigned u32;
typedef long long s64;
typedef unsigned long long u64;
const u32 P=1e9+7;
u32 &inc(u32 &a, u32 b) {return (a+=b)<P?a:(a-=P);}
u32 &dec(u32 &a, u32 b) {return (a-=b)&0x80000000?(a+=P):a;}
u32 sum(u32 a, u32 b) {return inc(a, b);}
u32 dif(u32 a, u32 b) {return dec(a, b);}

const int N=2e5+5;
int n, m;
u32 f[N];
char s[N];
int main() {
	assert(scanf("%d%d%s", &n, &m, s)==3);
	if(s[0]=='B') for(int i=0; i<m; ++i) s[i]^='B'^'R';
	if(std::count(s, s+m, 'B')==0) {
		f[0]=f[1]=1;
		for(int i=2; i<=n; ++i) f[i]=sum(f[i-1], f[i-2]);
		printf("%u\n", sum(f[n], f[n-2]));
		return 0;
	}
	if(n%2==1) {
		puts("0");
		return 0;
	}
	int ml=n;
	for(int l=0, r; l<m; l=r+1) {
		for(r=l; r<m&&s[r]=='R'; ++r);
		if(r==m) break;
		if(l==0) cmin(ml, r+1); else if((r-l)%2==1) cmin(ml, r-l);
	}
	ml=(ml+1)/2;
	n/=2;
	u32 s=0;
	for(int i=1; i<=n; ++i) {
		f[i]=s;
		if(i<=ml) inc(f[i], 2*i);
		inc(s, f[i]);
		if(i>=ml) dec(s, f[i-ml]);
	}
	printf("%u\n", f[n]);
	return 0;
}


Measured memory_bytes: 1280
use proconio::input;
use std::cmp::{max, min};

const MAX_L: u32 = 100_000;
const BASE_SIZE: usize = 1 << 10;

fn main() {
    input! {
        n: usize,
        vw: [(u32, u32); n],
        q: usize,
        vl: [(usize, u32); q],
    }
    let dp = make_dp(&vw);
    for vlq in vl {
        println!("{}", calc(&vw, vlq.0 - 1, vlq.1 as usize, &dp));
    }
}

fn make_dp(vw: &Vec<(u32, u32)>) -> Vec<Vec<u32>> {
    let mut dp = Vec::<Vec<u32>>::new();

    dp.push(
        (0..=MAX_L)
        .map(|i| if i < vw[0].1 { 0 } else { vw[0].0 })
        .collect()
    );

    let n = min(vw.len(), BASE_SIZE - 1);
    for c in 1..n {
        let p = (c - 1) / 2;
        dp.push(
            (0..=MAX_L)
            .map(|i| {
                let w = vw[c].1;
                let v = if i < w { 0 } else { dp[p][(i - w) as usize] + vw[c].0 };
                max(dp[p][i as usize], v)
            })
            .collect()
        );
    }

    dp
}

fn calc(
    vw: &Vec<(u32, u32)>,
    u: usize, 
    l: usize,
    dp: &Vec<Vec<u32>>
) -> u32 {
    if u < dp.len() {
        dp[u][l]
    } else {
        let p = (u - 1) / 2;
        let w = vw[u].1;
        let pv1 = calc(vw, p, l, dp);
        let pv2 = if l < w as usize { 0 } else { calc(vw, p, l - w as usize, dp) + vw[u].0 };
        max(pv1, pv2)
    }
}


Measured memory_bytes: 411008
use proconio::input;
use std::cmp::*;

fn main() {
    input! {
        n: usize,
        a: [i64;n]
    }

    let mut ai: Vec<(i64, i64)> = a.iter().enumerate().map(|(i, &a)| (a, i as i64)).collect();
    ai.sort();
    ai.reverse();
    // 降順に並んだ。
    // println!("{:?}", ai);
    let mut dp = vec![vec![0; n + 1]; n + 1];
    dp[0][0] = 0;

    let mut ans = 0;
    for (i, &(a, j)) in ai.iter().enumerate() {
        if i < n - 1 {
            for l in 0..i + 1 {
                let r = i - l;
                dp[l + 1][r] = max(dp[l + 1][r], dp[l][r] + a * (j - (l as i64)));
                dp[l][r + 1] = max(dp[l][r + 1], dp[l][r] + a * ((n - 1 - r) as i64 - j));
            }
        } else {
            for l in 0..i + 1 {
                let r = i - l;
                ans = max(ans, dp[l][r] + a * (j - (l as i64)));
                ans = max(ans, dp[l][r] + a * ((n - 1 - r) as i64 - j));
            }
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 33388
N = int(input())
A = list(map(int, input().split()))

if __name__ == "__main__":
    cnt = 0
    for i in range(N):
        tmp_idx = i
        for j in range(i+1, N):
            if A[tmp_idx] > A[j]:
                tmp_idx = j
        if tmp_idx != i:
            A[i], A[tmp_idx] = A[tmp_idx], A[i]
            cnt += 1

    print(" ".join(map(str, A)))
    print(cnt)


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

int main() {
  int N;
  cin >> N;
  vector<int>A(N),copy(N);
  for(int i=0; i<N; i++){
    cin >> A[i];
    copy[i] = A[i];
  }
  int iti,ni;
  sort(copy.begin(),copy.end());
  iti = copy[N-1];
  ni=copy[N-2];
  for(int i=0; i<N; i++){
    if(A[i] == iti){
      cout << ni << endl;
    }
    else{
      cout << iti << endl;
    }
  }
}


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

#define BR "\n"
#define SP " "
#define SHOW(x) for(int i = 0; i < x.size(); i++) { cout << x[i] << SP; } cout << BR;
#define SHOW2(x) for(int j = 0; j < x.size(); j++) { SHOW(x[j]); } cout << BR;

int main() {
  ll R, C;
  cin >> R >> C;

  vector<vector<ll>> cell(R, vector<ll>(C));
  vector<ll> rsum(R, 0);
  vector<ll> csum(C, 0);

  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      cin >> cell[i][j];
      rsum[i] += cell[i][j];
      csum[j] += cell[i][j];
    }
  }

  for (int i = 0; i < R; i++) {
    for (int j = 0; j < C; j++) {
      cout << cell[i][j] << SP;
    }
    cout << rsum[i] << BR;
  }

  ll total = 0;
  for (int i = 0; i < C; i++) {
    cout << csum[i] << SP;
    total += csum[i];
  }
  cout << total << BR;

  return 0;
}


Measured memory_bytes: 3188
R,G,B=map(int,input().split())
K=int(input())

M=0

while R>=G or G>=B:
    if R>=G:
        G*=2
        M+=1
    
    if G>=B:
        B*=2
        M+=1

if M<=K:
    print('Yes')
else:
    print('No')


Measured memory_bytes: 9052