akhauriyash/Code-Regression huggingface dataset:
from collections import defaultdict
from sys import stdin
from collections import deque, defaultdict

N, M = [int(x) for x in stdin.readline().rstrip().split()]

par = [i for i in range(N+1)]
rank = [0] * (N+1)
size = [1] * (N+1)


def find(x):
    if par[x] == x:
        return par[x]
    else:
        par[x] = find(par[x])
        return par[x]


def union(x, y):
    x = find(x)
    y = find(y)
    if x == y:
        return
    if rank[x] < rank[y]:
        par[x] = y
        size[y] += size[x]
        size[x] = 0

    else:
        par[y] = x
        size[x] += size[y]
        size[y] = 0
        if rank[x] == rank[y]:
            rank[x] += 1


def check_same(x, y):
    return find(x) == find(y)


def check_size(x):
    return size[find(x)]


adj = defaultdict(set)

for _ in range(M):
    A, B = [int(x) for x in stdin.readline().rstrip().split()]
    union(A, B)
    adj[A].add(B)
    adj[B].add(A)

print("Yes")
ans = [0]*(N+1)
visit_p = deque()
visit_p.append((1, 0))
visited = [False]*(N+1)
visited[1] = True
dist = [10**18]*(N+1)

while visit_p:
    now_v, distance = visit_p.popleft()
    for v in adj[now_v]:
        if visited[v]:
            continue

        if distance < dist[v]:
            ans[v] = now_v
        dist[v] = min(dist[v], distance)

        visited[v] = True
        visit_p.append((v, 1+distance))

for k in ans[2:]:
    print(k)

Measured memory_bytes: 73744
/*input
100000

*/ 
#include <iostream>
#include <bits/stdc++.h>
#include <math.h> 
    
using namespace std;
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define gc getchar_unlocked
#define fo(i,n) for(ll i=0;i<n;i++)
#define Fo(i,k,n) for(ll i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define int long long
#define si(x)   scanf("%d",&x)
#define sl(x)   scanf("%lld",&x)
#define ss(s)   scanf("%s",s)
#define pi(x)   printf("%d\n",x)
#define pl(x)   printf("%lld\n",x)
#define ps(s)   printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
#define ms(a,x) memset(a, x, sizeof(a));
#define endl "\n"
typedef pair<int, int>  pii;
typedef vector<int>     vi;
typedef vector<pii>     vpii;
typedef vector<vi>      vvi;
int mpow(int base, int exp); 
void ipgraph(int m);
//void dfs(int u, int par);
const int mod = 1000000007;
const int N = 1000000001;
const int MAX = 1e5+1;


int32_t main() {
    IOS;
	int n;
	cin>>n;
	if(n<9){
		cout<<n<<endl;
		return 0;
	}
	priority_queue<int,vector<int>,greater<int>> q;
	for(int i=1;i<=9;i++){
		q.push(i);
	}
	int ans = 0;
	while(n>0){
		int num = q.top();
		q.pop();
		n--;
		ans = num;
		if(num%10==0){
			q.push(num*10);
			q.push(num*10+1);
		}
		else if(num%10==9){
			q.push(num*10+9);
			q.push(num*10+8);
		}
		else{
			int l = num%10;
			
			q.push(num*10+l-1);
			q.push(num*10+l);
			q.push(num*10+1+l);
		}
	}
	cout<<ans<<endl;
	return 0;
}






Measured memory_bytes: 2420
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <limits.h>
#include <queue>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define LIM 100001

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

bool prime[LIM];
bool prime_2017[LIM];
int primesum[LIM];
int main(){
    REP(i,LIM){
        prime[i] = is_prime(i);
        if(prime[i] & prime[(i+1)/2]) prime_2017[i] = true;
    }
    REP(i,LIM){
        primesum[i] = prime_2017[i] ? 1 : 0;
        if(i == 0) continue;
        primesum[i] += primesum[i-1];
        //printf("%d:%d\n",i,primesum[i]);
    }
    
    int Q;
    cin >> Q;
    int l,r;
    REP(i,Q){
        cin >> l >> r;
        cout << primesum[r] - primesum[l-1] << endl;
    }
    return 0;
}


Measured memory_bytes: 1280
#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<math.h>

using namespace std;

#define INF 1000000007
#define LINF 1000000000000000007

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


int n, a[1010];

int main(){
	cin >> n;
	int cnt = 0;
	for(int i = 0; i < n; i++){
		cin >> a[i];
		if(a[i] != i+1){
			cnt++;
		}
	}
	if(cnt < 3) cout << "YES" << endl;
	else cout << "NO" << endl;
	
	return 0;
}

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

int main(){
    int n,k;
    cin >> n >> k;
    vector<int> p(n);
    rep(i,n) cin >> p[i];
    sort(p.begin(),p.end());
    int ans=0;
    rep(i,k) ans += p[i];
    cout << ans << endl;
}

Measured memory_bytes: 3652
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
typedef vector<int> vi;
// END NO SAD

typedef long long ll;
typedef pair<int, int> pii;

void solve() {
  map<int, int> dp;
  int n;
  cin >> n;
  for(int i = 0; i < n; i++) {
    int d;
    cin >> d;
    if(i == 0 && d != 0) {
      cout << "0\n";
      return;
    }
    dp[d]++;
  }
  ll ret = 1;
  for(auto out: dp) {
    if(out.first == 0) {
      if(out.second != 1) {
        cout << "0\n";
        return;
      }
    }
    else {
      for(int i = 0; i < out.second; i++) {
        ret *= dp[out.first-1];
        ret %= 998244353;
      }
    }
  }
  cout << ret << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
}


Measured memory_bytes: 5632
let N = stdin.ReadLine() |> int

let B = stdin.ReadLine().Split() |> Array.map int

let items =
    let rec loop i b =
        if i = N then
            []
        else
            let bx = b |> Array.indexed |> Array.filter (fun (a, b) -> a+1 = b)
            if Array.isEmpty bx then
                [-1]
            else
                let target = Array.last bx
                let j = fst target
                let x = snd target
                x :: loop (i+1) (Array.append b.[0..j-1] b.[j+1..]) 
    loop 0 B

if items |> Seq.exists ((=) -1) then do
    stdout.WriteLine -1
else
    for x in items |> List.rev do
        stdout.WriteLine x

Measured memory_bytes: 13268
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int d, t, s;
        d = sc.nextInt();
        t = sc.nextInt();
        s = sc.nextInt();
        if (d > t * s) {
            System.out.println("No");
        } else {
            System.out.println("Yes");
        }
        sc.close();
    }
}


