akhauriyash/Code-Regression huggingface dataset:
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <map>
#include <set>
#include <functional>
#include <iostream>
#define INF 500
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

struct edge{
	int to,cap,rev;
	edge(){}
	edge(int tt,int cc,int rr){
		to=tt;
		cap=cc;
		rev=rr;
	}
};

vector<edge> G[60000];
bool used[60000];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

void add_edge(int f,int t,int cap){
	G[f].push_back(edge(t,cap,G[t].size()));
	G[t].push_back(edge(f,0,G[f].size()-1));
}

int dfs(int v,int t,int f){
	if(v==t)return f;
	used[v]=true;
	for(int i=0;i<G[v].size();i++){
		edge &e=G[v][i];
		if(!used[e.to] && e.cap>0){
			int d=dfs(e.to,t,min(f,e.cap));
			if(d>0){
				e.cap-=d;
				G[e.to][e.rev].cap+=d;
				return d;
			}
		}
	}
	return 0;
}

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

int h,w;
int fie[101][101];

int solve(){
	int S=h*w*2,T=S+1;
	for(int i=0;i<h;i++){
		if(fie[i][0]==1)return -1;
		add_edge(i*w*2+1,T,INF);
		if(fie[i][w-1]==1)return -1;
		add_edge(((i+1)*w-1)*2+1,T,INF);
	}
	for(int i=0;i<w;i++){
		if(fie[0][i]==1)return -1;
		add_edge(i*2+1,T,INF);
		if(fie[h-1][i]==1)return -1;
		add_edge(((h-1)*w+i)*2+1,T,INF);
	}
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
			if(fie[i][j]==1){
				add_edge((i*w+j)*2,(i*w+j)*2+1,INF);
				add_edge(S,(i*w+j)*2+1,INF);
			}else{
				add_edge((i*w+j)*2,(i*w+j)*2+1,1);
			}
			for(int k=0;k<4;k++){
				int nx=j+dx[k],ny=i+dy[k];
				if(nx>=0 && nx<w && ny>=0 && ny<h){
					add_edge((i*w+j)*2+1,(ny*w+nx)*2,INF);
				}
			}
		}
	}
	int res=max_flow(S,T);
	return res==INF?-1:res;
}

int main(void){
	scanf("%d%d",&h,&w);
	for(int i=0;i<h;i++){
		string s;
		cin >> s;
		for(int j=0;j<w;j++){
			if(s[j]=='X'){
				fie[i][j]=1;
			}
		}
	}
	printf("%d\n",solve());
	return 0;
}

Measured memory_bytes: 4608
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

int main()
{
    char str_tbd[12];
    cin >> str_tbd;
    int n5=str_tbd[5];
    int n6=str_tbd[6];
    if(n5==48 && n6<=52){
        cout << "Heisei";
    }else{
        cout << "TBD";
    }
    return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int main(){
  long n;
  long a[200000];
  long k = 1;
  
  scanf("%ld", &n);
  for(long i = 0; i < n; i++){
    scanf("%ld", &a[i]);
  }
  
  for(long i = 0; i < n; i++){
    if(a[i] == k){
      k++;
    }
  }
  
  if(k == 1){
    printf("-1\n");
  }
  else{
    printf("%ld\n", n - k + 1);
  }
  
  
  return 0;
}


Measured memory_bytes: 1664
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#define p pair <int,int>
using namespace std;
int n;
int main(int argc,char const* argv[])
{
  cin >> n;
  vector<int> T(n);
  vector<int> A(n);
  for(int i=0;i<n;i++)
    {
      cin >> T[i] >> A[i];
    }
  long long int takahashi=T[0];
  long long int aoki=A[0];
  long long int ts,as;
  long long int times;
  for(int i=1;i<n;i++)
    {
      ts=takahashi/T[i];
      as=aoki/A[i];
      if(takahashi % T[i] != 0) ts++;
      if(aoki % A[i] != 0) as++;
      times=max(ts,as);
      aoki=A[i]*times,takahashi=T[i]*times;
    }
  cout << takahashi+aoki << endl;
  return 0;
}


Measured memory_bytes: 256
import sys
n,q=input().split()
s=input();a=[0]*2
for i in range(2,int(n)+1):
 a+=[a[i-1]+s[i-2:i].count("AC")]
for e in sys.stdin:
 l,r=map(int,e.split())
 print(a[r]-a[l])

Measured memory_bytes: 7864
#include<bits/stdc++.h>
#define ll long long 
using namespace std;
int main(){
 ll l,d,r;
 cin>>l>>r>>d;
 ll cnt=0;
 for(int i=l;i<=r;i++)
 {
 	if(i%d==0)
 	cnt++;
 }
 cout<<cnt<<endl;
}

Measured memory_bytes: 3620
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
 
long int N,a[3001],A[3001][3001]={};

long int dp(int n,int m)
{
  long int k,temp,temp1,temp2;
  
  if(A[n][m]>0)
  {
    return A[n][m];
  }
  else if(n==m)
  {
    temp=a[n];
  }
  else if(n==m-1)
  {
    temp=fmax(a[n],a[m]);
  }
  else
  {  
    temp1=fmin(dp(n+1,m-1),dp(n+2,m));
    temp2=fmin(dp(n,m-2),dp(n+1,m-1));
    temp=fmax(a[n]+temp1,a[m]+temp2);
  }
  
  A[n][m]=temp;
  
  return temp;
  
}
 
int main()
{
  
  int i,j,x;
  long int total;
  scanf("%d",&N);
  
  total=0;
  for(i=1;i<N+1;i++)
  {
    scanf("%d",&a[i]);
    total+=a[i];
  }
  
  printf("%ld",2*dp(1,N)-total);
  
  
  return 0;
}


Measured memory_bytes: 70016
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] X = new int[N];
        double sum = 0;
        for (int i = 0; i < N; i++) {
            X[i] = sc.nextInt();
            sum += X[i];
        }
        int P = (int)Math.round(sum/N);
        
        int dis=0;

        for (int i = 0; i < N; i++) {
            dis+=Math.pow((X[i]-P), 2);
        }
        System.out.println(dis);
    }
}

