akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

//https://atcoder.jp/contests/abc152/submissions/10025034?lang=ja
//上記の提出のバグとり
ll N;
vector<vector<ll>> to;
ll M;
vector<pair<ll,ll>> P;

ll sum = 0LL;

ll num = 0LL; map<pair<ll,ll>,ll> mp;

vector<vector<ll>>edges; //edges[x][y]で、xとyの間に含まれる辺を全てbitで管理

long long pow(long long x, long long n){
	long long ans = 1;
	for(;n;n>>=1,x*=x)
		if(n&1)ans*=x;
	return ans;
}
void dfs(ll par,ll pos){
  //0起点のdfsで、辺にナンバリングしていく
  if(par != -1LL ){
    //一番初め par == -1の場合は記録しようがない
    
    mp[{par,pos}] = num; 
    mp[{pos,par}] = num; 
    //向きなしで記録する
    num++; //木だから、同じ辺を二回更新することはないはず
  }
 
  for(auto v : to[pos]){
    if(v == par){ continue;}
    
    dfs(pos,v);
  }
  
  return;
}

void ddfs(ll par, ll pos, ll start,ll val){
  
  ll res = val;
  
  if( par != -1 ){ res |= (1LL << mp[{par,pos}]);}
  
  if( par != -1 ){ 
    //par == -1の時、pos == startだから、同じ点同士を結ぶ辺を記録することになり無意味
 
    edges[start][pos] |= res;
    edges[pos][start] |= res; //こちらも向きなしで記録する
    
  }
   
  for(auto u : to[pos]){
    if( u == par ){ continue;}
    
    ddfs(pos,u,start,res); //startは変化なし
  
  }
  
  return ;
}

  
signed main(void){ 
  
  cin >> N;
  to.resize(N);
  edges = vector<vector<ll>> (N,vector<ll>(N));
  
  for(ll i = 1; i < N; i++){
    ll a,b; cin >> a >> b; a--; b--;
    to[a].push_back(b); to[b].push_back(a);
  }
  
  dfs(-1LL,0LL);
  //ここまでOK
  
  for(ll i = 0LL; i < N; i++){ 
    ddfs(-1LL,i,i,0LL);
  }
  
  //ここまでOK
  
  cin >> M; P.resize(M); //条件を列挙する
  
  for(ll i = 0LL; i < M; i++){
    ll u,v; cin >> u >> v; u--; v--;
    //uとvの間は少なくとも一つは黒で塗る必要がある
    P[i] = {u,v};
  }
   
  for(ll bit = 1LL; bit < (1LL << M); bit++){
    //立ったbit全ての積を取って、包除原理を適用 i = 0,,,M-1はそれぞれの条件を使うか表す
    
    ll value = 0LL;
    bool even = true;
    
    for(ll i = 0; i < M; i++){
      if( (bit >> i) & 1LL ){  // P[i].firstとP[i].secondを結ぶ辺全てを記録
        even = !even;
        //i番目の条件に含まれている辺全てを追加
        value |= edges[P[i].first][P[i].second];
        
      }
    }
    //valueに含まれているbit全てを白で塗る必要がある
    
    ll p = 0LL;
    
    for(int i = 0LL; i <= N; i++){ //valueはNケタぐらいまではいく
      if( (value >> i) & 1LL ){ p++;}
    }
    
    ll res = 0LL;
    
    if(!even){
      res += pow(2LL,N-1LL-p);
    }
    else{
      res -= pow(2LL,N-1LL-p);
    }
    
    sum += res;
    
  }
  
  cout << pow(2LL,N-1LL)-sum << endl;
  
  return 0;
}
    

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ff first
#define ss second
#define IO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
const ll inf = 1e18L + 5;

int main()
{
    int n, k;
    cin >> n >> k;
    bitset<101> bs;
    for(int i = 0; i < k; i++){
        int d, a;
        cin >> d;
        while(d--)
            cin >> a, bs[a-1] = true;
    }
    cout << n - bs.count() << '\n';
    return 0;
}


Measured memory_bytes: 3596
#include <bits/stdc++.h>
 
#define For(i, j, k) for(int i = j; i <= k; i++)
 
using namespace std;
 
const int N = 3010;
const int Mod = 1e9 + 7;
 
void add(int &x, int y){
	x += y;
	if(x >= Mod) x -= Mod;
}
 
int dp[N][N][2], n, m;
 
int main(){
	scanf("%d%d", &n, &m);
 
	For(i, 0, n) dp[0][i][i == 0] = 1;
	For(i, 0, m - 1) For(j, 0, n) For(v, 0, 1){
		int val = dp[i][j][v];
		if(j){
			add(dp[i + 1][j][v | (j == 1)], val);
			add(dp[i + 1][j - 1][v | (j == 1)], val);
		}
		if(n - j){
			add(dp[i + 1][j][v], val);
			add(dp[i + 1][j + 1][v], val);
		}
	}
 
	int ans = 0;
	For(i, 0, n) add(ans, dp[m][i][1]);
	printf("%d\n", ans);
	return 0;
}

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

int main(){
    int a[5][4][11], n;
    for(int i = 1; i <= 4; i++){
        for(int j = 1; j <= 3; j++){
            for(int k = 1; k <= 10; k++){
                a[i][j][k] = 0;
            }
        }
    }
    
    cin >> n;
    for(int i = 0; i < n; i++){
        int b, f, r, v;
        cin >> b >> f >> r >> v;
        a[b][f][r] += v;
    }
    
    for(int f = 1; f <= 4; f++){
        if(f > 1) cout << "####################" << endl;
        for(int b = 1; b <= 3; b++){
            for(int r = 1; r <= 10; r++){
                cout << " " << a[f][b][r];
            }
            cout << endl;
        }
    }
    return 0;
}


Measured memory_bytes: 3112
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,m;cin>>n>>m;
    vector<long long> a(n),b(n),c(m),d(m);
    for(int i=0;i<n;i++){
        cin>>a[i]>>b[i];
    }
    for(int j=0;j<m;j++){
        cin>>c[j]>>d[j];
    }
    long long kyori;
    for(int i=0;i<n;i++){
        int ans =1;
        long long saitan = abs(a[i]-c[0])+abs(b[i]-d[0]);
        for(int j=1;j<m;j++){
            kyori = abs(a[i]-c[j])+abs(b[i]-d[j]);
            if(saitan>kyori){
                saitan = kyori;
                ans = j+1;
            }
        }
        cout<<ans<<endl;
    }
}

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

int main() {
    int n, q;
    string s;
    cin >> n >> q >> s;
    for (int i = 0; i < n - 1; ++i) if (s[i] == 'A' && s[i + 1] == 'C') s[i] = 'a';
    vector<int> ac(n + 1);
    for (int j = 1; j < n; ++j) ac[j] = (s[j - 1] == 'a' ? ac[j - 1] + 1 : ac[j - 1]);
    vector<pair<int, int>> query(q);
    for (int k = 0; k < q; ++k) cin >> query[k].first >> query[k].second;
    for (pair<int, int> l : query) cout << ac[l.second - 1] - ac[l.first - 1] << endl;
}


