akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define F(i,a,n) for(int i=a;i<n;i++)
#define INF 1000000007
using namespace std;

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

int main(){
  int x,y,a,b,c;
  scanf("%d%d%d%d%d",&x,&y,&a,&b,&c);
  long p[a+1],q[b+1],r[c+1];
  rep(i,a)cin >> p[i];
  rep(i,b)cin >> q[i];
  rep(i,c)cin >> r[i];
  sort(p,p+a,greater<>());
  sort(q,q+b,greater<>());
  sort(r,r+c,greater<>());
  int pi=0,qi=0,ri=0;
  p[x]=-1;
  q[y]=-1;
  long ans=0;
  int sum=x+y;
  while(sum>0){
    if(ri<c){
      long tmp=max(p[pi],max(q[qi],r[ri]));
      if(tmp==p[pi]){
        if(pi<x){
          ans+=p[pi];
          pi++;
        }else{
          tmp=max(q[qi],r[ri]);
          if(tmp==q[qi]){
            ans+=q[qi];
            qi++;
          }else{
            ans+=r[ri];
            ri++;
          }
        }
      }else if(tmp==q[qi]){
        if(qi<y){
          ans+=q[qi];
          qi++;
        }else{
          tmp=max(p[pi],r[ri]);
          if(tmp==p[pi]){
            ans+=p[pi];
            pi++;
          }else{
            ans+=r[ri];
            ri++;
          }
        }
      }else{
        ans+=r[ri];
        ri++;
      }
    }else{
      if(pi>=x){
        ans+=q[qi];
        qi++;
      }else if(qi>=y){
        ans+=p[pi];
        pi++;
      }else{
        long tmp=max(p[pi],q[qi]);
        if(tmp==p[pi]){
          ans+=p[pi];
          pi++;
        }else{
          ans+=q[qi];
          qi++;
        }
      }
    }
    sum--;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 2560
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);



		int number = scanner.nextInt();

		int[] array = new int[number];


		for(int i=0; i<array.length; i++){
			array[i] = scanner.nextInt();
		}

	

		for (int i = array.length - 1; i >= 1; i--) {
			System.out.print(array[i] + " ");

		}
		System.out.println(array[0]);

	}
}

Measured memory_bytes: 18992
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int A,B,C;
  cin >> A >> B >> C;
  
  int ans = C - (A - B);
  if(ans > 0){
    cout << ans << endl;
  }else{
    ans = 0;
    cout << ans << endl;
  }
}

Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>
#define max(a, b) ((a)<(b) ? (b) : (a))
int main(){
  int n;  scanf("%d", &n);
  char s[102];  scanf("%s", s);

  char al[100];
  int ans = 0;
  for(int i=1; i<strlen(s); i++){
    int alsize = 0;
    for(int j=0; j<i; j++){
      for(int k=i; k<strlen(s); k++){
	if(s[j] == s[k]){
	  int flag = 0;
	  for(int l=1; l<=alsize; l++){
	    if(al[l] == s[j])  break;
	    if(l == alsize)  flag = 1;
	  }
	  if(flag || alsize == 0){
	    al[++alsize] = s[j];
	  }
	}
      }
    }
    ans = max(ans, alsize);
  }
  printf("%d\n", ans); 
  return 0;
}


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


void solve(long long N, long long A, long long B){
    long long b = N / (A+B);
    long long a = N % (A+B);
    long long c;
    if (a <= A) c = b * A + a;
    else c = b * A + A;
    cout << c << endl;
}

// Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    long long A;
    scanf("%lld",&A);
    long long B;
    scanf("%lld",&B);
    solve(N, A, B);
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#define REP(i, n) for(int i = 0; i < (n); i++)
#define ALL(c) c.begin(),c.end()

using namespace std;

int main()
{
  int N;
  cin >> N;
  vector<int> L(2*N);
  REP(i, 2*N) cin >> L[i];
  sort(ALL(L));
  int ans = 0;
  for(int i = 0; i < 2*N; i += 2)
    ans += L[i];
  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

class Main
{
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        
        int a = scan.nextInt();
        
        int b = scan.nextInt();
        
        System.out.print(a * b);
        
        System.out.print(" ");

        System.out.println((a * 2) + (b * 2));
    }
}


Measured memory_bytes: 26084
@dir = [[0, 0, 1], [0, 1, 0], [1, 0, 0],
  [0, 1, 1], [0, 1, -1], [1, 0, 1], [1, 0, -1], [1, 1, 0], [1, -1, 0],
  [1, 1, 1], [1, 1, -1], [1, -1, -1], [1, -1, 1]]
 
def out(dir, x, y, z, n, j)
  dir.zip([x, y, z]).any?{ |a, b| n <= a * j + b || a * j + b < 0 }
end
 
def check(balls, ok, n, m, turn)
  n.times do |x|
    n.times do |y|
      n.times do |z|
        if balls[x][y][z] == turn
          13.times do |i|
            if ok[x][y][z][i]
              is_ok = true
              m.times do |j|
                if out(@dir[i], x, y, z, n, j)
                  is_ok = false
                  break
                end
                space = balls[x + @dir[i][0] * j][y + @dir[i][1] * j][z + @dir[i][2] * j]
                if !space
                  is_ok = false
                elsif space == 1 - turn
                  (0..j).each do |k|
                    ok[x + @dir[i][0] * k][y + @dir[i][1] * k][z + @dir[i][2] * k][i] = false
                  end
                  is_ok = false
                  break
                end
              end
              return true if is_ok
            end
          end
        end
      end
    end
  end
  false
end
 
loop do
  n, m, p = gets.split.map(&:to_i)
  break if n == 0
  z_plane = n.times.map{ [0] * n }
  fin = false
  balls = n.times.map{ n.times.map{ [false] * n } }
  ok = 2.times.map{ n.times.map{ n.times.map{ n.times.map { [true] * 13 } } } }
  p.times do |i|
    x, y = gets.split.map(&:to_i)
    next if fin
    z = (z_plane[y - 1][x - 1] += 1)
    balls[x - 1][y - 1][z - 1] = i % 2
    13.times{ |k| ok[1 - i % 2][x - 1][y - 1][z - 1][k] = false }
    if check(balls, ok[i % 2], n, m, i % 2)
      printf "%s %d\n" % [['Black', 'White'][i % 2], i + 1]
      fin = true
    end
  end
  puts 'Draw' unless fin
end

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

using namespace std;
using ll = long long;

int main(int argc, const char *argv[]) {
  //
  int n, k;
  cin >> n >> k;

  vector<ll> va(n);
  for (int i = 0; i < n; ++i) {
    cin >> va[i];
  }

  for (int i = k, l = 0; i < n; ++i, ++l) {
    if (va[l] < va[i]) {
      cout << "Yes" << '\n';
    } else {
      cout << "No" << '\n';
    }
  }

  return 0;
}

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

