akhauriyash/Code-Regression huggingface dataset:
def main():
    N = int(input())
    AN = list(map(int, input().split()))
    BN = list(map(int, input().split()))

    res = 0

    for i in range(N - 1, -1, -1):
        res += min(AN[i + 1], BN[i])
        BN[i] = max(0, BN[i] - AN[i + 1])

        res += min(BN[i], AN[i])
        AN[i] = max(0, AN[i] - BN[i])

    print(res)


if __name__ == '__main__':
    main()


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

int main(){
  int n, m, x;
  cin >> n >> m >> x;
  vi c(n);
  vector<vector<int>> a(n, vector<int>(m));
  rep(i,n){
    cin >> c[i];
    rep(j,m) cin >> a[i][j];
  }
  ll ans = 1000000000000000000;
  rep(is,1<<n){
    ll money=0;
    vi total(m,0);
    rep(i,n){
      if(is>>i&1){
        rep(j,m) total[j] += a[i][j];
        money += c[i];
      }
    }
    sort(total.begin(),total.end());
    //cout << total[0] << endl;
    if(total[0] >= x) ans = min(ans, money);
  }
  if(ans == 1000000000000000000) cout << -1 << endl;
  else cout << ans << endl;
}


Measured memory_bytes: 3512
#include <bits/stdc++.h>
using namespace std;
struct Fast {
    Fast() {
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout << fixed << setprecision(20);
    }
} fast;
template <typename A, size_t N, typename T> void Fill(A (&array)[N], const T& val) {
    std::fill((T*)array, (T*)(array + N), val);
}
long long mod = 998244353;
#define REP(i, n) for (long long i = 0; i < (n); i++)

long long modpow(long long m, long long n) {
    if (n == 0) return 1;
    if (n % 2 == 0) {
        long long t = modpow(m, n / 2);
        return t * t;
    } else {
        return modpow(m, n - 1) * m;
    }
}

long long moddiv(long long a, long long b) { return (a * modpow(b, mod - 2)) % mod; }

long long nCkmod(long long n, long long k) {
    long long denomi = 1;
    REP(i, n) {
        denomi *= (i + 1);
        denomi %= mod;
    }
    long long numera = 1;
    for (int i = n - k; i > 0; i--) {
        numera *= i;
        numera %= mod;
    }
    REP(i, k) {
        numera *= (i + 1);
        numera %= mod;
    }
    return moddiv(denomi, numera);
}

void print_binary(long long a) {
    for (int i = 31; i >= 0; i--) {
        cout << (a >> i & 1);
    }
    cout << endl;
}
int n, a;
vector<int> x;
long long memo[3000][51][51];
long long dfs(int sum, int t, int r) {
    if (memo[sum][t][r] != -1) {
        return memo[sum][t][r];
    }
    if (r == n) {
        if (sum == a * t) {
            return 1;
        } else {
            return 0;
        }
    }
    memo[sum][t][r] = dfs(sum + x[r], t + 1, r + 1) + dfs(sum, t, r + 1);
    return memo[sum][t][r];
}
signed main() {
    Fill(memo, -1);
    cin >> n >> a;
    x.resize(n);
    REP(i, n) { cin >> x[i]; }
    cout << dfs(0, 0, 0) - 1 << endl;
}

Measured memory_bytes: 61184
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		// standard input
		String s = sc.next();

		// process
		boolean isStrLenEvenNumber = (s.length() % 2 == 0);
		boolean isSameFirstAndLast = (s.charAt(0) == (s.charAt(s.length() - 1)));

		// standard output
		System.out.println((isStrLenEvenNumber && isSameFirstAndLast) || (!isStrLenEvenNumber && !isSameFirstAndLast)? 
				"First": "Second");
	}
}


Measured memory_bytes: 13492
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;

#define N 53
class uf{
  private:
    int par[N]={},r[N]={},size[N]={};
  
  public:
    uf(int n){
      int i;
      for (i=0;i<n;i++){
        par[i]=i;
        size[i]=1;
      }
    }
    
    int root(int a){
      if (par[a]==a) return a;
      return par[a]=root(par[a]);
    }
    
    void unite(int a,int b){
      a=root(a);
      b=root(b);
      if (a==b) return;
      if (r[a]>r[b]){
        par[b]=a;
        r[a]++;
        size[a]+=size[b];
      }else{
        par[a]=b;
        r[b]++;
        size[b]+=size[a];
      }
    }
    
    int show_size(int a){
      return size[a];
    }
    
    int group_num(int n){
      int i,t,res=0;
      bool u[N]={false};
      for (i=0;i<n;i++){
        t=root(i);
        if (!u[t]){
          res++;
          u[t]=true;
        }
      }
      return res;
    }
    
};


int main(void){
  int n,i,j,k,l,x[53],y[53],ans=1000,p,q,dx,dy;
  
  cin >> n;
  for (i=0;i<n;i++) cin >> x[i] >> y[i];
  
  if (n==1){
    cout << 1 << endl;
    return 0;
  }
  
  for (i=0;i<n-1;i++) for (j=i+1;j<n;j++){
    uf tguf(n);
    p=x[i]-x[j];
    q=y[i]-y[j];
    
    for (k=0;k<n-1;k++) for (l=k+1;l<n;l++){
      dx=x[k]-x[l];
      dy=y[k]-y[l];
      if ((dx==p && dy==q) || (dx==-p && dy==-q)){
        tguf.unite(k,l);
      }
    }
    
    ans=min(ans,tguf.group_num(n));
    
  }
  
  cout << ans << endl;
  
  return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int N = sc.nextInt();
		int W = sc.nextInt();

		int[] w = new int[N + 10];
		int[] v = new int[N + 10];
		long[][] dp = new long[N + 10][W + 10];

		for (int i = 0; i < N; i++) {
			w[i] = sc.nextInt();
			v[i] = sc.nextInt();
		}

		for (int i = 0; i < N; i++) {
			for (int sum_w = 0; sum_w <= W; sum_w++) {

				if (sum_w - w[i] >= 0) {
					dp[i + 1][sum_w] = Math.max(dp[i + 1][sum_w], dp[i][sum_w - w[i]] + v[i]);
				}

				dp[i + 1][sum_w] = Math.max(dp[i + 1][sum_w], dp[i][sum_w]);

			}
		}

		System.out.println(dp[N][W]);

		sc.close();
	}
}