Measured memory_bytes: 2176
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <sstream>
#include <set>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
 
#define ll long long
using namespace std;

int main(){
    int N,k;
    cin >> N;
    vector<int> h(N);
    for(int i=0;i<N;i++){
        cin >> h.at(i);
    }
    vector<int> dp(N);
    dp[0] = 0;
    dp[1] = abs(h[1]-h[0]);
    
    for(int i=0;i<N-2;i++){
        dp[i+2] = min(dp[i]+abs(h[i+2]-h[i]), dp[i+1]+abs(h[i+2]-h[i+1]));
    }
    cout << dp[N-1] << endl;
    
    return 0;
}

Measured memory_bytes: 1024
#include <stdio.h>

int main(void) {
    int n, i, j,k,x,y;
    int count=0;
    int num=0;
    int ans[1000];
    
    //printf("n? ");
    while(1){
    scanf("%d", &n);
    scanf("%d",&x);
        count=0;
    if(x==0 && n==0){
        break;
           }else{
               for (i = 1; i < n; i++) {
                   for (j = i + 1; j < n+1; j++) {
                       for(k=j+1; k< n+1; k++){
                           if (i != k) {
                               //printf("%d, %d, %d\n", i, j,k);
                               if(i+j+k==x){count++;}
                           }
                       }
                   }
               }
               //printf("%d\n",count);
               ans[num]=count;
               num++;

        }
    }
    
    for (y=0; y<num; y++) {
        printf("%d\n",ans[y]);

    }
    
    
    return 0;
}

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

long long MOD = 1e9+7;
int MAX_INT = 1e9;

