akhauriyash/Code-Regression huggingface dataset:
//	C++ (Clang3.8.0)
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main(){
	string str;
	
	cin >> str;
	
	if(str[2]==str[3] && str[4]==str[5]){
		cout << "Yes";
	}else {
		cout << "No";
	}
	
	return 0;
}

Measured memory_bytes: 888
n,*L=map(int,open(0).read().split())
g=[[]for _ in range(n)]
for a,b in zip(*[iter(L)]*2):
	g[a-1].append(b-1)
	g[b-1].append(a-1)
b=[-1]*n
b[0]=0
q=__import__("collections").deque([0])
while q:
	c=q.pop()
	for x in g[c]:
		if b[x]<0:b[x]=1-b[c];q.append(x)
lab=[-1]*n
Z=[i for i,x in enumerate(b) if x==0]
O=[i for i,x in enumerate(b) if x]
z=len(Z)
if z<=n//3 or n-z<=n//3:
	(L,H)=(Z,O) if z<=n//3 else (O,Z)
	a,b=1,3
	for i in L:lab[i]=b;b+=3
	for i in H:
		if b<=n:lab[i]=b;b+=3
		else:lab[i]=a;a+=1 if a%3==1 else 2
else:
	a,b,c=1,2,3
	for i in Z:
		if a<=n:lab[i]=a;a+=3
		else:lab[i]=c;c+=3
	for i in O:
		if b<=n:lab[i]=b;b+=3
		else:lab[i]=c;c+=3
print(*lab)




Measured memory_bytes: 78780
from sys import exit

N = int(input())
D = list(map(int, input().split()))

if D[0] != 0:
    print(0)
    exit()

c = {}
for i in D:
    if i in c:
        c[i] += 1
    else:
        c[i] = 1

if c[0] != 1:
    print(0)
    exit()

result = 1
for i in range(1, N + 1):
    if i not in c:
        if max(D) == i - 1:
            break
        else:
            print(0)
            exit()
    result = result * (c[i - 1] ** c[i]) % 998244353
print(result)


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

