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

using namespace std;

#define REP(i,n) for(ll (i) = (0);(i) < (n);++i)
#define REV(i,n) for(ll (i) = (n) - 1;(i) >= 0;--i)
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define SHOW1d(v,n) {REP(W,n)cerr << v[W] << ' ';cerr << endl << endl;}
#define SHOW2d(v,i,j) {REP(aaa,i){REP(bbb,j)cerr << v[aaa][bbb] << ' ';cerr << endl;}cerr << endl;}
#define ALL(v) v.begin(),v.end()
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL
#define MOD 1000000007

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

ll x[4];
ll y[4];

int main(){

	cin >> x[0] >> y[0] >> x[1] >> y[1];

	ll u = x[1] - x[0];
	ll v = y[1] - y[0];
	x[2] = x[1] - v;
	y[2] = y[1] + u;
	x[3] = x[2] - u;
	y[3] = y[2] - v;
	cout << x[2] << " " << y[2] << " " << x[3] << " " << y[3] << endl;


	return 0;
}


Measured memory_bytes: 256
#include <math.h>
#include <assert.h>
#include <algorithm>
#include <set>
#include <iostream>
#include <vector>
#include <iomanip>
#include <queue>
#include <map>
#include <string>
#include <cstring>
#include <functional>
#include <stack>
#include <array>
#include <random>
#include <chrono>
#include <climits>
#include <bitset>
using namespace std ;
#define rep(i, a, b) for (int i=a; i<(b); i++)
#define forn(i, a) for (int i=0; i<(a); i++)
#define repd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define ford(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a,x) for (auto& a : x)
#define int long long
#define ii pair<int,int>
#define ar array
#define FAST_IO ios_base::sync_with_stdio(false) ;cin.tie(NULL);cout.tie(NULL)
#define ms(x,a) memset(x,(int)(a),sizeof(x))
#define sz(x) ((int)(x).size())
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert
#define pb push_back
#define endl '\n'
#define ff first
#define ss second
//const int N = 3e5+7 ;
//const int INF = 1e18 ;
//const int MOD = 1e9 + 7 ;

void solve(){
	int n , k , d ;
	cin >> n >> k >> d ;
	if(n<0) n = -n ;
	int tmp = n/d ; // This much move is required atleast
	if(k<tmp){
		cout << n-k*d ;
	}else{
		int r = n%d ;
		int rem = k-tmp ;
		if(rem%2==0) cout << r ;
		else cout << d-r << endl ;
	}
}


int32_t main(){
    FAST_IO ;
    int t = 1 ;
//    cin >> t ;
    while(t--){
        solve() ;
    }
}


Measured memory_bytes: 3624
#coding:utf-8
n = int(input())
num = len(str(n))
tmp = pow(10, num)-1
s = 0
for i in range(num+1):
    if i == num:
        s = sum(list(map(int, str(tmp-9*pow(10, num-1)+(int(str(n)[0])-1)*pow(10, num-1)))))
        break
    elif tmp-pow(10, i) <= n:
        s = sum(list(map(int, str(tmp-pow(10, i)))))
        break
if sum(list(map(int, str(n)))) > s:
    print(sum(list(map(int, str(n)))))
else:
    print(s)

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;
  cin >> N;
  vector<int> stunum(N);
  for(int i = 1; i <= N; i++ ){
    int a;
    cin >> a;
    stunum.at(a-1) = i;
  }
  for(int i = 0; i < N; i++ ){
    cout << stunum.at(i) << " ";
  }
  cout << endl;
}


Measured memory_bytes: 1152
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <utility>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef  long long ll;
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define  MP make_pair
#define  PB push_back
#define inf  1000000007