int main(void){
  string A,B,C;
  cin >> A >> B >> C;
  if ((A.back() == B.front()) && (B.back() == C.front())){
    cout << "YES" << endl;
  }
  else{
    cout << "NO" << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <vector>

using namespace std;
using LLONG = long long;
const LLONG MOD = 1000000007;

int main()
{
    // 操作回数がK<=100なのでシミュレーションで間に合う
    vector<int> cookies(2);
    int K;
    cin >> cookies[0] >> cookies[1] >> K;
    for (int i = 0; i < K; ++i)
    {
        int first = i % 2 == 0 ? 0 : 1;
        int second = i % 2 == 0 ? 1 : 0;
        if (cookies[first] % 2 == 1) --cookies[first];
        cookies[first] /= 2;
        cookies[second] += cookies[first];
    }
    cout << cookies[0] << ' ' << cookies[1] << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=205;

int h, w;
int val[maxn][maxn];

int main(){
    scanf("%d%d", &h, &w);
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            scanf("%d", &val[i][j]);
        }
    }
    for(int k=0;k<10;k++){
        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                if(i==j)continue;
                val[i][j]=min(val[i][j], val[i][k]+val[k][j]);
            }
        }
    }
    int ans=0, tmp;
    for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
            scanf("%d", &tmp);
            if(tmp==-1)continue;
            ans+=val[tmp][1];
        }
    }
    printf("%d\n", ans);
    return 0;
}


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

A=[list(input()) for i in range(N)]
B=[list(input()) for i in range(M)]

point=0

for nx in range(N-M+1):
  for ny in range(N-M+1):
    ans=[a[ny:ny+M] for a in A[nx:nx+M]]
    if ans == B:
      point+=1

if point==0:
  print('No')
else:
  print('Yes')
     

Measured memory_bytes: 3060
/*

*/
#include<iostream>
#include<iomanip>
#include<ios>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<string>
#include<algorithm>
#include<utility>
#include<math.h>
#include<cstdlib>
#define rep(i, n) for(int i=0;i<n;i++)
#define repb(i, a, b) for(int i=a;i<b;i++)
#define MAX_N 10000

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

const int INF = 1<<30;
const ll MOD = ll(1e9+7);

int main(){
  ll n, a, b;cin>>n>>a>>b;
  ll ans;
  if(a+b==0){
    cout<<0<<endl;return 0;
  }else if(n%(a+b)!=0){
    ans=n/(a+b)*a;
    ans+=min((n%(a+b)), a);
  }else{
    ans=n/(a+b)*a;
  }
  cout<<ans<<endl;
  // system("read -n 1 -s -p \"Press any key to continue...\"");
}


Measured memory_bytes: 256
function Main(input) {
	var str = input;
	var ans = "YES";
	var c = 0;
	str = str.split("\n");
	var l = str[1].split(" "); 
	var len = l.length;
	l.map( str => parseInt(str, 10) );
	l.sort(function(a, b) {
  	return a - b;	});
	
// 	for(var j =0;j< len -1;j++){	
 		for(var i =0; i<len;i++){
 			 if(l[i]===l[i+1]){
	 			ans = "NO";
	 			break;
 			}	 	
 		}
//  		c++;
// 	} 	
	console.log(ans);
}
 //*おまじない
Main(require("fs").readFileSync("/dev/stdin", "utf8"));


Measured memory_bytes: 37788
#include <iostream>
#include <vector>
#include <queue>
#include <map>
using namespace std;
#define debug(x) std::cerr << #x << " = " << (x) << std::endl
typedef long long LL;
const int MAXN = 1e5+17;
vector<pair<LL,LL > > G[MAXN];
LL in[MAXN],out[MAXN],plc[MAXN],vis[MAXN];
int main(int argc ,char const *argv[])
{
   	 #ifdef GoodbyeMonkeyKing
    freopen("Input.txt","r",stdin);freopen("Output.txt","w",stdout);
    #endif
    int n,m;
    cin>>n>>m;
    for (int i = 0; i < m; ++i)
    {
    	int u,v,d;
    	scanf("%lld%lld%lld",&u,&v,&d);
    	G[u].push_back({v,d});
    	G[v].push_back({u,-d});
    	in[v]++;
    	out[u]++;
    }
    for (int i = 1; i <= n; ++i)
    {
    	plc[i] = -0x3fffffffffff;
    	vis[i] = 0;
    }
    queue<int > q;
    bool can = true;
    for (int i = 1; i <= n; ++i)
    {
    	if(out[i]||in[i]&&!vis[i])
    	{
    		q.push(i);
		    while(q.size()&&can)
		    {
		    	int u = q.front();
		    	q.pop();
		    	vis[u] = 1;
		    	if(plc[u]==-0x3fffffffffff) plc[u] = 317;
		    	debug(u);
		    	debug(plc[u]);
		    	for (int i = 0; i < G[u].size(); ++i)
		    	{
		    		LL v = G[u][i].first,c = G[u][i].second;
		    		if(plc[v]==-0x3fffffffffff) 
		    		{
		    			plc[v]=plc[u]+c;
		    			if(!vis[v])
		    				q.push(v);
		    		}
		    		else 
		    			if(plc[v]!=plc[u]+c) 
		    				can = false;
		    	}
		    }
    	}
    }
    if(can) cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}



Measured memory_bytes: 16384
a=int(input())
b=int(input())
c=int(input())
x=int(input())
ans=0
for i in range(a+1):
  for j in range(b+1):
    for k in range(c+1):
      tmp = i * 500 + j * 100 + k * 50
      if tmp == x:
        ans+=1

print(ans)

Measured memory_bytes: 3060
#include <iostream>
using namespace std;
int main(void){
    for(int x, t = 1; ; t++){ //for文での空の条件式は、常にtrueとみなされる
    cin >> x;
        if(x == 0) break;
        cout << "Case " << t << ": " << x << endl;
    }
    
    return 0;
}



Measured memory_bytes: 3000
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define all(x) (x).begin(),(x).end()
using namespace std;
const int INF=1145141919,MOD=1e9+7;
const int64_t LINF=8931145141919364364,LMOD=998244353;
// const int dy[]={0,-1,0,1,1,-1,-1,1};
// const int dx[]={1,0,-1,0,1,1,-1,-1};

int main() {
	int n; cin >> n;
	int64_t k; cin >> k;
	int64_t a[n]; rep(i,n) cin >> a[i];
	int64_t f[n]; rep(i,n) cin >> f[i];
	sort(a, a + n);
	sort(f, f + n);
	reverse(f, f + n);
	int64_t ok = LINF, ng = -1;
	while(abs(ok - ng) > 1) {
		int64_t mid = (ok + ng) / 2;
		int64_t cnt = 0;
		for(int i = 0; i < n; ++i) {
			cnt += max<int64_t>(0, a[i] - mid / f[i]);
			// cerr << cnt << endl;
		}
		if(cnt <= k) {
			ok = mid;
		} else {
			ng = mid;
		}
	}
	cout << ok << endl;
	return 0;
}