int main() {
    ll n, k;
    cin >> n >> k;
    vector<int> a(n);
    rep(i, n) cin >> a[i];

    const ll mod = 1e9 + 7;
    ll ans = 0;
    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++){
            if (a[i] == a[j]) continue;
            if (a[i] > a[j]) ans = (ans + (k + 1) * k / 2) % mod;
            else
                ans = (ans + (k - 1) * k / 2) % mod;
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define endl "\n"
using namespace std;
typedef long long int lli;
lli value(lli x)
{
    return ((x*(x+1))/2);
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    lli n,k,M=1e9+7;
    cin>>n>>k;
    lli i,mn,ma,ans=0,cur,add;
    
    for(i=k;i<=n+1;i++)
    {
        mn=i-1;
        mn=value(mn);
        
        ma=n;
        cur=ma-i;
        if(cur<=0) ma=value(ma);
        else       ma=value(ma)-value(cur);
        
        add=ma-mn+1;
        ans=(ans+add)%M;
    }
    cout<<ans;
    return 0;
}

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

using namespace std;

#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define endl '\n'
#define f first
#define s second
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;
#define PI 3.141592653589793

const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fll;

int main(){ _
	long double a, b, h, m; cin >> a >> b >> h >> m;
	long double ang;
	ang = 2 * PI * (h/12 + m/60/12 - m/60);
	long double ans;
	ans = sqrt(a * a + b * b - (2*b*a*cos(ang)));
	printf("%.15Lf \n", ans);

	exit(0);
}


Measured memory_bytes: 4136
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def f(a, b):
    return pow(a, 5) - pow(b, 5)
 
def main():
    x = int(readline())    
    for i  in range(-1000, 1001):
        for j in range(-1000, 1001):
            if i**5 - j**5 == x:
                print(i, j)
                return
if __name__ == '__main__':
    main()


Measured memory_bytes: 64700
#include <bits/stdc++.h>
using namespace std;
int n, q, l, r, ps[101010];
string s;
int main(){
    cin >> n >> q;
    cin >> s;
    for(int i = 1; i < n; i++){
        ps[i] = ps[i - 1];
        if(s[i] == 'C' && s[i - 1] == 'A')
            ps[i]++;
    }
    for(int i = 0; i < q; i++){
        cin >> l >> r;
        l--, r--;
        cout << ps[r] - ps[l] <<endl;
    }

}


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

const int M = int(1e9 + 7);

using vec = vector<int>;
using mat = vector<vec>;
using cub = vector<mat>;

signed main() {
  int N;
  cin >> N;

  mat G(N);
  for (int i = 0; i + 1 < N; ++i) {
    int X, Y;
    cin >> X >> Y;
    G[X - 1].emplace_back(Y - 1);
    G[Y - 1].emplace_back(X - 1);
  }

  vec g(N + 1);
  g[0] = 1;
  for (int i = 2; i < g.size(); i += 2) g[i] = 1LL * g[i - 2] * (i - 1) % M;

  cub dp(N, mat(2));
  function<void(int, int)> dfs = [&](int u, int fa) {
    dp[u] = {{0, 1}, {0, 0}};
    for (int v : G[u]) {
      if (v == fa) continue;
      dfs(v, u);
      mat w(2, vec(dp[u][0].size() + dp[v][0].size() - 1));
      for (int f = 0; f < 2; ++f)
        for (int s = 1; s < dp[u][0].size(); ++s)
          for (int h = 0; h < 2; ++h)
            for (int t = 1; t < dp[v][0].size(); ++t) {
              (w[f ^ h ^ 1][s] += 1LL * dp[u][f][s] * g[t] % M * dp[v][h][t] % M) %= M;
              (w[f ^ h][s + t] += 1LL * dp[u][f][s] * dp[v][h][t] % M) %= M;
            }
      swap(w, dp[u]);
    }
  };

  dfs(0, -1);

  int ans = 0;
  for (int i = 0; i < 2; ++i)
    for (int j = 1; j <= N; ++j)
      (ans += (i == 0 ? 1LL : -1LL) * dp[0][i][j] * g[j] % M) %= M;

  if (ans < 0) ans += M;
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 70784
#include <iostream>
#include <list>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;
typedef long long lli;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<vector<lli> > mat;
typedef vector<vector<bool> > matb;
typedef vector<string> vst;
typedef pair<lli,lli> pll;
typedef pair<double,double> pdd;

lli n;
lli min_r = 10000000000;
lli ans = -1000000000;

int main(){
    cin >> n;
    for(lli i = 0;i < n;i++){
        lli r;
        cin >> r;
        ans = max(ans,r-min_r);
        min_r = min(min_r,r);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3108
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
  int n,ans=0;
  cin>>n;
  for(int i=1;i<100000000;i++)
  {
    if(i*i<=n) ans=max(ans,i*i);
    else break;
  }
  cout<<ans;
}

Measured memory_bytes: 3584
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define gc() fgetc(stdin)
ll read() {
	ll a = 0; int b = 1, c;
	while (((c=gc()) > '9' || c <'0') && c != '-');
	if (c=='-') b=-1,c=gc();
	while (c >= '0' && c <= '9') a=a*10+c-'0',c=gc();
	return a*b;
}
#define gs(s) scanf("%s",s)
#define gi(i) i=read()
#define gl(i) i=read()
#define pc(c) putchar(c)
#define ps(s) printf("%s",s)
#define pi(i) printf("%d",i)
#define pl(i) printf("%lld",i)

int n, X[200005], V[200005];

int main() {
	gi(n);
	for (int i = 0; i < n; i++) gi(X[i]);
	for (int i = 0; i < n; i++) V[i] = X[i];
	sort(V, V+n);
	for (int i = 0; i < n; i++) {
		if (X[i] < V[n/2]) {
			pi(V[n/2]);
		} else {
			pi(V[n/2-1]);
		}
		pc('\n');
	}
	return 0;
}


Measured memory_bytes: 3712
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
const int N = 305; 
using namespace std;

int n, K, mod, c[N][N], f[N][N], s[N][N]; 

template < typename T >
inline T read()
{
	T x = 0, w = 1; char c = getchar();
	while(c < '0' || c > '9') { if(c == '-') w = -1; c = getchar(); }
	while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return x * w; 
}

int main()
{
	n = read <int> (), K = read <int> (), mod = read <int> (); 
	for(int i = 0; i <= n; i++)
		for(int j = 0; j <= i; j++)
			c[i][j] = (!j ? 1 : (c[i - 1][j - 1] + c[i - 1][j]) % mod); 
	for(int i = 0; i <= K; i++)
	{
		f[1][i] = 1;
		for(int j = i - 1; j >= 0; j--)
			s[1][j] = (s[1][j] + 1) % mod; 
	}
	for(int i = 2; i <= n + 1; i++)
		for(int j = 0; j <= K; j++)
		{
			for(int l = 1; l < i; l++)
				f[i][j] = (f[i][j] + 1ll * f[l][j] * s[i - l][j] % mod * c[i - 2][l - 1] % mod) % mod; 
			for(int l = j - 1; l >= 0; l--)
				s[i][l] = (s[i][l] + f[i][j]) % mod; 
		}
/*	for(int i = 1; i <= n + 1; i++)
		for(int j = 0; j <= K; j++)
			printf("%d%c", f[i][j], j == K ? '\n' : ' '); */
	printf("%d\n", f[n + 1][0]); 
	return 0; 
}


Measured memory_bytes: 1280
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define int long long
#define inf (int)1e18+7
const int mod = (int)1e9+7;

string k; // 999999
int D;

int dp[10010][2][110];

int solve(int id, bool isUp, int sum) {

  if (id == k.size()) {
    if (sum % D == 0) {
      return 1;
    }
    return 0;
  }

  int lim = 9;
  if (isUp) {
    lim = k[id] - '0';
  }

  int &res = dp[id][isUp][sum];
  if (res != -1) return res;
  res = 0;
  for (int i = 0; i <= lim; i++) {
    bool tmp = false;
    if (isUp == true && i == lim) {
      tmp = true;
    }
    res += solve(id + 1, tmp, (sum + i) % D);
    res %= mod;
  }
  return res;
};

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

  cin >> k >> D;

  memset(dp, -1, sizeof dp);
  int res = solve(0, 1, 0) - 1;
  if (res < 0) {
    res += mod;
    res %= mod;
  }
  cout << res << endl;

  return 0;
}

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


double distance(double x1 , double y1,double x2 , double y2){

    double d = ((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1));
    return sqrt(d);
}


int main(){

int n;
double arr[15][3];
int per[10];
cin >> n;

for(int i=0;i<n;i++)
    per[i] = i+1;

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

   double sum = 0;
do{
    double tSum = 0;
     for(int i=0;i<n-1;i++){

           double x1 = arr[per[i]][0];
           double y1 = arr[per[i]][1];
           double x2 = arr[per[i+1]][0];
           double y2 = arr[per[i+1]][1];


        tSum = tSum + distance(x1,y1,x2,y2);
     }
     sum+=tSum;

}
while(next_permutation(per,per+n));

int res = 1;

for(int i=1;i<=n;i++){
    res = res * i;
}

cout << fixed << setprecision(10) << sum/res << endl;

return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <cctype>
#include <cmath>
#include <queue>
#include <map>
#include <numeric>
#include <unordered_map>
#include <iomanip>
#include <functional>
#include <bitset>
#include <complex>
#include <stack>
#include <cstdint>



#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define rrep(i, n) for(ll i = (ll)(n-1); i >= 0; i--)
#define repi(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rrepi(i,a,b) for(ll i=(ll)(b-1);i>=(ll)(a);i--)

#define all(x) (x).begin(),(x).end()

template<class T>inline bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>inline bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

typedef long long ll;

using namespace std;


int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  ll n,m;cin>>n>>m;
  if(n<m){
    rep(i,m)cout<<n;
  }
  else{
    rep(i,n)cout<<m;
  }
  cout<<endl;

  return 0;

}


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

int main() {
	int N;
	cin >> N;
	vector<int>A(N); 
	double tmp = 0;
	for (int n = 0;n<N;++n) {
		cin >> A[n];
		tmp += 1.0 / A[n];
	}
	printf("%.10f\n", 1/tmp);
	
	return 0;
}

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

using namespace std;
using lint = std::int_fast64_t;

vector<lint> P, BP;

lint f(lint n, lint x) {
  if (n == 0) {
    return x <= 0 ? 0 : 1;
  } else if (x <= BP[n - 1] + 1) {
    return f(n - 1, x - 1);
  } else {
    return P[n - 1] + f(n - 1, x - (BP[n - 1] + 2)) + 1;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  lint N, X;
  cin >> N >> X;
  P.resize(N + 1), BP.resize(N + 1);
  P[0] = 1, BP[0] = 1;
  for (int i = 1; i <= N; i++) {
    P[i] = 2 * P[i - 1] + 1;
    BP[i] = 2 * BP[i - 1] + 3;
  }
  cout << f(N, X) << '\n';
  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++)
#define all(x) (x).begin(), (x).end()

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

    // start
    int a, p;
    cin >> a >> p;
    p += a * 3;
    cout << p / 2 << endl;
    // end

    return 0;
}


Measured memory_bytes: 256
s,x,y,h,w,k,n;main(i){for(;scanf("%d",&n),s=400*n;printf("%d\n",s))for(;n--;s+=k*200)for(k=2-scanf("%d%d%d%d",&x,&y,&h,&w),i=6;x+y+h<i*20+41&w<"(*-27<A"[i]-39;)k=i--;}

Measured memory_bytes: 0
n = int(input())
a = [int(input()) for x in range(n)]
b = sorted(a[::])
a_1, a_2 = b[-1], b[-2]
c = a.count(a_1)

for i in a:
  if c == 1 and i == a_1:
    print(a_2)
  else:
    print(a_1)
  

Measured memory_bytes: 14940
#include <iostream>

using namespace std;

int main(void) {
	string S;
	cin >> S;
	bool hei = true;
	if (S.substr(0,4) >= "2019") {
		if (S.substr(0,4) == "2019") {
			if (S.substr(5,2) > "04") {
				hei = false;
			}
		}
		else {
			hei = false;
		}
	}
	if (hei) {
		cout << "Heisei" << endl;
	}
	else {
		cout << "TBD" << endl;
	}

	return 0;
}


Measured memory_bytes: 256
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::iter::FromIterator;

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());

    let a = scan.read::<i64>();
    let b = scan.read::<i64>();
    let ret = if a < 10 && b < 10 {a * b} else {-1};
    println!("{}", ret);
}