// 最大公約数
long long gcd(long long a, long long b){
  if(b == 0) return a;
  return gcd(b, a % b);
}
// 最小公倍数
long long lcm(long long a, long long b) {
  return a / gcd(a, b) * b;
}

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int n;
  long long x = 0, y = 0, ans = LLONG_MAX;
  cin >> n;
  vector<long long> a(n);

  for (int i = 0; i < n; ++i){
    cin >> a[i];
    x += a[i];
  }
  for (int i = 0; i < n; ++i){
    y += a[i];
    if (i + 1 < n) ans = min(ans, abs(x - 2*y));
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 4732
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>

struct item
{
  std::vector<std::string> materials;
  int price = 0;
  int true_price = -1;
  item(int price) : price(price){};
};

int calc_cost (std::unordered_map<std::string, item>& items, const std::string& name) {
  auto& target = items.at(name);
  if ( target.true_price != -1 )
    return target.true_price;

  if ( target.materials.empty() ){
    target.true_price = target.price;
    return target.true_price;
  }
  
  int sum_of_price = 0;
  for(const std::string& material : target.materials){
    sum_of_price += calc_cost(items, material);
  }

  return target.true_price = std::min(target.price, sum_of_price);
}

int main(){
  while(true) {
    std::unordered_map<std::string, item> items;
    int N;

    std::cin >> N;
    if (N == 0)
      break;

    for(int i = 0; i < N; ++i) {
      std::string name;
      int price;
      std::cin >> name >> price;
      items.insert({name, price});
    }

    std::cin >> N;
    for(int i = 0; i < N; ++i){
      std::string name;
      std::cin >> name;

      int M;
      std::cin >> M;
      std::vector<std::string> materials(M);
      for(int j = 0; j < M; ++j){
        std::string mat_name;
        std::cin >> mat_name;
        materials.at(j) = mat_name;
      }

      items.at(name).materials = materials;
    }

    std::string name;
    std::cin >> name;
    std::cout << calc_cost(items, name) << std::endl;
  }
}

Measured memory_bytes: 3032
import java.util.Scanner;

public class Main {
	class Node{
		int id;
		int p = -1;
		int depth = -1;
		int[] childs;
		String nodeType = null;

		//コンストラクタの生成
		Node(int id, int[] childs){
			this.id = id;
			this.childs = childs;
		}

		void childsStr(StringBuilder sb) {
			if(childs.length != 0) {
				sb.append("[");
				sb.append(childs[0]);
				for(int i = 1; i < childs.length; i++) {
					sb.append(", ");
					sb.append(childs[i]);
				}
				sb.append("]");
				return;
			}
			sb.append("[]");
		}
		void print() {
			StringBuilder sb = new StringBuilder();
			sb.append("node ");
			sb.append(id);
			sb.append(": parent = ");
			sb.append(p);
	        sb.append(", depth = ");
	        sb.append(depth);
	        sb.append(", ");
	        sb.append(nodeType);
	        sb.append(", ");
	        childsStr(sb);
	        System.out.println(new String(sb));
		}
	}

	public static int findRoot(boolean[] rs) {
		for(int i = 0; i < rs.length; i++) {
			if(!rs[i]) {
				return i;
			}
		}
		System.out.println("could not find root.");
        return -1;
	}

	public static void search(Node[] nodes, int id, int depth, int parent) {
		if(nodes[id].depth != -1) {
			System.out.println("error");
			return;
		}
		if(nodes[id].childs.length != 0) {
			for(int i = 0; i < nodes[id].childs.length; i++) {
				search(nodes, nodes[id].childs[i], depth+1, id);
			}
			nodes[id].nodeType = "internal node";
		}else {
			nodes[id].nodeType = "leaf";
		}
		if(parent == -1) {
			nodes[id].nodeType = "root";
		}
		nodes[id].p = parent;
		nodes[id].depth = depth;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		Node[] nodes = new Node[n];
		boolean[] Root = new boolean[n];
		for(int i = 0; i < n; i++) {
			int id = sc.nextInt();
			int k = sc.nextInt();
			int[] childs = new int[k];
			for(int j = 0; j < k; j++) {
				childs[j] = sc.nextInt();
				Root[childs[j]] = true;
			}
			Main rt = new Main();
			nodes[id] = rt.new Node(id , childs);
		}
		sc.close();
		int root = findRoot(Root);
		search(nodes, root, 0, -1);
		for (int i = 0; i < nodes.length; i++) {
            nodes[i].print();
        }
	}

}



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

int main(void) {
    int n;
    cin >> n;
    bool can = false;
    int j = 1;
    for (int i = 1; i <= n; ++ i) {
        int x;
        cin >> x;
        if (j == x) ++ j;
    }

    if (j == 1) cout << -1 << endl;
    else {
        cout << n - j + 1 << endl;
    }

    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define chmin(x, v) x = min(x, v)
#define chmax(x, v) x = max(x, v)
#define int long long
using namespace std;

int t[2*(1<<18)];

void upd(int i, int ni)
{
//	cout << i << " " << n << endl;
	i += 1 << 18;
	t[i] = ni;
	for (int j = i/2;j > 0; j/= 2)
		t[j] = max(t[j*2], t[j*2+1]);
}

int que(int l, int r, int rl, int rr, int no)
{
	if (l>=rl && r <=rr)return t[no];
	if(l>rr || r <rl) return -1;

	int m =(l+r)/2;
	return max(que(l, m, rl, rr, no*2), que(m+1, r, rl, rr, no*2+1));
}

signed main()
{
	int n;
	cin >> n;
	int h[n], a[n];
	for (int i=0;i<n;++i)cin>>h[i];
	for (int i=0;i<n;++i)cin>>a[i];

	for (int i=0;i<n;++i) upd(/*1 << 17 +*/ h[i], que(1, 1 << 18, 1, h[i], 1) + a[i]);
	cout << t[1];
	return 0;
}

Measured memory_bytes: 7040
#include <bits/stdc++.h>
#define endl '\n'
#define ll long long
#define double long double
using namespace std;

string s;
ll kol, mux;

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> s;
    s += 'S';
    for (ll i = 0; i < 4; ++i)
        if (s[i] == 'R')
            ++kol;
        else {
            mux = max(mux, kol);
            kol = 0;
        }
    cout << mux;
    return 0;
}


Measured memory_bytes: 3684
#include<iostream>
#include<map>
#include<queue>
using namespace std;
typedef pair<int,int> P;
#define fr first
#define sc second
#define rep(i,n) for(int i = 0 ; i < n ; ++i )

int n,m,p,q,r;int a;
deque<P> card;

int query(){
  int sum = 0,ans = 0;
  while(sum + card.front().sc - card.front().fr + 1 < p){
    sum += card.front().sc - card.front().fr + 1;
    card.pop_front();
  }
  if(sum + 1 != p) card.front().fr = card.front().fr + p - sum;
  sum = p;
  while(sum + card.front().sc - card.front().fr + 1 <= q){
    if(card.front().sc <= r){ //完全に含む
      ans += card.front().sc - card.front().fr + 1;
    }else if(card.front().fr <= r){ //微妙に含む
      ans += r - card.front().fr + 1;
    }
    sum += card.front().sc - card.front().fr + 1;
    card.pop_front();
  }
  if(!card.empty() && sum != q){
    card.push_front(P(card.front().fr,card.front().fr+q-sum-1));
    if(card.front().sc <= r){ //完全に含む
      ans += card.front().sc - card.front().fr + 1;
    }else if(card.front().fr <= r){ //微妙に含む
      ans += r - card.front().fr + 1; 
    }
  }
  return ans;
}

void deb(deque<P>& tmp){
  cout << endl;
  for(deque<P>::iterator it = tmp.begin();it!=tmp.end();it++){
    cout << (*it).fr << " " << (*it).sc << endl;
  }
}
void shuffle(int x,int y){
  deque<P> A,B;
  int sum = 0;
  while(sum + card.front().sc - card.front().fr + 1 <= x){
    A.push_back(card.front());
    sum += card.front().sc - card.front().fr + 1;
    card.pop_front();
  }

  if(sum != x){
    A.push_back(P(card.front().fr,card.front().fr + x - sum - 1));
    card.front().fr = card.front().fr + x - sum;
    sum = x;
  }
  while(sum + card.front().sc - card.front().fr + 1 <= y){
    B.push_back(card.front());
    sum += card.front().sc - card.front().fr + 1;
    card.pop_front();
  }

  if(sum != y){
    B.push_back(P(card.front().fr,card.front().fr + y - sum - 1));
    card.front().fr = card.front().fr + y - sum;
    sum = y;
  }
  card.insert(card.end(),B.begin(),B.end());
  card.insert(card.end(),A.begin(),A.end());
}

int main(){
  while(cin >> n , n){
    cin >> m >> p >> q >> r;
    p--;
    card.push_back(P(1,n));
    while(m--){
      int x,y;
      cin >> x >> y;
      shuffle(x,y);
    }
    cout << query() << endl;
    card.clear();
  }
}

Measured memory_bytes: 1336
#include<stdio.h>
#include<string.h>
int main(){
    char a[11],b[11],c[11];
    int lena,lenb;
    scanf("%s %s %s",a,b,c);
    lena = strlen(a);
    lenb = strlen(b);
    if(a[lena-1]==b[0]&&b[lenb-1]==c[0])printf("YES\n");
    else printf("NO\n");
    return 0;
}


Measured memory_bytes: 128
#include<bits/stdc++.h>
using namespace std;
typedef complex<double> P; //Point
typedef pair<P,P> L; //Line, Segment
 
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
 
namespace std {
  bool operator < (const P& a, const P& b){
    return fabs(real(a)-real(b)) < EPS ? imag(a) < imag(b) : real(a) < real(b);
  }
};
double dot(P a, P b){ return a.real() * b.real() + a.imag() * b.imag(); }
double cross(P a, P b){ return   a.real() * b.imag() - a.imag() * b.real(); }
double area(vector<P> v){
  double sum = 0.0;
  int n = v.size();
  for(int i=0;i<n;i++) sum += (real(v[i]) - real(v[(i+1)%n])) * (imag(v[i]) + imag(v[(i+1)%n]));
  return fabs(sum) / 2;
};
P rotate(P p, double theta){
  theta = theta * M_PI / 180.0;
  double x = real(p) * cos(theta) - imag(p) * sin(theta);
  double y = real(p) * sin(theta) + imag(p) * cos(theta);
  return P(x,y);
}
int ccw(P a, P b, P c){
  b -= a;
  c -= a;
  if(cross(b,c) > EPS) return 1; //テ・ツ渉催ヲツ卍づィツィツ暗・ツ崢榲」ツつ?
  if(cross(b,c) < -EPS) return -1; // テヲツ卍づィツィツ暗・ツ崢榲」ツつ?
  if(dot(b,c) < -EPS) return 2; // c -- a -- b テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?
  if(norm(b) < norm(c)) return -2; // a -- b -- c テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?
  return 0; // a -- c -- b テ」ツ?ョテ、ツクツ?ァツ崢エテァツキツ?
}
P crossPoint(L l, L m){
  double A = cross(l.second - l.first, m.second - m.first);
  double B = cross(l.second - l.first, l.second - m.first);
  if(fabs(A) < EPS && fabs(B) < EPS) return m.first;
  else if(fabs(A) >= EPS) return m.first + B / A * (m.second - m.first);
}
int N;
typedef pair<bool,double> PR;
double getA( double pi ){
  if( pi+EPS > M_PI ) return -M_PI;
  return pi;
}
int main(){
  while( ~scanf("%d",&N) && N){
    P v=P(0,1.0/sqrt(3));
    set<P> sp;
    vector< pair<PR,L> > ss;
    for(int i=0;i<N;i++){
      P v1 = v;
      for(int i=0;i<3;i++){
        P v2 = rotate( v1, 120.0 );  
        //cout << " " << v1 << " "<< v2 << endl;      
        ss.push_back( make_pair( PR(0,getA(arg(v1))), L( v1, v2 ) ) );        
        ss.push_back( make_pair( PR(1,getA(arg(v2))), L( v2, v1 ) ) );
        v1 = v2;
      }
      v = rotate( v, 360.0/(double)N );
    }    
    sort( ss.begin(), ss.end() );
    int cnt = 0;
    for(int i=0;i<(int)ss.size()-1-cnt;i++){
      // cout << ss[i].first.second << endl;
      if( ss[i].first.first == ss[i+1].first.first && abs( ss[i].first.second - ss[i+1].first.second ) < EPS ){
        cnt++;
        for(int j=i+1;j<(int)ss.size()-1;j++)
          ss[j] = ss[j+1];       
        i--;
      }
    }    
    ss.erase( ss.end()-cnt, ss.end() );    
    int n = ss.size();
    vector<P> p;
    for(int i=0;i<n/2;i++){
      L s1 = ss[i].second;
      L s2 = ss[i+1].second;
      L s3 = ss[(i+1+n/2==n)?(n/2):(i+1+n/2)].second;
      //  cout <<" x" << s1.first << " " << s1.second << endl;
      //cout << " y"<< s3.first <<" "<<s3.second << endl;
      p.push_back( s1.first );
      P mp = crossPoint( s1, s3 );      
      if( abs( mp - s2.first ) > EPS) 
        p.push_back( mp );      
    } 
    printf("%.9lf\n",area( p ) );
  }  
}

Measured memory_bytes: 1956
#include <bits/stdc++.h>
using namespace std;
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define all(x) (x).begin(),(x).end()
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
typedef long long ll;
const long long INF = 1LL << 60;
typedef pair<int, int> P;
int main()
{
    int D, T, S;
    cin >> D >> T >> S;

    int t = (D + S - 1) / S;
    if (T >= t) cout << "Yes" << endl;
    else cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 3632
// AOJ 2331: A Way to Invite Friends
// 2017.10.4 bal4u@uu
// 2017.11.22

#include <stdio.h>
#include <stdlib.h>

#define MAX 100001
int tbl[MAX];

char buf[20], *p;
int getint()
{
	int n = 0;
	while (*p >= '0') n = (n<<3) + (n<<1) + (*p++ & 0xf);
	return n;
}

int main()
{
	int n, a, b, i, sum, ans;

	fgets(p=buf, 10, stdin), n = getint();
	for (i = 0; i < n; i++)	{
		fgets(p=buf, 20, stdin);
		a = getint()-1, p++, b = getint()-1;
		tbl[a]++, tbl[b+1]--;
	}
	sum = ans = 0;
	for (i = 0; i <= MAX; i++) {
		sum += tbl[i];
		if (sum >= i) ans = i;
	}
	printf("%d\n", ans);
	return 0;
}

Measured memory_bytes: 2120
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
typedef long long ll;

int main(){
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    rep(i,n) cin >> a[i];
    ll sum = 0;
    rep(i,n) sum += a[i];
    set<ll> candidates;
    for(int i=1; i*i <= sum; i++){
        if(sum%i == 0){
            candidates.insert(i);
            candidates.insert(sum/i);
        }
    }
    ll ans = 1;
    for(ll x : candidates){
        ll need;
        {
            vector<ll> r(n);
            rep(i,n) r[i] = a[i]%x;
            sort(r.begin(), r.end());
            ll B = 0;
            rep(i,n) B += x-r[i];
            ll A = 0;
            need = 1e18;
            rep(i,n){
                A += r[i];
                B -= x-r[i];
                need = min(need, max(A,B));
            }
        }
        if(need <= k) ans = max(ans, x);
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 256
#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::cmp::*;
use std::collections::*;
use std::io::Write;

#[allow(unused_macros)]
macro_rules! debug {
    ($($e:expr),*) => {
        #[cfg(debug_assertions)]
        $({
            let (e, mut err) = (stringify!($e), std::io::stderr());
            writeln!(err, "{} = {:?}", e, $e).unwrap()
        })*
    };
}

fn calc_b(grid: &Vec<Vec<usize>>) -> usize {
    let h = grid.len();
    let w = grid[0].len();

    let b_cand = (0..h).map(|y| grid[y][0]).sum::<usize>();
    let b_cand = min(b_cand, h - b_cand);
    for x in 1..w {
        let b_temp = (0..h).map(|y| grid[y][x]).sum::<usize>();
        let b_temp = min(b_temp, h - b_temp);
        if b_temp != b_cand {
            return std::usize::MAX;
        }
    }
    b_cand
}

fn main() {
    let v = read_vec::<usize>();
    let (h, w, a, b) = (v[0], v[1], v[2], v[3]);

    {
        let mut grid = vec![vec![0; w]; h];
        for y in 0..b {
            for x in 0..a {
                grid[y][x] = 0;
            }
            for x in a..w {
                grid[y][x] = 1;
            }
        }

        for y in b..h {
            for x in 0..a {
                grid[y][x] = 1;
            }
            for x in a..w {
                grid[y][x] = 0;
            }
        }

        if b == calc_b(&grid) {
            for y in 0..h {
                for x in 0..w {
                    print!("{}", grid[y][x]);
                }
                println!("");
            }
            return;
        }
    }
    println!("No");
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}


Measured memory_bytes: 9844
import java.util.Scanner;

public class Main {

	public static void main(String[] arg) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		int m = scan.nextInt();
		int[] h = new int[n];
		boolean[] res = new boolean[n];
		for(int i = 0; i < n; i++){
			h[i] = scan.nextInt();
		}
		int result = 0;
		for(int i = 0; i < m; i++){
			int a = scan.nextInt();
			int b = scan.nextInt();
			if(h[a-1]>h[b-1]){
				res[b-1]=true;
			}else if(h[a-1]<h[b-1]){
				res[a-1]=true;
			}else if(h[a-1]==h[b-1]){
				res[a-1]=true;
				res[b-1]=true;
			}
		}
		for(int i = 0; i < n; i++){
			if(res[i]==false)result++;
		}
		System.out.println(result);
	}
}


Measured memory_bytes: 59744
from itertools import permutations

n = int(input())
p = [int(i) for i in input().split()]
q = [int(i) for i in input().split()]

perm = list(permutations(sorted(p), n))    #; print(perm)
a = perm.index(tuple(p))
b = perm.index(tuple(q))

print(abs(a-b))

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

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

int main() {
    int N;
    while (cin >> N) {
        vector<long long> A(N);
        REP(i, N) {
            cin >> A[i];
        }

        vector<vector<long long>> dp(N, vector<long long>(2));
        dp[0][0] = A[0];
        dp[0][1] = -1e12;
        for (int i = 1; i < N; ++i) {
            // -1 倍しない場合
            dp[i][0] = A[i] + max(dp[i-1][0], dp[i-1][1]);
            // -1 倍する場合
            dp[i][1] = -A[i] + max(dp[i-1][0] - 2*A[i-1], dp[i-1][1] + 2*A[i-1]);
        }

        cout << max(dp[N-1][0], dp[N-1][1]) << endl;
    }
}

Measured memory_bytes: 6528
n = int(input())
w = [str(input()) for _ in range(n)]
tmp = 0

for i in range(n-1):

    if w.count(w[i])==1 and str(w[i])[-1]==str(w[i+1])[0]:
        tmp += 1

if tmp==n-1:
    print('Yes')
else:
    print('No')

Measured memory_bytes: 3060
#include <cmath>
#include <iostream>
#include <string>
#include <vector> 
#include <algorithm>
#include <numeric>
#include <map>   
#include <stdio.h>

#define FOR(i,a,b) for(i=(a);i<(b);++i) 
#define FORR(i,a,b) for(i=(a);i>(b);--i)
#define REP(i,n) for(i=0;i<(n);++i)
#define REPR(i,n) for(i=(n);i>0;--i)
#define PB push_back
#define ll long long
#define INT_MAX 2147483647
#define LL_MAX 9223372036854775807
#define sortR(a,b) sort(a, b, greater<int>())

using namespace std;

//最大公約数
	long long GCD(long long a, long long b) {
		if (a < b)
			swap(a, b);
		if (b == 0) return a;
		else return GCD(b, a % b);
	}
	//素数判定
	bool sosuu_check(int num) {
		int i;
		for (i = 2; i <= sqrt(num); ++i) {
			if (num%i == 0)
				return false;
		}
		return (num < 2) ? false : true;
	}
	
	int check(int N, int a, int b) {
		int A,B,i;
		if (a > b) {
			A = a;
			B = b;
		}
		else {
			A = b;
			B = a;
		}
		for (i = N / b; i > 0; i--) {
			if ((N - b * i) % a == 0) {
				return i;
			}
		}
		return 0;
	}


int main() {
	int N, M,i,Ai,K,j;
	string S,y,m1,m2;
	cin >> S;
	m1 = S[5];
	m2 = S[6];
	if (atoi(m1.c_str()) == 1) {
		cout << "TBD" << endl;
		return 0;
	}
	else if (atoi(m2.c_str()) > 4) {
		cout << "TBD" << endl;
		return 0;
	}
	else {
		cout << "Heisei" << endl;
		return 0;
	}
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define in insert
#define PI 3.1415926535897932384626433832795
#define MOD 100000007
#define F first
#define S second
#define aLL(str) (str).begin(), (str).end()
#define IOS                  \
	ios::sync_with_stdio(0); \
	cin.tie(0);              \
	cout.tie(0)
#define ll long long
#define ld long double
#define lcm(a, b) (a * b) / __gcd(a, b)
#define flu(i, a, b) for (ll i = a; i < b; i++)
#define blu(i, a, b) for (ll i = a; i >= b; i--)
#define upper(s) transform(s.begin(), s.end(), s.begin(), ::toupper)
#define lower(s) transform(s.begin(), s.end(), s.begin(), ::tolower)
#define Unique(X) (X).erase(unique(all(X)), (X).end())
#define all(v) v.begin(), v.end()
typedef vector<ll> vl;
typedef map<ll, ll> ml;
typedef vector<string> vs;
typedef vector<vector<int>> vvi;
char e = '\n';
ll i, j, le;

int main()
{
	IOS;
	int t;
	t=1;
	while (t--)
	{ll t1;
	cin>>t1;
if(t1>=30)cout<<"Yes"<<e;
else cout<<"No"<<e;		
	}
	return 0;
}

Measured memory_bytes: 3648
N = int(input())
a = [int(input()) for i in range(N)]
def gcd(m, n):
    while n:
	    m, n = (n, m % n)
    return m
def lcm(m, n):
    return m * n // gcd (m, n)

p = a[0] 

for i in range(N-1):
    t = lcm(p, a[i+1])
    p = t

print(p)


Measured memory_bytes: 3064
import sys
import socket


hostname = socket.gethostname()

if hostname == 'F451C':
    sys.stdin = open('a1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    R = read_int()
    res = 2 * 3.1415926 * R
    print(res)


main()


Measured memory_bytes: 9876
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <string>
#include <string.h>
#include <bitset>
#include <map>
#include <climits>
#include <set>
#include <cmath>

using namespace std;

int main()
{
    using integer = unsigned long long;
    integer X, Y;
    cin >> X >> Y;
    integer count = 0;
    integer i = X;
    while (i <= Y)
    {
        ++count;
        i *= 2;
    }
    cout << count << endl;
    return 0;
}

Measured memory_bytes: 256
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif

#include <bits/stdc++.h>

using namespace std;

mt19937 mrand(random_device{} ()); 

int rnd(int x) {
  return mrand() % x;
}

typedef long double ld;
typedef long long ll;

#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stderr)
#else
#define eprintf(...) ;
#endif

#define pb push_back
#define mp make_pair
#define sz(x) ((int) (x).size())
#define TASK "text"

const int inf = (int) 1.01e9;
const ld eps = 1e-9;
const ld pi = acos((ld) -1.0);

void precalc() {
}

const int maxn = (int) 1e6 + 10;
char s[maxn], q[maxn];
int n;

int read() {
  if (scanf("%d%s%s", &n, s, q) < 3) {
    return 0;
  }
  return 1;
}

void solve() {
  if (!strcmp(s, q)) {
    printf("0\n");
    return;
  }

  int res = 1;
  int left = 0;
  vector<int> st;

  for (int i = n - 1, j = n - 1; i >= 0;) {
    int i0 = i;
    while (i >= 0 && q[i] == q[i0]) {
      --i;
    }
    j = min(j, i + 1);
    while (j >= 0 && s[j] != q[i0]) {
      --j;
    }
    if (j < 0) {
      printf("-1\n");
      return;
    }
    while (left < sz(st) && i + 1 <= st[left] - (sz(st) - left)) {
      ++left;
    }
    if (j < i + 1) {
      st.pb(j);
      res = max(res, sz(st) - left + 1);
    }
  }
  printf("%d\n", res);
}

int main() {
  precalc();
#ifdef LOCAL
  freopen(TASK ".out", "w", stdout);
  assert(freopen(TASK ".in", "r", stdin));
#endif

  while (1) {
    if (!read()) {
      break;
    }
    solve();
#ifdef DEBUG
    eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
  }
  return 0;
}


Measured memory_bytes: 6384
/*
    A - Pair
    https://atcoder.jp/contests/abc108/tasks/abc108_a
*/
#include <bits/stdc++.h>
using namespace std;

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

    int n;
    cin >> n;

    if (n % 2 == 0) {
        cout << (n * n / 4) << endl;
    } else {
        cout << ((n / 2) * (n + 1) / 2) << endl;
    }
}

Measured memory_bytes: 256
H,W,K = map(int,input().split())
S = [[int(i) for i in input()] for _ in range(H)]
ans = float("inf")

# bit全探索
for i in range(2**(H-1)):
    start = [0]
    end = []
    for j in range(H-1):
        if i >> j & 1 == 1:
            j += 1
            start.append(j)
            end.append(j)
    end.append(H)
    L = len(end)
    # 横に割った回数
    divcnt = L-1
    # 横に割った区割りごとの白チョコの個数
    wcnt = [0]*L
    # 探索列の横に割った区割りごとの白チョコの個数
    tmp = [0]*L
    for w in range(W):
        # 探索列の直前で縦に割ったか
        flag = False
        for idx in range(L):
            cnt = 0
            for h in range(start[idx],end[idx]):
                cnt += S[h][w]
                if cnt > K: break
            else:
                tmp[idx] = cnt
                wcnt[idx] += cnt
                if wcnt[idx] > K: flag = True
                continue
            break
        else:
            if flag:
                # 縦に割ったらその列から数え直し
                wcnt = tmp[:]
                divcnt += 1
            continue
        break
    else:
        ans = min(ans,divcnt)

print(ans)


Measured memory_bytes: 45660
import math
N=int(input())

def isPrime(n):
  for k in range(2,int(math.sqrt(n))+2):
    if n%k==0:
      return False
  else:
    return True

plist=[2]
p=11
while(len(plist)<N):
  if p%5==1 and isPrime(p):
    plist.append(p)
  p+=2
    
print(" ".join(map(str,plist)))

Measured memory_bytes: 3064
#include <iostream>
using namespace std;
int main()
{
	int N, X, T;
	N = 0; X = 0; T = 0;
	cin >> N >> X >> T;
	if (1 <= N && N <= 1000 && 1 <= X && X <= 1000 && 1 <= T && T <= 1000) {
		if (N%X == 0) {
			cout << T * (N / X) << endl;
		}
		else{
			cout << T * (1 + N / X) << endl;
		}
	}
	return 0;
}

Measured memory_bytes: 3592
#include<bits/stdc++.h>
#define INTMAX 2147483647LL
#define PII pair<int,int>
#define MK make_pair
#define re register
#define Eps (1e-10)
#define Equal(a,b) (fabs((a)-(b))<Eps)
using namespace std;
typedef long long ll;
typedef double db;
const double Pi=acos(-1.0);
const int Inf=0x3f3f3f3f;

static const int COUNTER_CLOCKWISE = 1;
static const int CLOCKWISE = -1;
static const int ONLINE_BACK = 2;
static const int ONLINE_FRONT = -2;
static const int ON_SEGMENT = 0;

inline int read(){
	re int x=0,f=1,ch=getchar();
	while(!isdigit(ch))f=ch=='-'?-1:1,ch=getchar();
	while(isdigit(ch))x=x*10+ch-48,ch=getchar();
	return x*f;
}
inline ll readll(){
	re ll x=0,f=1,ch=getchar();
	while(!isdigit(ch))f=ch=='-'?-1:1,ch=getchar();
	while(isdigit(ch))x=x*10+ch-48,ch=getchar();
	return x*f;
}

struct Point{
	db x,y;
	Point(){}
	Point(db xx,db yy):x(xx),y(yy){}
	inline Point operator +(const Point &p){return Point(x+p.x,y+p.y);}
	inline Point operator -(const Point &p){return Point(x-p.x,y-p.y);}
	inline Point operator *(const db &k){return Point(x*k,y*k);}
	inline Point operator /(const db &k){return Point(x/k,y/k);}
	inline db Norm(){return x*x+y*y;}
	inline db abs(){return sqrt(Norm());}
	inline bool operator <(const Point &p)const{return x!=p.x?x<p.x:y<p.y;}
	inline bool operator ==(const Point &p)const{return fabs(x-p.x)<Eps&&fabs(y-p.y)<Eps;}
}; 

typedef Point Vector;
typedef vector<Point> Poly;

struct Segment{
	Point p1,p2;
	Segment(){}
	Segment(Point p1_,Point p2_):p1(p1_),p2(p2_){}
};
typedef Segment Line;
struct Circle{
	Point c;db r;
	Circle(Point cc,db rr):c(cc),r(rr){}
};

double norm(Vector a){return a.x*a.x+a.y*a.y;}
double abs(Vector a){return sqrt(norm(a));}
double dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
double cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}

inline bool Is_Vertical(Vector a,Vector b){return Equal(dot(a,b),0.0);}
inline bool Is_Vertical(Point a1,Point a2,Point b1,Point b2){return Is_Vertical(a1-a2,b1-b2);}
inline bool Is_Vertical(Segment s1,Segment s2){return Equal(dot(s1.p2-s1.p1,s2.p2-s2.p1),0.0);}

inline bool Is_Parallel(Vector a,Vector b){return Equal(cross(a,b),0.0);}
inline bool Is_Parallel(Point a1,Point a2,Point b1,Point b2){return Is_Parallel(a1-a2,b1-b2);}
inline bool Is_Parallel(Segment s1,Segment s2){return Equal(cross(s1.p2-s1.p1,s2.p2-s2.p1),0.0);}

Point Project(Segment s,Point p){
	Vector bse=s.p2-s.p1;
	double res=dot(p-s.p1,bse)/norm(bse);
	return s.p1+bse*res;
}
Point Reflect(Segment s,Point p){return p+(Project(s,p)-p)*2.0;}

inline int CCW(Point p0,Point p1,Point p2){
	Vector a=p1-p0,b=p2-p0;
	if(cross(a,b)>Eps) return COUNTER_CLOCKWISE;
	if(cross(a,b)<-Eps)return CLOCKWISE;
	if(dot(a,b)<-Eps)  return ONLINE_BACK;
	if(a.Norm()<b.Norm()) return ONLINE_FRONT;
	return ON_SEGMENT; 
}

Point p1,p2,p3,p4;
int n,q;

int main(){
	n=read();
	while(n--){
		cin>>p1.x>>p1.y>>p2.x>>p2.y>>p3.x>>p3.y>>p4.x>>p4.y;
		if(Is_Parallel(p1,p2,p3,p4)) printf("2\n");
		else if(Is_Vertical(p1,p2,p3,p4)) printf("1\n");
		else printf("0\n");
	}
	return 0;
}


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

const int INF = (int)1e9;
signed main(void)
{
	int n, k;
	cin >> n >> k;
	int h[n], dp[n];
	rep(i, n) cin >> h[i];
	rep(i, n) dp[i] = INF;
	dp[0] = 0;
	for(int i = 1; i < n; i++)
	{
		for(int j = 1; j <= k; j++)
		{
			if (i - j >= 0)
			dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]));
		}
	}
	cout << dp[n - 1] << endl;
	return 0;
}