Measured memory_bytes: 125012
import java.util.*;
class Main{
    static Scanner sr = new Scanner(System.in);

    public static void main(String[] args) throws java.lang.Exception {
        long a = sr.nextLong();
        long b = sr.nextLong();
        long c = sr.nextLong();
        long d = sr.nextLong();
        long ans[]=new long[4];
        ans[0]=a*c;
        ans[1]=a*d;
        ans[2]=b*c;
        ans[3]=b*d;
        Arrays.sort(ans);
        System.out.println(ans[3]);


        }
    }




Measured memory_bytes: 27076
#include <iostream>
#include <algorithm>
#include <string>
#include <cmath>

using namespace std;
typedef long long ll;

#define INF (long long)(10e9+7)
#define REP(i,n) for(ll i = 0; i < n; i++)

int main(void)
{
    int n, a;
    cin >> n >> a;
    cout << (((n%500)<=a)?"Yes":"No") << endl;
}


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

int main()
{
  int H, W, K;
  cin >> H >> W >> K;

  vector<string> c(H, "");
  vector<string> backup(H, "");

  rep(i, H) {
    cin >> c[i];
  }
  backup = c;

  int ans = 0;
  for (int row = 0; row < (1<<H); ++row)
  {
    for (int col = 0; col < (1<<W); col++)
    {
      c = backup;
      // 赤く塗る
      rep(i, H)
      {
        if (row & (1<<i)) { // rowのi番目が1かどうか
          rep(j, W) {
            c[i][j] = 'R';
          }
        }
      }
      rep(i, W) {
        if (col & (1<<i)) { // colのi番目が1かどうか
          rep(j, H) {
            c[j][i] = 'R';
          }
        }
      }

      // 黒をカウント
      int count = 0;
      rep(i, H) {
        rep(j, W) {
          if (c[i][j] == '#') count++;
        }
      }
      if (count == K) ans++;
    }
  }
  cout << ans;
}

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

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define MOD 1000000007
#define el endl
#define INF 1e8
typedef long long ll;
typedef long double ld;

int main() {
    ll n;
    cin >> n;
    ll ans = 0;
    for(ll k = 1; k*k < n; k++){
        if(n % k == 0){
            ll m = (n/k) - 1LL;
            if(n/m == n%m) ans += m;
            if(k > 1){
                m = k - 1;
                if(n / m == n % m) ans += m;
            }
        }
    }
    cout << ans << endl;
}



Measured memory_bytes: 256
#include <array>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
#include <tuple>
using namespace std;


typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}

template <class T>
T clampMax(const T& a, const T& b) { return min(a, b); }

template <class T>
T clampMin(const T& a, const T& b) { return max(a, b); }

template <class T>
bool updateMax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }

template <class T>
bool updateMin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }

#define REP(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define RALL(v) (v).rbegin(),(v).rend()
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define PB push


#define MOD 1000000007LL
#define EPS 1e-10

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

// 入力
int N;

// DP テーブル
double dp[310][310][310];
double rec(int i, int j, int k) {
    if (dp[i][j][k] >= 0) return dp[i][j][k];
    if (i == 0 && j == 0 && k == 0) return 0.0;

    double res = 0.0;
    if (i > 0) res += rec(i-1, j, k) * i;
    if (j > 0) res += rec(i+1, j-1, k) * j;
    if (k > 0) res += rec(i, j+1, k-1) * k;
    res += N;
    res *= 1.0 / (i + j + k);

    return dp[i][j][k] = res;
}

int main() {
    cin >> N;
    int one = 0, two = 0, three = 0;
    for (int i = 0; i < N; ++i) {
        int a; cin >> a;
        if (a == 1) ++one;
        else if (a == 2) ++two;
        else ++three;
    }

    // 初期化
    memset(dp, -1, sizeof(dp));

    // 答え
    cout << fixed << setprecision(10) << rec(one, two, three) << endl;
}

Measured memory_bytes: 233216
#include<iostream>
#include <bits/stdc++.h>
#include<stdio.h>
#include <stdlib.h>
#include<string>
#include<queue>
#include<algorithm>
#include<math.h>
#include<map>
using namespace std;