// region template
#[allow(dead_code)]
fn iu(i: i64) -> usize { i as usize }

#[allow(dead_code)]
fn ui(i: usize) -> i64 { i as i64 }

#[allow(dead_code, deprecated)]
fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
    let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
    strings.connect(sep)
}

#[allow(dead_code)]
fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T> where S: FnMut(usize) -> T + 'a {
    (0..n).map(|i| f(i)).collect::<Vec<T>>()
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion

Measured memory_bytes: 4352
import numpy as np
import sys
sys.setrecursionlimit(100000)
N,M=map(int,input().split())
d=[[] for i in range(N)]
for i in range(M):
    x,y=map(int,input().split())
    d[x-1].append(y-1)
dp=[0 for i in range(N)]

def depth(now,score,d):
    global dp
    if dp[now]==0 or not(dp[now]):
        if d[now]:
            _score=[]
            for i in d[now]:
                _score.append(depth(i,score+1,d)-score)
            dp[now]=max(_score)
        else:
            return score

        return dp[now]+score

    else:
        return dp[now]+score



Max=0
for i in range(N):
    m=depth(i,0,d)
    if  m>Max:            
        Max=m
print(Max)



Measured memory_bytes: 75220
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <math.h>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
 
// #include <bits/stdc++.h>
// #include <epxt/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef pair<LL,LL> pii;
// tree <pii,null_tpype,greater<pii>,rb_tree_tag,tree_order_statistics_node_update> tr;
// cin.tie(0);
// std::ios::spync_with_stdio(false);
typedef long long LL;
const int N = 200005;
const int M = 205;
// const int V = 207;
// const int E = 60007;
const int INF = 0x3f3f3f3f;
const double inf = 2e9;
const double Pi = acos(-1);
const int MOD = 1e9+7;
const double eps=1e-13;
int n,m;
int k;
int vis[N];
int s[N];
bool cmp(int a,int b){return a>b;}
int main()
{
    while (~scanf("%d%d",&n,&k))
    {
        for (int i=0;i<n;i++)
        {
            int x;
            scanf("%d",&x);
            vis[x]++;
        }
        sort(vis,vis+200000,cmp);
        int ans = 0;
        for (int i=k;i<=200000;i++)
            ans += vis[i];
        printf("%d\n", ans);
    }
    return 0;
}


/*



*/

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

using namespace std;

typedef long long ll;

const int MAXN = 1e5 + 10;

ll sm = 0;
ll n, a[MAXN];

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++){
		cin >> a[i];
		sm += a[i];
	}
	ll tt = 1ll*n*(n+1)/ 2;
	if (sm % tt){
		cout << "NO\n";
		return 0;
	}
	ll x = sm/tt;

	ll ta = 0, tb = 0;
	for (int i = 0; i < n; i++){
		ll y = a[i==n-1?0:i+1] - a[i];
		ll temp = y + x*(n-1);
		ll f = temp/n;
		ll g = x-f;

		if (min(f, g) < 0 || max(f, g) > x){
			cout << "NO\n";
			return 0;
		}
		ta += f;
		tb += g;
	}
	if (tb == x && ta == x*(n-1))
		cout << "YES\n";
	else
		cout << "NO\n";
	return 0;
}


Measured memory_bytes: 1152
#include <iostream>
#include <algorithm>
using namespace std;
int main(void){
    // Your code here!
    
    int H, W, N;
    
    cin >> H >> W >> N;
    
    int rem = 0;
    int s = 0;
    if (H >= W)
    {
        s = H;
    }else{
        s = W;
    }
    
    for (int i = 0; i < W * H; ++i)
    {
        if(N <= s * i) 
        {
            rem = i;
            break;        
        }
    }
    
    cout << rem << endl;

}


Measured memory_bytes: 256
#include <algorithm>
#include <string>
#include <iostream>
#include <vector>

#define MAX 200000

typedef long long ll;

using namespace std;
int N;

//提出時には消す
#include <fstream>

ll merge_sort(vector<int> &data){
    int n = data.size();
    if(n <= 1){
        return 0;
    }

    ll count = 0;
    vector<int> left(data.begin(), data.begin() + n / 2);
    vector<int> right(data.begin() + n / 2, data.end());

    count += merge_sort(left);
    count += merge_sort(right);

    int data_i = 0, left_i = 0, right_i = 0;
    while (data_i < n)
    {
        if( left_i < left.size() && (right_i == right.size() || left[left_i] <= right[right_i]) ){
            data[data_i++] = left[left_i++];
        }else{
            count += n / 2 - left_i;
            data[data_i++] = right[right_i++];
        }
    }
    return count;    
}

int main()
{

    cin.tie(0);

    int n;
    cin >> N;
    vector<int> data;

    for (int i = 0; i < N; i++)
    {
        cin >> n;
        data.emplace_back(n);
    }

    ll count = merge_sort(data);
    cout << count << "\n";
    // for (int i = 0; i < N; i++)
    // {
    //     cout << sequence[i];
    // }

    return 0;
}


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