Measured memory_bytes: 3328
#include "bits/stdc++.h"
#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##_len=(b);i<i##_len;++i)
#define BUGAVOID(x) x
#define rep(...) BUGAVOID(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define sz(c) (int)(c.size())
#define all(c) c.begin(),c.end()
#define mp make_pair
#define write(x) cout<<(x)<<"\n"
using namespace std; typedef long long ll;
typedef vector<int> vi; typedef vector<ll> vll; template<class T, class U>using vp = vector<pair<T, U>>;
template<class T>using vv = vector<vector<T>>; template<class T, class U>using vvp = vv<pair<T, U>>;
template<class T>vv<T> vvec(size_t n, size_t m, T v) { return vv<T>(n, vector<T>(m, v)); }
template<class T>bool chmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template<class T>bool chmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
constexpr int INF = 1 << 28, MAX = 1e5 + 5, MOD = 1e9 + 7;
constexpr ll LINF = 1ll << 60; constexpr double EPS = 1e-6;
constexpr int dy[4] = { 0,1,0,-1 }, dx[4] = { 1,0,-1,0 };
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); }; }aaaa;

int R, C, N;
vp<int, int> sepa;

int crd(int x, int y) {
    if (x == 0) return y;
    else if (y == C) return C + x;
    else if (x == R) return R + 2 * C - y;
    else if (y == 0) return 2 * R + 2 * C - x;
    else return -1;
}

int main() {
    cin >> R >> C >> N;

    // 両端が辺に乗ってるもののみ
    // 左上0時計回りに座標振る
    rep(i, N) {
        int X1, Y1, X2, Y2;
        cin >> X1 >> Y1 >> X2 >> Y2;

        int crd1 = crd(X1, Y1);
        int crd2 = crd(X2, Y2);
        if (crd1 == -1 || crd2 == -1) continue;
        if (crd1 > crd2) swap(crd1, crd2);

        sepa.emplace_back(crd1, crd2);
    }
    sort(all(sepa));

    bool ans = true;
    if (!sepa.empty()) {
        int i = 0;
        vector<int> st;
        st.push_back(2 * R + 2 * C);

        while (i < sepa.size()) {
            if (sepa[i].first < st.back()) {
                if (sepa[i].second >= st.back()) ans = false;
                st.push_back(sepa[i].second);
                i++;
            }
            else {
                st.pop_back();
            }
        }
    }

    write(ans ? "YES" : "NO");
}

Measured memory_bytes: 1524
#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

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

using namespace std;
map<int,pair<int,int> > dp;
int field[2048][2048];
map<int,pair<int,int> >itop;
vector<bool> p(1048576,true);
int m;
pair<int,int> solve(int n)
{
  if(m<n) return make_pair(0,0);
  if(dp.count(n)) return dp[n];
  int x,y;
  tie(x,y)=itop[n];
  pair<int,int> after=make_pair(0,0);
  FOR(i,-1,2) after=max(after,solve(field[x+i][y+1]));
  dp[n]=after;
  if(p[n])
    if(after==make_pair(0,0)) dp[n]=make_pair(1,n);
    else ++dp[n].first;
  return dp[n];
}
int main(void)
{
  p[0]=false;
  p[1]=false;
  REP(i,1024){
    if(!p[i]) continue;
    for(int j=i*i;j<1048576;j+=i) p[j]=false;
  }
  int tmp=1,x=1024,y=1024;
  field[x][y]=tmp;
  itop[tmp]=make_pair(x,y);
  for(int i=1;;++i){
    REP(j,i){
      if(i%2) ++x;
      else --x;
      ++tmp;
      field[x][y]=tmp;
      itop[tmp]=make_pair(x,y);
    }
    REP(j,i){
      if(i%2) --y;
      else ++y;
      ++tmp;
      field[x][y]=tmp;
      itop[tmp]=make_pair(x,y);
    }
    if(tmp>1048576) break;
  }
  for(;;){
    int n;
    cin >> m >> n;
    if(!n) return 0;
    dp.clear();
    pair<int,int> answer=solve(n);
    cout << answer.first << ' ' << answer.second << endl;

  }
}

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

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int,int> pii;

#define ones(x) __builtin_popcount(x)
#define onesl(x) __builtin_popcountl(x)
#define onesll(x) __builtin_popcountll(x)

#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define scn(n) scanf("%d",&n)
#define scnll(n) scanf("%lld",&n)
#define scn2(n,m) scanf("%d%d",&n,&m)
#define scn3(n,m,w) scanf("%d%d%d",&n,&m,&w)
#define scn2ll(n,m) scanf("%lld%lld",&n,&m)
#define atoz(v) v.begin(),v.end()
#define ratoz(v) v.rbegin(),v.rend()
#define Fill(a,v) memset(a,v,sizeof(a))
#define sz(v) v.size()
#define fi first
#define se second
#define inf 1e9
#define pi acos(-1.0)
#define sqr(x) x*x
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define ten(n) (int)1e##n
#define tenll(n) (lli)1e##n
#define mod 1000000007
#define FastIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

int Set(int N,int pos)
{
    return N=N | (1<<pos);
}
int reset(int N,int pos)
{
    return N = N & ~(1<<pos);
}
bool check(int N,int pos)
{
    return (bool)(N & (1<<pos));
}

///priority_queue< int, vector< int >, greater< int > > minHeap;

void mor(){cout<<"NO"; exit(0);}


int main()
{
    ///freopen("output.txt","w",stdout);
    ///freopen("input.txt","r",stdin);

    ///FastIO;

    string s,t;
    cin>>s;

    int ans = s.size(), cnt = 0;

    for(int i=0; i<s.size(); i++){ ///01010
        if(i%2)cnt += (s[i]!='1');
        else cnt += (s[i]!='0');
    }
    ans = min(cnt,ans);
    cnt = 0;
    for(int i=0; i<s.size(); i++){ ///101010
        if(i%2)cnt += (s[i]!='0');
        else cnt += (s[i]!='1');
    }
    ans = min(cnt,ans);
    cout<<ans;

    return 0;
    ///return main();
}

///sin and cos expect input in radians not degrees. so use , sin(degrees * pi / 180)


Measured memory_bytes: 512
void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int n; rd(n);
  auto a=readln.split.to!(long[]);

  a=0L~a~0L;

  long s=0;
  import std.math;
  for(int i=0; i<=n; i++){
    s+=(a[i+1]-a[i]).abs;
  }
  for(int i=1; i<=n; i++){
    auto t=s;
    t-=(a[i]-a[i-1]).abs;
    t-=(a[i+1]-a[i]).abs;
    t+=(a[i+1]-a[i-1]).abs;
    writeln(t);
  }
}