int main(){
	long long int a,ans;
	double b;
	cin>>a;
	b=sqrt(a)+1;
	for(int i=1;i<b;i++){
		if(a%i==0){
			ans=a/i;
		}
	}
	cout<<to_string(ans).size()<<endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<iostream>
using namespace std;

int main()
{
	char a[10];
	scanf("%s",a);
	if(strlen(a) == 2)
		printf("%s\n",a);
	else 
	   {
	   	   for(int i = 2; i >= 0; i--)
	   	   	   printf("%c",a[i]);
	   }
	   printf("\n");
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<cstdio>
#define initdp(a,b) for(int i=0;i<=a;i++)for(int j=0;j<=b;j++)dp[i][j]=-1;
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define ppi pair<pii,int>
#define pip pair<int,pii>
#define ll long long
#define pll pair<ll,ll>
#define rep(i,n) for(int i=0;i<n;i++)
#define repd(i,n) for(int i=n-1;i>=0;i--)
#define inf 1000000001
#define inf1 1000000000000000001
#define mod 1000000007
#define pie 3.14159265358979323846
#define N 1000005
#define mid(l,r) l+(r-l)/2
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
int ddx[8]={1,1,0,-1,-1,-1,0,1},ddy[8]={0,1,1,1,0,-1,-1,-1};
void mad(ll &a,ll b){a=(a+b)%mod;if(a<0)a+=mod;}
ll gcd(ll a,ll b){ if(!a)return b;return gcd(b%a,a);}

void solve(){
	int n;
	cin>>n;
	ll a[n];
	rep(i,n)cin>>a[i];
	ll pre[n+1];
	pre[n]=0;
	for(int i=n-1;i>=0;i--)pre[i]=(pre[i+1]+a[i])%mod;
	ll ans=0;
	for(int i=0;i<n;i++){
		ans=(ans+a[i]*pre[i+1])%mod;
	}
	cout<<ans;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	int t;
	//cin>>t;
	t=1;
	while(t--){
		solve();
	}
}

Measured memory_bytes: 6776
#include <stdio.h>


int main(int argc, char const *argv[]) {



  char s[4];
  scanf("%s", s);

  int d;
  if (s[0] == 'S' && s[1] == 'U'){
      d = 7;
  }
  else if (s[0] == 'M' && s[1] == 'O'){
      d = 6;
  }
  else if (s[0] == 'T' && s[1] == 'U'){
      d = 5;
  }
  else if (s[0] == 'W' && s[1] == 'E'){
      d = 4;
  }
  else if (s[0] == 'T' && s[1] == 'H'){
      d = 3;
  }
  else if (s[0] == 'F' && s[1] == 'R'){
      d = 2;
  }
  else{
      d = 1;
  }




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



  return 0;
}



Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
long long john(long long x,long long n)
{
    long long ans;
    if(n%x==0)
    return n*2-x;
    ans=n/x*x*2+john(n%x,x);
    return ans;
}
int main()
{
    long long n,x;
    long long ans;
    cin>>n>>x;
    ans=john(x,n-x)+n;
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int sx = scanner.nextInt();
        int sy = scanner.nextInt();
        int tx = scanner.nextInt();
        int ty = scanner.nextInt();

        int dv = ty-sy;
        int dx = tx-sx;

        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < dv; i++) {
            builder.append("U");
        }
        for (int i = 0; i < dx; i++) {
            builder.append("R");
        }
        for (int i = 0; i < dv; i++) {
            builder.append("D");
        }
        for (int i = 0; i < dx; i++) {
            builder.append("L");
        }

        builder.append("L");
        for (int i = 0; i <= dv; i++) {
            builder.append("U");
        }
        for (int i = 0; i <= dx; i++) {
            builder.append("R");
        }
        builder.append("DR");
        for (int i = 0; i <= dv; i++) {
            builder.append("D");
        }
        for (int i = 0; i <= dx; i++) {
            builder.append("L");
        }
        builder.append("U");
        System.out.println(builder.toString());
    }
}

Measured memory_bytes: 9932
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define in scanf
#define out printf
#define lin puts("");
int main()
{
    int n;
    in("%d",&n);
    out("%d",((n/500)*1000+((n%500)/5)*5));
    lin
}


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

using namespace std;

const unsigned long long MOD = 1000000007LL;
long long GCD(long long a, long long b){return b == 0 ? a : GCD(b, a % b);}
long long fast_exp(long long base, long long exp) {
    long long tot = 1;
    for(;exp > 0;exp >>= 1) {
       if((exp & 1) == 1) tot = tot * base % MOD;
       base = base * base % MOD;
    }
    return tot;
}

int deg[100001], dist[100001];
pair<int, int> parent[100001];
vector<int> adj[100001];

int main(){

    cin.sync_with_stdio(0); cin.tie(0); cout.tie(0);

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

    // if the original tree is uniquely determined, then there must be one node that has an indegree of 0

    // proof: suppose that X is the original root. if we add a new edge from Y to X, then it is possible that Y was the root, and it becomes ambiguous

    // now, we have to find the right edges to pair up with the nodes

    // each of the new M edges will shorten some path between two nodes
    // therefore it should work if we pair up a child node to the node that is furthest from the root

    int n, m; cin >> n >> m;

    for(int i = 0; i < n - 1 + m; ++i){
        int a, b; cin >> a >> b;
        --a; --b;
        adj[a].push_back(b);
        ++deg[b];
    }

    queue<int> q;

    for(int i = 0; i < n; ++i){
        dist[i] = -1;
        parent[i] = make_pair(-1, -1);
        if(deg[i] != 0) continue;
        q.push(i);
        dist[i] = 0;
        parent[i] = make_pair(0, 0);
    }

    while(!q.empty()){
        int node = q.front();
        q.pop();
        for(auto nx : adj[node]){
            dist[nx] = max(dist[nx], dist[node] + 1);
            if(--deg[nx] == 0)
                q.push(nx);
        }
    }

    for(int i = 0; i < n; ++i)
        for(int j : adj[i])
            parent[j] = max(parent[j], make_pair(dist[i], i + 1));

    for(int i = 0; i < n; ++i)
        cout << parent[i].second << "\n";

    return 0;

}


Measured memory_bytes: 7936
import sys
input = sys.stdin.readline

def main():
  h, w, d = map(int, input().split())
  axy = [None for _ in range(h*w+1)]
  for y in range(h):
    for x, a in enumerate(map(int, input().split())):
      axy[a] = (x, y)

  dlist = [0 for _ in range(h*w+1)]
  for a in range(d+1, h*w+1):
    dlist[a] = dlist[a-d] + abs(axy[a][0] - axy[a-d][0]) + abs(axy[a][1] - axy[a-d][1])

  q = int(input())
  for _ in range(q):
    l, r = map(int, input().split())
    print(dlist[r] - dlist[l])


if __name__ == '__main__':
  main()


Measured memory_bytes: 55676
#include <bits/stdc++.h>
#define N 100
#define INF 1e9 + 5
using namespace std;
vector<vector<int>> v;
vector<vector<int>> memo;
int solve(int n, int k)
{
    if(n==-1) return 0;
    //기저사례만들기
    //처리하기
    if(memo[n][k]!=-1) return memo[n][k];
    int best = -1;
    for (int i = 1; i <= 3; i++)
    {
        
        if (k != i)
        {
            best = max(best, solve(n - 1, i) + v[n][k]);
        }
    }
    memo[n][k]=best;
    return best;
}

int main()
{
    int n, k;
    scanf("%d", &n);
    v.resize(n + 1, vector<int>(4));
    memo.resize(n + 1, vector<int>(4, -1));
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &v[i][1]);
        scanf("%d", &v[i][2]);
        scanf("%d", &v[i][3]);
    }

    //solve 는 n일때..
    cout<<solve(n, 0);

    return 0;
}