int main() {
  string S;
  
  cin >> S;
  
  if (S == "Sunny"){
    cout << "Cloudy" << endl;
  }else if(S == "Cloudy"){
    cout << "Rainy" << endl;
  }else{
    cout << "Sunny"<< endl;
  }
  
  return 0;
}

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

int main(){
 int d[6],q,a,b,tmp,j;

 for(int i=0;i<6;i++){
  cin >> d[i] ;
 }

 cin >> q ;

 for(int i=0;i<q;i++){
  j=0;
  cin >> a >> b ;
  while(1){
   if(d[1]==b)break;
   tmp=d[0];
   d[0]=d[1];
   d[1]=d[5];
   d[5]=d[4];
   d[4]=tmp;
   if(j==4){
    tmp=d[0];
    d[0]=d[3];
    d[3]=d[5];
    d[5]=d[2];
    d[2]=tmp;
   }
  j++;
  }
  while(1){
   if(d[0]==a)break;
   tmp=d[0];
   d[0]=d[2];
   d[2]=d[5];
   d[5]=d[3];
   d[3]=tmp;
  }

  cout << d[2] << endl ;
 }
 return 0;
}

 

Measured memory_bytes: 3044
class UnionFind

  def initialize(n)
    @parents = Array.new(n, -1) # if negative value means size
  end

  def unite(a, b)
    a = root(a)
    b = root(b)

    return false if a == b

    a, b = b, a if -@parents[a] < -@parents[b]
    @parents[a] += @parents[b] # if negative value means size
    @parents[b] = a
    true
  end

  def root(a)
    @parents[a] < 0 ? a : (@parents[a] = root(@parents[a]))
  end

  def same?(a, b)
    root(a) == root(b)
  end

  def size(a)
    -@parents[root(a)]
  end
end

n, m = gets.to_s.split.map{|t|t.to_i}
bridges = Array.new(m){ gets.to_s.split.map{ |t| t.to_i - 1 } }

bridges.reverse!
uft = UnionFind.new(n)

ans = [n * (n-1) / 2]
bridges.each do |a, b|
  s = uft.size(a)
  t = uft.size(b)
  ans.unshift (ans[0] - (uft.unite(a, b) ? s * t : 0))
end

puts ans.drop(1)

Measured memory_bytes: 12488
import sys
import collections


def solve():
    input = sys.stdin.readline
    mod = 10 ** 9 + 7
    n = int(input().rstrip('\n'))
    s = list(str(input().rstrip('\n')))
    s1 = collections.deque(s)
    s2 = collections.deque(s)
    while True:
        ln = len(s1)
        while True:
            s1 = collections.deque(list("".join(s1).replace("()", "")))
            if len(s1) == ln:
                break
            else:
                ln = len(s1)
        if len(s1) == 0:
            break
        elif s1[0] == "(":
            s1.append(")")
            s2.append(")")
        else:
            s1.appendleft("(")
            s2.appendleft("(")
    print("".join(s2))


if __name__ == '__main__':
    solve()


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

int main(){
	string s;int a[10];
	while(cin >> s){
		for(int i = 0;i < 10;i++)a[i] = s[i]-'0';
		for(int i = 9;i > 0;i--)
			for(int j = 0;j < i;j++)
				a[j] = (a[j] + a[j+1]) % 10;
		cout << a[0] << endl;
	}
	return 0;
}