Measured memory_bytes: 23764
#include<iostream>
#include<string>
using namespace std;
int main()
{
	int a,b,c;
	int m=0;
	cin>>a>>b>>c;
	for(int i=a;i<=10000;i=i+a)
	{
		if(i%b==c)
		{
			m=1;
		}
	}
	if(m==0)
	{
		cout<<"NO"<<endl;
	}
	else
	{
		cout<<"YES"<<endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); 
for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b) { return a/gcd(a,b)*b;}
// head
const int mxN = 400;
int n;
ll a[mxN], pf_sum[mxN + 1], dp[mxN][mxN];
int main() {
	ios::sync_with_stdio(0); 
	cin.tie(0); cout.tie(0);
	
	cin >> n;
	rep(i, 0, n) {
		cin >> a[i];
		pf_sum[i+1] = pf_sum[i] + a[i];
	}
	memset(dp, 0x3f, sizeof(dp));
	rep(len, 1, n+1){
		rep(l, 0, n){
			int r = l + len - 1;
			if(r >= n) break;
			if(len == 1){
				dp[l][r] = 0;
			}
			else if(len == 2){
				dp[l][r] = a[l] + a[r];
			}
			else{
				rep(k, l, r){
					dp[l][r] = min(dp[l][r], dp[l][k] + dp[k+1][r]);
				}
				dp[l][r] += pf_sum[r+1] - pf_sum[l];
			}
			
		}
	}
	cout << dp[0][n-1] << endl;
}




Measured memory_bytes: 1536
import math


def is_prime(n):
    if n == 1:
        return False

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

    return True


q = int(input())
scope = [tuple(map(int, input().split())) for _ in range(q)]
prime_cnt = [0] * (10 ** 5 + 1)
for i in range(1, 10 ** 5 + 1):
    prime_cnt[i] = prime_cnt[i - 1]
    if ((i + 1) / 2) % 1 == 0 and is_prime(i) and is_prime((i + 1) / 2):
        prime_cnt[i] += 1

for i in range(q):
    left = scope[i][0]
    right = scope[i][1]
    print(prime_cnt[right] - prime_cnt[left - 1])

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