void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  assert(l.length==x.length);
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}


Measured memory_bytes: 5496
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

template<unsigned P> struct ModInt {
  using M = ModInt;
  unsigned v;
  constexpr ModInt() : v(0) {}
  constexpr ModInt(auto x) : v(x >= 0 ? x % P : (P - -x % P) % P) {}
  constexpr ModInt(unsigned v, int) : v(v) {}
  static constexpr unsigned p() { return P; }
  M operator+() const { return *this; }
  M operator-() const { return {v ? P - v : 0, 0}; }
  explicit operator bool() const noexcept { return v; }
  bool operator!() const noexcept { return !(bool)*this; }
  M operator*(M r) const { return M(*this) *= r; }
  M operator/(M r) const { return M(*this) /= r; }
  M operator+(M r) const { return M(*this) += r; }
  M operator-(M r) const { return M(*this) -= r; }
  bool operator==(M r) const { return v == r.v; }
  bool operator!=(M r) const { return !(*this == r); }
  M& operator*=(M r) { v = (uint64_t)v * r.v % P; return *this; }
  M& operator/=(M r) { return *this *= r.inv(); }
  M& operator+=(M r) { if ((v += r.v) >= P) v -= P; return *this; }
  M& operator-=(M r) { if ((v += P - r.v) >= P) v -= P; return *this; }
  M inv() const {
    int a = v, b = P, x = 1, u = 0;
    while (b) {
      int q = a / b;
      swap(a -= q * b, b);
      swap(x -= q * u, u);
    }
    assert(a == 1);
    return x;
  }
  M pow(auto n) const {
    if (n < 0) return pow(-n).inv();
    M res = 1;
    for (M a = *this; n; a *= a, n >>= 1) if (n & 1) res *= a;
    return res;
  }
  friend M operator*(auto l, M r) { return M(l) *= r; }
  friend M operator/(auto l, M r) { return M(l) /= r; }
  friend M operator+(auto l, M r) { return M(l) += r; }
  friend M operator-(auto l, M r) { return M(l) -= r; }
  friend ostream& operator<<(ostream& os, M r) { return os << r.v; }
  friend istream& operator>>(istream& is, M& r) { lint x; is >> x; r = x; return is; }
  friend bool operator==(auto l, M r) { return M(l) == r; }
  friend bool operator!=(auto l, M r) { return !(l == r); }
};
using Mint = ModInt<(unsigned)1e9 + 7>;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int n, k; cin >> n >> k;
  VV<Mint> dp(n + 1, V<Mint>(n * n + 1));
  dp[0][0] = 1;
  for (int i = 0; i < n; ++i) {
    VV<Mint> ndp(n + 1, V<Mint>(n * n + 1));
    for (int x = 0; x <= n; ++x) for (int y = 0; y <= n * n; ++y) if (dp[x][y]) {
      ndp[x + 1][y + 2 * x] += dp[x][y];
      ndp[x][y + 2 * x] += dp[x][y] * (2 * x);
      if (x) ndp[x - 1][y + 2 * x] += dp[x][y] * (x * x);
      ndp[x][y + 2 * x] += dp[x][y];
    }
    swap(dp, ndp);
  }
  cout << dp[0][k] << '\n';
}

Measured memory_bytes: 1332
#include<stdio.h>
#include<math.h>
int main(void){
	int x,y,z;
	for(;;){
	scanf("%d%d%d",&x,&y,&z);
	if(x==0)break;

	int n;
	scanf("%d",&n);

	int i;
	int r[10000];
	for(i=0;i<n;i++){//入力
        scanf("%d",&r[i]);
	}
	double a,b,a2,b2,c2,csub;
	if(x>=y&&x>=z)a=y,b=z;
	else if(y>=x&&y>=z)a=x,b=z;
	else if(z>=x&&z>=y)a=x,b=y;
	a2=a/2;
	b2=b/2;
	csub=a2*a2+b2*b2;
	c2=sqrt(csub);
	//printf("%lf\n",c2);
	for(i=0;i<n;i++){//計算
	    if(c2<r[i])printf("OK\n",r[i]);
	    else printf("NA\n",r[i]);

	}

	}


	return 0;
}

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

using namespace std;
using ll = long long int;
using lc = complex<double>;

int main(void) {
    constexpr ll MOD = 1e9 + 7;
    constexpr double PI = acos(-1);
    cout << fixed << setprecision(32);
    cin.tie(0); ios::sync_with_stdio(false);

    ll x, k, d;
    cin >> x >> k >> d;

    ll t = abs(x) / d;
    t = min(t, k);
    x += (x>0?-1:1)*d*t;
    k -= t;
    if(k&1) x = (x>0 ? x-d : x+d);
    cout << abs(x) << endl;
}

Measured memory_bytes: 3620
#include<bits/stdc++.h>
using namespace std;
template<typename T = int>T in() { T x; cin >> x; return x; }
#define REP(i,n) for(int i=0;i<(n);++i)
#define ALL(obj) (obj).begin(),(obj).end()
using arll3 = array<long long, 3>;
bool comp0(const arll3&arg1, const arll3&arg2) { return +arg1[0] + arg1[1] + arg1[2] > +arg2[0] + arg2[1] + arg2[2]; }
bool comp1(const arll3&arg1, const arll3&arg2) { return +arg1[0] + arg1[1] - arg1[2] > +arg2[0] + arg2[1] - arg2[2]; }
bool comp2(const arll3&arg1, const arll3&arg2) { return +arg1[0] - arg1[1] + arg1[2] > +arg2[0] - arg2[1] + arg2[2]; }
bool comp3(const arll3&arg1, const arll3&arg2) { return +arg1[0] - arg1[1] - arg1[2] > +arg2[0] - arg2[1] - arg2[2]; }
bool comp4(const arll3&arg1, const arll3&arg2) { return -arg1[0] + arg1[1] + arg1[2] > -arg2[0] + arg2[1] + arg2[2]; }
bool comp5(const arll3&arg1, const arll3&arg2) { return -arg1[0] + arg1[1] - arg1[2] > -arg2[0] + arg2[1] - arg2[2]; }
bool comp6(const arll3&arg1, const arll3&arg2) { return -arg1[0] - arg1[1] + arg1[2] > -arg2[0] - arg2[1] + arg2[2]; }
bool comp7(const arll3&arg1, const arll3&arg2) { return -arg1[0] - arg1[1] - arg1[2] > -arg2[0] - arg2[1] - arg2[2]; }
int main()
{
	const int n = in(), m = in();
	vector<arll3>xyz(n);
	REP(i, n)cin >> xyz[i][0] >> xyz[i][1] >> xyz[i][2];
	long long ans = 0;
	for (auto c : { comp0, comp1, comp2, comp3, comp4, comp5, comp6, comp7 })
	{
		sort(ALL(xyz), c);
		long long sum[3] = { 0,0,0 };
		REP(i, m)REP(j, 3)sum[j] += xyz[i][j];
		const long long ans_tmp = abs(sum[0]) + abs(sum[1]) + abs(sum[2]);
		if (ans < ans_tmp)ans = ans_tmp;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> VI;
typedef pair <int,int> ii;
typedef long long LL;
#define pb push_back
const int INF = 2147483647;
const int N = 500005;

char t[105];
string s;
int n;

bool check(int a, int b) {
	int i = a - 1;
	int j = b - 1;
	while (i < j) {
		if (s[i] != s[j]) return false;
		i++;
		j--;
	}
	return true;
}

int main() {
scanf("%s", t);
s = t;
n = s.size();
if (check(1, n) && check(1, n / 2) && check((n+3)/2, n)) printf("Yes\n"); else printf("No\n");
return 0;
}

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

using namespace std;
const long long inf =  1e18L + 5;


int main() {
	long long n;
	cin>>n;
	long long w;
	cin>>w;
	vector<long long> values(n);
	vector<long long > weight(n);
	
	for(int i = 0;i<n;i++) {
		cin>>weight[i]>>values[i];
	}

	int sum_p = 0;
	for(int i: values) {
		sum_p += i;
	}
	
	vector<long long> dp(sum_p+1, inf);
	dp[0] = 0;
	for(int i = 0;i<n;i++) {
		for(int value = sum_p - values[i]; value >=0; value--) {
			dp[value + values[i]] = min(dp[value + values[i]], dp[value] + weight[i]);
		}
	}
	
	long long ans = 0;
	for(int i = 0;i<=sum_p;i++) {
		if(dp[i] <= w) {
			ans = max(ans, (long long)i);
		}
	}
	
	cout<<ans<<endl;
}

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

int main() {
	int n;
	cin >> n;
	int ans = 0;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j * i < n; j++)
		{
			ans++;
		}
	}
	cout << ans << '\n';
}