Measured memory_bytes: 3012
#include <iostream>
#include <string>
using namespace std;
int main()
{
    
    int a,b;
    cin >> a >> b;
    
    if(a+b<=a-b&&a*b<=a-b)
    {
        cout<<a-b<<endl;
    }
    
    else if(a-b<=a+b&&a*b<=a+b)
    {
        cout<<a+b<<endl;
    }
    
    else
    {
        cout<<a*b<<endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
int n, h; cin >> n >> h;
vector<int> a(n), b(n); 
for (int i = 0; i < n; ++i) cin >> a[i] >> b[i];
sort(begin(a), end(a));
sort(begin(b), end(b));
int c = 0;
while (h > 0) h -= max(a[n-1], c>n-1 ? 0 : b[n-1-c]), c++;
cout << c << endl;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define debug(x) cerr<<#x<<'='<<x<<' '
#define f first
#define s second
typedef long long ll;
typedef pair<int,int> pi;
const int INF = 1e9+7;

char grid[25][25];
int dist[25][25];
int dx[4] = {0,0,-1,1}, dy[4] = {1,-1,0,0};

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int h,w; cin >> h >> w;
    queue<pi> q, source;
    for (int i = 0; i < h; ++i) {
		for (int j = 0; j < w; ++j) {
			cin >> grid[j][i];
			if (grid[j][i]=='.') {source.emplace(j,i);}
		}
	}
	int ans = 0;
	while (source.size()) {
		memset(dist,-1,sizeof(dist));
		dist[source.front().f][source.front().s] = 0;
		q.emplace(source.front().f,source.front().s);
		source.pop();
		while (q.size()) {
			int cx = q.front().f;
			int cy = q.front().s;
			q.pop();
			for (int i = 0; i < 4; ++i) {
				int nx = cx+dx[i];
				int ny = cy+dy[i];
				if (nx<0||nx>=w||ny<0||ny>=h) continue;
				if (grid[nx][ny]=='#') continue;
				if (~dist[nx][ny]) continue;
				dist[nx][ny] = dist[cx][cy]+1;
				q.emplace(nx,ny);
			}
		}
		for (int i = 0; i < h; ++i) {
			for (int j = 0; j < w; ++j) {
				if (dist[j][i] != INF) ans = max(ans,dist[j][i]);
			}
		}
	}	
	cout << ans;
    return 0;
}


Measured memory_bytes: 256
#include<string>
#include<iostream>
using namespace std;
int main(void)
{
    int n;
  	string s;
  	ios::sync_with_stdio(false);
  	cin.tie(NULL);
  	cout.tie(NULL);
  	cin >> n >> s;
  	if(n%2)
    {
      	cout << "No";
      	return 0;
    }
  	else
    {
      	for(int i=0;i<n/2;i++)
        {
          	if(s[i]!=s[i+n/2])
            {	
              	cout << "No";
              	return 0;
            }
        }
    }
  	cout << "Yes";
  	return 0;
}

Measured memory_bytes: 256
def divisor(n): 
    i = 1
    table = []
    while i * i <= n:
        if n%i == 0:
            table.append(i)
            table.append(n//i)
        i += 1
    table = list(set(table))
    return table

if __name__ == '__main__':
	
	n = int(input())

	#n = a * b のためnの約数を求める
	A = divisor(n)

	B = []
	c_tmp = 11
	
	for i in A:
		tmp = n // i
		#tmp i の桁数をチェック
		c1 = len(str(tmp))
		c2 = len(str(i))
		c2_tmp = max(c1,c2)
		c_tmp = min(c2_tmp,c_tmp)

	print(c_tmp)




Measured memory_bytes: 9100
#include <iostream>
#include <cstdio>

using namespace std;

typedef long long ll;

ll n, m;
ll a[100005];
ll b[300005];
ll now;
ll ans;

int main()
{
    scanf("%lld%lld", &n, &m);
    for(ll i = 0; i < n; i++)
    {
        scanf("%lld", &a[i]);
    }
    for(ll i = 0; i < n - 1; i++)
    {
        ll t1 = a[i];
        ll t2 = a[i + 1];
        if(t1 == t2) continue;
        if(t1 > t2) t2 += m;
        b[t1 + 2]--, b[t2 + 1]++;
        b[t2 + 1] += (t2 - t1 - 1);
        b[t2 + 2] -= (t2 - t1 - 1);
        t1 += m;
        t2 += m;
        b[t1 + 2]--, b[t2 + 1]++;
        b[t2 + 1] += (t2 - t1 - 1);
        b[t2 + 2] -= (t2 - t1 - 1);
    }
    for(ll i = 1; i <= 3 * m; i++)
    {
        b[i] += b[i - 1];
    }
    for(ll i = 0; i < n - 1; i++)
    {
        ll t1 = a[i + 1] - a[i];
        if(t1 < 0) t1 += m;
        if(a[i] > a[i + 1]) t1 = a[i + 1];
        now += t1;
    }
    ans = now;
    for(ll i = 2; i <= m; i++)
    {
        now += b[i + m];
        ans = min(ans, now);
    }
    printf("%lld\n", ans);
    return 0;
}


Measured memory_bytes: 3328
K = int(input())

if K == 7:
    print(1)
elif K % 2 == 0 or K % 5 == 0:
    print(-1)
else:
    count = 0
    num = 0
    while True:
        num = (num * 10 + 7) % K
        count += 1
        if num == 0:
            print(count)
            break

Measured memory_bytes: 9060
A,B,X = map(int,input().split())
from math import atan2, degrees, radians, tan

b = degrees(atan2(B,A))

def is_ok(d):
    if d > b:
        h = B * tan(radians(90-d))
        return X <= A*B*h/2
    else:
        h = B - A * tan(radians(d))
        return X <= A*A*(h+B)/2
ok = 0
ng = 90
for _ in range(100):
    m = (ok+ng)/2
    if is_ok(m):
        ok = m
    else:
        ng = m
print(ok)

Measured memory_bytes: 9508
#include <bits/stdc++.h>
using namespace std;
int pre[104];
int main()
{
       int a,b,c,x,t=0;
       cin>>a>>b>>c>>x;
       for(int i=0;i<=a;i++)
        for(int j=0;j<=b;j++)
        for(int k=0;k<=c;k++)
        if(i*500+j*100+k*50==x)
        t++;
       cout<<t;
    return 0;
   }

Measured memory_bytes: 3632
S = input()

if S == "RRR":
  print("3")
if S == "SRR":
  print("2")
if S == "RRS":
  print("2")
if S == "SSS":
  print("0")
if S == "SSR":
  print("1")
if S == "SRS":
  print("1")
if S == "RSS":
  print("1")
if S == "RSR":
  print("1")

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

int main() {
    int N,M;
    scanf("%d%d", &N,&M);
    int a[M],b[M];
    for (int i=0;i<M;i++){
        scanf("%d%d",&a[i],&b[i]);
    }
    pair<int,int> itv[M];
    for (int i=0;i<M;i++){
        itv[i].first=a[i];
        itv[i].second=b[i];
    }
    sort(itv,itv+M);
    int res=0, e=itv[0].second;
    for (int i=1;i<M;i++){
        if (itv[i].first<e && i!=M-1){
            e=min(e,itv[i].second);
        }
        else{
        res++;
        if (itv[i].first>=e && i==M-1) res++;
        e=itv[i].second;
        }
    }
    if(M==1) res=1;
    printf("%d\n", res);
}

Measured memory_bytes: 1792
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <set>
#include <map>
#include <iomanip>
#include <stack>
#include <queue>
#include <deque>

using namespace std;

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

#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define pb push_back
#define ppb pop_back
#define mkp make_pair
#define F first
#define S second
#define show(a) cerr << #a <<" -> "<< a <<"\n"
#define fo(a, b, c ,d) for(int a = b; a <= c; a += d)
//#define int ll

const int N = 4e5;

string s;
ll ans = 1;
int cnt[N];

int main () {
	ios_base::sync_with_stdio(0);
	cin >> s;
	for(int i = 0; i < sz(s); ++i) {
		ans += i;
		ans -= cnt[(s[i] - 'a')]++;
	}
	cout << ans;
	return 0;
}

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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

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

#define DEBUG(x) cout<<#x<<": "<<(x)<<'\n'
#define DEBUG_VEC(v) cout<<#v<<":";REP(i, v.size())cout<<' '<<v[i];cout<<'\n'
#define ALL(a) (a).begin(), (a).end()

template<typename T> inline void CHMAX(T& a, const T b) {if(a<b) a=b;}
template<typename T> inline void CHMIN(T& a, const T b) {if(a>b) a=b;}

constexpr ll MOD=1000000007ll;
// constexpr ll MOD=998244353ll;
#define FIX(a) ((a)%MOD+MOD)%MOD

const double EPS=1e-9;
#define EQ0(x) (abs((x))<EPS)
#define EQ(a, b) (abs((a)-(b))<EPS)

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  // cout<<setprecision(10)<<fixed;
  string s;
  cin>>s;
  bool ok=true;
  int n=s.size();
  REP(i, n/2){
    if(s[i]!=s[n-1-i]){
      ok=false;
      break;
    }
  }
  REP(i, (n-1)/4){
    if(s[i]!=s[(n-1)/2-1-i]){
      ok=false;
      break;
    }
  }
  FOR(i, (n+3)/2-1, (n-1-((n+3)/2)+1)/2){
    if(s[i]!=s[n-1-i]){
      ok=false;
      break;
    }
  }
  cout<<(ok ? "Yes" : "No")<<'\n';
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n;
  cin>>n;
  int m=0;
  int s=0;
  for(int i=0;i<n;i++){
    int l;
    cin>>l;
    m=max(m,l);
    s+=l;
  }
  cout<<(m<s-m?"Yes":"No")<<endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
using ll = long long;
using vin=vector<int>;
using vll=vector<ll>;
using P = pair<int, int>;
const int inf=1e9+7;
const ll INF=1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
int dx[4]={1,0,0,-1};
int dy[4]={0,1,-1,0};
int main() {
//最短経路を求める問題
  int h,w;
  cin>>h>>w;
  vector<string> s(h);
    rep(i,0,h) cin>>s[i];
  ////////
  int dist[h][w];
  int white=0;
  rep(i,0,h){
    rep(j,0,w) {
      if(s[i][j]=='.') white++; 
      dist[i][j]=-1;
    }
  }
 
  queue<P> bfs;
  dist[0][0]=1;
  bfs.push(mp(0,0));
  while(!bfs.empty()){
    P now=bfs.front();
    bfs.pop();
    int a=now.first;
    int b=now.second;
    rep(i,0,4){
      int na=a+dx[i];
      int nb=b+dy[i];
      if(na<0||nb<0||na>=h||nb>=w||s[na][nb]=='#')continue;
      if(dist[na][nb]!=-1)continue;
      bfs.push(mp(na,nb));
      dist[na][nb]=dist[a][b]+1;
      //cout<<dist[na][nb]<<endl;
    }
  }
  if(dist[h-1][w-1]!=-1){
  white-=dist[h-1][w-1];
 // cout<<dist[0][1]<<endl;
  cout<<white<<endl;
  }
  else cout<<-1<<endl;
}

Measured memory_bytes: 3640
#include <iostream>
using namespace std;
int main()
{
    long long n, a[10000], i, ans=0;
    cin >> n;
    for(i=0;i<n;i++){
        cin >> a[i];
    }
    for(i=0;i<n;i++){
        ans=ans+a[i]-1;
    }
    cout << ans <<endl;
    return 0;
}

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

int dp1[1000001], dp2[1000001];
int solve(vector<int> const& v, int n, int dp[]) {
  for (int i = 0; i <= n; i++)
    dp[i] = i;
  for (int i = 0; i < v.size(); i++) {
    for (int j = v[i]; j <= n; j++) {
      dp[j] = min(dp[j], dp[j-v[i]] + 1);
    }
  }
  return dp[n];
}

int main(void) {
  vector<int> tetra, tetra_odd;

  for (int i = 1; ; i++) {
    int t = (i*(i+1)*(i+2) / 6);
    if (t > 1000000)
      break;
    tetra.push_back(t);
    if (t % 2 == 1)
      tetra_odd.push_back(t);
  }

  solve(tetra, 1000000, dp1);
  solve(tetra_odd, 1000000, dp2);

  int n;
  while (cin >> n, n) {
    cout << dp1[n] << ' ' << dp2[n] << endl;
  }

  return 0;
}


Measured memory_bytes: 10952
//#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4.1,sse4.2,avx,avx2,popcnt,tune=native")
//
//#include <immintrin.h>
//#include <emmintrin.h>

#include <bits/stdc++.h>
//#pragma GCC optimize("O2")
#define vi vector<int>
#define pii pair<int, int >
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define LL long long
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=n;i>=a;i--)
#define all(x) (x).begin(), (x).end()
#define all2(x,n) (x+1), (x+1+n)
#define sz(x) ((int)(x).size())
#define mod(x) ((x)%MOD)
#define debug(x) cerr<<#x<<" : "<<x<<endl
#define mt make_tuple
#define eb emplace_back
#define o(X) (1<<(X))
#define oL(X) (1LL<<(X))
#define contain(S,X) (((S)&o(X))!=0)
#define containL(S,X) (((S)&oL(X))!=0)
using namespace std;
const int INF=0x3f3f3f3f,N=1e6+5,MOD=998244353;
const LL INF_LL=0x3f3f3f3f3f3f3f3fLL;
inline int getplc(int x,int y) { return (x>>y)&1; }
template<typename T>
T square(T x) {return x*x;}
LL qpow(LL a,LL b=MOD-2,LL _MOD=MOD){
	LL res=1;
	for(;b;b>>=1,a=a*a%_MOD){
		if(b&1)res=res*a%_MOD;
	}
	return res;
}
// Smax
//int Smax() { return -INF; }
template <typename T>
T Smax(T x) { return x; }
template<typename T, typename... Args>
T Smax(T a, Args... args) { return max(a, Smax(args...)); }
// Smin
template <typename T>
T Smin(T x) { return x; }
template<typename T, typename... Args>
T Smin(T a, Args... args) { return min(a, Smin(args...)); }
template <typename T>
// erro
#define errorl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); errl(_it, args); }

void errl(istream_iterator<string> it) {}
template<typename T, typename... Args>
void errl(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << " = " << a << endl;
	errl(++it, args...);
}

#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cerr<<endl;}
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
	cerr << *it << "=" << a << " # ";
	err(++it, args...);
}
void Solve();
int main() {
#ifndef ONLINE_JUDGE
//	freopen("in.txt","r",stdin);
//    freopen("o1.txt","w",stdout);
#endif
	ios::sync_with_stdio(false);cin.tie(0),cout.tie(0);
	Solve();
	return 0;
}

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