int main(){
	ll k,a,b;
	cin >> k >> a >> b;
	if(a-b<=0){
		if(k>a){
			cout << -1 << endl;
		}else{
			cout << 1 << endl;
		}
		return 0;
	}
	ll s = 0;
	ll ans = 0;
	ans = (k-a)/(a-b);
	s = (a-b)*ans;
	ans *=2;
	for(ll i=0;i<1000000;i++){
		if(i%2==0){
			ans++;
			s += a;
			if(s>=k){
				cout << ans << endl;
				return 0;
			}
		}else{
			ans++;
			s -= b;
		}
	}
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
using namespace std;
int n,k,arr[100005],dp[100005];
int main()
{
	cin>>n>>k;
	for(int i=0;i<n;i++){
		cin>>arr[i];
	}
	dp[1]=abs(arr[0]-arr[1]);
	for(int i=2;i<n;i++){
		int b1=1000000005;
		for(int j=1;j<=min(i,k);j++){
			b1=min(b1,abs(arr[i]-arr[i-j])+dp[i-j]);
		}
		dp[i]=b1;
	}
	cout<<dp[n-1];
	return 0;
}

Measured memory_bytes: 4416
N = int(input())

def joinnum(N, keta, anscount, aaa):
    if aaa.count('3') > 0 and aaa.count('5') > 0 and aaa.count('7') > 0 and int(aaa) <= N:
        anscount += 1

    if keta == len(str(N)):
        return anscount
    else:
        for i in ['3', '5', '7']:
            anscount = joinnum(N, keta + 1, anscount, i + aaa)

        return anscount

print(joinnum(N, 0, 0, ''))

Measured memory_bytes: 3064
object Main extends App {
  val S = io.StdIn.readLine

  val c1 = S.startsWith("A")
  val c2 = (S.drop(2).dropRight(1)).count(s => s == 'C') == 1
  val c3 = S.filter(s => s != 'A' && s != 'C').mkString.matches("[a-z]+")

  val out = if (c1 && c2 && c3) "AC" else "WA"

  println(out)
}


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

int main(){
    int a,b,c,d;
    cin >> a >>b >>c >>d;
    int area1 = a*b;
    int area2 = c*d;

    int result = area1;
    if (area2 > area1){
        result = area2;
    }

    cout << result << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
template<typename A, typename B> inline bool chmax(A &a, B b) { if (a<b) { a=b; return 1; } return 0; }
template<typename A, typename B> inline bool chmin(A &a, B b) { if (a>b) { a=b; return 1; } return 0; }
typedef long long ll;
typedef pair<int, int> pii;
const ll INF = 1ll<<30;
const ll longINF = 1ll<<60;
const ll MOD = 1000000007;
const bool debug = 0;
//---------------------------------//

int main() {
	int N;
	cin >> N;
	vector<int> P(N);
	REP(i, N) scanf("%d", P.begin() + i);
	
	vector<int> ord(N);
	REP(i, N) ord[i] = i;
	
	sort(ALL(ord), [&](int x, int y){
		return P[x] > P[y];
	});
	
	multiset<int> ss;
	REP(i, 2) {
		ss.insert(-1);
		ss.insert(N);
	}
	
	ll ans = 0;
	for (int i : ord) {
		auto it = ss.lower_bound(i);
		ll r1 = *it;
		ll r2 = *next(it, 1);
		ll l1 = *prev(it, 1);
		ll l2 = *prev(it, 2);
		
		ll pat = (l1 - l2) * (r1 - i) + (i - l1) * (r2 - r1);
		ans += pat * P[i];
		
		ss.insert(i);
	}
	
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 5760
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Tarek
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DIHateFactorization solver = new DIHateFactorization();
        solver.solve(1, in, out);
        out.close();
    }

    static class DIHateFactorization {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long x = in.nextLong();
            for (long i = 0; i < 3000; i++) {
                for (long j = 0; j < 3000; j++) {
                    long a = i * i * i * i * i;
                    long b = j * j * j * j * j;
                    if ((a - b) == x) {
                        out.println(i + " " + j);
                        return;
                    }
                    if ((a + b) == x) {
                        out.println(i + " " + -j);
                        return;
                    }
                }
            }

        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

    }
}



Measured memory_bytes: 36952
x1, y1, x2, y2 = gets.split(' ').map(&:to_i)

x3 = x2 - (y2 - y1)
y3 = y2 + (x2 - x1)
x4 = x3 - (y3 - y2)
y4 = y3 + (x3 - x2)
puts "#{x3} #{y3} #{x4} #{y4}"

Measured memory_bytes: 1788
n = int(input())
a = [int(e) for e in input().split()]
b = [int(e) for e in input().split()]
result = 0
for i in range(n):
  if a[i] > b[i]:
    result += b[i]
    a[i] -= b[i]
    b[i] = 0
  else:
    result += a[i]
    b[i] -= a[i]
    a[i] = 0
    if a[i + 1] > b[i]:
      result += b[i]
      a[i + 1] -= b[i]
      b[i] = 0
    else:
      result += a[i + 1]
      b[i] -= a[i + 1]
      a[i + 1] = 0
print(result)

Measured memory_bytes: 82020
#include<stdio.h>
int main (void){
    int h,i,j,n,t;
    char m,dummy;
    scanf("%d",&n);
    scanf("%c",&dummy);
    int a[4][13]={0,};
    for(i=0;i<n;i++){
        scanf("%c %d",&m,&t);
        scanf("%c",&dummy);
        if(m=='S'){
            a[0][t-1] = 1;
        }else if(m=='H'){
            a[1][t-1] = 1;
        }else if(m=='C'){
            a[2][t-1] = 1;
        }else if(m=='D'){
            a[3][t-1] = 1;
        }
    }
    for(h=0;h<4;h++){
        for(j=0;j<13;j++){
            if(h==0 && a[h][j]!=1){
                printf("S %d\n",j+1);
            }else if(h==1 && a[h][j]!=1){
                printf("H %d\n",j+1);
            }else if(h==2 && a[h][j]!=1){
                printf("C %d\n",j+1);
            }else if(h==3 && a[h][j]!=1){
                printf("D %d\n",j+1);
            }
        }
    }
    return 0;
}


Measured memory_bytes: 2040
s=int(input())
a=set()
a.add(s)
prev=s
for m in range(2,10**6+1):
    #print(a)
    if prev%2:
        if prev*3+1 in a:
            print(m)
            exit()
        else:
            prev=prev*3+1
        a.add(prev)
    else:
        if prev//2 in a:
            print(m)
            exit()
        else:
            prev=prev//2
        a.add(prev)

Measured memory_bytes: 3060
#include <iostream>
using namespace std;
 
int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  int f = 1;
  while(1) {
    if(f) {
    	c-=b;
      	f = 0;
        if(c <= 0) {
        	cout<<"Yes"<<endl;
        	break;
      	}
    } else {
      a-=d;
      f = 1;
      if(a <= 0) {
      	cout<<"No"<<endl;
      	break;
    	}
    }
  }
  return 0;
}

Measured memory_bytes: 3636
#include <iomanip>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> NUM(N, 0);
  rep(i, N-1) {
    int a;
    cin >> a;
    NUM[a-1]++;
  }
  rep(i, N) {
    cout << NUM[i] << endl;
  }
  return 0;
}


Measured memory_bytes: 4052
#include <stdio.h>
#include <ctype.h>

#define MAX_N 2000001
#define MAX_K 10001

char buf[MAX_N];

int getInteger();
void countingSort(int,int*);

int main(){
  int n,i;
  static int A[MAX_N];
  scanf("%d",&n);
  for(i = 0 ; i < n ; i++){
    scanf("%d",A+i);
  }
  countingSort(n,A);
  return 0;
}

void countingSort(int n,int *A){
  int i,C[MAX_K] = {0};
  static int B[MAX_N];
  for(i = 0 ; i < n ; i++){ C[A[i]]++; }
  for(i = 1 ; i < MAX_K ; i++){ C[i] += C[i-1]; }
  for(i = n-1 ; i >= 0 ; i--){
    B[C[A[i]]] = A[i]; C[A[i]]--;
  }
  for(i = 0 ; i < n ; i++){
    printf("%d%c",B[i+1],i != n-1 ? ' ' : '\n');
  }
}

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

int main() {
  int N, sum = 0;
  cin >> N;
  
  for (int i = 0; i < N; i++){
    int a, b;
    cin >> a >> b;
    
    sum += b - a + 1;
  }
  cout << sum << endl;
}

Measured memory_bytes: 256
import bisect
n, k = map(int, input().split())
X = list(map(int, input().split()))

ans = float('inf')

for i in range(n):
    # 左に走る
    if X[i] <= 0:
        if 0 <= i+k-1 < n:
            temp = 0
            temp+= -X[i]*2
            if X[i+k-1] >= 0:
                temp += X[i+k-1]
                ans = min(ans, temp)
        if 0 <= i-k+1 < n:
            ans = min(ans, -X[i-k+1])

    # 右に走る
    else:
        if 0 <= i-k+1 < n:
            temp = 0
            temp+=X[i]*2
            if X[i-k+1] <= 0:
                temp -= X[i-k+1]
                ans  = min(ans, temp)
        if 0 <= i+k-1 < n:
            ans = min(ans, X[i+k-1])

if n == 1:
    ans = abs(X[0])

print(ans)