Measured memory_bytes: 35652
#n = int(input())
#n,k = map(int,input().split())
#x = list(map(int,input().split()))

o = list(input())
e = list(input())
ans = ""
for i in range(len(e)):
    ans += o[i]
    ans += e[i]

if len(e) != len(o):
    ans += o[-1]

print(ans)
    





Measured memory_bytes: 2940
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
#define INF 1007654321
#define PI 3.14159265358979
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define repp(i, s, e) for(int i = (s); i <= (e); ++i)
#define sz(x) ((int)x.size())
#define all(x) x.begin(), x.end()
#define FAST_IO() ios::sync_with_stdio(0); cin.tie(0)
template<typename T>
ostream& operator<<(ostream &os, const vector<T> &v) { for (auto x : v) os << x << " "; return os << "\n"; }
template<typename U, typename V>
ostream& operator<<(ostream &os, const pair<U, V> &p) { return os << "(" << p.first << "," << p.second << ")";}
 
ll upsqrt(ll N) {
    ll sqrtN = sqrt(max(0LL, N - 100));
    for(ll i = sqrtN; i <= sqrtN + 100; ++i) {
        if(i * i >= N) return i;
    }
    return 0;
} 

int main() {
    /* The area of a Triangle
    S = | X1 X2 X3 | = (X1*Y2 + X2*Y3 + X3*Y1 - X2*Y1 - X3*Y2 - X1*Y3)
        | Y1 Y2 Y3 |
    Let (X1, Y1) = (0, 0),
    S = (X2 * Y3 - X3 * Y2)
    */
    ll S; cin >> S;
    ll rS = upsqrt(S);
    ll remain = rS * rS - S;
    // (0, 0) (rS, 1) (remain, rS)
    cout << 0 << " " << 0 << " " << rS << " " << 1 << " " << remain << " " << rS << endl;
}

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

using namespace std;

#define ll long long
#define ull unsigned long long

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

    int n, k, r, s, p;
    cin >> n >> k >> r >> s >> p;
    string t;
    cin >> t;
    int ans = 0;
    for (int i = 0; i < k; i++) {
        string str;
        for (int j = i; j < n; j += k) str += t[j];
        bool suc = false;
        for (int j = 0; j < (int) str.length(); j++) {
            if (suc) suc = false;
            else {
                if (j > 0 && str[j] == str[j - 1]) {
                    suc = true;
                    continue;
                }
            }
            if (str[j] == 'r') ans += p;
            if (str[j] == 's') ans += r;
            if (str[j] == 'p') ans += s;
        }
    }
    cout << ans << '\n';
}

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const ull mod = 1e9 + 7;
#define REP(i,n) for(int i=0;i<(int)n;++i)

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

template < typename T >
void vprint(T &v){
	REP(i, v.size()){
		cout << v[i] << " ";
	}
	cout << endl;
}

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

	cout << min(A*N, B) << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
  ll n, d;
  cin >> n >> d;
  vector<ll> x(n);
  vector<ll> y(n);
  ll ans = 0;
  rep(i,n) {
    cin >> x[i] >> y[i];
  }
  rep(i,n) {
    ll s, t;
    s = x[i]*x[i];
    s += y[i]*y[i];
    t = d*d;
    if(s <= t) ans++;
  }
  cout << ans;
}

Measured memory_bytes: 6344
#include<bits/stdc++.h>
#define ll long long
#define N 5010
#define mod 1000000007
#define rep(i,l,r) for (int i=l;i<=r;++i)
using namespace std;
ll jc[N],inv[N],f[N],g[N],res;
int n,x,y,sz[N],a[N],tot,cnt;
vector<int>G[N];
ll K(ll x,int y=mod-2){
	ll t=1; for (;y;y>>=1,x=x*x%mod) if (y&1) t=t*x%mod; return t;
}
ll C(int x,int y){
	return jc[x]* inv[y]%mod *inv[x-y]%mod;
}
void U(ll &x,ll y){
	if ((x+=y)>=mod) x%=mod;
}

void dfs(int x,int fa){
	int mx=0; sz[x]=1;
	for (auto y:G[x]) if (y-fa)
		dfs(y,x), sz[x]+=sz[y], mx=max(mx,sz[y]);
	mx=max(mx,n-sz[x]);
	if (mx*2==n) exit(!(cout<<jc[mx]*jc[mx]%mod));
	if (mx*2>n) return;
	for (auto y:G[x]) if (y-fa) a[++cnt]=sz[y];
	if (x-1) a[++cnt]=n-sz[x];
	memset(f,0,sizeof f); f[tot=0]=1;
	rep(i,1,cnt){
		memset(g,0,sizeof g);
		rep(j,0,a[i]) rep(k,0,tot)
			U(g[k+j],f[k]*C(a[i],j)%mod*C(a[i],j)%mod*jc[j]%mod);
		memcpy(f,g,sizeof g);
		tot+=a[i];
	}
	rep(i,0,tot) U(res,mod+(i&1?-1:1)*f[i]*jc[n-i]%mod);
	exit(!(cout<<res));
}

int main(){
	jc[0]=1; rep(i,1,N-1) jc[i]=jc[i-1]*i%mod;
	inv[N-1]=K(jc[N-1]); for(int i=N;i--;) inv[i-1]=inv[i]*i%mod;
	cin>>n;
	rep(i,1,n-1) cin>>x>>y, G[x].push_back(y), G[y].push_back(x);
	dfs(1,0);
}

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

using namespace std;

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

#define rep(i,n) for(int i=0;i<(n);++i)
#define repi(i,a,b) for(int i=int(a);i<(b);i++)
#define repr(i,b,a) for(int i=int(b);i>=(a);i--)
#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool valid(int lx,int ux,int ly,int uy,int x,int y){
	return lx<=x&&x<ux&&ly<=y&&y<uy; 
}
ll power(ll x,ll p){
	if(p==0)return 1;
	ll res=power(x*x%mod,p/2);
	if(p%2==1)res=res*x%mod;
	return res;
}

ll f2(int n, int p, string s){
	ll ans=0;
	reverse(all(s));
	rep(i,n){
		if((s[i]-'0')%p==0) ans += (n-i);
	}
	return ans;
}

ll f5(int n, int p, string s){
	ll ans=0;
	reverse(all(s));
	rep(i,n){
		if((s[i]-'0')%p==0) ans += (n-i);
	}
	return ans;
}