Measured memory_bytes: 18944
#if !defined(__clang__) && defined(__GNUC__)
#include <bits/stdc++.h>
#else
#include <cstdlib>
#include <climits>
#include <iostream>
#include <cstdint>
#include <vector>
#include <string>
#include <complex>
#include <bitset>
#include <queue>
#include <deque>
#include <stack>
#include <utility>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <regex>
#endif //  !defined(__clang__) && defined(__GNUG__)
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/multi_array.hpp>
#include <boost/optional.hpp>
#include <boost/math/common_factor_rt.hpp>

using namespace std;

int main()
{
    int64_t N, M;
    std::cin >> N >> M;

    int64_t sum = 0;
    std::unordered_map<int64_t, int64_t> R;
    R[0]++;
    for (decltype(N) i = 0; i < N; ++i) {
        int64_t t;
        std::cin >> t;
        sum += t;
        int64_t r = sum % M;
        R[r]++;
    }

    int64_t ans = 0;
    for (auto r: R) {
        ans += r.second * (r.second - 1) / 2;
    }

    std::cout << ans << std::endl;

    return 0;
}


Measured memory_bytes: 3596
#include <stdio.h>
int main(void){
    int min=1000,i,j,a[5];
    for(i=0;i<5;i++) scanf("%d",&a[i]);
    for(i=0;i<5;i++){
        int tmp=a[i];
        for(j=0;j<5;j++){
            if(i!=j){
                if(a[j]%10==0) tmp+=a[j];
                else tmp+=(a[j]+10-a[j]%10);
            }
        }
        if(min>tmp) min=tmp;
    }
    printf("%d",min);
}

Measured memory_bytes: 128
import java.util.*;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int x = sc.nextInt();
    int t = sc.nextInt();
    int count = 0;
    while (true) {
      if (x * count >= n) {
        break;
      }
      count++;
    }
    System.out.println(count * t);
  }
}


Measured memory_bytes: 35680
import std.stdio, std.string, std.range, std.conv, std.array, std.algorithm, std.math, std.typecons;

void main() {
    auto N = readln.chomp.to!int;
    auto A = 0 ~ readln.split.to!(long[]);

    auto cost = iota(N+1).map!(i => abs(A[i] - A[(i+1)%(N+1)])).array;
    
    auto total = cost.sum;

    N.iota.array.each!(i => writeln(total - cost[i] - cost[(i+1)%(N+1)] + abs(A[i] - A[(i+2)%(N+1)])));
}



Measured memory_bytes: 7416
#include <bits/stdc++.h>
#define rep(i, n) for (ll i = 0; i < (ll)n; i++)
#define rep1(i, n) for (ll i = 1; i <= (ll)n; i++)
#define PI 3.14159265358979323846
#define vll vector<long long>
#define vvll vector<vector<long long>>
#define pll pair<long long, long long>
using namespace std;
using ll = long long;
using ld = long double;

// グローバル変数（入力）
ll n;
vll a;

void input() {
  cin >> n;
  rep(i, n) {
    ll A;
    cin >> A;
    a.push_back(A);
  }
}

void solve() {
  ll M = 0;
  rep(i, n) { M = max(M, a[i]); }
  ll m;
  if (M!=0){
    m = (ll)floor(log2((ld)M)) + 1;
  } else {
    m = 0;
  }
  // cout << "m:" << m << endl;
  vvll b(n, vll(m));
  rep(i, n) {
    for (ll j = m - 1; j >= 0; j--) {
      b[i][j] = a[i] % 2;
      a[i] /= 2;
    }
  }
  /*
  rep(i,n){
    rep(j,m){
      cout << b[i][j] << " ";
    }
    cout << endl;
  }
  */
  vll c(m, 0);
  rep(j, m) {
    rep(i, n) { c[j] += b[i][j]; }
    // cout << c[j] << " ";
  }
  rep(j, m) {
    if (c[j] % 2 == 1) {
      rep(i, n) {
        b[i][j]=(b[i][j]+1)%2;
      }
    }
  }
  rep(i,n){
    ll s=0;
    rep(j,m){
      s+=(ll)pow(2,m-j-1)*b[i][j];
    }
    cout << s;
    if (i!=n-1){
      cout << " ";
    } else {
      cout << endl;
    }
  }
}

int main() {
  input();
  solve();
  return 0;
}


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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int64 i = 0;i < (n);i++)
#define FOR(i, a, b) for(int64 i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-10;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

template<typename T>
vector<T> make_v(size_t a){return vector<T>(a);}