char s[15][15];
void Solve(){
  int n,m,k;
  cin>>n>>m>>k;
  rep(i,1,n){
    cin>>s[i]+1;
  }
  int ans=0;
  rep(i,0,o(n)-1){
    rep(j,0,o(m)-1){
      int tmp=0;
      rep(x,1,n){
        rep(y,1,m){
          if(!getplc(i,x-1)&&!getplc(j,y-1)){
            tmp+=(s[x][y]=='#');
          }
        }
      }
      if(tmp==k){
        ans++;
      }
    }
  }
  cout<<ans<<endl;
}

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

class UnionFind {
  vector<int> parent;
  
public:
  void init(int n) {
    for (int i = 0; i < n; i++) parent.push_back(i);
  }
  
  void merge(int m, int n) {
    parent.at(find(m)) = find(n);
  }
  
  int find(int n) {
    if (parent.at(n) == n) return n;
    return parent.at(n) = find(parent.at(n));
  }
};

int main() {
  int n, k, l;
  cin >> n >> k >> l;
  UnionFind road, rail;
  road.init(n);
  rail.init(n);
  for (int i = 0; i < k; i++) {
    int p, q;
    cin >> p >> q;
    road.merge(p - 1, q - 1);
  }
  for (int i = 0; i < l; i++) {
    int r, s;
    cin >> r >> s;
    rail.merge(r - 1, s - 1);
  }
  map<pair<int, int>, int> connection;
  for (int i = 0; i < n; i++) {
    connection[make_pair(road.find(i), rail.find(i))]++;
  }
  for (int i = 0; i < n; i++) {
    cout << connection[make_pair(road.find(i), rail.find(i))];
    if (i == n - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}


Measured memory_bytes: 13556
n,*as=$<.read.split.map &:to_i
s=t=1
mins,maxs=[],[]
min=max=0
n.downto(0){|i|
  e=as[i]
  mins[i]=min=(min+1)/2+e
  maxs[i]=max+=e
}
if min>1||max<1
  puts -1
  exit
end
ans=t=1
1.upto(n){|i|
  ans+=t=[(t-as[i-1])*2,maxs[i]].min
}
puts ans

Measured memory_bytes: 22944
def solve():
    H,N = map(int, input().split())
    # Aが消費HP, Bが消費MP
    ABs = [list(map(int, input().split())) for _ in range(N)]
    # dp_table = [[10**9]*(H+1)]+[[0]*(H+1) for _ in range(N)]
    dp_table = [[0]*(H+1) for _ in range(N+1)]
    # dp_table[i][j] : i番目の魔法から消化HPがj以上となる、MPの最小値
    for j in range(1,H+1): dp_table[0][j] = 10**9
    # print(dp_table)
    for i in range(N):
        for j in range(H+1):
            if j-ABs[i][0]<0:
                dp_table[i+1][j] = min(dp_table[i][j], ABs[i][1])
            else:
                dp_table[i+1][j] = min(dp_table[i][j], dp_table[i+1][j-ABs[i][0]]+ABs[i][1])
                
    print(dp_table[N][H])
    
solve()

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

using namespace std;

int ctoi(char c) {
    return c - '0';
}

int main() {
    string n;
    int k;
    cin >> n >> k;
    vector<vector<int>> dp0(n.size() + 1, vector<int>(k + 1, 0));
    vector<vector<int>> dp1(n.size() + 1, vector<int>(k + 1, 0));
    dp1[0][0] = 1;
    for (int i = 0; i < n.size(); ++i) {
        if (i != 0) dp0[i][0] = 1;
        for (int j = 0; j < k; ++j) {
            dp0[i + 1][j + 1] += dp0[i][j + 1];
            dp0[i + 1][j + 1] += dp0[i][j] * 9;
            dp0[i + 1][j + 1] += dp1[i][j] * max(ctoi(n[i]) - 1, 0);
            dp0[i + 1][j + 1] += dp1[i][j + 1] * min(ctoi(n[i]), 1);
            if (ctoi(n[i]) == 0) {
                dp1[i + 1][j + 1] = dp1[i][j + 1];
            } else {
                dp1[i + 1][j + 1] = dp1[i][j];
            }
        }
    }
    cout << dp0[n.size()][k] + dp1[n.size()][k] << endl;
}


Measured memory_bytes: 3564
#include <bits/stdc++.h> // Tomasz Nowak
using namespace std;     // XIII LO Szczecin
 
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);

	int n;
	cin >> n;
	vector<int> w(n), s(n), f(n);
	for(int i = 0; i < n; ++i)
		cin >> w[i] >> s[i] >> f[i];

	vector<int> perm(n);
	iota(perm.begin(), perm.end(), 0);
	sort(perm.begin(), perm.end(), [&](int l, int r) {
		return s[l] + w[l] < s[r] + w[r];
	});

	int mxWeight = *max_element(s.begin(), s.end()) + *max_element(w.begin(), w.end());
	vector<long long> dp(mxWeight + 1);

	for(int i = 0; i < n; ++i) {
		int p = perm[i];
		for(int weight = s[p]; weight >= 0; --weight)
			dp[weight + w[p]] = max(dp[weight + w[p]], dp[weight] + f[p]);
	}
	cout << *max_element(dp.begin(), dp.end()) << '\n';
}