Measured memory_bytes: 3648
import sys
def input():
    return sys.stdin.readline()[:-1]
inf=float("inf")
n,c=map(int,input().split())
xv=[list(map(int,input().split())) for i in range(n)]

ra=[0]*(n+1)
ra[1]=xv[0][1]-xv[0][0]
for i in range(1,n):
    ra[i+1]=ra[i]+xv[i][1]-(xv[i][0]-xv[i-1][0])
mra=[0]*(n+1)
mra[1]=ra[1]
for i in range(1,n):
    mra[i+1]=max(mra[i],ra[i+1])
# print(ra)
# print(mra)

lb=[0]*(n+1)
lb[1]=xv[-1][1]-(c-xv[-1][0])
for i in range(1,n):
    lb[i+1]=lb[i]+xv[-1-i][1]-(xv[-i][0]-xv[-1-i][0])
mlb=[0]*(n+1)
mlb[1]=lb[1]
for i in range(1,n):
    mlb[i+1]=max(mlb[i],lb[i+1])
# print(lb)
# print(mlb)

ans=max(0,lb[0]+mra[n])
for i in range(1,n+1):
    ans=max(ans,lb[i]+mra[n-i]-(c-xv[-i][0]))
ans=max(ans,ra[0]+mlb[n])
for i in range(1,n+1):
    ans=max(ans,ra[i]+mlb[n-i]-xv[i-1][0])
print(ans)

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

int main()
{
  ll a, b, c, x, y, ans = 1e9 + 7;
  cin >> a >> b >> c >> x >> y;

  for (ll i = 0; i < x + y; i++)
  {
    ll m = (2 * c * i) + (a * max(x - i, (ll)0)) + (b * max(y - i, (ll)0));
    ans = min(ans, m);
  }
  cout << ans << endl;
}


Measured memory_bytes: 3612
s=str(input())
n=int(input())
for i in range(n):
    a=list(input().split())
    x=int(a[1])
    y=int(a[2])
    if a[0]=="print":
        print(s[x:y+1])
    elif a[0]=="replace":
        c=a[3]
        s=s[0:x]+c+s[y+1:]
    elif a[0]=="reverse":
        s=s[:x]+s[x:y+1][::-1]+s[y+1:]
        


Measured memory_bytes: 5612
//*******************************************************************************
//       DoublyLinkedList                 
//*******************************************************************************
//     < ???????????? >
//       DoublyLinkedList ??????????????????
//       ??\?????????????????? char ??????????????¨?????????
//*******************************************************************************
#include <iostream>
#include <list>
using namespace  std;

void fnLinkedListSet(list<int>& rltnNum)
{
  list<int>::iterator itltn;
  list<int>::iterator itltEND = rltnNum.end();
  char acCmd[80];
  int nMaxCmd, nNum;

  cin.tie(0);
  ios::sync_with_stdio(false);
 
  cin >> nMaxCmd;
  for (int i = 0; i < nMaxCmd; i++)
  {
    cin >> acCmd;
    if (acCmd[0] == 'i')
    {
      cin >> nNum;
      rltnNum.push_front(nNum);
    }
    else if (acCmd[6] == 'F')
      rltnNum.pop_front();
    else if (acCmd[6] == 'L')
      rltnNum.pop_back();
    else if (acCmd[0] == 'd')
    {
      cin >> nNum;
      for (itltn = rltnNum.begin(); itltn != itltEND; ++itltn)
        if (*itltn == nNum)
        {
          rltnNum.erase(itltn);
          break;
        }
    }
  }
}

void fnLinkedListGet(const list<int>& cnrltnNum)
{
  list<int>::const_iterator itltn;
  list<int>::const_iterator itltEND = cnrltnNum.end();

  for (itltn = cnrltnNum.begin(); itltn != itltEND; ++itltn)
  {
    if (itltn != cnrltnNum.begin()) cout << " ";
    cout << *itltn;
  }
  cout << endl;
}

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

  list<int> ltnNum;
  fnLinkedListSet(ltnNum);
  fnLinkedListGet(ltnNum);

  return 0;
}

Measured memory_bytes: 34284
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define mp       make_pair
#define pb       push_back
#define all(x)   (x).begin(),(x).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define repi(i,a,b) for(int i = (a); i < (b); i++)

using namespace std;

typedef    long long          ll;
typedef    unsigned long long ull;
typedef    vector<bool>       vb;
typedef    vector<int>        vi;
typedef    vector<vb>         vvb;
typedef    vector<vi>         vvi;
typedef    pair<int,int>      pii;

const int INF=1<<29;
const double EPS=1e-9;

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