int main() {
    int n;cin>>n;
    int a[n],b[n],ans=0;
    
    for(int i=0;i<n;++i){
        cin>>a[i];
        if(a[i]==i+1)b[i]=1;
        else b[i]=0;
    }
    for(int i=0;i<n-1;++i){
        if(b[i]==1 && b[i+1]==1){
            b[i+1]=0;
            ans+=1;
        }
        else if(b[i]==1)ans+=1;
    }
    if(b[n-1]==1)ans+=1;
    
    cout<<ans<<endl;

	return 0;
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
#define MOD 1000000007
#define IO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
using namespace std;

main()
{
	IO;
	int n;
	cin>>n;
	
	vector<long double> v(n),dp(n+1,0);
	
	for(int i=0;i<n;i++)
	cin>>v[i];
	
	dp[1]=v[0];
	dp[0]=1-v[0];
	
	for(int i=1;i<n;i++)
	{
		for(int j=i+1;j>0;j--)
		{
			dp[j]=dp[j-1]*v[i]+dp[j]*(1-v[i]);
			//dp[j-1]=dp[j-1]*(1-v[i]);
		}
		dp[0]=dp[0]*(1-v[i]);
	}
	
	long double ans=0;
	
	for(int i=(n+1)/2;i<=n;i++)
	ans+=dp[i];
	
	cout<<setprecision(10)<<ans;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define set_zero(a) memset(a,0,sizeof(a));
#define ll long long
#define PI 2 * acos(0.0)
#define MAX 1000000000
#define INF 1000000 
#define M 1000000007
using namespace std;
int main(){
   fast_io
   string s;
   cin>>s;
   map<ll,ll> mp;
   for(ll i = 0;i<9;i++){
      mp[i] = 0;
   }
   for(ll i = 0;i<s.length();i++){
      ll y = s[i] - '0';
      mp[y]++;
   }
   ll ans = 0;
   for(ll i = 0;i<9;i++){
      ans += (mp[i]*i);
   }
   if(ans%9 == 0){
      cout<<"Yes"<<endl;
   }
   else{
      cout<<"No"<<endl;
   }
   return 0;
}

Measured memory_bytes: 3712
#include <bits/stdc++.h>
#define REP(i, a, n) for(ll i = ((ll) a); i < ((ll) n); i++)
#define INF (1LL << 60)
using namespace std;
typedef long long ll;

int main(void) {
  ll N;
  cin >> N;
  vector<ll> X(N + 1);
  REP(i, 0, N) cin >> X[i];
  X[N] = INF;
  ll L, Q;
  cin >> L >> Q;
  vector<ll> A(Q), B(Q);
  REP(i, 0, Q) {
    cin >> A[i] >> B[i]; A[i]--; B[i]--;
    if(A[i] > B[i]) swap(A[i], B[i]);
  }

  vector< vector<ll> > p(20, vector<ll>(N + 1));
  REP(i, 0, N) {
    ll j = upper_bound(X.begin(), X.end(), X[i] + L) - X.begin() - 1;
    p[0][i] = j;
  }
  p[0][N] = N;
  REP(k, 0, p.size() - 1) REP(i, 0, N) p[k + 1][i] = p[k][p[k][i]];

  REP(i, 0, Q) {
    ll ans = 0, t = A[i];
    for(ll j = p.size() - 1; j >= 0; j--) {
      if(p[j][t] < B[i]) {
        t = p[j][t];
        ans += pow(2LL, j);
      }
    }
    cout << ans + 1 << endl;
  }
}


Measured memory_bytes: 19072
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
#include <boost/range/adaptor/reversed.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
typedef long long int ll;

#define PI (acos(-1))
#define REP(i,n) for(ll (i)=0;(i)<(n);(i)++)

#define dump(x)  cerr << #x << " = " << (x) << endl;
#define dumparr(x) cerr << #x ; for(auto elem: x){cerr << " " << elem;} cerr << endl;
#define dumppairs(x) cerr << #x ; for(auto elem: x){cerr << "(" << elem.first << " " << elem.second << ") ";} cerr << endl;
#define dumpline cerr << "**********************" << endl;
#define dumpwline(x) cerr << "******** " << #x << "=" << (x) << " ********" << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0]))

int main(void){
    ll k; cin >> k;
    ll x; cin >> x;
    if (500*k >= x){
        cout << "Yes" << endl;
    } else {
        cout << "No" << endl;
    }
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define RREP(i,a,b) for(int i=(a);i>=(b);i--)
#define pq priotity_queue
#define P pair<int,int>
#define P2 pair<int,P>
#define P3 pair<int,P2>
typedef long long ll; typedef long double ld;
using namespace std;
const int INF=1e9, MOD=1e9+7, around[]={0,1,1,-1,-1,0,-1,1,0,0};
const ll LINF=1e18;
const ld PI=abs(acos(-1));
string s;

int main(){
	cin >> s;
	
	REP(i,0,(1<<3)){
		int pos=s[0]-'0';
		
		REP(j,0,3){
			if((i>>j)&1) pos+=s[j+1]-'0';
			else pos-=s[j+1]-'0';
		}
		
		if(pos==7){
			REP(j,0,3){
				cout << s[j];
				if((i>>j)&1) cout << '+';
				else cout << '-';
			}
			cout << s[3] << "=7" << endl;
			break;
		}
	}
	
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define ff first
#define ss second
using namespace std;
/*ll power(ll x,ll y)
{
    ll res=1;
    while(y>0)
    {
        if(y%2==1)
            res=(res*x)%mod;
        x=(x*x)%mod;
        y=y/2;
    }
    return res;
}*/
int main()
{
ll a,b,i,n,r=0,t=0;
cin>>n;
vector<pair<ll,ll> >v;
for(i=0;i<n;i++)
{
    cin>>a>>b;
    v.pb({b,a});
}
sort(v.begin(),v.end());
for(i=0;i<n;i++)
{
    t=t+v[i].ss;
    if(t>v[i].ff)
    {
        cout<<"No"<<endl;
        return 0;
    }
}
cout<<"Yes"<<endl;
}


Measured memory_bytes: 6256
import java.util.Scanner;
public class Main{
public void solve(){
    Scanner sc=new Scanner(System.in);
    int x=sc.nextInt();
    int h=x/3600;
    int m=(x-3600*h)/60;
    int s=(x-3600*h-60*m);
    
    System.out.println(h+":"+m+":"+s);
}
    public static void main(String[]args){
        Main obj=new Main();
        obj.solve();
    }
}

    

Measured memory_bytes: 17356
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
#include<vector>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#include"debug_deque.hpp"
#define vector DebugDeque
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(prints) FUNC(printe) FUNC(printw) FUNC(printew) FUNC(printb) FUNC(printd) FUNC(printde) FUNC(printdu);
#endif
template <class S, class T>
istream& operator >> (istream& in, pair<S, T>& p){ in >> p.first >> p.second; return in; }
template <class T>
istream& operator >> (istream& in, vector<T>& v){ for(auto& e : v){ in >> e; } return in; }

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m;
    cin >> n >> m;
    vector<int> as(n); cin >> as;
    map<ll, int> st;
    ll ans = 0, acc = 0;
    for(int i = 0; i < n; i++){
        acc += as[i];
        acc %= m;
        if(acc == 0){
            ans++;
        }
        ans += st[acc];
        st[acc]++;
    }
    printde(st);
    cout << ans << '\n';
}


Measured memory_bytes: 4608
import java.util.*;

class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        long n = scan.nextLong();
        long k = scan.nextLong();
        scan.close();

        if(k == 1 || n == k) System.out.println("0");

        else if(k == 2){
            if(n % k == 0) System.out.println("0");
            else System.out.println("1");
        }

        else{
            while(true){
                if(n * 2 <= k) break;
                else if(Math.abs(n - k) > n) break;
                else if(n >= (long)Math.pow(k,2)) n = n % k;
                else n = Math.abs(n - k);
            }
            System.out.println(n);
        }
    }
}

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

#define rep(i, a, b) for(int i = (a); i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define endl '\n'

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;

const ll mod = 1000000007;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
// head

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

    int n, k;
    cin >> n >> k;
    vi a(n);
    vector<bool> dp(k + 1, true);

    dp[0] = false; // taro doesnt win

    rep(i,0,n) cin >> a[i];

    for(int i = 1; i <= k; i++) {
        bool ok = false;
        for(int j = 0; j < n; j++) {
            if(i >= a[j]) {
                ok |= !dp[i - a[j]];
            }
        }
        dp[i] = ok;
    }

    cout << (dp[k] ? "First" : "Second") << endl;

    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<bitset>
using namespace std;
//#define MODE 1
#ifdef MODE
#define DEB(X) cout<< #X <<": "<<X<<" ";
#define ARDEB(i,X) cout<< #X <<"["<<i<<"]: "<<X[i]<<" ";
#define END cout<<endl;
#else
#define DEB(X) {}
#define ARDEB(i,X) {}
#define END {}
#endif
typedef long long int ll;
typedef pair<int,int> P;
struct edge{int to,cost;};
#define REP(i,n) for(int i=0;i<(n);i++)
const int INF=100000000;

int main(){
	int x,y;
	cin>>x>>y;
	cout<<x+y/2<<endl;
	return 0;
}

Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def YesNo(x): return 'Yes' if x else 'No'
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from collections import defaultdict

def main():
    N = II()
    xy = []
    for _ in range(N):
        xy.append(LI())
    xy2g = defaultdict(int)
    g2xy = defaultdict(set)
    for x, y in xy:
        y = -y
        gx = xy2g[x]
        gy = xy2g[y]
        if gx == 0 or gy == 0:
            g = gx or gy or x
            xy2g[x] = xy2g[y] = g
            g2xy[g] |= {x, y}
        elif gx != gy:  # merge
            if len(g2xy[gx]) < len(g2xy[gy]):
                gx, gy = gy, gx
            for xy in g2xy[gy]:
                xy2g[xy] = gx
            g2xy[gx] |= g2xy[gy]
            g2xy[gy].clear()
    ans = -N
    for xys in g2xy.values():
        nx = 0
        for xy in xys:
            if xy > 0:
                nx += 1
        ny = len(xys) - nx
        ans += nx * ny
    return ans

print(main())

Measured memory_bytes: 114780
#include <bits/stdc++.h>
#define _overload3(_1,_2,_3,name,...)name
#define _rep(i,n)repi(i,0,n)
#define repi(i,a,b)for(int i=int(a),i##_len=(b);i<i##_len;++i)
#define MSVC_UNKO(x)x
#define rep(...)MSVC_UNKO(_overload3(__VA_ARGS__,repi,_rep,_rep)(__VA_ARGS__))
#define all(c)c.begin(),c.end()
#define write(x)cout<<(x)<<'\n'
using namespace std; using ll = long long; template<class T>using vv = vector<vector<T>>;
template<class T>auto vvec(int n, int m, T v) { return vv<T>(n, vector<T>(m, v)); }
constexpr int INF = 1 << 29, MOD = int(1e9) + 7; constexpr ll LINF = 1LL << 60;
struct aaa { aaa() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(10); }; }aaaa;

int main() {
    int N, K;
    cin >> N >> K;
    vector<int> cnt(N);
    rep(i, K) {
        int d;
        cin >> d;
        rep(i, d) {
            int a;
            cin >> a;
            cnt[a - 1]++;
        }
    }

    int ans = 0;
    rep(i, N) ans += cnt[i] == 0;
    write(ans);
}