template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts){
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value!=0>::type
fill_v(U &u,const V... v){u=U(v...);}

template<typename T,typename U,typename... V>
typename enable_if<is_same<T, U>::value==0>::type
fill_v(U &u,const V... v){
  for(auto &e:u) fill_v<T>(e,v...);
}

int main(void) {
  int64 N;
  while (cin >> N && N) {
    int64 res = 0;
    for (int64 i = 1; i <= sqrt(N); i++) {
      if (i * i == N) res++;
      for (int64 j = i; j <= sqrt(N-i*i); j++) {
        if (i * i + j * j == N) res++;
        for (int64 k = j; k <= sqrt(N-i*i-j*j); k++) {
          if (i * i + j * j + k * k == N) res++;
          int64 rest = i * i + j * j + k * k;
          rest = N-rest;
          int64 sq = sqrt(rest);
          if (sq >= k && sq*sq == rest && rest != 0) res++;
        }
      }
    }
    cout << res << endl;
  }
}


Measured memory_bytes: 3068
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
typedef pair<int, int> P;
typedef unsigned int ui;
int main(){
	int n, q,m,d;
	while (cin >> n >> q) {
		if (n == 0)break;
		int ok[101] = {};
		for (int i = 0; i < n; i++) {
			cin >> m;
			for (int j = 0; j < m; j++) {
				cin >> d; ok[d]++;
			}
		}
		int ma = 0; int check = 0;
		for (int i = 1; i < 101; i++) {
			if (ma < ok[i]) {
				ma = ok[i]; check = i;
			}
		}
		if (ma < q)cout << 0 << endl;
		else cout << check << endl;
	}
	return 0;
}


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

#define ld long double
#define ll long long
#define ALL(a)  (a).begin(),(a).end()
#define spa << " " <<

int main(){
  
  ll i, j, k;
  ll n;
  cin >> n;
  list<ll> b;
  ll buf;
  bool judge = true;
  list<ll>::iterator itr;
  for(i = 0; i < n; i++){
    cin >> buf;
    //buf = b[i + 1 - b[n]]
    if(i - buf + 1 < 0){
      judge = false;
      break;
    }
    /*buf = b[b[i] - 1];
    b[b[i] - 1] = b[i];
    b[i] = buf;*/
    for(j = 0, itr = b.end(); j < i - buf + 1; j++){
      itr--;
    }
    //cout << i - buf + 2<< endl;
    b.insert(itr, buf);
    /*for(itr = b.begin(); itr != b.end(); itr++)
      cout << *itr << endl;*/
  }
  if(judge)
    for(i = 0, itr = b.begin(); i < n; i++, itr++)
      cout << *itr << endl;
  else
  cout << -1 << endl;

  return 0;
}

Measured memory_bytes: 256
def find_card(mark,input,result):
    for i in xrange(13):
        if input[i] == 0:
            result.append(i)
    result.sort()
    for x in result:
        print mark + " " +  str(x+1)

n = input()
s = [0]*13
h = [0]*13
c = [0]*13
d = [0]*13
for i in xrange(n):
    m,n = map(str,raw_input().split())
    n = int(n)-1
    if m in "S" :
        s[n] = 1
    elif m in "H":
        h[n] = 1
    elif m in "C":
        c[n] = 1
    elif m in "D":
        d[n] = 1

res_s=[]
res_h=[]
res_c=[]
res_d=[]

find_card("S",s,res_s)
find_card("H",h,res_h)
find_card("C",c,res_c)
find_card("D",d,res_d)

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

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

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

const int n=8;
bool aru[8][8];

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

  rep(i,n)rep(j,n){
    if(i%2==0&&j%2==1)aru[i][j]=false;
    else aru[i][j]=true;
  }

  int q;
  cin>>q;
  rep(_,q){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    a--;b--;
    int res=0;
    repl(i,a,c)repl(j,b,d){
      if(aru[i][j])res++;
    }
    cout<<res<<endl;
  }

  return 0;
}



Measured memory_bytes: 3180
//C
#include<iostream>
#include<string>
#include<algorithm>    
#include<cmath>
#include<map>
#include<vector>
#include<math.h>
#include<stdio.h>
#include<stack>
#include<queue>
#include<tuple>
#include<cassert>
#include<set>
#include<functional>
//#include<bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, x) for(ll i = 0; i < x; i++)
#define rep2(i, x) for(ll i = 1; i <= x; i++)
#define all(a) (a).begin(),(a).end()
using ll = long long;
using ld = long double;
using namespace std;
const ll INF = 1000000000000000000;
const ll mod = 1000000007;
const ld pi = 3.141592653589793238;

bool isprime(int p) {
	if (p == 1) return false;
	for (int i = 2; i < p; i++) {
		if (p % i == 0) return false;
	}
	return true;
}
ll gcd(ll a, ll b) {
	if (a < b)swap(a, b);
	if (a % b == 0)return b;
	return gcd(b, a % b);
}
ll lcm(ll a, ll b) {
	return a / gcd(a, b) * b;
}
ll keta(ll n) {
	ll res = 0;
	while (n >= 1) {
		res += n % 10; n /= 10;
	}
	return res;
}
ll modpow(ll x, ll y) {
	ll res = 1;
	while (y) {
		if (y % 2) { res *= x; res %= mod; }
		x = x * x % mod; y /= 2;
	}
	return res;
}
ll nCk(ll n, ll k) {
	ll a = 1, b = 1;
	for (int h = n - k + 1; h <= n; h++) { a *= h; a %= mod; }
	for (int h = 1; h <= k; h++) { b *= h; b %= mod; }
	return a * modpow(b, mod - 2) % mod;
}
//printf("%.10f\n", n);

ll test[1234567];
signed main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	
	string s, t;
	ll n, m, a = 0, b = 0, c = 0, cnt = 0, ans = 0;
	cin >> n;
	rep2(i, n) {
		ll ok = 0, ng = n + 1, mid = (ok + ng) / 2;
		while (ng - ok > 1) {
			if (i * mid < n) { ok = mid; }
			else { ng = mid; }
			mid = (ok + ng) / 2;
		}
		ans += ok;
		//cout << i << ' ' << ok << endl;
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3648
#include<bits/stdc++.h>
using namespace std;
#define MAXN 200005
#define ll long long
int main()
{
    string s;
    cin>>s;
    s[0]=s[s.length()-1];
    if(s[0]=='3')
        cout<<"bon"<<endl;
    else if(s[0]=='0'||s[0]=='1'||s[0]=='6'
    ||s[0]=='8')
        cout<<"pon"<<endl;
    else
        cout<<"hon"<<endl;
}

Measured memory_bytes: 3628
#include<iostream>
#include<vector>

template<class T> inline void chmax(T& a, T b){
	if(a < b){
		a = b;
		return;
	}
	return;
}

int main(){
	int n, W;
	std::cin >> n >> W;
	std::vector<int> w(n);
	std::vector<long long> v(n);
	for(int i=0; i<n; i++){
		std::cin >> w[i] >> v[i];
	}

	std::vector<std::vector<long long>> dp(n+1, std::vector<long long>(W+1, 0));
	for(int i=0; i<n; i++){
		for(int j=0; j<=W; j++){
			chmax(dp[i+1][j], dp[i][j]);
			if(j-w[i] >= 0){
				chmax(dp[i+1][j], dp[i][j-w[i]] + v[i]);
			}
		}
	}

	std::cout << dp[n][W] << std::endl;
	return 0;
}

Measured memory_bytes: 80256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
	public static void main(String args[]) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Solver solver = new Solver();
		solver.init();
		solver.readHead(in.readLine());
		// for (; solver.hasNext();) {
		// solver.readBody(in.readLine());
		// }
		// solver.solve();
		// }
	}
}

class Solver {
	int N;
	int cnt;

	public void init() {
		N = 0;
		cnt = 0;
	}