Measured memory_bytes: 384
#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#include <cstring>
#include <math.h>
#include <bitset>
#include <time.h>
#include <set>
#define MAXN 200000
#define ll long long
using namespace std;

int main() {
    string S;
    cin>>S;
    set<int> uniqs;
    for(int j=0;j<S.size();j++) {
        uniqs.insert(S[j]);
    }
    
    int a = ((int)(uniqs.find('N')==uniqs.end()) + (int)(uniqs.find('S')==uniqs.end()))%2;
    int b = ((int)(uniqs.find('E')==uniqs.end()) + (int)(uniqs.find('W')==uniqs.end()))%2;
    if (a == 1 || b == 1) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }
    
}

Measured memory_bytes: 256
import math

n, m, x = map(int, input().split())
books = []
for i in range(n):
    books.append(list(map(int, input().split())))
n_bin = '1'*n
_n = (int(n_bin, 2))+1
ans = 10**9

for i in range(_n):
    target = bin(i).replace('0b', '').zfill(n)
    tmp_skill = [0 for i in range(len(books[0])-1)]
    tmp_price = 0
    
    for j in range(len(target)):
        if target[j] == '1':
            tmp_price += books[j][0]
            for k in range(len(tmp_skill)):
                tmp_skill[k] += books[j][k+1]
        for k in range(len(tmp_skill)):
            if tmp_skill[k] < x:
                break
        else:
            ans = min(ans, tmp_price)
if ans == 10**9:
    print(-1)
else:
    print(ans)

Measured memory_bytes: 9252
#include<bits/stdc++.h>
using namespace std;
int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int n, k;
	cin >> n >> k;
	vector<int> p(n + 1), sum(n + 1);
	for (int i = 1; i <= n; i++)
		cin >> p[i];
	for (int i = 1; i <= n; i++)
		sum[i] = p[i] + sum[i-1];
	int ans = 0;
	for (int i = k; i<=n; i++) {
		ans = max(ans, sum[i] - sum[i-k]);
	}
	ans += k;
	cout << ans/2;
	if (ans & 1) cout << ".5";
	cout << "\n";
	return 0;
}

Measured memory_bytes: 1792
#include <iostream>
#include <vector>
#include <string>

using namespace std;

const int MOD = 1000000007;

long long modPow(long long a, long long p){
    if(p == 0) return 1;
    long long res = modPow(a, p/2);
    res = (res * res) % MOD;
    if(p%2) res = (res * a) % MOD;
    return res;
}

int main(){
    int N;
    while(cin >> N){
        string S; cin >> S;
        vector<vector<long long>> dp(N+1, vector<long long>(N+1, 0));
        dp[0][0] = 1;
        for(int i=0;i<N;i++){
            for(int j=0;j<N;j++){
                dp[i+1][j+1] += 2 * dp[i][j];
                dp[i+1][j+1] %= MOD;
                dp[i+1][max(0, j-1)] += dp[i][j];
                dp[i+1][max(0, j-1)] %= MOD;
            }
        }
        cout << (dp[N][S.size()] * modPow(modPow(2, S.size()), MOD-2)) % MOD << endl;
    }
}