int main(){
	char board[10][10];
	rep(i, 8)rep(j, 8){
		cin>>board[i][j];
	}
	for(int i =90;i <=270;i+=90){
		char tmp[10][10];
		cout <<i<<endl;
		for(int j = 0;j <8;j++){
			for(int k = 0;k <8;k++){
				tmp[j][k]=board[7-k][j];
			}
		}
		rep(j,8){
			rep(k,8){
				cout <<tmp[j][k];
			}
			cout <<endl;
		}
		rep(j, 8)rep(k, 8)board[j][k]=tmp[j][k];

	}
	return 0;
}

Measured memory_bytes: 1136
#include <bits/stdc++.h>
#define REP(i,n) for(int i = 0; i < (int)(n); ++i)
using namespace std;
int flag = 1;

int main(){
    cin >> flag;
    while(flag){
        int temp = 0, score = 0;
        int M = 0, m = 1000;
        REP(i,flag){
            cin >> temp;
            score += temp;
            M = max(temp,M);
            m = min(temp,m);
        }
        cout << (score-M-m)/(flag-2) << endl;
        cin >> flag;
    }
    return 0;
}

Measured memory_bytes: 1160
import sys

a, b, c, d = map(int, sys.stdin.readline().split())

def main():
    l = a + b
    r = c + d
    if l > r:
        return 'Left'
    elif l == r:
        return 'Balanced'
    else:
        return 'Right'

if __name__ == '__main__':
    ans = main()
    print(ans)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
using LL  = long long;
using VI  = vector<LL>;
using VVI = vector<VI>;
using VB  = vector<bool>;
using VS  = vector<string>;
using PII = pair<LL, LL>;
using VP  = vector<PII>;
#define PB           push_back
#define MP           make_pair
#define SZ(a)        LL((a).size())
#define EACH(x, c)   for (auto x : (c))
#define ALL(c)       (c).begin(), (c).end()
#define REVERSE(c)   reverse(ALL(c))
#define SORT(c)      stable_sort(ALL(c))
#define RSORT(c)     stable_sort((c).rbegin(), (c).rend())
#define FSORT(c)     stable_sort(ALL(c), [] (auto& x, auto& y) {return x.first < y.first;});
#define FRSORT(c)    stable_sort(ALL(c), [] (auto& x, auto& y) {return x.first > y.first;});
#define SSORT(c)     stable_sort(ALL(c), [] (auto& x, auto& y) {return x.second < y.second;});
#define SRSORT(c)    stable_sort(ALL(c), [] (auto& x, auto& y) {return x.second > y.second;});
#define FOR(i, a, b) for (LL i = (a); i < (b); ++i)
#define REP(i, n)    FOR(i, 0, n)
#define DEBUG true
#define $(x) {if (DEBUG) {cout << #x << " = " << (x) << endl;}}


int main() {
    string S;
    cin >> S;
    if (SZ(S) < 26) {
        set<char> X;
        REP(i, 26) {
            X.insert('a' + i);
        }
        EACH(c, S) {
            X.erase(c);
        }
        cout << S + *X.begin() << endl;
    } else {
        int i;
        for (i = SZ(S) - 2; i >= 0; --i) {
            if (S[i] < S[i + 1]) {
                int j;
                FOR(k, i + 1, SZ(S)) {
                    if (S[i] < S[k]) {
                        j = k;
                    }
                }
                cout << S.substr(0, i) + S[j] << endl;
                return 0;
            }
        }
        cout << "-1" << endl;
    }
	
    return 0;
}


Measured memory_bytes: 256
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0514
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

struct Result {
	int A;
	int B;
	int C;
	int res;
	Result(int a, int b, int c, int res) :A(a), B(b), C(c), res(res) {}
	bool operator<(const Result &o) const {
		return res < o.res;
	}
};

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	while (true) {
		int a, b, c; cin >> a >> b >> c;
		if (a == 0 && b == 0 && c == 0) break;
		int N;	cin >> N;
		vector<int> ans(a + b + c, 2);
		vector<Result> Res;
		for (int i = 0; i < N;i++) {
			int A, B, C, res; cin >> A >> B >> C >> res;
			A--; B--; C--;
			Res.push_back(Result(A, B, C, res));
		}
		sort(Res.begin(), Res.end());

		for (int i = N - 1; i >= 0; i--) {
			if (Res[i].res == 1) {
				ans[Res[i].A] = 1;
				ans[Res[i].B] = 1;
				ans[Res[i].C] = 1;
			}
			else {
				if (ans[Res[i].A] == 1 && ans[Res[i].B] == 1) ans[Res[i].C] = 0;
				if (ans[Res[i].B] == 1 && ans[Res[i].C] == 1) ans[Res[i].A] = 0;
				if (ans[Res[i].C] == 1 && ans[Res[i].A] == 1) ans[Res[i].B] = 0;
			}
		}
		for (auto n : ans) {
			cout << n << endl;
		}
	}
}

Measured memory_bytes: 3168
#include<iostream>

using namespace std;
int n, A[50];

int solve(int i, int m){
    if(m == 0) return 1;
    if(i >= n) return 0;
    int res = solve(i + 1, m) || solve(i + 1, m - A[i]);
    return res;
}

int main(){
    int q, tmp;
    cin >>n;
    for(int i = 0; i < n; i++){
        cin >>A[i];
    }
    cin >>q;
    int m[q];
    for(int i = 0; i < q; i++){
        cin >>tmp;
        if(solve(0, tmp)){
            cout << "yes" << endl;
        }else{
            cout << "no" << endl;
        }
    }


}


Measured memory_bytes: 3088
import java.util.Scanner;
public class Main {
	void run(){
		Scanner s = new Scanner(System.in);
		while(true){
			int x = s.nextInt();
			int y = s.nextInt();
			int sum = s.nextInt();
			int beTax =  x + 100;
			int afTax =  y + 100;
			int max = 0;
			if(x == 0 && y == 0 && sum == 0)break;
			for(int i = 1;i <= Math.floor(sum/2);i++){
				int taxin = (int) Math.floor(i * beTax / 100);
				int t = sum - taxin;
				int taxin2 = 0;
				int j = 1;
				while(true){
					taxin2 = (int) Math.floor(j * beTax / 100);
					if(taxin2 == t)break;
					if(taxin2 > t){
						taxin2 = -1;
						break;
					}
					j++;
				}
				if(taxin2 < 0)continue;
				int afSum = (int) Math.floor(i * afTax / 100) + (int) Math.floor(j * afTax / 100);
				if(max < afSum)max = afSum;
			}
			System.out.println(max);
		}
	}
	public static void main(String args[]){
		new Main().run();
	}
}