Measured memory_bytes: 14548
N = int(input())
A_list = list(map(int, input().split()))

R1 = sum(A_list) - 2 * sum(A_list[i] for i in range(1,N,2))

Ri_list = [0] * N

Ri_list[0] = R1

for i in range(1,N):
    # print(A_list[i-1])
    Ri_list[i] = A_list[i-1] * 2 - Ri_list[i-1]

for item in Ri_list:
    print(item, end=' ')

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

typedef pair<int,int> P;
#define ll long long
#define ld long double
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define sz(x) ((int)(x).size())
#define chmin(x,y) x = min(x,y)
#define chmax(x,y) x = max(x,y)
#define all(x) (x).begin(),(x).end()

const int INF = 1e9;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;
int main()
{
    int n,k;cin >> n >> k;
    vector<int> x(n);rep(i,n)cin >> x[i];
    int ans = 0;
    rep(i,n)
    {
        ans += min(2*abs(0-x[i]),2*abs(k-x[i]));
    }
    cout << ans << endl;
}

Measured memory_bytes: 3596
gets
t, a = gets.chomp.split.map(&:to_i).map { |i| i * 1000 }
ans=-1
dif=10**20
cnt=0
for i in gets.chomp.split.map(&:to_i) do
  cnt+=1
  if (a-t+i*6).abs<dif then
    dif=(a-t+i*6).abs
    ans=cnt
  end
end
p ans

Measured memory_bytes: 1788
#include <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