	public void readHead(String str) {
		String[] strArr = str.split(" ");
		N = Integer.parseInt(strArr[0]);
		// System.out.println(N);
		for (;; N++) {
			if (N / 100 == ((N / 10) % 10) && (N / 100) == (N % 10)) {
				System.out.println(N);
				break;
			}
		}
	}

	public boolean hasNext() {
		return cnt < N;
	}

	public void readBody(String str) {
		// System.out.println(str);
		cnt++;
	}

	public void solve() {

	}
}


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

int main() {
  int N;
  cin >> N;
  
  int sum=1;
  
  vector<int> d(N);
  for (int i=0; i<N; i++) {
    cin >> d.at(i);
  }
  
  int a=0;
  
  for (int i=0; i<N; i++) {
    if (a < d.at(i)) {
      a=d.at(i);
    }
  }
  
  for (int i=0; i<N; i++) {
    int b=0;
    
    for (int j=0; j<N; j++) {
      if (b < d.at(j) && d.at(j) < a) {
        b = d.at(j);
      }
    }
    
    if (b != 0) {
      sum++;
      a = b;
    }
    else {
      break;
    }
  }
  cout << sum << endl;
}

Measured memory_bytes: 3632
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int H = sc.nextInt();
        int W = sc.nextInt();
        int h = sc.nextInt();
        int w = sc.nextInt();
        int ans = (H - h) * (W - w);
        System.out.println(ans);
    }
}

Measured memory_bytes: 22996
answers = Array.new
 
while true
    exp = gets.split(" ")
    break if exp[1] == "?"
    answer = exp[0].to_i + exp[2].to_i if exp[1] == "+"
    answer = exp[0].to_i - exp[2].to_i if exp[1] == "-"
    answer = exp[0].to_i * exp[2].to_i if exp[1] == "*"
    answer = exp[0].to_i / exp[2].to_i if exp[1] == "/"
    answers.push answer
end
 
answers.each{|ans|
    puts ans
}

Measured memory_bytes: 8704
N, K=map(int, input().split())
mod=10**9+7
ans=0
gcd=[0]*(2*K)
for i in range(K,0,-1):
  n=K//i
  now=pow(n,N,mod)
  #print(i)
  #print(now)
  for j in range(2*i,K+1,i):
    now-=gcd[j-1]%mod
    #print(i,j)
  gcd[i-1]=now
  #print(now)
  ans+=(now%mod)*(i%mod)
  ans%=mod
  

print(ans)

Measured memory_bytes: 12204
N,M = map(int,input().split())
l = 1
r = N
for i in range(M):
    L,R = map(int,input().split())
    l = max(l,L)
    r = min(r,R)
if r-l >= 0:
    print(r-l+1)
else:
    print(0)


Measured memory_bytes: 3060
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include<algorithm>
#include<sstream>
#include<iomanip>
#include<deque>
#include<list>


using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

const ll MOD_CONST = 1000000007;
const ll BIG_NUM = 1000000000000000000;
const int BIG_INT = 1000000000;

const int SIZE = 500;

int main() {
	int n;
	ll a, b;
	cin >> n >> a >> b;

	vector<int> x(n);

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

	ll cost = 0;
	for (int i = 1;i < n; i++) {
		cost += min((x[i] - x[i - 1])*a, b);
	}


	
	cout << cost << endl;
}


Measured memory_bytes: 640
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>
#include<map>
#include<algorithm>

using namespace std;

int main()
{
    char x, y;
    cin >> x >> y;

    if (x < y) {
        puts("<");
    } else if (y < x){
        puts(">");
    } else {
        puts("=");
    }

    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ
		Scanner sc=new Scanner(System.in);
		for(;;){


			int n = sc.nextInt(),k=sc.nextInt();
			if(n+k==0)break;
			int[] a = new int[n+1];
			
			int[] b = new int[n+2];
			int[] c = new int[n+2];
			for(int i=0;i<k;i++){
				a[sc.nextInt()]=1;
			}
			int max=-1;
			for(int i=1;i<=n;i++){
				if(a[i]==1){
					b[i]=b[i-1]+1;
					max=Math.max(max, b[i]);
				}
			}
			for(int i=n;i>=1;i--){
				if(a[i]==1){
					c[i]=c[i+1]+1;
					max=Math.max(max, c[i]);
				}
			}
			if(a[0]==1){
				for(int i=1;i<=n;i++){
					if(a[i]==0){
						max=Math.max(max, b[i-1]+c[i+1]+1);
					}
				}
			}
			System.out.println(max);
		}
	}
}

Measured memory_bytes: 62716
#pragma once
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <deque>
#include <numeric>
#include <iomanip>
#define M_PI acos(-1)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1000000007;
const int MAX_V = 201;
int now = 0;

const int MAX = 200000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

// テーブルを作る前処理
void COMinit() {
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for (int i = 2; i < MAX; i++) {
		fac[i] = fac[i - 1] * i % MOD;
		inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
		finv[i] = finv[i - 1] * inv[i] % MOD;
	}
}