ll fp(int n, int p, string s){
	ll ans=0;
	reverse(all(s));
	vector<int> res(n,0);
	rep(i,n){
		res[i] = (s[i]-'0') % p;
	}
	ll ten=10;
	repi(i,1,n){
		res[i] = (res[i]*ten + res[i-1]) % p;
		ten = ten*10 % p;
	}
	map<int, ll>mp;
	rep(i,n){
		mp[res[i]] += 1;
	}


	// mp[0]++;
	rep(i,p) ans += mp[i] * (mp[i]-1)/2;
	ans += mp[0];
	return ans;
}

int main()
{
	int n,p;
	string s;
	cin>>n>>p>>s;
	ll ans;
	if(p==2) ans = f2(n,p,s);
	else if(p==5) ans = f5(n,p,s);
	else ans = fp(n,p,s);
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 2052
def main():
    s_list = list(input())
    f = s_list[0] == s_list[1] or s_list[1] == s_list[2] or s_list[2] == s_list[3]
    message = 'Bad' if f else 'Good'
    print(message)


if __name__ == '__main__':
    main()

Measured memory_bytes: 2940
//g++  5.4.0

#include<bits/stdc++.h>
using namespace std;

#define ff first
#define ss second
#define int long long
#define pb push_back
#define pii pair< int,int >
#define fast ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;

const int inf = 1e10;
signed main()
{
    fast;
    int n;
    cin >> n;
    int a[3] = {0};
    while( n-- )
    {
        int b[3];
        for(int i=0 ; i<3 ; i++ ) cin >> b[i];
        int c[3] ={0} ;
        for(int i=0 ; i<3 ; i++ )
        {
            
            for(int j=0 ; j<3 ; j++ )
            {
                if( i!=j )
                    c[i] = max( c[i], a[j] );
            }
        }
        for(int i=0 ; i<3 ; i++ )
            a[i] = b[i] + c[i];
    }
    cout << *max_element( a , a+3 );
}





Measured memory_bytes: 256
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define cinf(n,x) for(int i=0;i<(n);i++)cin>>x[i];
#define ft first
#define sc second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define LB(a,x) lb(all(a),x)-a.begin()
#define UB(a,x) ub(all(a),x)-a.begin()
#define mod 1000000007
//#define mod 998244353
#define FS fixed<<setprecision(15)
using namespace std;
typedef long long ll;
const double pi=3.141592653589793;
template<class T> using V=vector<T>;
using Graph = vector<vector<int>>;
using P=pair<ll,ll>;
typedef unsigned long long ull;
typedef long double ldouble;
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> inline void out(T a){ cout << a << '\n'; }
void YN(bool ok){if(ok) cout << "Yes" << endl; else cout << "No" << endl;}
//void YN(bool ok){if(ok) cout << "YES" << endl; else cout << "NO" << endl;}


const ll INF=1e18;
const int mx=200005;

int main(){
    cin.tie(0);ios::sync_with_stdio(false);
    string s,t;
    cin>>s>>t;
    int n=s.size();
    int m=t.size();
    int ans=100000;
    rep(i,n-m+1){
        int cnt=0;
        rep(j,m){
            if(t[j]!=s[i+j]) cnt++;
        }
        chmin(ans,cnt);
    }
    out(ans);
}
//ペナルティ出しても焦らない　ACできると信じろ！！！
//どうしてもわからないときはサンプルで実験　何か見えてくるかも
//頭で考えてダメなら紙におこせ！！
/*
V,P(大文字)使用不可
乗算などの際にオーバーフローに注意せよ！
(適切にmodをとれ　にぶたんで途中で切り上げろ)
制約をよく読め！
{
    ・全探索できるなら全探索しろ
    ・異常な制約ならそこに注目
}
stringの計算量(扱い)注意
コーナー注意！(特に数値が小さいものについては要検証)
N行出力のときは'¥n'
グリッド上では行先が範囲内におさまるかif文で確認(RE注意)
if文ではちゃんと比較演算子==を使え('='でもエラー出ない)
配列(vector)の大きさが0か1以上かで場合分けせよ(RE注意)
(vector<int> a(m)でm==0というものはできない)
modはなるべく最後に取れ！
doubleを扱うときには(abs)ではなく'fabs'!!!
*/

Measured memory_bytes: 3644
N=int(input())
X=[list(map(int,input().split())) for i in range(N)]
Y=set()
B=[[1,1] for i in range(N)]
for i in range(N):
  for j in range(2):
    if X[i][j]<0:
      B[i][j]=0
      X[i][j]*=-1
for i in range(N):
  Y.add(sum(X[i])&1)
if len(Y)==2:
  print(-1)
  exit()
Y=Y.pop()
def f(x):
  r=[]
  y=x
  for i in range(20):
    r.append(y%3)
    y//=3
  return r

P=[[] for i in range(N)]
if Y:
  print(39)
  print(*[3**(i//2) for i in range(39)],sep=' ')
else:
  print(40)
  print(*[3**(i//2) for i in range(40)],sep=' ')
Z=[[f(X[i][0]),f(X[i][1])] for i in range(N)]
V=0
for i in range(N):
  V=[0]*2
  for j in range(20):
    for k in range(2):
      Z[i][k][j]+=V[k]
      V[k]=Z[i][k][j]//3
      Z[i][k][j]%=3
    if Z[i][0][j]==0:
      if Z[i][1][j]==0:
        P[i].append('R')
        P[i].append('L')
      elif Z[i][1][j]==1:
        P[i].append('U')
        P[i].append('U')
        V[1]+=1
      else:
        P[i].append('D')
        P[i].append('D')
    elif Z[i][0][j]==1:
      if Z[i][1][j]==0:
        P[i].append('R')
        P[i].append('R')
        V[0]+=1
      elif Z[i][1][j]==1:
        P[i].append('D')
        P[i].append('L')
      else:
        P[i].append('L')
        P[i].append('U')
        V[1]+=1
    else:
      if Z[i][1][j]==0:
        P[i].append('L')
        P[i].append('L')
      elif Z[i][1][j]==1:
        P[i].append('D')
        P[i].append('R')
        V[0]+=1
      else:
        P[i].append('R')
        P[i].append('U')
        V[1]+=1
        V[0]+=1
    if j==18 and Y:
      break
  if Y:
    if V[0]:
      P[i].append('L')
    else:
      P[i].append('D')
for i in range(N):
  for j in range(len(P[0])):
    if P[i][j]=='L':
      if B[i][0]:
        P[i][j]='R'
    elif P[i][j]=='R':
      if B[i][0]:
        P[i][j]='L'
    elif P[i][j]=='U':
      if B[i][1]:
        P[i][j]='D'
    else:
      if B[i][1]:
        P[i][j]='U'
  print(*P[i],sep='')

Measured memory_bytes: 78660
#include <bits/stdc++.h>
using namespace std;
int main()
{
 string s;
 cin >> s;
 sort(s.begin(),s.end());
 if(s[0]==s[1] && s[2]==s[3] && s[1]!=s[2])
 cout << "Yes";
 else
 cout << "No";
 return 0;
}

Measured memory_bytes: 256
#include<stdio.h>

#define MAX 1000000
#define sqrtMAX 1000

int main(void)
{
  int n;
  int b[MAX+1] = {0};
  int i, j;

  // ツエツδ可トツスツテツネツスツづ個づ督づゥツつ「
  b[0] = 1;
  b[1] = 1;
  for(i=4; i<=MAX; i+=2)
      b[i] = 1;
  for(i=3; i<=sqrtMAX; i+=2)
      for(j=i+i; j<=MAX; j+=i)
          b[j] = 1;

  while(scanf("%d", &n)!=EOF) {
      for(i=n-1; i>=2; i--) {
          if(b[i]==0) {
              printf("%d", i);
              break;
          }
      }
      for(i=n+1; i<=MAX; i++) {
          if(b[i]==0) {
              printf(" %d\n", i);
              break;
          }
      }
    }
  return 0;
}

Measured memory_bytes: 0
n = int(input())
h = list(map(int,input().split()))
now = [0 for _ in range(n)]
cnt = 0
l = 0
while True:
  if now == h:
    break
  elif now[l] == h[l]:
    l += 1
  else:
    r = l
    try:
      while now[r] < h[r]:
        r += 1
    except:
      pass
    for i in range(l,r):
      now[i] += 1
    cnt += 1
    #print(now)
print(cnt)

Measured memory_bytes: 3064
import itertools
n = int(input())
P = list(map(int, input().strip().split()))
Q = list(map(int, input().strip().split()))

P_s = tuple(P)
Q_s = tuple(Q)

p_n = 0
q_n = 0

j = 1
for i in itertools.permutations(range(1, n + 1), n):
    if i == P_s:
        p_n = j
    if i == Q_s:
        q_n = j
    j += 1
print(abs(p_n - q_n))


Measured memory_bytes: 9116
package main

import (
	"fmt"
)

func gcd(n, m int) int {
	if n%m == 0 {
		return m
	}
	return gcd(m, n%m)
}

func main() {
	var k int
	fmt.Scan(&k)

	var res int
	for i := 1; i < k+1; i++ {
		for j := 1; j < k+1; j++ {
			for l := 1; l < k+1; l++ {
				res += gcd(gcd(i, j), l)
			}
		}
	}
	fmt.Println(res)
}


Measured memory_bytes: 1776
from bisect import bisect_right

n, x = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
SUM = [a[0]]

for i in range(1, n):
    SUM.append(SUM[-1]+a[i])

if SUM[-1] < x:
    print(n-1)
else:
    print(bisect_right(SUM, x))

Measured memory_bytes: 3060
M, D = map(int, input().split())
ans = 0
if D >= 22 and M >= 4:
    for m in range(1, M+1):
        for i in range(2, 10):
            if m%i == 0 and 1 < m//i < 10 and 10 * i + m//i <= D:
                ans += 1
print(ans)

Measured memory_bytes: 3060
N, M = map(int, input().split())

a = [int(input()) for i in range(M)]


for i in range(M - 1):
    if a[i] + 1 == a[i+1]:
        print(0)
        exit()

dp = [0] * (N + 1)
flag_a = [0] * (N + 1)

for i in range(M):
    flag_a[a[i]] = -1


if N == 1 and M != 1:
    print(1)
    exit()
elif N == 1 and M == 1:
    print(0)
    exit()

if 1 in a and 2 in a:
    print(0)
    exit()

if 1 in a:
    dp[1] = 0
    dp[2] = 1
elif 2 in a:
    dp[1] = 1
    dp[2] = 0
else:
    dp[1] = 1
    dp[2] = 2

for i in range(3,N+1):
    if flag_a[i] == -1:
        dp[i] = 0
    else:
        dp[i] = (dp[i - 1] + dp[i - 2]) % 1000000007

print(dp[N])

Measured memory_bytes: 51692
def mondai(W, H, x, y, r):
    if (x - r < 0 or
        x + r > W or
        y - r < 0 or
        y + r > H):
        print('No')
    else:
        print('Yes')

W, H, x, y, r = [int(x) for x in input().split()]
mondai(W, H, x, y, r)

Measured memory_bytes: 7716
using System;
class p
{
  static void Main()
  {
    string[] input = Console.ReadLine().Split(' ');
    int N = int.Parse(input[0]);
    int K = int.Parse(input[1]);
    input = Console.ReadLine().Split(' ');
    int count = 0;
    for (int i = 0; i < N; i++) {
      if (K <= int.Parse(input[i])) {
        count++;
      }
    }
    Console.WriteLine(count);
  }
}

Measured memory_bytes: 15584
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    string s;
    while(cin>>s)
    {
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='9')
                s[i]='1';
            else
                s[i]='9';
        }
        cout<<s<<endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main() {
	string s;
  	int n, a;
  	vector<tuple<string, int, int> > v;
  	cin >> n;
  	for (int i = 1; i<=n; ++i) {
    	cin >> s >> a;
      	v.emplace_back(s, -a, i);
    }
  	sort(v.begin(), v.end());
  	for (int i = 0; i<n; ++i) {
        cout << get<2>(v[i]) << endl;
    }
}


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

void swap(char & a,char &b)
{
	char c;
	c=a;
	a=b;
	b=c;
}

int main()
{
	int t;
	cin>>t;
	char a[t];
	for(int i=0;i<t;i++){cin>>a[i];}
	int point=0;
	int connt=0;
	for(int i=t-1;i>=point;i--)
	{
		if(a[i]=='R')
		{
			while(a[point]!='W' && point<i)
			{
				point++;
			}
			if(point!=i){
			swap(a[i],a[point]);
			connt++;
			point++;}
			else
			{
				break;
			}

		}
	}
	cout<<connt<<endl;
	return 0;
}

Measured memory_bytes: 3812
# exec(open('test.py').read()) 
import math
 
N = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

def lexCal(p):
	ans = 0
	used = set()
	for i in range(N):
		x = p[i]
		for y in range(1,x):
			if not y in used:
				ans += math.factorial(N-1-i)
		used.add(x)
	return ans

print(abs(lexCal(p)-lexCal(q)))

Measured memory_bytes: 38512
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
using namespace std;
///////////////////library zone!!!!!!!!!!!!!!!!!!!!!!!!!!!!
typedef long long ll;
typedef long double ld;
#define all(a) (a).begin(),(a).end()
#define EPS (1e-5)
#define bit(n,k) ((n>>k)&1)
const ll Mod = 1000000007;
const ll mod = 998244353;
struct H {
	ll x, y;
	bool operator<(const H &b) const {
		if (x != b.x) return x < b.x;
		return y < b.y;
	}
	bool operator>(const H &b) const {
		if (x != b.x) return x > b.x;
		return y > b.y;
	}
	bool operator==(const H &b) const { return x == b.x&&y == b.y; }
	bool operator!=(const H &b) const { return (*this) != b; }
};
struct P {
	ll pos, cost;
	bool operator<(const P &b) const { return cost < b.cost; }
	bool operator>(const P &b) const { return cost > b.cost; }
};
struct B {
	ll to, cost;
};
struct E {
	ll from, to, cost;
	bool operator<(const E &b) const { return cost < b.cost; }
	bool operator>(const E &b) const { return cost > b.cost; }
};
template<typename T, typename U>
void chmin(T &a, U b) {
	if (a > b) a = b;
}
template<typename T, typename U>
void chmax(T &a, U b) {
	if (a < b) a = b;
}
template<typename T>
T max_0(T a) {
	if (a < 0) return 0;
	return a;
}
template<typename T>
T min_0(T a) {
	if (a > 0) return 0;
	return a;
}
ll read() {
	ll u;
	ll k = scanf("%lld", &u);
	return u;
}
ll gcd(ll i, ll j) {
	if (i > j) swap(i, j);
	if (i == 0) return j;
	return gcd(j%i, i);
}
ll mod_pow(ll x, ll n, ll p) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % p;
		x = x * x % p;
		n >>= 1;
	}
	return res;
}//x^n%p
vector<string>split(string s, char c = ' ') {
	vector<string>vec;
	string h = "";
	for (int i = 0; i < s.size();) {
		int j = i;
		while (j < s.size() && s[j] != ' ') {
			h += s[j++];
		}
		if (h.size() > 0) vec.push_back(h);
		h = "";
		i = j + 1;
	}
	return vec;
}
const ll Inf = 3023372036854775807;
const int inf = 1500000000;
#define int long long
//----------------------------------------------------
signed main() {
	int n, k;
	cin >> n >> k;
	if (2 * k - 1 <= n) {
		cout << "YES\n";
	}
	else cout << "NO\n";
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
 
#define ll long long
#define pb push_back
#define mp make_pair
#define all(X) (X).begin(), (X).end()
 
#define cmp_all(X) (X).begin(), (X).end(), cmp
 
#define B begin()
#define E end()
#define sz size()
#define skip continue
 
#define ppi pair<pair<int, int>, int>
#define pii pair<int, int> 
#define pli pair<ll, int>
 
#define F first
#define S second
 
using namespace std;
 
const int maxn = 1e7 + 17;
const int MAXN = maxn * 4;
const int darr = 1e3 + 17;
const ll INF = 1e17 + 5;
const int mod = 1e9 + 7;
 
void Need_For_Speed () {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

ll n, x;

ll get (ll n, ll x) {
	if (n % x == 0) {
		return ((n / x) * x) * 2 - x;
	}
	return (((n - (n % x)) / x) * x) * 2 + get(x, n % x);
}

int main () {
	Need_For_Speed();
	cin >> n >> x;
	cout << get(n - x, x) + n;
	return 0;
}

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

int main()
{
    int d;
    cin >> d;
    if(d == 25) {
        cout << "Christmas";
    }
    if(d == 24) {
        cout << "Christmas Eve";
    }
    if(d == 23) {
        cout << "Christmas Eve Eve";
    }
    if(d == 22) {
        cout << "Christmas Eve Eve Eve";
    }
}


Measured memory_bytes: 3556
package main

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

var sc = bufio.NewScanner(os.Stdin)

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func nextFloat64() float64 {
	f, _ := strconv.ParseFloat(next(), 64)
	return f
}

func nextInts(n int) []int {
	slice := make([]int, n)
	for i := 0; i < n; i++ {
		slice[i] = nextInt()
	}
	return slice
}

func main() {
	sc.Split(bufio.ScanWords)
	cnt := make([]int, 4)
	for i := 0; i < 3; i++ {
		a, b := nextInt(), nextInt()
		cnt[a-1]++
		cnt[b-1]++
	}
	one := 0
	for _, v := range cnt {
		if v == 1 {
			one++
		}
	}

	if one == 2 {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}
}


Measured memory_bytes: 640
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if (n == 1) {
            System.out.println("Hello World");
            return;
        }
        int a = sc.nextInt();
        int b = sc.nextInt();
        System.out.println(a + b);
    }

}


Measured memory_bytes: 21588
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define pb push_back
#define all(v) v.begin(),v.end()
#define fi first
#define se second
#define bigger (char)toupper
#define smaller (char)tolower
using namespace std;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef long long ll;
typedef unsigned long long ull;

ll gcd(ll a, ll b) {
    if(a%b==0) return b;
    return gcd(b,a%b);
}

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

int main() {
    ll A,B,C,D;
    cin>>A>>B>>C>>D;
    ll countC=0;
    ll countD=0;
    ll countCD=0;
    countC=B/C-(A-1)/C;
    countD=B/D-(A-1)/D; 
    ll L=lcm(C,D);
    countCD=B/L-(A-1)/L;
    ll answer=B-A+1-countC-countD+countCD;
    cout<<answer<<endl;
}

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


int main() {
  int n;
  vector<int> u;
  cin >> n;
  for (int i= 0; i< n; i++) {
    int x;
    cin >> x;
    u.push_back(x);
  }

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

  double average;
  average = (u[0] + u[1]) / 2.0;

  for (int i = 2; i < n; i++) {
    average = (average + u[i]) / 2.0;
  }

  cout << setprecision(16) << average << endl;

}


Measured memory_bytes: 256
import java.util.Scanner

object Main extends App {
  val in = new Scanner(System.in)
  val N = in.nextInt()
  val L = in.nextInt()
  val K = in.nextLong()
  val ad: Array[(Long, Long)] = (for (_ <- 0 until N) yield {
    val a: Long = in.nextInt()
    val d: Long = in.nextInt()
    (a, d)
  }).toArray

  val sum = ad.map {
    case (a, d) => (a + (a + (L - 1) * d)) * L / 2
  }

  // imos[i+1] = sum(0...i)
  val imos = new Array[Long](N + 1)
  for (i <- 0 until N) {
    imos(i + 1) = imos(i) + sum(i)
  }


  def calc(a0: Long, da: Long, b0: Long, db: Long, p: Int): Long = {
    def value(x: Double): Double = (b0 - a0) * x + x * (x - 1) * (db - da) / 2

    var left = 0.0
    var right = p.toDouble
    var pl = -1.0
    var pr = -1.0

    for (_ <- 0 to 200) {
      pl = left
      pr = right
      var w = (right - left) / 3
      if (value(left + w) < value(right - w)) {
        left += w
      } else {
        right -= w
      }
    }

    val x = left.toInt
    var max = 0.0
    for {
      i <- x - 10 to x + 10
      if i >= 0 && i <= p
    } {
      max = math.max(max, value(i))
    }
    //    println(s"max=$max, x=$x, p=$p")
    max.toLong
  }


  if (K == N.toLong * L) {
    println(imos(N))
  } else {
    val ans = (for {
      head <- 0 until N
      if head + K / L < N
    } yield {
      val a0 = ad(head)._1
      val da = ad(head)._2

      val idx = (head + K / L).toInt
      val rest = (K % L).toInt
      val p = L - rest

      val b0 = ad(idx)._1 + rest * ad(idx)._2
      val db = ad(idx)._2

      val sum1 = imos(idx) - imos(head) + (ad(idx)._1 + ad(idx)._1 + (rest - 1) * db) * rest / 2
      val sum2 = calc(a0, da, b0, db, p) + sum1

      if (idx + 1 < N && rest >= 2) {
        val sum3 = sum1 + (b0 + b0 + (p - 1) * db) * p / 2 - (a0 + a0 + (p - 1) * da) * p / 2

        val c0 = ad(idx + 1)._1
        val dc = ad(idx + 1)._2

        val ax = a0 + p * da

        val sum4 = calc(ax, da, c0, dc, rest) + sum3

        //        println(s"$sum2 $sum4")
        math.max(sum2, sum4)
      } else {
        sum2
      }
    }).max
    println(ans)
  }
}

Measured memory_bytes: 253156
#include<bits/stdc++.h>
#define ll long long

using namespace std;

const int N=25000010;
int gA,gB,sA,sB,bA,bB;
ll n,f[N];

void chk(ll&x,ll y){if(x<y)x=y;}

int main(){
//	freopen("d.in","r",stdin);
//	freopen("d.out","w",stdout);
	scanf("%lld%d%d%d%d%d%d",&n,&gA,&sA,&bA,&gB,&sB,&bB);
	f[0]=0;
	for(int i=1;i<=n;++i){
		f[i]=f[i-1]+1;
		if(i>=gA)chk(f[i],f[i-gA]+gB);
		if(i>=sA)chk(f[i],f[i-sA]+sB);
		if(i>=bA)chk(f[i],f[i-bA]+bB);
	}
	n=f[n];
	for(int i=1;i<=n;++i){
		f[i]=f[i-1]+1;
		if(i>=gB)chk(f[i],f[i-gB]+gA);
		if(i>=sB)chk(f[i],f[i-sB]+sA);
		if(i>=bB)chk(f[i],f[i-bB]+bA);
	}
	n=f[n];
	cout<<n<<endl;
	return 0;
}


Measured memory_bytes: 195584
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define F first
#define S second
#define pii pair<int, int>
#define eb emplace_back
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep3(i, l, n) for (int i = l; i < (n); ++i)
#define sz(v) (int)v.size()
#define inf (int)(1e9+7)
template<typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); }





int n;
vector<string> a, b; // |ai| < |bi|
string ga, de;
int ans;

void dfs(int d, string s) {
    if (d > 10) return; // inf loop
    // cout << d << " " << s << endl;
    
    rep(j, n) { // どのaを使うか
        // aを使える限り使う
        string u = s;
        rep(i, sz(u)) {
            rep(k, sz(a[j])) {
                if (i + k >= sz(u)) break;
                if (u[i + k] != a[j][k]) break;
                
                if (k == sz(a[j]) - 1) {
                    // u = uの前 + a->b + uの後(u2)
                    string u2 = u.substr(i + sz(a[j]), sz(u)); // 多めにしとけばええ
                    u = u.substr(0, i);
                    // cout << "u u2 " << u << " " << u2 << endl;
                    u += b[j];
                    u += u2;
                    i += sz(b[j]) - 1;
                    // cout << u << " " << i << endl;
                }
            }
        }
        if (u == de) {
            chmin(ans, d + 1);
        }
        if (s != u && sz(u) < sz(de)) dfs(d + 1, u);
    }
    
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
   
    while (cin >> n && n) {
        a.clear(); b.clear();
        a.resize(n); b.resize(n);
        rep(i, n) cin >> a[i] >> b[i];
        
        cin >> ga >> de;
        if (sz(ga) > sz(de)) { cout << -1 << endl; continue; }
        if (sz(ga) == sz(de) && ga != de) { cout << -1 << endl; continue; }
        
        ans = inf;
        dfs(0, ga);
        if (ans == inf) cout << -1 << endl;
        else cout << ans << endl;
    }
    
}

/*
 sed : 文字列いじるコマンド
 substitute : 代わりに立つ -> 置き換える
 
 input stringのすべてのある文字列を置き換える
 ただし, 左から
 
 intact : contact 触れる -> 触れられてない
 
 γからδへ最小手数で
 
 */



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

int keta;
int d[64];

void cu2(int k, int v)
{
  d[k] = v;
  if( k > 0 ){
    for(int i=k-1; i>=0; i--){
      d[i] = d[i+1] - 1;
      if( d[i] < 0 ) d[i] = 0;
    }
  }
}


int cu()
{
  int i,j;
  j = 0;
  while(1){
    if( j > keta - 1) keta++;
    d[j]++;
    if( d[j]>9 || ( j < keta - 1 && d[j+1]+1 < d[j] ) ){
      d[j] = -1;
      j++;
      continue;
    } else if( j > 0 && d[j-1] != -1 && d[j-1]+1 < d[j] ){
      cu2(j,d[j]);
      return 0;
    }
    for(i=0;i<j;i++){
      if(d[i]==-1) break;
    }
    if(i<j){
      cu2(j,d[j]);
    }
    return 0;
  }
  return 0;
}

int main()
{
  int k;
  cin >> k;
  
  for(int i=0;i<64;i++){
    d[i]= 0;
  }
  keta = 1;
  
  for(int i=0;i<k;i++){
    (void)cu();
  }
  for(int i=0;i<keta;i++){
    char c = '0' + d[keta-1 - i];
    putc(c,stdout);
  }
  putc('\n',stdout);
  return 0;
}




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

using E = System.Linq.Enumerable;

public class Program
{
    public static void Main()
    {
        var nk = Console.ReadLine().Split().Select(x => int.Parse(x)).ToArray();
        var xs = Console.ReadLine().Split().ToArray();

        var n = nk[0];
        var k = nk[1];
        var m = Array.IndexOf(xs, "1");
        var t = (m + k - 2) / (k - 1);
        var s = k - (m - (t - 1) * (k - 1)) % k;
        var r = (n - m - 1 - (s - 1) + k - 2) / (k - 1);
        Console.WriteLine(t + r);
    }
}

Measured memory_bytes: 19680
#include <stdio.h>

int main(){
  int i, j, mini, N, A[1000], n, swap=0;
  
  scanf("%d", &N);
  for(i=0;i<N;i++){
    scanf("%d", &A[i]);
  }
  
  for(i=0;i<N;i++){
    mini = i;
    for(j=i;j<N;j++){
      if(A[j] < A[mini]) mini = j;
    }
    if(mini != i){
    n = A[i];
    A[i] = A[mini];
    A[mini] = n;
    swap++;
    }
  }
  
  for(i=0;i<N;i++){
    if(i>0)printf(" ");
    printf("%d", A[i]);
  }
  printf("\n");
  printf("%d\n", swap);
  
  return 0;
}

Measured memory_bytes: 608
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#define cs ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define rep(i,b)  for(int i=0;i<(int)(b);i++)
#define Rep(i,n) for(int i=1;i<(int)(n);i++)
#define repp(i,a,b)  for(int i=a;i<=(int)(b);i++)
#define Repp(i,a,b)  for(int i=a;i>=(int)(b);i--)
#define bye(x) {cout<<x;return 0;}
typedef long long ll;
typedef long double ld;

using namespace std;
struct s{
    int a,b,c,d;
};
int main(){
    cs
    ll t=0,tt;bool bb;
    int n,m,q,w;cin>>n>>m>>q;
    vector<int> v(n,1);
    vector
 vv(q);
    rep(i,q) cin>>vv[i].a>>vv[i].b>>vv[i].c>>vv[i].d;
    while(1) {
        tt=0;bb=1;
        rep(i,q) if(v[vv[i].b-1]-v[vv[i].a-1]==vv[i].c) tt+=vv[i].d;
        t=max(t,tt);
        w=n-1;
        while(w>=0) {
            if(v[w]<m) {v[w]++;repp(i,w+1,n-1) v[i]=v[w];break;}
            w--;
        }
        if(w==-1) break;
    }

    cout<<t;
}

Measured memory_bytes: 3652
#include<bits/stdc++.h>
        using namespace std;
        using ll = long long;
        int main()
        {
            int n,m,k;
            cin >> n >> m >> k;
            vector<ll> a(n+1),b(m+1);
            for(int i=1;i<=n;i++){
                ll x;
                cin >> x;
                a[i] = a[i-1] + x;
            }
            for(int i=1;i<=m;i++){
                ll x;
                cin >> x;
                b[i] = b[i-1] + x;
            }
            int ans = 0;
            for(int i=0;i<=n;i++){
                ll temp = a[i];
                int index = upper_bound(b.begin(),b.end(),k-a[i]) - b.begin();
                index--;
                if(index >= 0 && b[index] <= (ll)k-a[i])
                  ans = max(ans,i+index);
                 //cout << index << "\n";
            }
            cout << ans << "\n";
        }

Measured memory_bytes: 6408
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define rrepLRE(i, l, r) for (ll i = (l); i >= (r); --i)
#define Sort(v) sort(v.begin(), v.end())
#define rSort(v) sort(v.rbegin(), v.rend())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

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

/* Macros reg. ends here */

const ll INF = 1LL << 50;

static const long long mod = 1000000007;

int main() {
  // ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);

  ll n;
  cin >> n;

  vll hs(n), as(n);
  rep(i, n) cin >> hs[i];
  rep(i, n) cin >> as[i];

  vll BIT(n + 1);
  auto bset = [&](ll i, ll x) {
    BIT[i] = x;
    while(i <= n){
      chmax(BIT[i], x);
      i += i & -i;
    }
  };
  auto bmax = [&](ll i) {
    ll ret = 0;
    while(i > 0){
      chmax(ret, BIT[i]);
      i -= i & -i;
    }
    return ret;
  };

  ll ans = 0;
  rep(i, n) {
    ll mv = (hs[i] == 1) ? 0 : bmax(hs[i] - 1);
    bset(hs[i], mv + as[i]);
    chmax(ans, mv + as[i]);
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 4992
#include <bits/stdc++.h>
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

const int MOD = 1000000007;

int D[50][50];
int counts[50][3];

int DP[50][8];

int main() {
  // ios_base::sync_with_stdio(0);

  int N, C;
  scanf("%d%d", &N, &C);
  REP(i,C)REP(j,C) scanf("%d", &D[i][j]);
  REP(i,N)REP(j,N) {
    int c;
    scanf("%d", &c);
    ++counts[c-1][(i+j+2)%3];
  }

  int result = MOD;

  vector<int> colors(3);
  for (colors[0] = 0; colors[0] < C; ++colors[0])
  for (colors[1] = 0; colors[1] < C; ++colors[1])
  for (colors[2] = 0; colors[2] < C; ++colors[2]) {
    bool failed = false;
    REP(i,3)REP(j,i) if (colors[i] == colors[j]) failed = true;
    if (failed) continue;

    int res = 0;
    REP(c,50)REP(i,3) res += counts[c][i] * D[c][colors[i]];
    result = min(result, res);
  }

  printf("%d\n", result);
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  #define int long
  int n,k,res=0;
  cin>>n>>k;
  vector<int> v(n);
  for(auto& c:v)cin>>c;
  if(n<=k){
    for(auto c:v)if(c>0)res+=c;
    cout<<res;
    return 0;
  }
  for(int take=k;take>=k-take;--take){
    for(int start=n-take;start<=n;++start){
      int cast=k-take;
      vector<int> jewelry;
      int res_t=0;
      for(int i=0;i<take;++i)jewelry.push_back(v[(start+i)%n]);
      sort(jewelry.begin(),jewelry.end());
      for(auto val:jewelry){
        if(val>=0||cast==0)res_t+=val;
        else cast--;
      }
      res=max(res,res_t);
    }
  }
  cout<<res;
  return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<bitset>
#include<string>
#include<cmath>
#include<algorithm>
#include<stack>
#include<list>
#include<queue>
#include<map>
#include<numeric>
using namespace std;
#define rep(i,x) for(int i=0;i<x;i++)
#define re(i,x,y) for(int i=x;i<y;i++)
int INF=1e9;
const long long mod=1e9+7;
typedef long long ll;
int main(){
    map<string,int>m;
    int q;
    cin>>q;
    while(q--){
        int t,y;
        string x;
        cin>>t;
        if(t==0){
            cin>>x>>y;
            m[x]=y;
        }
        if(t==1){
            cin>>x;
            cout<<m[x]<<endl;
        }
    }
    return 0;
}


Measured memory_bytes: 27912
def comb(n,k,p):
    """power_funcを用いて(nCk) mod p を求める"""
    from math import factorial
    if n<0 or k<0 or n<k: return 0
    if n==0 or k==0: return 1
    a=factorial(n) %p
    b=factorial(k) %p
    c=factorial(n-k) %p
    return (a*power_func(b,p-2,p)*power_func(c,p-2,p))%p
def power_func(a,b,p):
    """a^b mod p を求める"""
    if b==0: return 1
    if b%2==0:
        d=power_func(a,b//2,p)
        return d*d %p
    if b%2==1:
        return (a*power_func(a,b-1,p ))%p

n, k = map(int, input().split())
mod = 10**9 + 7
red_num = n - k
blue_num = k
ans = []
for i in range(1, k+1):
    must_red = i - 1
    rest_red = red_num - must_red
    red_space = i + 1
    must_blue = i
    rest_blue = blue_num - must_blue
    blue_space = i
    if rest_red < 0 or rest_blue < 0:
        ans.append(0)
        continue

    red_pattern = comb(rest_red + red_space - 1, red_space - 1, mod)
    blue_pattern = comb(rest_blue + blue_space - 1, blue_space - 1, mod)
    ans.append((red_pattern * blue_pattern) % mod)

for i in range(k):
    print(ans[i])

Measured memory_bytes: 80404
#include <iostream>
#include <stdio.h>
using namespace std;
int main(void){
    // Your code here!
    int N,sum=0,ans=0;
    int d[105];
    scanf("%d",&N);
    for(int i=1;i<=N;i++){
        scanf("%d",&d[i]);
        sum += d[i];
    }
    for(int i=1;i<=N;i++){
        ans += (sum-d[i])*d[i];
    }
    printf("%d",ans/2);
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define rep0(a,b) for(int a=0;a<b;a++)
#define rep1(a,b) for(int a=1;a<=b;a++)
#define repp(a,b,c) for(int a=b;a<c;a++)
#define rev(a,b,c) for(int a=b;a>=c;a--)
#define in insert
#define len length()
#define si size()
#define lb lower_bound
#define ub upper_bound
#define beg begin()
#define end end()
#define vec vector<ll>
#define se set<ll>
#define mults multiset<ll>
#define F first
#define S second
#define pr(a) cout<<a
#define pr2(a,b) cout<<a<<" "<<b
#define get(a) cin>>a
#define nextl cout<<endl
const ll mod = 1000000007;
const ll maxn = 500005;
const ll mm = 1e+18;
ll power(ll x, ll y)
{
    ll res = 1;
    x = x % mod;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % mod;
        y = y>>1;
        x = (x*x) % mod;
    }
    return res;
}
ll ncr(ll n, ll k)
{
    ll C[k+1];
    memset(C, 0, sizeof(C));

    C[0] = 1;

    for (ll i = 1; i <= n; i++)
    {
        for (ll j = min(i, k); j > 0; j--)
            C[j] = (C[j] + C[j-1])%mod;
    }
    return C[k];
}
void solve()
{
    int n;
    get(n);
    double arr[n];
    rep0(i,n) get(arr[i]);
    sort(arr,arr+n);
    double sum=arr[0];
    repp(i,1,n){
        sum=(sum+arr[i])/2;
    }
    pr(sum);
}
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int t=1;
    //cin>>t;
    while(t--){
        solve();
        //nextl;
    }
    return 0;
}


Measured memory_bytes: 256
a = input()
b = input()

length = len(a) - len(b) + 1

ans = []
for i in range(length):
    A = list('?'*i+b+'?'*(length-i-1))
    flag = True
    for j in range(len(a)):
        if a[j] == '?' or a[j] == A[j]:
            pass
        elif a[j] != '?' and A[j] == '?':
            A[j] = a[j]
        else:
            flag = False
    if flag:
        ans.append(''.join(A).replace('?','a'))

if ans:
    ans.sort()
    print(ans[0])
else:
    print('UNRESTORABLE')

Measured memory_bytes: 9024
// template version 1.12
using namespace std;
#include <iostream>
#include <bits/stdc++.h>

// varibable settings
#define infile "../test/sample-1.in"
#define int long long //{{{
const int INF=1e18;
const int MOD=1e9+7; //}}}

// define basic macro {{{
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)((b)-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define each(i,a) for (auto&& i : a)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ub upper_bound
#define lb lower_bound
#define posl(A, x) (lower_bound(all(A), x)-A.begin())
#define posu(A, x) (upper_bound(all(A),x)-A.begin())
template<class T> inline void chmax(T &a, const T &b) { if((a) < (b)) (a) = (b); }
template<class T> inline void chmin(T &a, const T &b) { if((a) > (b)) (a) = (b); }

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;

template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;

#if defined(PCM) || defined(LOCAL)
  #include "lib/dump.hpp"
#else
  #define dump(...) 42
  #define dump_1d(...) 42
  #define dump_2d(...) 42
#endif
//}}}

int solve(){
  int n,l;cin>>n>>l;

  int s=0;
  int r=INF;
  int x;
  rep(i, 0, n){
    s += l+i;
    if (abs(l+i)<r){
      r=abs(l+i);
      x=l+i;
    }
  }
  dump(s);
  dump(r, x);
  s -= x;
  cout << s << endl;

  return 0;
}

signed main() { //{{{
#ifdef INPUT_FROM_FILE
  std::ifstream in(infile);
  std::cin.rdbuf(in.rdbuf());
#endif
  solve();
  return 0;
} //}}}


Measured memory_bytes: 256
n = int(input())
b = list(map(int, input().split()))
a = [0] * n

a[0] = b[0]
a[n - 1] = b[n - 2]
for i in range(n - 2):
    if b[i + 1] >= b[i]:
        a[i + 1] = b[i]
    else:
        a[i + 1] = b[i + 1]

print(sum(a))

Measured memory_bytes: 61700