Measured memory_bytes: 195840
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")

#include <bits/stdc++.h>

using namespace std;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "


// CHECK THE LIMITS
typedef long long ll;

const int MOD = 1000000007;
const ll INFLL = 1e18;
const int INF = 1e9;
const int NMAX = 1000001;

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

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

    int n,m;
    cin >> m >> n;
    if(n >= m)
	cout << "unsafe";
    else
	cout << "safe";

    return 0;
}


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

typedef long long LL;

#define FOR(i, a, b) for (int i = (a), i##_END_ = (b); i <= i##_END_; i++)
#define DNF(i, a, b) for (int i = (a), i##_END_ = (b); i >= i##_END_; i--)

template <typename Tp> void in(Tp &x) {
	char ch = getchar(), f = 1; x = 0;
	while (ch != '-' && (ch < '0' || ch > '9')) ch = getchar();
	if (ch == '-') ch = getchar(), f = -1;
	while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
	x *= f;
}

template <typename Tp> void out(Tp x) {
	if (x > 9) out(x / 10);
	putchar(x % 10 + '0');
	return;
}

template <typename Tp> Tp Max(const Tp &x, const Tp &y) {return x > y ? x : y;}
template <typename Tp> Tp Min(const Tp &x, const Tp &y) {return x < y ? x : y;}
template <typename Tp> bool chkmax(Tp &x, Tp y) {return x >= y ? 0 : (x=y, 1);}
template <typename Tp> bool chkmin(Tp &x, Tp y) {return x <= y ? 0 : (x=y, 1);}

const int MAXN = 100010;
const int MOD = 1000000007;

int n, m, k;
int cnt, head[MAXN], data[MAXN << 1], nxt[MAXN << 1];

void add(int x, int y)
{
	nxt[cnt] = head[x]; data[cnt] = y; head[x] = cnt++;
	nxt[cnt] = head[y]; data[cnt] = x; head[y] = cnt++;
}

int st[MAXN];
int dfn[MAXN], low[MAXN], INDEX;

int belong[MAXN], vp[MAXN], ep[MAXN];

void dfs(int now, int pa)
{
	dfn[now] = low[now] = ++INDEX;

	for (int i = head[now]; i != -1; i = nxt[i]) {
		if (data[i] == pa) continue;
		if (!dfn[data[i]]) {
			st[++st[0]] = i;
			dfs(data[i], now);
			chkmin(low[now], low[data[i]]);
			if (low[data[i]] >= dfn[now]) {
				++belong[0];
				do {
					ep[belong[0]]++;
					if (belong[data[st[st[0]]]] != belong[0]) {
						belong[data[st[st[0]]]] = belong[0];
						vp[belong[0]]++;
					}
					if (belong[data[st[st[0]] ^ 1]] != belong[0]) {
						belong[data[st[st[0]] ^ 1]] = belong[0];
						vp[belong[0]]++;
					}
					st[0]--;
				} while (st[st[0] + 1] != i);
			}
		}
		else if (dfn[data[i]] < dfn[now]) {
			chkmin(low[now], dfn[data[i]]);
			st[++st[0]] = i;
		}
	}
}

int fac[MAXN];
int inv[MAXN];

int power(int x, int y)
{
	int ret = 1;
	while (y) {
		if (y & 1) ret = 1ll * ret * x % MOD;
		x = 1ll * x * x % MOD;
		y >>= 1;
	}
	return ret;
}

int C(int x, int y)
{
	if (x < y) return 0;
	return 1ll * fac[x] * inv[y] % MOD * inv[x - y] % MOD;
}

int gcd(int x, int y)
{
	return !y ? x : gcd(y, x % y);
}

int main()
{
	in(n); in(m); in(k);
	memset(head, -1, sizeof head);

	fac[0] = 1;
	FOR(i, 1, MAXN - 10) fac[i] = 1ll * fac[i - 1] * i % MOD;

	inv[MAXN - 10] = power(fac[MAXN - 10], MOD - 2);
	DNF(i, MAXN - 10 - 1, 0) inv[i] = 1ll * inv[i + 1] * (i + 1) % MOD;

	FOR(i, 1, m) {
		int x, y; in(x); in(y); add(x, y);
	}

	FOR(i, 1, n) if (!dfn[i]) dfs(i, 0);

	int ans = 1;
	
	FOR(i, 1, belong[0]) {
		if (vp[i] == 2) ans = 1ll * ans * k % MOD;
		else if (vp[i] == ep[i]) {
			int tmp = 0;
			FOR(j, 1, vp[i]) tmp = (tmp + power(k, gcd(j, vp[i]))) % MOD;
			ans = 1ll * ans * tmp % MOD * power(vp[i], MOD - 2) % MOD;
		}
		else ans = 1ll * ans * C(ep[i] + k - 1, k - 1) % MOD;
	}

	printf("%d\n", ans);
	
	return 0;
}


Measured memory_bytes: 3456
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <fstream>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef tuple<int, int, int> iii;
typedef vector<iii> viii;
typedef priority_queue<int> pqi;
typedef priority_queue<ii> pqii;
typedef map<int, int> mii;
typedef map<int, string> mis;
typedef map<string, int> msi;
typedef set<int> si;
typedef set<ll> sl;
typedef map<ll, ll> mll;

#define pb push_back
#define mp make_pair
#define rep(i, n) for (int i = 0 ; i < n ; i++)

bool overlaps(ii a, ii b) {
    int x1 = a.first;
    int x2 = b.first;
    int w1 = a.second - a.first;
    int w2 = b.second - b.first;
    if (x2 < x1 + w1 && x1 < x2 + w2) {
        return true;
    }
    return false;
}

vi readList(int n) {
    vi a(n);
    for (int i = 0 ; i < n ; i++) {
        cin >> a[i];
    }
    return a;
}

void printList(vi a) {
    for (int i = 0 ; i < a.size() ; i++) {
        cout << a[i];
        if (i < a.size() - 1) {
            cout << " ";
        }
    }
    cout << endl;
}

bool comp(ll a, ll b) {
    if (a < b) {
        return false;
    }
    return true;
}

void solve() {
    
    ll n, k;
    cin >> n >> k;
    ll ans = min(n % k, k - n % k);
    cout << ans << endl;
    
}

void querySolve() {
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
}

int main() {
    //querySolve();
    solve();
}


Measured memory_bytes: 256