int main(void){

	int n,a,b;
	scanf("%d%d%d",&n,&a,&b);
	printf("%d\n",min(a*n,b));

	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <set>
#include <string>
using namespace std;

int main(){
    int d,n; cin >> d >> n;
    int ans = 0;
    for ( int i = 1 ; i <= 100*100*100*100 ; i++ ){
        int cnt = 0, k = i;
        while ( k > 0 ) {
            if ( k % 100 != 0)break;
            else { cnt++;k /= 100;}
        }
        if ( cnt == d ) ans++;
        if ( ans == n ) {cout << i << endl; return 0;}
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define inc(l,i,r) for (int i=l;i<=r;i++)
#define dec(r,i,l) for (int i=r;i>=l;i--)
#define mid ((l+r)>>1)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define pii pair<int,int>
#define ___ putchar(10);
#define N 200200
using namespace std;
typedef long long ll;

int n, a[N], f[N<<2];
bool laz[N<<2];

inline void push_down(int k) {
	if (laz[k]) {
		laz[k<<1] = laz[k<<1|1] = 1;
		f[k<<1] = f[k<<1|1] = 0;
		laz[k] = 0;
	}
}

void mdf(int l,int r,int k,int ql,int qr) {
	if (ql <= l && r <= qr) {f[k] = 0;  laz[k] = 1;  return ;}
	push_down(k);
	if (mid >= ql) mdf(l,mid,k<<1,ql,qr);
	if (mid < qr) mdf(mid+1,r,k<<1|1,ql,qr);
}
int query(int l,int r,int k,int x) {
	if (l == r) return f[k];
	push_down(k);
	if (x <= mid) return query(l,mid,k<<1,x);  else return query(mid+1,r,k<<1|1,x);
}
void add(int l,int r,int k,int x,int y) {
	if (l == r) return void(f[k] += y);
	push_down(k);
	if (x <= mid) return add(l,mid,k<<1,x,y);  else return add(mid+1,r,k<<1|1,x,y);
}

inline void ADD(int x,int pos) {
	add(1,n,1,pos,1);
	while (pos > 1 && query(1,n,1,pos) == x) {
		add(1,n,1,pos,-x);
		add(1,n,1,--pos,1);
	}
}

inline bool check(int x) {
	mdf(1,n,1,1,1);
	inc(1,i,n) {
		if (a[i] <= a[i-1]) {
			if (a[i] <= n) ADD(x,a[i]);
			if (query(1,n,1,1) >= x) return 0;
		} else if (a[i-1] < n) {
			mdf(1,n,1,a[i-1]+1,a[i]>n ? n : a[i]);
		}
	} return 1;
}

int main() {
	scanf("%d",&n);
	inc(1,i,n) scanf("%d",a+i);
	
	int l = 1, r = n, M;
	while (l != r) {
		M = (l+r) >> 1;
		if (check(M)) r = M;  else l = M+1;
	}
	printf("%d\n",l);
}

Measured memory_bytes: 4608
#include<iostream>
using namespace std;
int main()
{
	int n,t;
	cin>>n>>t;
	int c,ti;
	int res=2000;
	for(int i=0;i<n;i++)
	{
		cin>>c>>ti;
		if(ti<=t&&c<res) res=c;
	}
	if(res!=2000)
		cout<<res<<endl;
	else cout<<"TLE"<<endl;
} 

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
#define all(x) (x).begin(), (x).end()
#define endl '\n'
#define fsp(x) cout << fixed << setprecision(x)
const ll inf = LLONG_MAX;
const long double pi = M_PI;
void Yes() {cout << "Yes" << endl;}
void No() {cout << "No" << endl;}
void YES() {cout << "YES" << endl;}
void NO() {cout << "NO" << endl;}

int main() {
  ll m, d;
  cin >> m >> d;
  ll ans = 0;
  for (ll i = 1; i <= m; i++) {
    for (ll j = 1; j <= d; j++) {
      ll d10 = j / 10, d1 = j % 10;
      if (d10 >= 2 && d1 >= 2 && d10 * d1 == i) {
        ans++;
        //cout << i << " " << j << endl;
      }
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

const int mod=1000000007;
const int INF=1001001001;

vector<pair<int64_t,int64_t>>prime_factorize(int64_t x){
  vector<pair<int64_t,int64_t>>p;
  for(int64_t i=2;i*i<=x;i++){
    int cnt=0;
    if(x%i==0){
    while(x%i==0){cnt++;x/=i;}
    p.push_back(make_pair(i,cnt));
    }
  }
  if(x!=1){p.push_back(make_pair(x,1));}
  return p; 
}

int main() {
  int N,X,T;
  cin>>N>>X>>T;
  cout<<(N+X-1)/X*T<<endl;
  return 0;  
}

Measured memory_bytes: 3596
# _n = gets.to_i
# ps = gets.split.map(&:to_i).reverse
# puts "#{ps}"
#
# count = 0
#
#
# ps.sort do |a,b|
#   puts "#{a<=>b}"
#   if (a<=>b) == -1
#     count += 1
#     if count > 1
#       puts "NO"
#       exit 0
#     end
#   end
#
#   #戻り値は必要でこれ
#   a<=>b
# end
#
# puts "#{ps}"
#
# puts "YES"

_n = gets.to_i
ps = gets.split.map(&:to_i)
count = 0
tntn = 51
pokotin = 0

ps.each_with_index do |i, k|
  for j in k+1..ps.length-1
    if i > ps[j]
      count +=1
      if tntn > ps[j]
        tntn = ps[j]
        pokotin = j
      end
    end

  end

  if count >1
    tmp = ps[k]
    ps[k] = tntn
    ps[pokotin] = tmp
    break
  end
end


if count > 0
  ps.each_with_index do |i, k|
    for j in k+1..ps.length-1
      if i > ps[j]
        puts "NO"
        exit 0
      end
    end
  end
end


puts "YES"

Measured memory_bytes: 1788
#include <iostream>
#include <algorithm>
#include <cmath>
#include <limits>
#include <vector>
#include <cstdio>
#include <bits/stdc++.h>
#include <set>
#include <map>
#include <stdio.h>
#include <stack>
#include <queue>
#include <deque>
#include <numeric>
#include <bits/stdc++.h>
#include <utility>
#include <iomanip>

#define ALL(obj) (obj).begin(), (obj).end()
#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define RFOR(i,a,b) for(int i = (a); (b) <= i; i--)
#define REP(i,n) for(int i = 0; i < (n); i++)
#define RREP(i,n) for(int i = n; n <= i; i--)
#define ABS(a) ((a < 0) ? ((-1)*(a)) : (a))
#define elif else if
#define MOD 1000000007
#define INF (1<<29)

using namespace std;

#define ld long double
#define ll long long

map <int ,int> mpa,mpb;
typedef pair<ll, ll> P;
priority_queue<P, vector<P>, greater<P>> pque;

int main(){
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  ll N,x;
  cin >> N;
  ll ans=0;
  ll A[100100]={0};
  for(int i=1;i<=N;i++){
    cin >> A[i];
  }
  for(int i=1;i<=N;i++){
    if(A[i]%2==1 && A[i+1]>0){
      A[i]--;
      A[i+1]--;
      ans++;
    }
    ans+=A[i]/2;
  }
  cout << ans << endl;
  return 0;
}


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

const int mod = 1e9 + 7;
const int N = 1e5 + 5;
long long dp [N][15];

int main () {
  memset (dp, 0, sizeof(dp));
  string w;
  cin >> w;
  int n = (int) w.length();
  if (w[0] == '?') {
    for (int i = 0; i <= 9; i++) {
      dp[0][i]++;
    }
  } else {
    dp[0][(w[0] - '0') % 13]++;
  }
  for (int i = 1; i < n; i++) {
    if (w[i] != '?') {
      for (int j = 0; j < 13; j++) {
        dp[i][(j * 10 + (w[i] - '0')) % 13] += dp[i - 1][j];
        dp[i][(j * 10 + (w[i] - '0')) % 13] %= mod;
      }
    } else {
      for (int j = 0; j < 13; j++) {
        for (int x = 0; x <= 9; x++) {
          dp[i][(j * 10 + x) % 13] += dp[i - 1][j];
          dp[i][(j * 10 + x) % 13] %= mod;
        }
      }
    }
  }
  cout << dp[n - 1][5] << '\n';
  return 0;
}

Measured memory_bytes: 12160
#include<stdio.h>

int main(void)
{
  int n,a,b,ans=0;
  scanf("%d %d %d",&n,&a,&b);
  if(n*a <= b)
  {
    ans = n*a;
  }
  else
  {
    ans = b;
  }
  printf("%d\n",ans);
  return 0;
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long a, b, c, k;
    cin >> a >> b >> c >> k;
    if (k <= a)
    {
        cout << k << endl;
    }
    else if (k <= a + b)
    {
        cout << a << endl;
    }
    else
    {
        cout << a - (k - a - b) << endl;
    }
}

Measured memory_bytes: 3568
#include <bits/stdc++.h>
using namespace std;
template<typename T = int, T mod = 1'000'000'007, typename U = long long>
struct umod{
	T val;
	umod(): val(0){}
	umod(U x){ x %= mod; if(x < 0) x += mod; val = x;}
	umod& operator += (umod oth){ val += oth.val; if(val >= mod) val -= mod; return *this; }
	umod& operator -= (umod oth){ val -= oth.val; if(val < 0) val += mod; return *this; }
	umod& operator *= (umod oth){ val = ((U)val) * oth.val % mod; return *this; }
	umod& operator /= (umod oth){ return *this *= oth.inverse(); }
	umod& operator ^= (U oth){ return *this = pwr(*this, oth); }
	umod operator + (umod oth) const { return umod(*this) += oth; }
	umod operator - (umod oth) const { return umod(*this) -= oth; }
	umod operator * (umod oth) const { return umod(*this) *= oth; }
	umod operator / (umod oth) const { return umod(*this) /= oth; }
	umod operator ^ (long long oth) const { return umod(*this) ^= oth; }
	bool operator < (umod oth) const { return val < oth.val; }
	bool operator > (umod oth) const { return val > oth.val; }
	bool operator <= (umod oth) const { return val <= oth.val; }
	bool operator >= (umod oth) const { return val >= oth.val; }
	bool operator == (umod oth) const { return val == oth.val; }
	bool operator != (umod oth) const { return val != oth.val; }
	umod pwr(umod a, U b) const { umod r = 1; for(; b; a *= a, b >>= 1) if(b&1) r *= a; return r; }
	umod inverse() const {
		U a = val, b = mod, u = 1, v = 0;
		while(b){
			U t = a/b;
			a -= t * b; swap(a, b);
			u -= t * v; swap(u, v);
		}
		if(u < 0)
			u += mod;
		return u;
	}
};
using U = umod<>;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n; cin >> n;
	vector<vector<int>> to(n);
	vector<int> sz(n);
	for(int i = 0; i < n - 1; i++){
		int x, y; cin >> x >> y; x--; y--;
		to[x].push_back(y);
		to[y].push_back(x);
	}
	U ans = 0;
	function<void(int,int)> dfs = [&](int u, int p){
		sz[u] = 1;
		for(int v : to[u]){
			if(v == p) continue;
			dfs(v, u);
			sz[u] += sz[v];
		}
		vector<int> rec;
		if(n - sz[u]) rec.push_back(n - sz[u]);
		for(int v : to[u]){
			if(v == p) continue;
			rec.push_back(sz[v]);
		}
		U acc = 1;
		for(int s : rec){
			U p = U(2) ^ s; p -= 1;
			ans += acc * p;
			ans -= p;
			acc += acc * p;
		}
	};
	dfs(0, -1);
	cout << (ans / (U(2) ^ n)).val << endl;
	return 0;
}


Measured memory_bytes: 32000
#include <iostream>

using namespace std;

int main(){
  int N, X, l;
  cin >> N >> X;
  int ans = 0;
  int p = 0;
  for (int i=0; i<N; i++){
    ans++;
    cin >> l;
    p += l;
    if (p > X) break;
  }
  if (p <= X) ans++;
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
N = int(input())
grp = [[0]*10 for i in range(10)]
for x in range(N+1):
    grp[int(str(x)[0])][int(str(x)[-1])]+=1
result = 0
for i in range(1,10):
    for j in range(1,10):
        result += grp[i][j]*grp[j][i]
print(result)

Measured memory_bytes: 3064
N = int(input())
ab = [list(map(int, input().split())) for _ in range(N)]
cd = [list(map(int, input().split())) for _ in range(N)]

ab = sorted(ab, key= lambda x: [x[0], x[1]])
cd = sorted(cd, key= lambda x: [x[0], x[1]])

kouho = []
ans = 0
temp = 0

used = [False for _ in range(N)]

for i, j in cd:
    for w, kl in enumerate(ab):
        k, l = kl
        if i < k or j < l or used[w] == True:
            continue
        else:
            kouho.append([l, w])
    if len(kouho) != 0:
        kouho = sorted(kouho, key= lambda x: x[0], reverse=True)
        ans += 1
        used[kouho[0][1]] = True
        kouho = []

print(ans)

Measured memory_bytes: 3188
#!/usr/bin/env python3
import sys

# https://en.wikipedia.org/wiki/Cycle_detection
def floyd_cycle_finding(f, x0):
    '''
    循環していない部分の長さをlam
    循環部分の長さをmuとして
    (lam, mu)
    を返す
    >>> floyd_cycle_finding(lambda x: x**2 % 3, 2)
    (1, 1)
    >>> floyd_cycle_finding(lambda x: x**2 % 1001, 2)
    (2, 4)
    >>> floyd_cycle_finding(lambda x: x**2 % 16, 2)
    (2, 1)
    >>> floyd_cycle_finding(lambda x: x*2 % 5, 3)
    (0, 4)
    >>> floyd_cycle_finding(lambda x: x*2 % 5, 0)
    (0, 1)
    '''
    # a_i = a_j
    # の関係が成り立つ時
    # a_{lam+n+k*mu} = a_{lam+n+k'*mu}
    # より|i-j|は循環の長さの整数倍になる
    # そこで
    # a_m = a_2m
    # なるmを見つける
    # 前述の性質からmは循環の長さの整数倍となっているため
    # 2m - m = m = k*mu = lam + n + k'*mu
    # したがって、lam + nはmuの倍数となるので
    # 地点mからlamステップ進むと循環の開始点に到達する
    # lamが求まれば後は流れでmuも求まる
    
    tortoise = f(x0)
    hare = f(f(x0))
    while tortoise != hare:
        tortoise = f(tortoise)
        hare = f(f(hare))
    lam = 0
    tortoise = x0
    while tortoise != hare:
        tortoise = f(tortoise)
        hare = f(hare)
        lam += 1
    mu = 1
    hare = f(tortoise)
    while tortoise != hare:
        hare = f(hare)
        mu += 1
    return lam, mu

def solve(N: int, X: int, M: int):
    f = lambda x: x**2 % M

    def g(x):
        while True:
            yield x
            x = f(x)
            
    lam, mu = floyd_cycle_finding(f, X)
    ans = 0
    gg = g(X)
    k = max((N - lam) // mu, 0)
    for i in range(min(N, lam)):
        ans += next(gg)
    for i in range(mu):
        ans += k * next(gg)
    for i in range((N-lam)%mu):
        ans += next(gg)
    return ans

# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    X = int(next(tokens))  # type: int
    M = int(next(tokens))  # type: int
    print(solve(N, X, M))


def test():
    import doctest
    doctest.testmod()

if __name__ == '__main__':
    test()
    main()


Measured memory_bytes: 17520
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repd(a,b,c) for(int a=c;a>=b;a--)
/*******************************/
#define pi pair<int,int> 
#define vi vector<int>
#define vb vector<bool>
#define vp vector<pi>
#define vvi vector<vi>
#define vvp vector<vp>
/****************************/
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define pb push_back
//#define mp make_pair
#define pbb pop_back
/****************************************/
const int N = 5e5+100;
const int mod=1e9+7;
const int inf=1e18;
/****************************************/
vvp g;
vb vis;
vi dis,p,pp;
map<pair<pi,int>,int> mp;
int power(int a,int b)
{
  int ans=1;
  while(b)
  {
    if(b&1)ans*=a;
    a*=a;
    b/=2;
  }
  return ans;
}
// void dijkstra(int s,int n)
// {
//   dis.assign(n+1,inf);
//   p.assign(n+1,-1);
//   dis[s]=0;
//  // vis[s]=true;
//   priority_queue<pi,vp,greater<pi>> pq;
//   pq.push({dis[s],s});
//   while(!pq.empty())
//   {
//     int u = pq.top().S;
//     pq.pop();
//     if(vis[u])continue;
//     vis[u]=true;
//     int par=dis[u];
//     for(auto it:g[u])
//     {
//       int to = it.F;
//       int len = it.S;
//       if((mp[{{p[u],u},to}]))continue;
//       if(par+len<dis[to])
//       {
//         dis[to]=par+len;
//         p[to]=u;
//         pq.push({dis[to],to});
//       }
//     }
//   }

// }
int add(int a, int b){return (a%mod + b%mod + mod)%mod;}
int sub(int a, int b){return (a%mod - b%mod + mod)%mod;}
int mul(int a, int b){return ((a%mod)*(b%mod) + mod)%mod;}
signed main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int T=1;
    //cin>>T;
    while(T--)
    {
          int n;
          cin>>n;
          bool ok=false;
          while(n)
          {
            int rem=n%10;
            if(rem==7)
            {
              ok=true;
              break;
            }
            n/=10;
          }
          if(ok)cout<<"Yes\n";
          else cout<<"No\n";




    }
  
    
    
               

                

     

        
        
      
      
      
    
    

    
    
}

Measured memory_bytes: 3652
#include<bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define ll long long
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for(ll i = 0; i < n; i++)
const ll INF = 1LL << 60;

int main(){
    int X; cin >> X;
    int ans = 0;
    if(X >= 1800)ans = 1;
    else if(X >= 1600) ans = 2;
    else if(X >= 1400) ans = 3;
    else if(X >= 1200) ans = 4;
    else if(X >= 1000) ans = 5;
    else if(X >= 800) ans = 6;
    else if(X >= 600) ans = 7;
    else if(X >= 400) ans = 8;
    cout << ans << endl;
} 

Measured memory_bytes: 3632
#include<iostream>
#include<stdio.h>
#include<algorithm>
using namespace std;

int main() {
    int n, x, cnt;
    while(true) {
        cnt = 0;
        cin >> n >> x;
        if(n==0 && x==0) break;
        for(int i = 1; i < n-1; ++i) {
            for(int j = i+1; j < n; ++j) {
                for(int k = j+1; k < n+1; ++k) {
                    if(i+j+k == x) {
                        ++cnt;
                    }
                }
            }
        }
        cout << cnt << endl;
    }
    return 0;
}


Measured memory_bytes: 3096
#include<algorithm>
#include<iostream>
#include<cstdio>
#define MN 100000
#define N 262144
using namespace std;
inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
long long f[MN+5],ans=1e18,tot=0; 
int n,m,a[MN+5],b[MN+5],T[N*2+5],S[MN*2+5],num=0,L[MN+5],R[MN+5];
#define M(x) ((x%m+m)%m)
inline int Query(int x)
{
	int res(1e9);
	for(x+=N;x;x>>=1) res=min(res,T[x]);
	return res;	
}
void Modify(int l,int r,int v)
{
	if(l>r) return; 
	for(l+=N-1,r+=N+1;l^r^1;l>>=1,r>>=1)
	{
		if(~l&1) T[l+1]=v;
		if( r&1) T[r-1]=v;
	}
}
int main()
{
	n=read();m=read();
	for(int i=1;i<=n;++i) 
	{
		int x=read();tot+=x;b[i]=read();
		if(b[i]==1&&x*2>m) return 0*puts("-1");
		a[i]=(a[i-1]+x)%m;
	}
	S[num=1]=m-1;
	for(int i=1;i<=N*2+1;++i) T[i]=int(1e9);
	for(int i=1;i<=n;++i) 
		S[++num]=L[i]=M(-2*a[i]),S[++num]=R[i]=M(-2*a[i-1]);
	sort(S+1,S+num+1);num=unique(S+1,S+num+1)-S-1;
	for(int i=1;i<=n;++i) 
		L[i]=lower_bound(S+1,S+num+1,L[i])-S,
		R[i]=lower_bound(S+1,S+num+1,R[i])-S;
	for(int i=n;i;--i)
	{
		int x=Query(R[i]);
	//	cout<<i<<" "<<x<<endl;
		f[i]=x>n?0:f[x]+M(S[R[x]]-S[R[i]]);
		if(b[i]==1)
		{
			if(L[i]<R[i]) Modify(L[i]+1,R[i]-1,i);
			else Modify(L[i]+1,num,i),Modify(1,R[i]-1,i);
		}
	}
	for(int i=1;i<=num;++i) 
	{
		int x=Query(i);
	//	cout<<i<<" "<<x<<endl;
		ans=min(ans,x>n?0:f[x]+M(S[R[x]]-S[i]));
	}
	printf("%lld\n",ans+2*tot);
	return 0;
}



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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

/////////////////////////////////////////////////////////////
ll mypow_core(ll x, int n, ll result) {
  if (n == 0) {
    return result;
  }

  if (n % 2 == 0) {
    return mypow_core(x * x, n / 2, result);
  }
  else {
    return mypow_core(x, n - 1, x * result);
  }
}

ll mypow(ll x, int n) {
  return mypow_core(x, n, 1);
}
/////////////////////////////////////////////////////////////

ll n;

ll ans;

bool check(ll next, ll target) {
  return (n % next <= target);
}

ll search(ll ok, ll ng, ll target) {
  while (abs(ok - ng) > 1) {
    ll next = (ok + ng) / 2;
    if (check(next, target)) {
      ok = next;
    }
    else {
      ng = next;
    }
  }

  if (n % ok == target) {
    return ok;
  }
  else {
    return -1LL;
  }
}

int main(void) {
  // Here your code !
  scanf("%lld", &n);

  // for (ll i = 1; i <= n; ++i) {
  //   printf("%lld: %lld - %lld\n", i, n % i, n / i);
  // }

  // TODO: cur_mod == n

  set<ll> done;

  ans = 0;

  for (ll cur_div = 1; cur_div * cur_div <= n + 1; ++cur_div) {
    if (done.find(cur_div) != done.end()) continue;

    ll start = n / cur_div;
    ll goal = n / (cur_div + 1);

    ll tmp = search(start, goal, cur_div);
    ans += tmp < 0 ? 0 : tmp;

    done.insert(cur_div);

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

    ll opposite_div = n / cur_div;

    if (done.find(opposite_div) != done.end()) continue;

    start = n / opposite_div;
    goal = n / (opposite_div + 1);

    tmp = search(start, goal, opposite_div);
    ans += tmp < 0 ? 0 : tmp;

    done.insert(opposite_div);
  }

  printf("%lld\n", ans);

  return 0;
}


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

int main() {
    int N, A;
    cin >> N >> A;
    vector<int> x(N);
    for (int i = 0; i < N; i++) {
        cin >> x[i];
    }

    int sum = 50 * 50;

    vector<vector<vector<long long>>> dp(N + 1, vector<vector<long long>>(N + 1, vector<long long>(sum + *max_element(x.begin(), x.end()) + 1, 0)));

    dp[0][0][0] = 1;
    for (int i = 1; i <= N; i++) {
        for (int j = 0; j <= i; j++) {
            for (int k = 0; k <= sum; k++) {
                dp[i][j][k] = dp[i - 1][j][k];
                if (i >= 1 && j >= 1 && k >= x[i - 1]) {
                    dp[i][j][k] += dp[i - 1][j - 1][k - x[i - 1]];
                }
            }
        }
    }

    long long ans = 0;
    for (int j = 1; j <= N; j++) {
        //printf("dp[%d][%d][%d] = %lld\n", N, j, j * A, dp[N][j][j * A]);
        ans += dp[N][j][j * A];
    }
    cout << ans << endl;
}

Measured memory_bytes: 53248
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <array>
#include <cmath>

using namespace std;

using ll = long long int;

// GCDを計算します
template <typename NumType>
NumType calcGCD(NumType a, NumType b) {
    if (a < b) {
        NumType tmp = a;
        a = b;
        b = tmp;
    }

    NumType r = a % b;
    while (r != 0) {
        a = b;
        b = r;
        r = a % b;
    }

    return b;
}

// 約数を数えます
template <typename NumType>
NumType cntDivisor(const NumType & num) {
    NumType cnt = 0;
    for(NumType i = 1; i * i <= num; ++i) {
        if(num % i == 0) {
            cnt++;
            if(i * i != num) cnt++;
        }
    }

    return cnt;
}

// 約数を返す
template <typename NumType>
std::vector<NumType> divisor(NumType num) {
    std::vector<NumType> ans;

    ans.push_back(1);

    for(NumType i = 2; i * i <= num; ) {
        if (num % i == 0) {
            if (ans.back() != i) {
                ans.push_back(i);
            }
            num /= i;
        } else {
            i++;
        }
    }

    if (ans.back() != num)
        ans.push_back(num);

    return ans;
}

// 約数を返す
template <typename NumType>
std::vector<NumType> common_divisor(NumType a, NumType b) {
    std::vector<NumType> ans;

    NumType min = (a <= b) ? a : b;
    NumType max = (a > b) ? a : b;

    for (NumType i = 1; i*2 <= min; i++) {
        if (a % i == 0 && b % i == 0)
            ans.push_back(i);
    }

    if (max % min == 0) ans.push_back(min);

    return ans;
}


// 共通部分を返す
template <typename NumType>
std::vector<NumType> get_common(const std::vector<NumType> &a, const std::vector<NumType> &b) {
    std::vector<NumType> ans;

    const auto target = (a.size() <= b.size()) ? a : b;
    const auto compare = (a.size() > b.size()) ? a : b;

    auto tgt_it = target.cbegin();
    auto cmp_it = compare.cbegin();

    while (tgt_it != target.cend()) {
        for (; *cmp_it < *tgt_it && cmp_it != compare.cend(); cmp_it++);
        if (cmp_it == compare.cend()) break;

        if (*tgt_it == *cmp_it) ans.push_back(*tgt_it);
        cmp_it++, tgt_it++;
    }

    return ans;
}

// [beg, end)の範囲を，添え字indexでループするfor文に置換します
#define FOR_RANGE(index, beg, end)     for (ll index = beg; index < N; ++index)

#define DEBUG(x)     std::cout << #x << " : " << x << "\n"

template<class T>
T min(T a, T b) {
    return (a < b) ? a : b;
}

template<class T>
T max(T a, T b) {
    return (a > b) ? a : b;
}



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

    auto a_div = divisor(A);
    // for (auto n : a_div) {
    //     cout << n << " ";
    // }
    // cout << "\n";

    auto b_div = divisor(B);
    // for (auto n : a_div) {
    //     cout << n << " ";
    // }
    // cout << "\n";

    // auto common = get_common(a_div, b_div);
    // for (auto n : common) {
    //     cout << n << " ";
    // }
    // cout << "\n";

    int ans = 0;
    for (size_t i = 0; i < a_div.size(); i++) {
        for (size_t j = 0; j < b_div.size(); j++) {
            if (a_div[i] == b_div[j]) ans++;
        }
    }


    cout << ans;

    return 0;
}



Measured memory_bytes: 256
import java.util.*;class Main{public static void main(String[]z){Scanner s=new Scanner(System.in);for(int n,a[],i,p,r;(n=p=s.nextInt())>0;){for(a=new int[n],i=0;i<n;)a[i++]=s.nextInt();String x="31";for(--p,r=31;r>1;)x+="\n"+(r-a[p=(p+1)%n])+"\n"+(r-=5);System.out.println(x+"\n0");}}}

Measured memory_bytes: 12000
#include<bits/stdc++.h>
#include<ext/pb_ds/tree_policy.hpp>
using namespace std;
#define mod 1000000007
#define all(v) v.begin(),v.end()
#define rep(i,a,b) for(i=(int)a;i<(int)b;i++)
#define revrep(i,a,b) for(i=(int)a;i>=(int)b;i--)
#define ii pair<int,int>
#define pb push_back
#define f first
#define s second
#define ll long long int
#define vi vector<int>
#define rs resize
int main()
{  std::ios::sync_with_stdio(false);
   int n,m,i;
   cin>>n>>m;
   vector<string> v(n);
   rep(i,0,n)
     cin>>v[i];
   sort(all(v));
   rep(i,0,n)
    cout<<v[i];
   return 0;
}


Measured memory_bytes: 256
fun main(args: Array<String>) {
    val (N, M) = readLine()!!.split(" ").map { it.toInt() }
    val list = arrayListOf<List<Int>>()
    for (i in 1..N) list.add(readLine()!!.split(" ").map { it.toInt() }.filterIndexed { index, i -> index != 0 })

    var result = 0
    for (j in 1..M) {
        if (list.all { it.contains(j) }) result++
    }
    println(result)
}

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

const int maxn = 100010;
const int INF = 1<<30;

vector<int> g[maxn];
int lb[maxn], ub[maxn];
int father[maxn], color[maxn];
int n;
vector<int> odd,even;

void link(int u, int v) { g[u].push_back(v); g[v].push_back(u); }

void dfs(int u, int p)
{
    for (auto v : g[u])
    {
        if (v == p) continue;
        father[v] = u;
        color[v] = 1 - color[u];
        dfs(v,u);
        lb[u] = max(lb[u],lb[v]-1);
        ub[u] = min(ub[u],ub[v]+1);
    }
}

void dfs2(int u, int p)
{
    for (auto v : g[u])
    {
        if (v == p) continue;
        lb[v] = max(lb[v], lb[u]-1);
        ub[v] = min(ub[v], ub[u]+1);
        dfs2(v,u);
    }
}

bool check()
{
    for (int i=0;i+1<even.size();i++)
    {
        if (color[even[i]] != color[even[i+1]]) return false;
    }
    for (int i=0;i+1<odd.size();i++)
    {
        if (color[odd[i]] != color[odd[i+1]]) return false;
    }
    if (even.empty() || odd.empty()) return true;
    return color[even[0]] != color[odd[0]];
}

int main()
{
    //freopen("F.in","r",stdin);
    scanf("%d",&n);
    for (int i=1;i<n;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        link(u,v);
    }
    for (int i=1;i<=n;i++) lb[i] = -INF, ub[i] = INF;
    int k;
    scanf("%d",&k);
    for (int i=1;i<=k;i++)
    {
        int v,p;
        scanf("%d%d",&v,&p);
        if (p % 2 == 0) even.push_back(v);
        else odd.push_back(v);
        lb[v] = p;
        ub[v] = p;
    }

    int root = 1;
    dfs(root,0);

    if (!check()) { puts("No"); return 0; }

    dfs2(root,0);

    for (int i=1;i<=n;i++)
    {
        if (ub[i] < lb[i]) { puts("No"); return 0; }
    }
    puts("Yes");
    for (int i=1;i<=n;i++) printf("%d\n",lb[i]);
    return 0;
}

Measured memory_bytes: 14336
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author anand.oza
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DHandstand solver = new DHandstand();
        solver.solve(1, in, out);
        out.close();
    }

    static class DHandstand {
        public void solve(int testNumber, InputReader in, PrintWriter out) {
            int n = in.nextInt(), k = in.nextInt();
            int[] s = new int[n];
            String input = in.next();
            for (int i = 0; i < n; i++) {
                s[i] = input.charAt(i) == '0' ? -1 : 1;
            }

            List<Integer> runs = new ArrayList<>();
            int curSize = 1;
            for (int i = 1; i < n; i++) {
                if (s[i] != s[i - 1]) {
                    runs.add(curSize * s[i - 1]);
                    curSize = 0;
                }
                curSize++;
            }
            runs.add(curSize * s[n - 1]);

            long answer = 0;
            int i = 0, j = 0;
            int flips = 0;
            long length = 0;
            while (i < runs.size()) {
                if (j < runs.size() && (flips < k || runs.get(j) > 0)) {
                    length += Math.abs(runs.get(j));
                    if (runs.get(j) < 0)
                        flips++;
                    j++;
                } else {
                    length -= Math.abs(runs.get(i));
                    if (runs.get(i) < 0)
                        flips--;
                    i++;
                }
                answer = Math.max(answer, length);
            }

            out.println(answer);
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}



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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> ame(n, vector<int>(2));
  for(int i=0;i<n;i++) {
    cin >> ame[i][0];
  }
  for(int i=0;i<n;i++) {
    cin >> ame[i][1];
  }

  int max = 0;
  for(int i=0;i<n;i++) {
    int total = 0;
    for(int j=0;j<=i;j++) {
      total += ame[j][0];
    }
    for(int j=i;j<n;j++) {
      total += ame[j][1];
    }
    if(total > max) max = total;
  }

  cout << max << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<map>
using namespace std;

int read(){
	int x=0,f=1;
	char c=getchar();
	while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
	while(isdigit(c)){x=(x<<3)+(x<<1)+c-48;c=getchar();}
	return x*f;
}

int k,m,n;
char c[100];
int main(){
	scanf("%s",c+1);
	if(c[1]=='S')puts("Cloudy");
	if(c[1]=='C')puts("Rainy");
	if(c[1]=='R')puts("Sunny");
	return 0;
}

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

int main(){
    int N;
    cin >> N;
    int div4 = N / 4;
    int div7 = N / 7;
    for(int i = 0; i <= div7; i++){
        if(N-7*i >= 0){
        if((N-7*i)%4 == 0){
            cout << "Yes" << endl;
            return 0;
        } 
        }

    }
    cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 256
function main()
	n = parse.(split(readline()))
	i = 1
	d = 1
	if n[2]/n[1] != 1
		if n[1] == 1
			d = n[2]
		else
			while i <= sqrt(n[2]) && i <= n[2]/n[1]
				i += 1
				if n[2] % i == 0
					p = convert(Int, n[2] / i)
					if p <= n[2]/n[1] && d <= p
						d = p
					elseif d <= i
						d = i
					end
				end
			end
		end
	end
	print(d)
end


main()


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

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

static const int INF = 1E8;
struct Edge{int A, B, D, E;};
int N, M, L;
int A, B, D, E;
vector<Edge> g[111];
int d[111][111];
vector<int> tmp(3);

int main()
{
  while(cin >> N >> M >> L, N){
    rep(i, 111) g[i].clear();
    rep(i, M){
      cin >> A >> B >> D >> E;
      g[A - 1].push_back((Edge){A - 1, B - 1, D, E});
      g[B - 1].push_back((Edge){B - 1, A - 1, D, E});
    }

    rep(i, 111) rep(j, 111) d[i][j] = INF;
    d[0][0] = 0;
    priority_queue<vector<int>, vector<vector<int> >, greater<vector<int> > > que;
    tmp[0] = 0,  tmp[1] = 0,  tmp[2] = 0;
    que.push(tmp);
    while(!que.empty()){
      vector<int> top = que.top();  que.pop();
      int cnt = top[0];
      int cost = top[1];
      int h = top[2];
      //if(d[h][cost] < cnt) continue;
      rep(i, g[h].size()){
        Edge e = g[h][i];
        tmp[2] = e.B;
        if(cost <= L && d[e.B][cost] > cnt + e.E) {
          d[e.B][cost] = cnt + e.E;
          tmp[0] = cnt + e.E;
          tmp[1] = cost;
          que.push(tmp);
        }
        if(cost + e.D <= L && d[e.B][cost + e.D] > cnt){
          d[e.B][cost + e.D] = cnt;
          tmp[0] = cnt;
          tmp[1] = cost + e.D;
          que.push(tmp);
        }
      }
    }
    int res = INF;
    rep(i, 111) res = min(res, d[N - 1][i]);
    printf("%d\n", res);
  }

  return 0;
}

Measured memory_bytes: 2604
#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 9999999
using namespace std;
typedef pair<int, int> pii;
int main(){
    int n,m;
    while(cin>>n>>m,n+m){
        m-=n;
        int q=0,w=0,r=0;
        q=m/1000;
        w=(m-q*1000)/500;
        r=(m-q*1000-w*500)/100;
        cout<<r<<" "<<w<<" "<<q<<endl;
    }
}

Measured memory_bytes: 2988
package main

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

var scanner = bufio.NewScanner(os.Stdin)

func nextString() string {
	scanner.Scan()
	return scanner.Text()
}

func nextInt() int {
	n, err := strconv.Atoi(nextString())
	if err != nil {
		fmt.Printf("strconv.Atoi failed: %v\n", err)
	}
	return n
}

func main() {
	scanner.Split(bufio.ScanWords)

	prime := 1000000000 + 7

	nBalls := nextInt()
	nBoxes := nextInt()

	nComb := 1
	for iBall := 0; iBall < nBalls; iBall++ {
		nComb = (nComb * nBoxes) % prime
	}
	fmt.Println(nComb)
}



Measured memory_bytes: 1092