Measured memory_bytes: 29132
def coin(larger=False, single=False):
    """
    ナップサックに入れる重さが W 丁度になる時の価値の最小値
    :param single: True = 重複なし

    dp[weight <= W+1] = 重さを固定した時の最小価値
    dp[W+1] = 重さがWより大きい時の最小価値
    """
    W2 = W + 1              # dp[W+1] に W より大きい時の全ての場合の情報を持たせる
    dp_max = float("inf")   # 総和価値の最大値
    dp = [dp_max] * (W2 + 1)
    dp[0] = 0               # 重さ 0 の時は価値 0

    for item in range(N):
        if single:
            S = range(W2, weight_list[item] - 1, -1)
        else:
            S = range(W2)
        for weight in S:
            dp[min2(weight + weight_list[item], W2)] = min2(dp[min2(weight + weight_list[item], W2)], dp[weight] + cost_list[item])
    if larger:
        return min(dp[w] for w in range(W, W2+1))
    else:
        return dp[W]

#######################################################################################################
import sys
input = sys.stdin.readline


def max2(x, y):
    return x if x > y else y

def min2(x, y):
    return x if x < y else y

W, N = map(int, input().split())                                # N: 品物の種類 W: 重量制限

# cost_list = []
# weight_list = []
# for _ in range(N):
#     """ cost と weight が逆転して入力されている場合有り """
#     weight, cost = map(int, input().split())
#     cost_list.append(cost)
#     weight_list.append(weight)

cost_list = [1]*N
weight_list = list(map(int, input().split()))

print(coin(single=False))


############################################################################################


Measured memory_bytes: 7544
#include <stdio.h>
#include <string.h>
char in[100001];
int m[100001][2];
int main()
{
  int n,k,i,j,t;
  char c;
  for(;;){
    scanf("%d%d",&n,&k);
    if(n == 0 && k == 0) break;
    memset(in,-1,sizeof(in));
    memset(m,0,sizeof(m));
    for(i=0;i<k;i++){
      scanf("%d",&j);
      in[j] = 1;
    }
    c = in[1]; j=1; t=1;
    for(i=2;i<=n;i++){
      if(in[i] == c) j++;
      else{
	m[t][0] = c;
	m[t++][1] = j;
	c = in[i];
	j = 1;
      }
    }
    m[t][0] = c;
    m[t++][1] = j;
    j = 0;
    if(in[0] == 1){
      for(i=1;i<t;i++){
	if(m[i][0] == -1 && m[i][1] == 1 && j <= (m[i-1][1] + m[i+1][1]))
	  j = m[i-1][1] + m[i+1][1] + 1;
      }
    }else{
      for(i=1;i<t;i++){
	if(m[i][0] == 1 && j < m[i][1]) {
	  j = m[i][1];
	}
      }
    }
    printf("%d\n",j);
  }
  return 0;
}

Measured memory_bytes: 0
#include <stdio.h>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <list>
#include <iterator>
#include <assert.h>
#pragma warning(disable:4996) 

typedef long long ll;
#define MIN(a, b) ((a)>(b)? (b): (a))
#define MAX(a, b) ((a)<(b)? (b): (a))
#define LINF 9223300000000000000
#define INF 2140000000
const long long MOD = 1000000007;
using namespace std;

int main(int argc, char* argv[])
{
    int n, K;
    scanf("%d%d", &n, &K);

    vector<int> a(n);
    ll sum=0;
    const double eps=1e-6;
    int i;
    for(i=0; i<n; i++) {
        scanf("%d", &a[i]);
        sum+=a[i];
    }

    vector<ll> z;
    for(i=1; i<sqrt((double)sum)+eps; i++) {
        if(sum%i==0) {
            z.push_back(i);
            if(sum/i>i) z.push_back(sum/i);
        }
    }
    sort(z.rbegin(), z.rend());

    int siz=(int)z.size();
    for(i=0; i<siz; i++) {
        vector<int> b;
        int k;
        ll sum0=0;
        for(k=0; k<n; k++) {
            int tmp=a[k]%z[i];
            if(tmp) b.push_back(tmp);
            sum0+=tmp;
        }
        sort(b.begin(), b.end());

        int siz0=(int)b.size();
        ll sumtmp=0;
        for(k=0; k<siz0-sum0/z[i]; k++) {
            sumtmp+=b[k];
        }
        if(sumtmp<=K) {
            printf("%lld\n", z[i]);
            return 0;
        }
    }


    return 0;
}


Measured memory_bytes: 256
import std.stdio;
import std.ascii;
import std.range;
import std.array;
import std.functional;
import std.algorithm;
import std.conv;
import std.container;
import std.math;
import std.numeric;
import std.string;
import std.c.string;
import std.random;
import std.regex;
import std.typecons;

/* 重みつきUnionFind木 */
class UnionFindW {
    alias long INT;
    static const INT INF = long.max / 4;
    struct Pair {
        int index;
        INT value;
    }
    int[] parent;
    INT[] value;
    this(int N) {
        parent = new int[N + 1]; parent[] = -1;
        value = new INT[N + 1];  value[] = 0;
    }
    /* 
     * root :: インデックス -> (ルートのインデックス, ルートとの距離)
     */
    Pair root(int x) {
        if (parent[x] == -1) return Pair(x, 0);
        int p = parent[x];
        auto v = value[x];
        auto proot = root(p);
        parent[x] = proot.index;
        value[x] = v + proot.value;
        return Pair(parent[x], value[x]);
    }
    /* 
     * query :: (インデックスx, インデックスy) -> yとxの差
     */
    INT query(int x, int y) {
        auto xroot = root(x),
             yroot = root(y);
        if (xroot.index != yroot.index) return INF;
        return yroot.value - xroot.value;
    }
    /*
     * merge :: (インデックスx, インデックスy, yとxの差w) -> ()
     *
     * xとyがすでに親子関係を持っていて,その差がwでなければ異常終了
     */
    void merge(int x, int y, INT w) {
        auto xroot = root(x),
             yroot = root(y);
        if (xroot.index == yroot.index) {
            assert(query(x, y) == w);
            return;
        }
        auto px = xroot.index, py = yroot.index;
        auto dx = xroot.value, dy = yroot.value;
        /* value[y] - value[x] = w
         * value[x] - value[px] = dx
         * value[y] - value[py] = dy
         * value[py] - value[px] = (value[y] - dy) - (value[x] - dx)
         *                       = (value[y] - value[x]) - (dy - dx)
         *                       = w - dy + dx
         */
        parent[py] = px;
        value[py] = w - dy + dx;
    }
}

void main() {
    int N, M;
    UnionFindW uf;

    bool solve() {
        scanf("%d %d\n", &N, &M);
        if (N == 0 && M == 0) return false;
        uf = new UnionFindW(N);
        foreach (i; 0 .. M) {
            string s; readf("%s ", &s);
            int a, b, w;
            if (i % 1000 == 0) {
                foreach (int j; 0 .. N) uf.root(j);
            }
            if (s == "!") {
                scanf("%d %d %d\n", &a, &b, &w);
                a--; b--;
                uf.merge(a, b, w);
            } else {
                scanf("%d %d\n", &a, &b);
                a--; b--;
                auto ans = uf.query(a, b);
                if (ans == UnionFindW.INF) {
                    writeln("UNKNOWN");
                } else {
                    writeln(ans);
                }
            }
        }
        return true;
    }
    
    while (solve) {}
}