Measured memory_bytes: 3660
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int d = sc.nextInt();
		
		int x = sc.nextInt();
		ArrayList<Integer> oneD = new ArrayList<Integer>();
		for(int i=0;i<x;i++){
			String s = sc.next();
			int m = sc.nextInt();
			if(s.equals("D")) oneD.add(m);
		}
		Collections.sort(oneD);
		
		int y = sc.nextInt();
		ArrayList<Integer> twoD = new ArrayList<Integer>();
		for(int i=0;i<y;i++){
			String s = sc.next();
			int m = sc.nextInt();
			if(s.equals("DD")) twoD.add(m);
		}
		Collections.sort(twoD);

		int sum = 0;
		
		while(true){
			if(d==0 || oneD.size()==0 && twoD.size()==0) break;
			int d11 = 0;
			int d12 = 0;
			int d2 = 0;
			if(d==1){
				if(oneD.size()>0) d11 = oneD.get(oneD.size()-1);
				sum += d11;
				break;
			}else{
				if(oneD.size()>0) d11 = oneD.get(oneD.size()-1);
				if(oneD.size()>1) d12 = oneD.get(oneD.size()-2);
				if(twoD.size()>0) d2 = twoD.get(twoD.size()-1);
				if(d11+d12<=d2){
					sum += d2;
					twoD.remove(twoD.size()-1);
					d-=2;
				}else{
					sum += d11;
					oneD.remove(oneD.size()-1);
					d--;
				}
			}
		}
		
		System.out.println(sum);

	}	
}

Measured memory_bytes: 17368
//Lowest Common Ancestor
//最小共通先祖
//二部探索を用いる方法

#include<bits/stdc++.h>
using namespace std;
#define MAX_V 100010
#define MAX_LOG_V 100
vector<int> G[MAX_V];                   //グラフの隣接リスト
int root;                               //根の頂点番号
int parent[MAX_LOG_V][MAX_V];           //parent[i][j] := 頂点jから2^i親をたどって到達する頂点
int depth[MAX_V];                       //根からの深さ

void dfs(int v, int p, int d){
    parent[0][v] = p;
    depth[v] = d;

    for(int i = 0; i < G[v].size(); i++){
        if(G[v][i] != p) dfs(G[v][i], v, d + 1);
    }
}

void init(int V){
    //parent[0]とdepthを初期化する
    dfs(root, -1, 0);
    //parentを初期化する
    for(int k = 0; k + 1 < MAX_LOG_V; k++){
        for(int v = 0; v < V; v++){
            if(parent[k][v] < 0) parent[k + 1][v] = -1;
            else parent[k + 1][v] = parent[k][parent[k][v]];
        }
    }
}

//uとvのLCAを求める
int lca(int u, int v){
    //uとvの深さが同じになるまで親をたどる
    if(depth[u] > depth[v]) swap(u, v);
    for(int k = 0; k < MAX_LOG_V; k++){
        if((depth[v] - depth[u] >> k) & 1){    //これあテクい
            v = parent[k][v];
        }
    }

    if(u == v) return u;


    //二部探索でLCAを求める
    for(int k = MAX_LOG_V - 1; k >= 0; k--){
        if(parent[k][u] != parent[k][v]){
            u = parent[k][u];
            v = parent[k][v];
        }
    }

    return parent[0][u];      //uの親を返す
}

int main(){

    //0-indexed
    root = 0;
    int V; cin >> V;
    for(int i = 1; i < V; i++){
        int a; cin >> a;
        a--;
        G[i].push_back(a);
        G[a].push_back(i);
    }

    init(V);

    vector<int> a;                //訪れる頂点の順番
    queue<int> q;
    q.push(0);
    vector<bool> used(V, false);
    used[0] = true;

    while(!q.empty()){
        int next = q.front();
        q.pop();

        a.push_back(next);

        for(int i = 0; i < G[next].size(); i++){
            if(!used[G[next][i]]){
                used[G[next][i]] = true;
                q.push(G[next][i]);
            }
        }
    }

    /*for(int i = 0; i < a.size(); i++){
        cout << a[i] << " ";
    }
    cout << endl;*/
    
    long long ans = 0;
    for(int i = 1; i < a.size(); i++){
        
        int u = a[i - 1];
        int v = a[i];
        int LCA = lca(u, v); 
        //cout << u << " " << v << " " << LCA << endl;
        ans += (long long)(depth[u] + depth[v] - 2*depth[LCA]);
    }

    cout << ans << endl;

    return 0;
}


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

#define rep(i,n) for(int i=0 ; i<(n) ; i++)
#define repr(i,n) for(int i=(n) ; i>=0 ; i--)
#define ALL(x) (x).begin(),(x).end()

typedef long long ll;
typedef unsigned long long ull;
 
const ll INF = 1LL<<60;
const int MINF = 1e9;
const int MOD = 1e9+7;

using vi = vector<int>;
using vii = vector<vi>;

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

template<typename T>
T GCD(T a,T b){ return b ? GCD(b,a%b) : a ;}
template<typename T>
T LCM(T a,T b){ return a*b / GCD(a,b) ;}

template<typename T>//文字列の置換/Boost:replace_all()
T replace_str(T str, string target, string str_to) {
    string::size_type pos = 0;
    while (pos = str.find(target, pos), pos != string::npos) {
        str.replace(pos, target.length(), str_to);
        pos += str_to.length();
    }
    return str;
}

int main(){
    int n,weight;cin >> n >> weight;
    vii knap(n,vi(2));
    rep(i,n){
        int w,v;
        cin >> w >> v;
        knap[i][0] = w;
        knap[i][1] = v;
    }

    vector<vector<ll>> dp(n+100,vector<ll>(weight+1,0));

    for(int i=0;i<n;i++){
        for(ll sum=0;sum<=weight;sum++){
            if(sum-knap[i][0]>=0){
                chmax(dp[i+1][sum],dp[i][sum-knap[i][0]] + knap[i][1]);
            }
            chmax(dp[i+1][sum],dp[i][sum]);
        }
    }
    cout << dp[n][weight] << endl;
    return 0;
}

Measured memory_bytes: 157824
#include<iostream>
#include<math.h>
#include<string.h>
#include<vector>
#include<algorithm>
#include<iomanip>
#include<deque>
using namespace std;