// 二項係数計算
long long COM(int n, int k) {
	if (n < k) return 0;
	if (n < 0 || k < 0) return 0;
	return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
struct edge {
	int t;
	ll cost;
	edge(int _t, ll _cost) : t(_t)
		, cost(_cost) {
	}
};
bool isPrime(int n) {

	if (n == 1) return false;
	for (ll i = 2; i * i <= n; i++)
	{
		if (n % i == 0) return false;
	}
	return true;
}
int anyDigitSum(int n) {
	int res = 0;
	while (n > 0)
	{
		res += n % 10;
		n /= 10;
	}
	return res;
}
ll f(double n) {

	n *= n;
	n += 4.0;
	return n / 8.0;
}
void Solve() {

	int n;
	cin >> n;
	int t[300];
	for (size_t i = 0; i < n * 2; i++)
	{
		cin >> t[i];
	}

	sort(t, t + n * 2);
	int res = 0;
	for (size_t i = 0; i < n * 2; i += 2)
	{
		res += t[i];
	}
	cout << res << endl;
}
int main() {
	Solve();
	return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
//cout << fixed << setprecision(15);
vector<int> forintinput(int n) {
	vector<int> x;
	for(int i = 0;i < n;i++){
		int ia;
		cin >> ia;
		x.push_back(ia);
	}
	return x;
}
int main(){
	int a,b;
	cin >> a >> b;
	cout << max(a+b,max(a*2-1,b*2-1)) << endl;
}

Measured memory_bytes: 3572
#include <algorithm>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <queue>
#define MOD (1000000007l)
#define rep(i, n) for(long i = 0; i < n; i++)

using namespace std;

long count (long num) {
  if (num == 0) return 0;
  return (num & 1) + count(num / 2);
}

int main(void) {
  cout.precision(12);

  long N, M;
  cin >> N >> M;

  vector<long> lites(M);
  rep (m, M) {
    long k;
    cin >> k;
    long lite = 0;
    rep (i, k) {
      long s;
      cin >> s;
      lite += 1 << (s - 1);
    }
    lites[m] = lite;
  }

  //rep (m, M) { cout << "lites " << m << ": " << lites[m] << endl; }

  vector<long> p(M);
  rep (m, M) cin >> p[m];

  //rep (m, M) { cout << p[m] << " "; } cout << endl;

  long result = 0;

  for (long i = 0; i < (1 << N); i++) {
    bool flag = true;
    rep (m, M) {
      //cout << "i: " << i << ", lites[" << m << "]: " << lites[m] << ", p[m]: " << p[m] << ", count: " << count(lites[m] & i) <<  endl;
      if (count(lites[m] & i) % 2 != p[m]) {
        flag = false;
        break;
      }
    }
    if (flag) result ++;
  }

  cout << result << endl;

  return 0;
}


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

#define LL long long
#define RG register

using namespace std;
template<class T> T gi() {
	T x = 0; bool f = 0; char c = getchar();
	while (c != '-' && (c < '0' || c > '9')) c = getchar();
	if (c == '-') f = 1, c = getchar();
	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
	return f ? -x : x;
}
const int N = 1e5 + 10;
int cnt[N];
char str[4];
int main() {
	//freopen(".in", "r", stdin);
	//freopen(".out", "w", stdout);
	int n = gi<int>(), m = gi<int>();
	int ans1 = 0, ans2 = 0;
	for (int i = 1; i <= m; i++) {
		int k = gi<int>(); cin >> str;
		if (str[0] == 'A') {
			if (cnt[k] != 666666) ans2 += cnt[k], ans1++, cnt[k] = 666666;
		}
		else if (cnt[k] != 666666) cnt[k]++;
	}
	printf("%d %d\n", ans1, ans2);
	return 0;
}


Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(){
	int n;
	cin>>n;
	int v[n+1];
	map<int,int>odd,even;
	for(int i=0; i<n; i++){
		cin>>v[i];
		if(i%2==0){odd[v[i]]++;}
		else even[v[i]]++;
	}
	vector<pair<int,int>>x,y;
	for(auto p:odd){
		x.push_back(make_pair(p.second,p.first));
	}
	x.push_back(make_pair(0,0));
	for(auto p:even){
		y.push_back(make_pair(p.second,p.first));
	}
	y.push_back(make_pair(0,0));
	sort(x.rbegin(),x.rend());
	sort(y.rbegin(),y.rend());
	if(x[0].second!=y[0].second){
		cout<<n-x[0].first-y[0].first<<endl; 
	}
	else{
		int a=n-x[0].first-y[1].first;
		int b=n-x[1].first-y[0].first;
		cout<<min(a,b)<<endl;
	}
}


Measured memory_bytes: 6520
n = int(input())
ans = [0 for _ in range(10050)]
for i in range(1,105):
    for j in range(1,105):
        for k in range(1,105):
            v = i*i+j*j+k*k+i*j+j*k+k*i
            if v<10050:
                ans[v]+=1
for i in range(n):
    print(ans[i+1])

Measured memory_bytes: 9236
#include <bits/stdc++.h>
#define INF 1e9
#define MAX_V 1001
using namespace std;
typedef map<int,int> M;
typedef M::iterator Mit;
M G[MAX_V],rG[MAX_V],g[MAX_V];
int used[MAX_V];  

int dfs(int v,int t,int f){
  if(v == t)return f;
  used[v]=true;
  
  for(Mit it=G[v].begin();it!=G[v].end() ;it++){
    int to=it->first;
    int &cap=it->second;
    if(!used[to] && cap > 0){
      int d = dfs(to ,t , min(f,cap));
      if(d > 0){
	cap -= d;
	G[to][v]+=d;
	return d;
      }
    }
  }
  return 0;
}

int max_flow(int s,int t){
  int flow = 0;
  for(;;){
    memset(used,0,sizeof(used));
    int f = dfs(s, t, INF);
    if(f == 0)return flow;
    flow += f;
  }
}

bool visited[MAX_V];
void visit(int pos){
  visited[pos]=1;
  for(Mit it=rG[pos].begin();it!=rG[pos].end() ;it++)
    if(!visited[it->first])visit(it->first);
}

int check2(int pos){
  if(used[pos]++) return 0;
  int res=0; 
  for(Mit it=G[pos].begin();it!=G[pos].end() ;it++)
    if(it->second)res+=check2(it->first);
  
  for(Mit it=g[pos].begin();it!=g[pos].end() ;it++)
    res+=(it->second)*visited[it->first];
  
  return res;
}

int main(){
  while(1){
    int n,m,s,t;
    cin>>n>>m>>s>>t;
    if(!n&&!m&&!s&&!t)break;
    s--,t--;
    for(int i=0;i<n;i++) rG[i].clear(),G[i].clear(),g[i].clear();
    
    for(int i=0,a,b;i<m;i++){
      cin>>a>>b;
      a--,b--;
      G[a][b]++;
      g[b][a]++;
    }

    int mx=max_flow(s,t);
    for(int i=0;i<n;i++)
      for(Mit it=G[i].begin();it!=G[i].end() ;it++){
	int to=it->first,cap=it->second;
	if(cap)rG[to][i]=cap;
      }
    
    memset(visited,0,sizeof(visited));
    memset(used,0,sizeof(used));
    visit(t);
    int cnt=check2(s);
    cout<<mx+(cnt>0)<<" "<<cnt<<endl;
  }
  
}

Measured memory_bytes: 4964
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        char[] ch = new char[s.length()];
        for(int i=0; i< s.length(); i++) {
            char c = s.charAt(i);
            if(i % 2 != 0) {
                switch(c) {
                case 'L':
                case 'U':
                case 'D':
                    break;
                default:
                    System.out.println("No");
                    return;
                }
            }
            else {
                switch(c) {
                case 'R':
                case 'U':
                case 'D':
                    break;
                default:
                    System.out.println("No");
                    return;
                }
            }
        }
        System.out.println("Yes");
    }
}