Measured memory_bytes: 6456
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<long long,long long> pll;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)
constexpr int mod=1000000007;
constexpr int mod1=998244353;
vector<int> dx={0,1,0,-1},dy={-1,0,1,0};
bool inside(int y,int x,int h,int w){
	if(y<h && y>=0 && x<w && x>=0) return true;
	return false;
}





int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;cin >> n;
	if(n % 2 == 0) cout << (double)n / 2 / n << endl;
	else cout << (double)(n + 1) / 2 / n << endl;
}


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

using namespace std;
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); ++i)
double const PI = 3.1415926535897932384626433;
using P = pair<int, int>;

int main() {
    int n;
    ll ans = 0;
    cin >> n;
    vector<int> d(n);
    rep(i, n) cin >> d[i];
    sort(d.begin(), d.end());
    ans = d[d.size() / 2] - d[d.size() / 2 - 1];
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3748
#include<bits/stdc++.h>
#define ll long long
#define nl "\n"
#define pi acos(-1)
#define fast ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define pcase printf("Case %d:\n",ca)
int ca=1;
using namespace std;
int main(){

   //  #ifndef HEY_LETS_CODE
   // freopen("Input.txt", "r", stdin);
   // freopen("Output.txt", "w", stdout);
   // #endif
   fast;
   string a;
   cin>>a;
   for(int i = 0;i<a.size();i++){
    cout<<"x";
   }
   cout<<nl;

}



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

#define endl '\n'
#define ll long long
#define re register
#define pb(x) push_back(x)
#define ce(x) cout << x << '\n';

using db = long double;
using pll = pair < ll, ll >;

#define rep(i,x,n,inc) for(i=x ; i<n ; i+=inc)
#define repr(i,x,n,inc) for( i=x ; i>n ; i+=inc)
#define all(a)      (a).begin(),(a).end()
#define unique_sort(x) sort(all(x)), x.resize(distance(x.begin(), unique(all(x))))

#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define hell 1000000007
const db pi = acos(-1);
const long long infl = LLONG_MAX;

#define Foxen(i,s) for (i=s.begin(); i!=s.end(); i++)
#define Fill(s,v) memset(s,v,sizeof(s))
#define cout_p(x, p) cout << fixed << setprecision((p)) << x << endl     //print with precision

#define tc(tt) \
    ll tt;    \
    cin >> tt; \
    for (ll _tt = 0; _tt < tt; _tt++) // testcase 

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

ll i,x,j,y,z,k,n;

string s[2],t;	ll a[2];
// cin>>s[0];
cin>>t;
rep(i,0,t.length(),1)	cout<<'x';
if(t==s[0])	a[0]--;
else a[1]--;
// ce(a[0]<<" "<<a[1])
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define int long long
#define for0(i, n) for(int i = 0; i < (n); i++)
#define for1(i, n) for(int i = 1; i <= (n);i++)
#define mp make_pair
#define all(x) x.begin(),x.end()
#define puts(x) cout << x << "\n"
constexpr int mod = 1000000007;
using namespace std;
int input() { int r; cin >> r; return r; }
int n, m;
int a[2345], b[2345], c[2345], p[1234];
bool b00l[1234];
signed main() {
	cin >> n >> m;
	for1(i, m)cin >> a[i] >> b[i] >> c[i];
	b00l[1] = 1;
	for1(valsqotch, 3 * n + 10) {
		for1(i, m) {
			if (b00l[a[i]]) {
				if (!b00l[b[i]]) {
					b00l[b[i]] = 1;
					p[b[i]] = p[a[i]] + c[i];
				}
				else {
					if (p[b[i]] < p[a[i]] + c[i]) {
						if (b[i] == n && valsqotch > 2 * n) {
							puts("inf"); return 0;
						}
						p[b[i]] = p[a[i]] + c[i];
					}
				}
			}
		}
	}
	puts(p[n]);
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#include<set>
#include<complex>
#include<map>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
#define MAX 9e5
using namespace std;
typedef pair<int, int> pii;
int main(){
    vector<bool>Primecheck(MAX+1,true);
    vector<int>Primenumber(MAX+1,0); //Primearray
    int Primecounter =0;                  //Primearray counter;
    for(int i = 2; i<MAX+1;i++){
        if(Primecheck[i]){
            for(int j = 2;i*j<MAX;j++)
                Primecheck[i*j] = false;//?´???°??????Primearray???????´??????????????????°?????¨???false???
            Primenumber[Primecounter] = i;
            Primecounter++;
        }
    }
    int n,m;
    while(cin>>n>>m,n+m>=0){
        int cnt=0;
        for(int i=0; i<Primenumber.size();i++){
            if(Primenumber[i]>n){cnt=i;break;}
        }
        vi v;
        for(int i=cnt; i<=cnt+m;i++){
            for(int j=i; j<=cnt+m;j++){
                v.push_back(Primenumber[i]+Primenumber[j]);
            }
        }
        sort(all(v));
        cout<<v[m-1]<<endl;
    }

}

Measured memory_bytes: 6632
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)
#define all(x) x.begin(),x.end()

int main() {
  int n,k;
  while(cin >> n >> k, n && k) {

    long long INF = 1e10;
    vector<vector<long long>> dist(n, vector<long long>(n, INF));
    for(int i = 0;i < n;i++) dist[i][i] = 0;

    for(int q = 0;q < k;q++) {
      int Q;
      cin >> Q;
      if(Q == 0) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        if(dist[a][b] < INF) {
          cout << dist[a][b] << endl;
        }
        else {
          cout << -1 << endl;
        }
      }
      else {
        long long c,d,e;
        cin >> c >> d >> e;
        c--;
        d--;
        dist[c][d] = min(dist[c][d], e);
        dist[d][c] = min(dist[d][c], e);

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

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



Measured memory_bytes: 3096
def cmb(a,b,c):
    b = min(b,a-b)
    num = 1
    for i in range(b):
        num = num*(a-i) % c
    den = 1
    for i in range(b):
        den = den*(i+1) % c
    return num * pow(den,c-2,c) % c
mod = 10**9 + 7
n,k = map(int,input().split())
inv = [0,1]
for i in range(2,n):
  inv += [inv[mod % i]*(mod - mod//i) % mod]
a = list(map(int,input().split()))
a.sort()
ans = 0
c = 1
x = 1
if k == 1:
    print(0)
    exit()
for i in range(n-k+1):
    ans -= a[n-k-i] * c
    ans += a[k+i-n-1] * c
    ans %= mod
    c = c*(i+k)*inv[i+1] % mod
if ans < 0:
    ans += mod
print(ans)

Measured memory_bytes: 18444