Measured memory_bytes: 1024
s=gets.chomp
answer="Yes"
m=s.split("")
s.size.times do |i|
  k=2*i
  l=2*i+1
  break if k > s.size || l > s.size
  if m[k]=="L"||m[l]=="R" then
    answer = "No"
    break
  end
end
puts answer


Measured memory_bytes: 1788
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,n) for(int i=0;i<(n);i++)
#define MOD 1000000007
typedef long long ll;
template <class T> inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}else{return 0;}}
template <class T> inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}else{return 0;}}
template <class T> inline T GCD(T a,T b){T c;while(b!=0){c=a%b;a=b;b=c;}return a;}
template <class T> inline T LCM(T a,T b){return a*b/GCD(a,b);}

int main(){
  ll n;
  cin >> n;
  ll d[100010];
  FOR(i,n){
    cin >> d[i];
  }
  sort(d,d+n);
  int ans=0;
  FOR(i,n){
    if(i==n/2-1){
      ans=d[i+1]-d[i];
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 1024
n, k = map(int, input().split())
A = list(map(int, input().split()))
for i in range(k):
  C = [0]*(n+1)
  for j in range(n):
    p = max(0, j-A[j])
    q = min(n-1, j+A[j])
    C[p] += 1
    C[q+1] -= 1
  v = 1
  for j in range(n):
    C[j+1]=C[j+1]+C[j]
    if C[j]!=n:
      v = 0
  if v:
    break
  A = C
C = [str(c) for c in C[:-1]]
print(' '.join(C))

Measured memory_bytes: 159200
import java.io.*;
import java.util.*;
import static java.lang.System.out;

public class Main {
    static MyReader in = new MyReader();

    public static void main(String[] args){
        int Q = in.i();
        for (int i = 0; i < Q; i++) {
            int A = in.i();
            int B = in.i();
            long pro = (long)A * B;
            long root = (long)Math.sqrt(pro);
            long ans = root + (pro - 1) / (root + 1) - 1;
            if (pro == root * root && A != B) {
                ans--;
            }
            out.println(ans);
        }
    }
}

class MyReader extends BufferedReader {
    char[] cbuf = new char[1024];
    int head = 0;
    int tail = 0;

    MyReader() {
        super(new InputStreamReader(System.in));
    }

    char next() {
        if (head == tail) {
            try {
                tail = read(cbuf, 0, cbuf.length);
            } catch (IOException e) {}
            head = 0;
        }
        return cbuf[head++];
    }

    void back() {
        head--;
    }

    boolean minus() {
        boolean minus;
        while (true) {
            char c = next();
            if (c != ' ' && c != '\n' && c != '\r') {
                if (!(minus = c == '-')) back();
                return minus;
            }
        }
    }

    void skip() {
        while (true) {
            char c = next();
            if (c != ' ' && c != '\n' && c != '\r') {
                back();
                break;
            }
        }
    }

    char[] s(final int N) {
        skip();
        char[] s = new char[N];
        for (int i = 0; i < s.length; i++) {
            s[i] = next();
        }
        return s;
    }

    int i() {
        boolean minus = minus();
        int n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    }

    int[] ii(final int N) {
        int[] a = new int[N];
        for (int j = 0; j < a.length; j++) a[j] = i();
        return a;
    }

    long l() {
        boolean minus = minus();
        long n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    }
}


Measured memory_bytes: 32620
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main(){
    string s;
    cin >> s;
    bool f=1;
    if(s.at(0)!='a'&&s.at(1)!='a'&&s.at(2)!='a')f=0;
    if(s.at(0)!='b'&&s.at(1)!='b'&&s.at(2)!='b')f=0;
    if(s.at(0)!='c'&&s.at(1)!='c'&&s.at(2)!='c')f=0;
    cout << (f?"Yes":"No") << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <iomanip>

using namespace std;

long long MOD = 1000000007;

int main(){
    string S;
    cin >> S;
    string akiba="AKIHABARA";
    int i=0;
    int j=0;
    int cnt=(int) S.size();

    while(true){
        if(cnt>9){
            cout << "NO" << endl;
            return 0;
        }
        if(j==9){
            cout << "YES" << endl;
            return 0;
        }

        if(S[i]!=akiba[j]){
            if(akiba[j]=='A'){
                cnt++;
                j++;
            }else{
                cout << "NO" << endl;
                return 0;
            }
        }else{
            i++;
            j++;
        }
    }

}

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

#include <boost/range/adaptors.hpp>
#include <boost/range/irange.hpp>

using namespace std;
using namespace boost;
using namespace boost::adaptors;

int main() {
    int64_t n;
    cin >> n;

    vector<int64_t> a(n), b(n);
    int64_t m = numeric_limits<int64_t>::max(), s = 0;
    for (auto i : irange(0L, n)) {
        cin >> a[i] >> b[i];
        s += a[i];
        if (a[i] > b[i]) {
            m = min(m, b[i]);
        }
    }

    if (m == numeric_limits<int64_t>::max()) {
        cout << 0 << endl;
        return 0;
    }

    cout << s - m << endl;
}

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

using namespace std;

#ifdef DGC
#include "debug.h"
#else
#define debug(...) 9715
#endif
typedef long long ll;
typedef long double ld;
typedef complex<double> point;
#define F first
#define S second



int main()
{
	#ifdef DGC
		//freopen("a.in", "r", stdin);
		//freopen("b.out", "w", stdout);
	#endif

	ios_base::sync_with_stdio(0), cin.tie(0);

	int n;
	cin >> n;
	vector<int> a(n), b(n);
	for (auto &i : a) cin >> i;
	for (auto &i : b) cin >> i;

	const int inf = 1e9;
	vector<vector<int>> dp(2*n+1, vector<int>(1<<n, inf));
	dp[2*n][0] = 0;

	vector<int> f(n);
	for (int i = 0; i < 1<<n; ++i)
	{
		int bp = __builtin_popcount(i);
		for (int k = 0; k <= 2*n; ++k)
		{
			int val = (k < 2*n) ? ( (k < n) ? a[k] : b[k-n] ) : 0;

			for (int j = n-1, x = 0; j >= 0; --j)
			{
				x += i>>j&1;
				f[j] = x;
			}

			for (int j = 0; j < n; ++j)
				if (~i>>j&1)
				{
					int cost = j+f[j] - bp;
					f[j] = (f[j] ^ cost) & 1;
					int val2 = f[j] ? b[j] : a[j];
					if (val2 >= val)
						dp[f[j]*n+j][i|1<<j] = min(dp[f[j]*n+j][i|1<<j], dp[k][i] + cost);
				}
		}
	}

	int ans = inf;
	for (int k = 0; k <= 2*n; ++k)
		ans = min(ans, dp[k][(1<<n)-1]);

	if (ans == inf) ans = -1;
	cout << ans << "\n";

	return 0;
}


Measured memory_bytes: 40316
#include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cctype>
#include<map>
#include<cstring>

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

using namespace std;
using ll = long long int;

const int inf = 1001001000;
const long long int Inf = 1001001001001001000;


void print(vector<vector<int>> a){
	for (int i = 0; i < a.size(); i++)
	{
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<vector<long long int>> a){
	for (int i=0;i<a.size();i++){
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(set<int> a){
	for (auto x:a)
		cout << x << " ";
	cout << endl;
}



int main() {
	int n;
	cin >> n;
	rep(i,9){
		rep(j,9){
			if((i+1)*(j+1)==n){
				cout << "Yes" << endl;
				return 0;
			}
		}
	}
	cout << "No" << endl;
	return 0;
}

Measured memory_bytes: 256
#include"stdio.h"
#include"string.h"

int table[400][400];//??????????????¨???????????§?¨?(1?????¨????????£?????¢????????¨???????????????table[y??§?¨?][x??§?¨?])
int N;              //??\???????????°
int plate[200][2];  //????????§??????????????£?????¢?????§?¨?
int input[2];       //??\?????????
int i,j;            //?????????
int small,big;      //??£?????¢??????????????£???
int ansLR,ansUD;    //??????

int max(int a,int b){
	if(a>b)return a;
	else   return b;
}
int min(int a,int b){
	if(a<b)return a;
	else   return b;
}

int main(void){
	while(scanf("%d",&N)!=EOF){
		if(N==0)break;
		//?????????
		memset(table,0,400*400*sizeof(int));
		memset(plate,0,200*2*sizeof(int));
		table[200][200]=1;//?????????1???????????????????????????
		plate[0][0]=200;
		plate[0][1]=200;
		small=-1;
		big=-1;
		ansLR=-1;
		ansUD=-1;
		
		for(i=1;i<N;i++){
			//??\???
			scanf("%d %d",&input[0],&input[1]);
			//????????????????????????
			switch(input[1]){
			case 0:
			case 2:
				//??£?????¢??????????????´????????????????????§???????????´???????¨????
				plate[i][0]=plate[input[0]][0];
				plate[i][1]=plate[input[0]][1]+(input[1]-1);
				break;
			case 1:
			case 3:
				plate[i][0]=plate[input[0]][0]-(input[1]-2);
				plate[i][1]=plate[input[0]][1];
				break;
			}
			//??????????????¨???table????¨????
			table[plate[i][0]][plate[i][1]]=1;
		}
		small=200;
		big=200;
		//??¨??¨????????£????????§?????????????¢?????????????
		//?¨?????¢????
		for(i=0;i<400;i++){
			for(j=0;j<400;j++){
				if(table[i][j]==1){
					//small?????£?????¢??????????????????????????????big??????
					small=min(small,j);
					big=max(big,j);
				}
			}
			//??????????????¨??????
			if(small==-1 && big ==-1)continue;
			else if(big==-1)ansLR=max(ansLR,1);
			else ansLR=max(ansLR,big-small+1);
		}
		small=200;
		big=200;
		//???????¢????
		for(j=0;j<400;j++){
			for(i=0;i<400;i++){
				if(table[i][j]==1){
					//small?????£?????¢??????????????????????????????big??????
					small=min(small,i);
					big=max(big,i);
				}
			}
			//??????????????¨??????
			if(small==-1 && big ==-1)continue;
			else if(big==-1)ansUD=max(ansUD,1);
			else ansUD=max(ansUD,big-small+1);
		}
		printf("%d %d\n",ansLR,ansUD);
		/*
		for(i=190;i<210;i++){
			for(j=190;j<220;j++){
				printf("%d ",table[i][j]);
				//if(i==200 && j==200)printf(".");
			}
			printf("\n");
		}
		*/
	}
	return 0;
}

Measured memory_bytes: 1232
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LINF = 1e18;
using namespace std;
 
#define dump(x)  cout << #x << " = " << (x) << endl;
#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl
#define POSSIBLE(n) cout << ((n) ? "POSSIBLE" : "IMPOSSIBLE"  ) << endl
#define Possible(n) cout << ((n) ? "Possible" : "Impossible"  ) << endl
#define possible(n) cout << ((n) ? "possible" : "impossible"  ) << endl
 
#define SANKOU(n,a,b) cout << ((n) ? (#a) : (#b) ) << endl
 
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPR(i,n) for(int i=n;i>=0;i--)
 
#define FOREACH(x,a) for(auto& (x) : (a) )
 
#define WFA(d,v) REP(k,v)REP(i,v)REP(j,v)d[i][j]=min(d[i][j],d[i][k]+d[k][j])
 
#define SCOUT(x) cout<<(x)<<" "
#define ENDL cout<<endl
 
#define VECCIN(x) for(auto&youso_: (x) )cin>>youso_
#define VECIN2(x,y) REP(i,x.size())cin>>x[i]>>y[i]
#define VECCOUT(x) for(auto tt=x.begin();tt!=x.end();tt++){if(tt!=x.begin())cout<<" ";cout<<(*tt);}cout<<endl
 
#define ALL(obj) (obj).begin(),(obj).end()
 
#define EXIST(n,x) (find(ALL(n),x)!=n.end())
#define UNIQUE(obj) sort(ALL( obj )); obj.erase(unique(ALL(obj)),obj.end())
#define COUT(x) cout<<(x)<<endl
void CINT(){}
template <class Head,class... Tail>
void CINT(Head&& head,Tail&&... tail){
    cin>>head;
    CINT(move(tail)...);
}
#define CIN(...) int __VA_ARGS__;CINT(__VA_ARGS__)
#define LCIN(...) ll __VA_ARGS__;CINT(__VA_ARGS__)
#define SCIN(...) string __VA_ARGS__;CINT(__VA_ARGS__)
 
template <class T = ll>
T IN(){T x;cin>>x;return (x);}
 
template <class Head>
void VT(Head head){}
template <class Head,class Seco,class... Tail>
void VT(Head&& head,Seco&& seco,Tail&&... tail){
    seco.resize(head);
    VT(head,move(tail)...);
}
void VT2(){}
template <class Head,class... Tail>
void VT2(Head&& head,Tail&&... tail){
  VECCIN(head);
  VT2(move(tail)...);
}
 
template <class Head>
void VT3(Head&& head){}
template <class Head,class Seco,class... Tail>
void VT3(Head&& head,Seco&& seco,Tail&&... tail){
  seco[head]=IN();
  VT3(head,move(tail)...);
}
 
#define VC1(n,...) V __VA_ARGS__;VT(n,__VA_ARGS__);VT2(__VA_ARGS__); //aaabbbccc
#define VC2(n,...) V __VA_ARGS__;VT(n,__VA_ARGS__);REP(i,n)VT3(i,__VA_ARGS__); //abcabcabc
 
// #include <boost/multiprecision/cpp_int.hpp>
// using namespace boost::multiprecision; // cpp_int
 
#define P pair<int,int>
#define V vector<ll>
#define M map<ll,ll>
#define S set<int>
#define PQ priority_queue<int>
#define PQG priority_queue<int,V,greater<int>>
 
#define str string
 
#define pb(a) push_back(a)
#define mp make_pair

int main(){
  CIN(n);
  VC1(n,a);
  ll sum = accumulate(ALL(a),0LL);
  ll one = 0;REP(i,n)one += i+1;
  if(sum%one){YES(0);return 0;}
  ll count = sum / one;
  V diff(n,0);
  FOR(i,1,n)diff[i]=a[i]-a[i-1];
  diff[0]=a[0]-a[n-1];
  //VECCOUT(diff);
  int ans=0;
  REP(i,n)if(diff[i]-count){
    if(count-diff[i]<0){YES(0);return 0;}
    if((count-diff[i])%n){YES(0);return 0;}
    ans+=(count-diff[i])/n;
    //dump(i);
  }
  YES(ans==count);

  return 0;
}


Measured memory_bytes: 1792