Measured memory_bytes: 35444
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int MAX_HW = 1005;
char S[MAX_HW][MAX_HW];
int ans[MAX_HW][MAX_HW];
int H,W;
queue<P> Q[500000];
int dx[5] = {-2,-1,0,1,2}, dy[5] = {-2,-1,0,1,2};
int dx2[4] = {0,1,0,-1}, dy2[4] = {1,0,-1,0};
int now = 0;

void init()
{
	rep(i,MAX_HW)
	{
		rep(j,MAX_HW)
		{
			ans[i][j] = 1e9;
		}
	}
}

void dfs(int i, int j, int c)
{
	//cout << i << " " << j << endl;
	ans[i][j] = min(ans[i][j],c);
	rep(ii,5)
	{
		rep(jj,5)
		{
			int newi = i+dx[ii], newj = j+dy[jj];
			if (newi >= 0 && newi < H && newj >= 0 && newj < W && S[newi][newj] == '.')
			{
				if (ans[newi][newj] > c)
				{
					ans[newi][newj] = c+1;
					Q[now+1].push(P(newi,newj));
				}
			}
		}
	}

	rep(ii,4)
	{
		int newi = i+dx2[ii], newj = j+dy2[ii];
		//cout << newi << " " << newj << endl;
		if (newi >= 0 && newi < H && newj >= 0 && newj < W && S[newi][newj] == '.')
		{
			//cout << "b" << endl;
			if (ans[newi][newj] > c)
			{
				dfs(newi,newj,c);
			}
		}
	}
	return;
}

int main()
{
	init();
	cin >> H >> W;
	int Ch,Cw,Dh,Dw;
	cin >> Ch >> Cw >> Dh >> Dw;
	rep(i,H)
	{
		rep(j,W)
		{
			cin >> S[i][j];
		}
	}
	dfs(Ch-1,Cw-1,0);
	int cnt = 1;
	while(true)
	{
		now++;
		while(!Q[now].empty())
		{
			P p = Q[now].front();
			Q[now].pop();
			//cout << "aa" << p.first << " " << p.second << " " << ans[p.first][p.second] << " " << cnt << endl;
			if (ans[p.first][p.second] == cnt)
			{
				//cout << "b" << endl;
				dfs(p.first,p.second,cnt);
			}
		}
		if (Q[now+1].empty()) break;
		cnt++;
	}

	/*
	rep(i,H)
	{
		rep(j,W)
		{
			cout << ans[i][j] << " ";
		}
		cout << endl;
	}
	*/
	if (ans[Dh-1][Dw-1] != 1e9)cout << ans[Dh-1][Dw-1] << endl;
	else cout << -1 << endl;
	return 0;
}


Measured memory_bytes: 345532
#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 (ll i = (ll)(a); i < (ll)(b); ++i)
#define rep(...) _overload3(__VA_ARGS__, repi, _rep, )(__VA_ARGS__)
#define ll long long
#define lld long double
#define ALL(x) x.begin(), x.end()
using namespace std;

const int IINF = 1 << 30;
const ll INF = 1ll << 60;
const ll MOD = 1e9 + 7;

int main(int argc, char const *argv[])
{
    int n;
    cin >> n;
    vector<int> v(n);
    ll sum = 0;
    rep(i, n)
    {
        cin >> v[i];
        sum += v[i];
    }
    ll ans = INF;
    ll cur = 0;
    rep(i, n - 1)
    {
        cur += v[i];
        ans = min(ans, abs(sum - 2 * cur));
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1024
#include<iostream>
    #include<stdio.h>
    //#include <bits/stdc++.h>
    #include<vector>
    #include<float.h>
    #include<iomanip>
    #include<algorithm>
    #include<string>
    #include<cstring>
    #include<math.h>
    #include<sstream>
    #include<set>
    #include<map>
    #include<queue>

    #define INF 1e9
    #define rep(i,n)for(int i=0;(i)<(int)(n);i++)
    #define REP(i,a,b)for(int i=(int)(a);(i)<=(int)(b);i++)
    #define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i;
    #define vec(type,n) vector<type>(n)
    #define vvec(m,n) vector<vector<int>> (int(m),vector<int>(n))

    using namespace std;
    using ll = long long;
	  
    int main(){
      int n,k;
      cin>>n>>k;
      int ans;
      if((n-1)%(k-1) == 0)ans = (n-1)/(k-1);
      else ans = (n-1)/(k-1)+1;
      cout<<ans;
    }

Measured memory_bytes: 256
n,m=map(int,input().split())
a=list(map(int,input().split()))
dp=[0]+[-1]*(9*n)
l=[0,2,5,5,4,5,6,3,7,6]
for i in range(n):
  for j in a:
    if dp[i]>=0:
      dp[i+l[j]]=max(dp[i+l[j]],dp[i]*10+j)
print(dp[n])

Measured memory_bytes: 15268
s = input()
cnt = 0
for i in range(1,len(s)):
    if s[i-1] == s[i]:
        new = (int(s[i])+1)%2
        """
        s_li = list(s)
        s_li[i] = str(new)
        s = ''.join(s_li)
        """
        s = s[:i] + str(new) + s[i+1:]
        cnt += 1
        
print(cnt)
    

Measured memory_bytes: 3536
n,k = map(int,input().split())
AB = []
for i in range(n):
    a,b = map(int,input().split())
    AB.append([a,b])
AB.sort()
ans = 0
cnt = 0
for i in range(n):
    cnt += AB[i][1]
    ans = AB[i][0]
    if cnt >=k:
        print(ans)
        exit()


Measured memory_bytes: 20100