int main(){
	int a,b,c,d;
	cin >> a >> b >> c >> d;
	int ans;
	int ans_max=min(b-a,d-c);
	if(a<c)
	{
		if(c>=b)
		{
			ans=0;
		}
		else
		{
			ans=min(abs(b-c),ans_max);
		}
	}
	else
	{
		if(a>=d)
		{
			ans=0;
		}
		else
		{
			ans=min(abs(d-a),ans_max);
		}
	}
	
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 256
/* Include */
#include <stdio.h>

/* Define */
#define NUM_BUILDINGS 4
#define NUM_FLOORS 3
#define NUM_ROOMS 10

/* Prototype */
void InitList(int list[NUM_BUILDINGS][NUM_FLOORS][NUM_ROOMS]);
void OutputList(int list[NUM_BUILDINGS][NUM_FLOORS][NUM_ROOMS]);
void OutputListBuildings(int val, int list[NUM_BUILDINGS][NUM_FLOORS][NUM_ROOMS]);
void OutputLine();
void ChangeList(int val1, int val2, int val3, int val4, int list[NUM_BUILDINGS][NUM_FLOORS][NUM_ROOMS]);

/* Main */
int main()
{
    int list_A[NUM_BUILDINGS][NUM_FLOORS][NUM_ROOMS];
    int kaisu;
    int b, f, r, v;

    InitList(list_A);

    scanf("%d", &kaisu);

    for (int i = 0; i < kaisu; i++) {
        scanf("%d %d %d %d", &b, &f, &r, &v);
        ChangeList(b, f, r, v, list_A);
    }

    OutputList(list_A);

}

/* Sub */
void InitList(int list[NUM_BUILDINGS][NUM_FLOORS][NUM_ROOMS])
{
    for (int i = 0; i < NUM_BUILDINGS; i++) {
        for (int j = 0; j < NUM_FLOORS; j++) {
            for (int k = 0; k < NUM_ROOMS; k++) {
                list[i][j][k] = 0;
            }
        }
    }
}

void OutputList(int list[NUM_BUILDINGS][NUM_FLOORS][NUM_ROOMS])
{
    OutputListBuildings(0, list);
    OutputLine();
    OutputListBuildings(1, list);
    OutputLine();
    OutputListBuildings(2, list);
    OutputLine();
    OutputListBuildings(3, list);
}

void OutputListBuildings(int num_buildings, int list[NUM_BUILDINGS][NUM_FLOORS][NUM_ROOMS])
{
    for (int i = 0; i < NUM_FLOORS; i++) {
        for (int j = 0; j < NUM_ROOMS; j++) {
            printf(" %d", list[num_buildings][i][j]);
        }
        printf("\n");
    }
}

void OutputLine()
{
    printf("####################\n");
}

void ChangeList(int val1, int val2, int val3, int val4, int list[NUM_BUILDINGS][NUM_FLOORS][NUM_ROOMS])
{
    list[val1 - 1][val2 - 1][val3 - 1] += val4;
}


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

using namespace std;

#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define eb emplece_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define Edge pair< i_i, int>
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
#define int2(x, y) int x, y; scanf("%lld %lld", &x, &y);
#define fir first
#define sec second
#define ffir first.first
#define fsec first.second
#define sfir second.first
#define ssec second.second
 
//int dxy[5] = {0, 1, 0, -1, 0};
// assign
 
signed main()
{
    int2(n, x);
    vi data(n);
    int ans = 0;
    rep(i, n) {
        cin >> data[i];
        ans += data[i];
    }
    vvi dp(n, vi(n));
    rep(i, n) {
        int tmp = dp[i][i] = data[i];
        for (int j = i+1; j < n; j++) {
            tmp = min(data[j], tmp);
            dp[i][j] = tmp;
        }
    }

    for (int i = 2; i < n+1; i++) {
        // 区間のサイズ
//        cout << "===size: " << i << endl;
        
        int sum = 0;
        rep(j, n) {
            // 始点
   //         cout << "point: " << j << " ";
            if (j + 1 < i) {
                sum += min(dp[0][j], dp[n-(i-j-1)][n-1]);
 //               cout << min(dp[0][j], dp[n-(i-j-1)][n-1]) << endl;
            } else {
                sum += dp[j-i+1][j];
  //              cout << dp[j-i+1][j] << endl;
            }
        }
//        cout << "========" << endl;
        ans = min(ans, sum + ((i - 1) * x));
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 33024
#include <bits/stdc++.h>
  using namespace std;
  vector<vector<int>> gr;
  int n, q;
  struct DSU{ 
      vector<int> par, siz;
      int n;
      DSU(int n_) {
          int n = n_ + 1;
          par.resize(n);
          siz.resize(n);
          fill(siz.begin(), siz.end(), 1);
          iota(par.begin(), par.end(), 0);
      }

      int get(int v) {
          if(par[v] == v) {
              return v;
          }
          return par[v] = get(par[v]);
      }
      void uni(int l, int r) {
          l = get(l);
          r = get(r);
          if(l != r) {
              if(siz[l] > siz[r]) {
                  swap(l, r);
			}
			par[l] = r;
			siz[r] += siz[l];
		}
	}
};
main() {
	cin >> n >>	q;

	DSU dsu(n);

	gr = vector<vector<int>> (n + 1, vector<int> ());
	while(q--) {
		int id;
		cin >> id;
		int l, r;
   	cin >> l >> r;
		if(id) {
     	cout << (dsu.get(l) == dsu.get(r)) << endl;
		} else {
  		dsu.uni(l, r);
		}
	}
}

Measured memory_bytes: 9168
#include<iostream>
#include<string>
#include<vector>
#include <algorithm>
#include<math.h>

using namespace std;

int main() {
	int x;
	cin >> x;

	if (x == 2)cout << x << endl;
	else {
		for (int j = x; j < x + 1000; j++) {
			bool judge = true;
			for (int i = 2; i < sqrt(j) + 1; i++) {
				if (j % i == 0) {
					judge = false;
					break;
				}
			}
			//cout << j;
			if (judge == true) {
				cout << j;
				break;
			}
		}
	}
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define rep(i,n,m) for(int i = (n); i <(m); i++)
#define rrep(i,n,m) for(int i = (n) - 1; i >=(m); i--)
using namespace std;
using ll = long long;
using pii = pair<int, int>;
int main()
{
    int n, h;
    cin >> n >> h;
    vector<pii> attack;
    rep(i, 0, n)
    {
        int a, b;
        cin >> a >> b;
        attack.emplace_back(a, 0);
        attack.emplace_back(b, 1);
    }
    sort(attack.begin(), attack.end());
    reverse(attack.begin(), attack.end());
    
    int cnt = 0;
    for (auto att: attack)
    {
        int damage = att.first;
        int a_type = att.second;
        // cout << damage << ' ' << a_type << endl;
        if (a_type == 0)
        {
            cnt += (h + damage - 1) / damage;
            break;
        }
        else
        {
            ++cnt;
            h -= damage;
            if (h <= 0) break;
        }
    }
    cout << cnt << endl;
    return 0;
}

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

int main()
{
	long long a, b;
	cin >> a >> b;

	long long g = __gcd(a, b);
	long long ans = 0;
	
	long long limit = sqrt(g);
	for(int i = 2; i <= limit; i++)
	{
		if(g % i == 0)
		{
			ans++;
			while(g % i == 0)
				g = g / i;
		}
		if(g == 1)
			break;
	}
	if(g != 1)
		ans++;

	cout << ans + 1<< endl;

	return 0;
}

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

using namespace std;

typedef long long ll;

const int maxn = 200000;

int n, d, a;
int t[4 * maxn];
int add[4 * maxn];
vector<pair<int, int>> arr;

void build(int v, int vl, int vr) {
    if (vl == vr) {
        t[v] = arr[vl].second;
    } else {
        int mid = (vl + vr) / 2;
        build(2 * v + 1, vl, mid);
        build(2 * v + 2, mid + 1, vr);
        t[v] = t[2 * v + 1] + t[2 * v + 2];
    }
}

void push(int v, int vl, int vr) {
    if (vl != vr) {
        add[2 * v + 1] += add[v];
        add[2 * v + 2] += add[v];
    }
    t[v] += (vr - vl + 1) * add[v];
    add[v] = 0;
}

void query(int v, int vl, int vr, int l, int r, int value) {
    push(v, vl, vr);
    if (vr < l || r < vl) {
        return;
    }
    if (l <= vl && vr <= r) {
        add[v] = value;
        push(v, vl, vr);
    } else {
        int mid = (vr + vl) / 2;
        query(2 * v + 1, vl, mid, l, r, value);
        query(2 * v + 2, mid + 1, vr, l, r, value);
        t[v] = t[2 * v + 1] + t[2 * v + 2];
    }
}

int get(int v, int vl, int vr, int ind) {
    push(v, vl, vr);
    if (vl == ind && vr == ind) {
        return t[v];
    } else {
        int mid = (vr + vl) / 2;
        if (ind <= mid) {
            return get(2 * v + 1, vl, mid, ind);
        } else {
            return get(2 * v + 2, mid + 1, vr, ind);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    cin >> n >> d >> a;
    arr.resize(n);
    for (auto &i:arr) {
        cin >> i.first >> i.second;
    }
    sort(begin(arr), end(arr));
    build(0, 0, n - 1);
    ll ans = 0;
    int left = 0, right = 0;
    while (left < n) {
        while (right < n - 1 && arr[right + 1].first <= arr[left].first + 2 * d) {
            right++;
        }
        ans += (get(0, 0, n - 1, left) + a - 1) / a;
        ll pow = ((get(0, 0, n - 1, left) + a - 1) / a) * a;
        query(0, 0, n - 1, left, right, -pow);
        while (left < n && get(0, 0, n - 1, left) <= 0) {
            left++;
        }
    }
    cout << ans;
    return 0;
}

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

int main(){
    double N,D;
    int count = 0;
    double s = 0;
    cin >> N >> D;
    vector<double> x (N);
    vector<double> y (N);
    for(double i=0;i<N;i++){
    cin >> x.at(i) >> y.at(i);
    }
    for(double i=0;i<N;i++){
        s = sqrt(x.at(i)*x.at(i) + y.at(i)*y.at(i)); 
        if(s <= D){
            count++;
            
        }
    }
    cout << count << endl;
}

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

using namespace std;

namespace FastRead {
  const int Dim(5000);
  char ibuf[Dim];
  int ipos, ilen;
  char nc() {
    if (ipos == ilen) {
      ipos = 0;
      ilen = fread(ibuf, 1, Dim, stdin);
      if (!ilen) return EOF;
    }
    return ibuf[ipos++];
  }
  template<class T> void read(T& x) {
    char ch;
    int sgn = 1;
    while (!isdigit(ch = nc()))
      if (ch == '-')
        sgn = -1;
    x = ch - '0';
    while (isdigit(ch = nc()))
      x = x * 10 + (ch - '0');
    x *= sgn;
  }
}
using namespace FastRead;

int main () {
  int N;
  read(N);
  int Size = 1;
  while (Size <= N)
    Size <<= 1;
  vector < long long > Tree (Size << 1);
  vector < int > h(N), a(N);
  for (int i = 0; i < N; ++i)
    read(h[i]);
  for (int i = 0; i < N; ++i)
    read(a[i]);
  vector < long long > dp(N + 1);
  for (int flower = 0; flower < N; ++flower) {
    int x = h[flower] + Size;
    long long best = 0;
    while (x > 1) {
      if (x & 1)
        best = max (best, Tree[x - 1]);
      x >>= 1;
    }
    dp[h[flower]] = best + a[flower];
    for (int i = Size + h[flower]; i > 0; i >>= 1)
      Tree[i] = max (Tree[i], dp[h[flower]]);
  }
  long long ans = 0;
  for (int i = 0; i <= N; ++i)
    ans = max (ans, dp[i]);
  cout << ans;
}


Measured memory_bytes: 10316
using System;

public class hello
{
    public static void Main()
    {
        var dx = new int[] { 0, -1, 0, 1 };
        var dy = new int[] { 1, 0, -1, 0 };
        while (true)
        {
            string[] line = Console.ReadLine().Trim().Split(' ');
            var w = int.Parse(line[0]);
            var h = int.Parse(line[1]);
            if (w == 0 && h == 0) break;
            var x = 0; var y = 0; var d = 3;
            while (true)
            {
                line = Console.ReadLine().Trim().Split(' ');
                var c = line[0];
                if (c == "STOP") { Console.WriteLine("{0} {1}", y + 1, x + 1); break; }
                else if (c == "LEFT")
                {
                    d--;
                    if (d == -1) d = 3;
                }
                else if (c == "RIGHT")
                {
                    d++;
                    if (d == 4) d = 0;
                }
                else if (c == "FORWARD")
                {
                    var c2 = int.Parse(line[1]);
                    var nx = x + c2 * dx[d];
                    var ny = y + c2 * dy[d];
                    if (nx < 0) nx = 0;
                    if (nx >= h) nx = h - 1;
                    if (ny < 0) ny = 0;
                    if (ny >= w) ny = w - 1;
                    x = nx;
                    y = ny;
                }
                else if (c == "BACKWARD")
                {
                    var nd = d + 2;
                    if (nd > 3) nd -= 4;
                    var c2 = int.Parse(line[1]);
                    var nx = x + c2 * dx[nd];
                    var ny = y + c2 * dy[nd];
                    if (nx < 0) nx = 0;
                    if (nx >= h) nx = h - 1;
                    if (ny < 0) ny = 0;
                    if (ny >= w) ny = w - 1;
                    x = nx;
                    y = ny;
                }
            }
        }
    }
}



Measured memory_bytes: 15940
import glob

# テスト用ファイル置き場のトップ
TOP_PATH = 'C:\\AtCoder'

# 問題ごとのディレクトリのトップからの相対パス
REL_PATH = 'ABC\\136'

# 絶対パス
ABS_PATH = TOP_PATH + '\\' + REL_PATH

#IS_TEST = 1


class Common:

    problem = []
    index = 0

    def initialize(self, path):
        file = open(path)
        self.problem = file.readlines()
        self.index = 0
        return

    def input_data(self):
        try:
            IS_TEST
            return self.problem[self.index]
            self.index += 1
        except NameError:
            return input()

    def resolve(self):
        pass

    def exec_resolve(self):
        try:
            IS_TEST
            for path in glob.glob(ABS_PATH + '/*.txt'):
                self.initialize(path)
                self.resolve()
        except NameError:
            self.resolve()

class A(Common):

    def resolve(self):

        q = [int(i) for i in self.input_data().split()]
        result = q[2] - q[0] +q[1]
        if result < 0:
            print('0')
        else:
            print(str(result))

solver = A()
solver.exec_resolve()

Measured memory_bytes: 3956
'use strict';
(function(stdin) {
  // ????????????????????????
  var EOF = '0 0';
  var inputs = stdin.toString().split('\n');
  var data = inputs.slice(0,inputs.indexOf(EOF));
  // console.log('array -> ' ,data)

  for (var i = 0; i < data.length; i++) {
  	var cols = data[i].split(' ').map(Number);
  	var x = cols[0];
  	var y = cols[1];
  	// console.log('x:',x,'y:',y);
  if(x > y){
  var tmp = x;
  x = y;
  y = tmp;
  }
    console.log(x,y);
  }
}(require('fs').readFileSync('/dev/stdin', 'utf8')));

Measured memory_bytes: 19380
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>

using namespace std;

int main(int argc, char const *argv[]) {
	long long n,temp,sum;
	sum = 0;
	std::vector<long long> v;
	std::cin >>  n;
	for (size_t i = 0; i < n; i++) {
		std::cin >> temp;
		sum += temp;
		v.push_back(temp);
	}
	long long sub = 0;
	long long submin = pow(10,10);

	for (size_t i = 0; i < n-1; i++) {
		sub += v[i];
		submin = min(submin, abs(sum - 2 * sub));
	}

	std::cout << submin << '\n';

	return 0;
}


Measured memory_bytes: 2420
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
char G[101][101];
int dp[101][101],g[101][101];
int main(){

  int h,w;
  cin >> h >> w;
  for(int i = 0 ; i < h ; i++ ) {
    for(int j = 0 ; j < w ; j++ ) {
      cin >> G[i][j];
      g[i][j] = G[i][j] - '0';
    }
  }
  fill(dp[0],dp[101],100000);
  dp[0][0] = 0;
  for(int i = 0 ; i < h ; i++ ) {
    for(int j = 0 ; j < w ; j++ ) {
      if(j >= 1)dp[i][j] = min(dp[i][j-1]+g[i][j],dp[i][j]);
      if(i >= 1)dp[i][j] = min(dp[i][j],dp[i-1][j]+g[i][j]);
    }
  }

  cout << dp[h-1][w-1] << endl;
  return 0;
}

Measured memory_bytes: 1228
#include <bits/stdc++.h>
#define LL long long int
#define Max(x, y) (x > y ? x : y)
#define Min(x, y) (x < y ? x : y)
#define Abs(x) ( (x) > 0 ? (x) : -(x) )
#define Swap(x,y) do{x = x ^ y; y = x ^ y; x = x ^ y;}while(0)
#define IO_BOOST                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(0)
#define setp(x) fixed << setprecision(x)
#define INF 0x3f3f3f3f
#define Lowbit(x) ((x) & (-x))
#define Pii pair<int, int>
#define x first
#define y second
#define MOD ((int)1e9+7)
using namespace std;

int n, m;
vector<int> mp[(int)1e5+5];
int in_degree[(int)1e5+5];
int dist[(int)1e5+5];
bool visted[(int)1e5+5];
void dfs(int a){
    visted[a] = true;
    for(auto i : mp[a]){
        dist[i] = Max( dist[i], dist[a]+1 );
        in_degree[i]--;
        if( in_degree[i] == 0 ){
            dfs(i);
        }
    }
}
void solve(){
    for(int i = 1 ; i <= n ; i++){
        if( !visted[i] && in_degree[i] == 0 ){
            dfs( i );
        }
    }
    int ans = 0;
    for(int i = 1 ; i <= n ; i++){
        ans = Max(ans, dist[i]);
    }
    cout << ans << '\n';
}
int main(){
    IO_BOOST;
    cin >> n >> m;
    for(int i = 0 ; i < m ; i++){
        int x, y;
        cin >> x >> y;
        mp[x].push_back(y);
        in_degree[y]++;
    }
    solve();

}

Measured memory_bytes: 9728
#include <bits/stdc++.h>
#include <math.h>
#define _GLIBCXX_DEBUG
#define _LIBCPP_DEBUG 0

using namespace std;
#define ll long long
#define rep(i,n) for (int i = 0; i < n; i++)
#define MOD (1000000007)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()

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 gcd(int a, int b) {
    if (b == 0) return a;
    else return gcd(b, a % b);
}

const ll INF = 1LL << 60;

int main(){
  int h,w; cin >> h >> w;
  int c[10][10]; rep(i, 10) rep(j, 10) cin >> c[i][j];
  vvi a(h, vi(w)); rep(i, h) rep(j, w) cin >> a[i][j];

  rep(k, 10) rep(i, 10) rep(j, 10) chmin(c[i][j], c[i][k]+c[k][j]);

  int ans = 0;
  rep(i, h) rep(j, w){
    if(a[i][j] == -1) continue;
    ans += c[a[i][j]][1];
  }

  std::cout << ans << '\n';
}


Measured memory_bytes: 384
import java.util.*; 

class Main{
public static void main(String[] args){
	Scanner sc=new Scanner(System.in);
        long x=sc.nextLong();
        long ans=0;
        long sum=0;

		while(sum<x){
        ans++;
        sum+=ans;
        }
    

    	System.out.println(ans);
    }
}


Measured memory_bytes: 35732
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main (void)
{
	int K, N;
	cin >> K >> N;
	vector<int> A (N);
	for (auto &e: A)
		cin >> e;
	int WorldRecord;
	
	sort (A.begin (), A.end ());
	WorldRecord = A [N-1] - A [0];
	for (int i = 1; i < N; i++)
		WorldRecord = min (WorldRecord, A [i-1] - A [i] + K);

	//反時計回りも
	WorldRecord = min (WorldRecord, K - A[0] + A[N-1]);
	for (int i = 1; i < N; i++)
		WorldRecord = min (WorldRecord, A [i] - A [i+1] + K);

	cout << WorldRecord << endl;

	return 0;
}


Measured memory_bytes: 1024
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 2000000;
int A[maxn];
int cnt = 0;
struct Ans{
    int a,b;
}ans[maxn];
int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 1;i <= n;i++){
        scanf("%d",&A[i]);
    }
    if(n == 1){
        printf("0\n");
        printf("%d %d\n",A[1],A[1]);
        return 0;
    }
    sort(A+1,A+1+n);
    if(A[1] >= 0){
        for(int i = 2;i <= n-1;i++){
            ans[++cnt].a = A[1];
            ans[cnt].b = A[i];
            A[1] -= A[i];
        }
        ans[++cnt].a = A[n];
        ans[cnt].b = A[1];
        printf("%d\n",A[n]-A[1]);
        for(int i = 1;i <= cnt;i++){
            printf("%d %d\n",ans[i].a,ans[i].b);
        }
        return 0;
    }
    for(int i = 1;i <= n;i++){
        if(A[i] <= 0&&A[i+1] <= 0){
            ans[++cnt].a = A[n];
            ans[cnt].b = A[i];
            A[n] -= A[i];
        }else if(A[i] <= 0&&A[i+1] > 0){
            for(int j = i+1;j <= n-1;j++){
                ans[++cnt].a = A[i];
                ans[cnt].b = A[j];
                A[i] -= A[j];
            }
            printf("%d\n",A[n] - A[i]);
            ans[++cnt].a = A[n];
            ans[cnt].b = A[i];
            for(int j = 1;j <= cnt;j++){
                printf("%d %d\n",ans[j].a,ans[j].b);
            }
            return 0;
        }
    }
    for(int i = 1;i <= cnt;i++){
        printf("%d %d\n",ans[i].a,ans[i].b);
    }
    return 0;
}

Measured memory_bytes: 4608
#include <iostream>
#include <string>
#define llint long long

using namespace std;

llint n;
string s;

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	cin >> n;
	cin >> s;
	
	llint ans = 1;
	for(int i = 0; i < n-1; i++){
		if(s[i] != s[i+1]) ans++;
	}
	cout << ans << endl;
	
	return 0;
}

Measured memory_bytes: 512
### constant

MAX_INT = 1 << 30

DXYS = [[1, 0], [0, -1], [-1, 0], [0, 1]]

### subroutines

def gen_dice
  dice = {}
  st = 123
  used = {}
  used[st] = true
  q = [st]

  while ! q.empty?
    u = q.shift

    u0 = u / 100
    u1 = (u / 10) % 10
    u2 = u % 10

    dice[u] = [
      (7 - u2) * 100 + u1 * 10 + u0,
      u1 * 100 + (7 - u0) * 10 + u2,
      u2 * 100 + u1 * 10 + (7 - u0),
      (7 - u1) * 100 + u0 * 10 + u2
    ]

    for v in dice[u]
      if ! used[v]
        used[v] = true
        q << v
      end
    end
  end

  dice
end

### main

dice = gen_dice
#p dice

loop do
  h, w = gets.split.map(&:to_i)
  break if h == 0 && w == 0

  mtx = h.times.map{gets.split.map(&:to_i)}
  sy, sx = gets.split.map(&:to_i)
  gy, gx = gets.split.map(&:to_i)
  #p mtx
  #p [sx, sy]
  #p [gx, gy]

  sid = sy * w + sx
  gid = gy * w + gx

  dists = Hash.new{MAX_INT}
  min_dist = MAX_INT

  st = sid * 1000 + 123
  dists[st] = 0
  q = [st]

  while ! q.empty?
    u = q.min{|a, b| dists[a] <=> dists[b]}
    q.delete(u)

    du = dists[u]

    udc = u % 1000
    uid = u / 1000
    uy = uid / w
    ux = uid % w
    #p [udc, ux, uy, du]

    if uid == gid
      min_dist = du if min_dist > du
      next
    end

    for i in (0..3)
      vx = ux + DXYS[i][0]
      vy = uy + DXYS[i][1]

      if vx >= 0 && vx < w && vy >= 0 && vy < h
        vdc = dice[udc][i]
        vid = vy * w + vx
        v = vid * 1000 + vdc

        dv = dists[v]
        ndv = du + (7 - vdc / 100) * mtx[vy][vx]

        if dv > ndv
          dists[v] = ndv
          if dv >= MAX_INT
            q << v
          end
        end
      end
    end
  end

  puts min_dist
end

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

using namespace std;

// Header files, namespaces,
// macros as defined above
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>


#define f first
#define s second
#define db long double
#define pb push_back
#define sz(c) (int)(c).size()
#define all(c) (c).begin(),(c).end()

typedef long long ll;
typedef pair <int , int> ii;
typedef pair <ii , ll> iii;
typedef vector <int> vi;
typedef vector <ii> vii;
typedef vector <iii> viii;
typedef vector <vi> vvi;
typedef vector <vii> vvii;


const int mod = 998244353;
//const db eps = 1e-13;
const ll oo = 1e18;
const int MAX = 1e6+6;
// basic operations
int mul( int a , int b ) {
    return (1ll*a*b)%mod;
}
int add( int a , int b ) {
    return (a+b)%mod;
}
int sub( int a , int b ) {
    return (a-b+mod)%mod;
}
void addSelf( int& a, int b, int m ) {
    a += b;
    b %= m;
}

int exp( int a, int b ) {

    int res = 1;
    while( b ) {

        if ( b % 2 ) {
            res = mul( res, a );
        }
        a = mul(a, a);
        b >>= 1;

    }
    return res;
}

//ll gcd[500005][20], A[500005], N, Q, a, b, r;
//void process2() {
//        int i, j;
//        for (i = 0; i < N; i++)
//                gcd[i][0] = i;
//        for (j = 1; 1 << j <= N; j++)
//                for (i = 0; i + (1 << j) - 1 < N; i++)
//                        gcd[i][j] = __gcd(gcd[i][j - 1], gcd[i + (1 << (j - 1))][j - 1]);
//}
//int RMQ(int a, int b) {
//        int k = (int) log2(b - a + 1);
//        return __gcd(gcd[a][k], gcd[b - (1 << k) + 1][k]);
//}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    ll n, b, r;
    cin>>n>>b>>r;


    cout<<(n/(b+r) * b)+min(n%(b+r), b)<<"\n";

}

Measured memory_bytes: 256
#include <stdio.h>

int main(void)
{
    int n, i, s;
    int ans;
    
    while (1){
        scanf("%d", &n);
        
        if (n == 0){
            break;
        }
        
        ans = 0;
        s = 1;
        i = 2;
        while (s < n){
            if ((n - s) % i == 0){
                ans++;
            }
            s += i;
            i++;
        }
        printf("%d\n", ans);
    }
    
    return (0);
}

Measured memory_bytes: 0