akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
#define INF (1<<30)
int N,M;
struct edge{
  int to,cap,rev;
};
vector<edge> G[333];
int E[333][333];

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

  G[from].push_back( (edge){to,0,G[to].size()} );
  G[to].push_back( (edge){from,cap ,G[from].size()-1} );
}

bool used[333];

int dfs(int v,int t,int f){
  if( v == t ) return f;
  used[v] = true;
  for(int i=0;i<(int)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;
  for(;;){
    memset(used,0,sizeof(used));
    int f = dfs(s,t,INF);
    if( f == 0 ) return flow;
    flow+=f;
  }
}

vector<int> ans;

void redfs(int v,int t){
  if( v == t ) return;
  if( used[v] ) return;
  used[v] = true;
  bool tof[333]={};
  for(int i=0;i<(int)G[v].size();i++){
    edge e = G[v][i];
    if( e.cap > 0 ) tof[e.to] = true;
  }
  for(int i=0;i<(int)G[v].size();i++){
    edge e = G[v][i];
    if( !tof[e.to] ) {
      // printf(" %d -> %d == %d \n",e.to,v,E[e.to][v]); 
      if( E[v][e.to] > 0 ) ans.push_back( E[v][e.to] );
      redfs(e.to,t);
    }
  }
  return ;
}

int main(){
  cin >> N >> M;
  for(int i=0;i<M;i++){
    int x,y;
    cin >> x >> y;
    E[y][x] = i+1;
    add_edge(y,x,1);
  }
  int s,t;
  cin >> s >> t;
  int res = max_flow(s,t);
  cout << res << endl;
  memset(used,0,sizeof(used));
  redfs(s,t);
  sort(ans.begin(),ans.end());
  ans.erase(unique(ans.begin(),ans.end()),ans.end());
  cout << ans.size() << endl;
  for(int i=0;i<(int)ans.size();i++) cout << ans[i] << endl;
}

Measured memory_bytes: 1756
import java.util.*;

public class Main{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		for(;;){
			int x = Integer.parseInt(sc.next());
			int y = Integer.parseInt(sc.next());
			if((x == 0) && (y == 0)) break;
			System.out.printf("%d %d\n", Math.min(x, y), Math.max(x, y));
		}
	}
}



Measured memory_bytes: 40380
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef pair<int,int> P;
typedef pair<int,P> IP;
int inf=1e8;
int dx[8]={1,1,1,0,0,-1,-1,-1};
int dy[8]={1,0,-1,1,-1,1,0,-1};
int N;
string st[5];
int nxt[6][1<<25];
inline bool is(int x,int y){
	return 0<=x&&x<N&&0<=y&&y<N;
}
int solve(){
	set<IP> vis;
	rep(i,N) cin>>st[i];
	int sx,sy,si=0;
	rep(i,N) rep(j,N){
		if(st[i][j]=='@') sx=i,sy=j;
		if(st[i][j]=='#') si|=1<<(i*N+j);
	}
	queue<IP> que;
	que.push(IP(si,P(sx,sy)));
	vis.insert(IP(si,P(sx,sy)));
	que.push(IP(-1,P(0,0)));
	int dist=0;
	int cnt=0;
	while(!que.empty()){
		cnt++;
		if(cnt%10000000==0) cout<<cnt<<endl;
		IP ip=que.front();
		que.pop();
		int bit=ip.fs;
		int x=ip.sc.fs,y=ip.sc.sc;
		if(bit<0){
			dist++;
			if(que.empty()) break;
			que.push(IP(-1,P(0,0)));
		}
		if(bit==0) return dist;
		rep(di,8){
			int nx=x+dx[di],ny=y+dy[di];
			if(is(nx,ny)&& !((bit>>(nx*N+ny))&1) ){
				int i=bit|(1<<(nx*N+ny));
				int ni=0;
				{
					bool a[5][5]={};
					rep(j,N) rep(k,N) a[j][k]=(i>>(j*N+k))&1;
					rep(j,N) rep(k,N){
						int cnt=0;
						rep(d,8){
							int nx=j+dx[d],ny=k+dy[d];
							if(is(nx,ny)&&a[nx][ny]) cnt++;
						}
						if(a[j][k]&&(cnt==2||cnt==3)) ni|=1<<(j*N+k);
						if(!a[j][k]&&cnt==3) ni|=1<<(j*N+k);
					}
				}
				if( (ni>>(nx*N+ny))&1 ) ni^=1<<(nx*N+ny);
				IP nip=IP(ni,P(nx,ny));
				if(vis.find(nip)!=vis.end()) continue;
				que.push(nip);
				vis.insert(nip);
			}
		}
	}
	return -1;
}
int main(){
	while(true){
		cin>>N;
		if(N==0) break;
		cout<<solve()<<endl;
	}
}

Measured memory_bytes: 2872
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
z = 0
d = defaultdict(int)
for _ in range(n):
    a,b = inpl()
    if a == 0 and b == 0:
        z += 1
        continue
    if a == 0: b = 1
    if b == 0: a = 1
    if b < 0: a,b = -a,-b
    g = math.gcd(a,b)
    a //= g; b //= g
    d[(a,b)] += 1
res = 1
seen = set()
for k in list(d):
    a,b = k
    if k in seen: continue
    if a < 0: x,y = b,-a
    else: x,y = -b,a
    if a == 0: x,y = 1,0
    if b == 0: x,y = 0,1
    res *= pow(2,d[(a,b)],mod) + pow(2,d[(x,y)],mod) -1
    res %= mod
    # print(res)
    seen.add(k); seen.add((x,y))
res += z - 1
print(res%mod)

Measured memory_bytes: 225120
#include<bits/stdc++.h>
using namespace std;
const int N=100005;
int n,ans,a[N];
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n;i++)scanf("%d",&a[i]);
	for (int i=1;i<=n;i++)
		if (a[a[i]]==i)ans++;
	printf("%d\n",ans/2);	
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
//#include <mylib.h>
using namespace std;
//cin.sync_with_stdio(false);

#define FOR(i,a,b) for(int i = (a); i < (b); ++i)
#define FOR_EQ(i,a,b) for(int i = (a); i <= (b); ++i)
#define FOR_RE(i,a,b) for(int i = (a); i > (b); --i)
#define rep(i,n) FOR(i,0,n)
#define rep_eq(i,n) FOR_EQ(i,0,n)
#define rep_re(i,n) FOR_RE(i,n,0)

#define DEBUG_VARIABLE(variable) cout << #variable << ":" << variable << endl

#define WHITE 1//（未訪問）
#define GRAY 2//（訪問）未だに訪問していない頂点への辺を持っている。スタックに退避。
#define BLACK 3//（訪問完了）未訪問への頂点への辺を持たない
#define N 100
#define INFTY (1 << 21) //10^21
#define INF (1 << 9)
int M[N][N];//隣接行列
int color[N];

//1個上から時計周り
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
//上右下左
const int dx[4] = {0, 1, 0, -1};
const int dy[4] = {-1, 0, 1, 0};
static const int NIL = -1;

typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> Vector;
typedef vector<Vector>	DVector;
int n;

void printArray(int *array, int);
void printVector(Vector &v);
void printDimention(DVector &dv);

int main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int a[3];
    cin >> a[0] >> a[1] >> a[2];
    sort(a, a + 3);
    
    int abs_num = 0;
    abs_num += a[2] - a[1];
    abs_num += a[2] - a[0];
        
    int cnt = abs_num / 2;
    if(abs_num % 2){
    	cnt += 2;
    }
    cout << cnt << endl;
}

//配列の表示
void printArray(int array[], int n){
    rep(i, n){
        if(i)   cout << " ";
        cout << array[i];
    }
    cout << endl;
}

//Vectorの表示
void printVector(Vector &v){
    rep(i, (int)v.size()){
        if(i)   cout << " ";
        cout << v[i];
    }
    cout << endl;
}

//二次元配列の表示
void printDimention(DVector &dv){
    rep(i, (int)dv.size()){
        rep(j, (int)dv[i].size()){
            if(j)   cout << " ";
            cout << dv[i][j];
        }
        cout << endl;
    }
}

//numの中に何個2がかかってるか
int factor2(int num){
    int cnt = 0;
    while(num % 2 == 0){
        num /= 2;
        cnt++;
    }
    return cnt;
}

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

const int N = 2e5 + 5;

int n, m, x, y;
vector<int> ins[N];

namespace seg {
	int a[N * 4], tag[N * 4];
# define ls rt << 1
# define rs ls | 1
	void build(int rt, int l, int r) {
		a[rt] = l;
		if(l == r) return ;
		int mid = l + r >> 1;
		build(ls, l, mid);
		build(rs, mid + 1, r);
		a[rt] = min(a[ls], a[rs]);
	}
	inline void pushdown(int rt) {
		if(!tag[rt]) return ;
		a[ls] += tag[rt], tag[ls] += tag[rt];
		a[rs] += tag[rt], tag[rs] += tag[rt];
		tag[rt] = 0;
	}
	void modify(int rt, int l, int r, int ql, int qr) {
		if(l > qr || r < ql) return ;
		if(l >= ql && r <= qr)
			return (void) (a[rt] -= 1, tag[rt] -= 1);
		pushdown(rt);
		int mid = l + r >> 1;
		modify(ls, l, mid, ql, qr);
		modify(rs, mid + 1, r, ql, qr);
		a[rt] = min(a[ls], a[rs]);
	}
	int query(int rt, int l, int r, int ql, int qr) {
		if(l > qr || r < ql) return 1e9;
		if(l >= ql && r <= qr) return a[rt];
		pushdown(rt);
		int mid = l + r >> 1;
		return min(query(ls, l, mid, ql, qr), query(rs, mid + 1, r, ql, qr));
	}
}

int main() {
	scanf("%d%d", &n, &m);
	++m;
	for(int i = 1; i <= n; ++i) {
		scanf("%d%d", &x, &y);
		y = m - y;
		ins[x].push_back(y);
	}
	seg :: build(1, 0, m);
	int ans = m - 1 - n;
	for(int i = 0; i <= m; ++i) {
		for(int j = 0; j < (int) ins[i].size(); ++j) {
			int pos = ins[i][j];
			seg :: modify(1, 0, m, pos, m);
		}
		ans = min(ans, i + seg :: query(1, 0, m, 0, m - i));
	}
	printf("%d\n", max(0, -ans));
	return 0;
}


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

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

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using P = pair<int,int>;

vector<int>bitSearch(int bit,int n){
    vector<int>S;
    rep(i,n)if(bit&(1<<i))S.push_back(i);
    return S;
}


int d[3] = {-1,0,1};
int main(){
    int n,k,x,y;cin>>n>>k>>x>>y;
    int res = min(n,k)*x;
    int mn = min(n,k);
    n -= mn;
    res += n*y;
    cout<<res;
}



Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main()
{
    int N;
    cin >> N;
  
    int a[N+1];
    for(int i=0; i<N+1; i++){
      cin >> a[i];
    }
  
    int b[N];
    for(int i=0; i<N; i++){
      cin >> b[i];
    }
    
    long ans = 0;
  
    for(int i=0; i<N; i++){
      if(a[i] <= b[i]){//a[i]を倒した時
        ans += a[i];
        b[i] -= a[i];
        a[i] = 0;
        if(a[i+1] <= b[i]) {//a[i]とa[i+1]を倒した時
          ans += a[i+1];
          b[i] -= a[i+1];
          a[i+1] = 0;
        }else{//a[i]は倒せたがa[i+1]は倒せなかった時
          ans += b[i];
          a[i+1] -= b[i];
          b[i] = 0;
        }
      }else{//a[i]が倒せなかった時
        ans += b[i];
        a[i] -= b[i];
        b[i] = 0;
      }
    }
  
    // 出力
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1024
//Radhe Radhe
#include<bits/stdc++.h>
//#include<boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
//#include<boost/math/common_factor.hpp>
#include <ext/numeric>
using namespace __gnu_cxx;
using namespace std;
#define int long long
#define endl "\n"
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define array(a,n) int a[n]; for(int i=0;i<n;i++) cin>>a[i];
#define loop(i,a,b,x) for(int i=a;i<b;i+=x)
const int MOD = 1000000007;
//const int N = 10001;
const int N=1e5+10;
//const int N = (2 * (1e5)) + 2;



vector<int> edges[N];
bool visited[N];
int d[N];

void dfs(int i)
{
    visited[i]=1;
    d[i]=-1;
    for(int v : edges[i])
    {
        if(!visited[v]) 
            dfs(v);

        d[i]=max(d[i],d[v]);
    }
    d[i]++;
}
int32_t main()
{
    ios_base::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
    
    int t = 1;
    //cin >> t;
    while (t--) {
        int n, m;
        cin >> n >> m;
        while (m--)
        {
            int a, b;
            cin >> a >> b;
            edges[a].push_back(b);
        }

        int answer = 0;
        for (int i = 1; i <= n; i++)
        {
            if (!visited[i])
                dfs(i);

            answer = max(answer, d[i]);
        }
        cout << answer << endl;
    }
    return 0;

}






Measured memory_bytes: 12620
import java.util.*;

public class Main {

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

        for (int i = 0; i < count; i++) {
            aArr[i] = sc.nextInt();
        }

        int roopCnt = 0;
        boolean hasOddNumber = false;
        while (true) {
            for (int i = 0; i < count; i++) {
                // 奇数がある
                if (aArr[i] % 2 != 0) hasOddNumber = true;
            }

            // 奇数がある時は抜ける
            if (hasOddNumber) break;

            for (int i = 0; i < count; i++) {
                aArr[i] /= 2;
            }

            roopCnt++;
        }

        System.out.println(roopCnt);
    }
}


Measured memory_bytes: 23636
#include <iostream>
#include <string>
using namespace std;
int main(void){
  string s;
  cin >> s;
  int c = 0;
  if(s[0] == 'R' && s[1] == 'R' && s[2] == 'R'){
    cout << 3 << endl;
  }else if((s[0] == 'R' && s[1] == 'R' )||(s[1] == 'R' && s[2] == 'R')){
    cout << 2 << endl;
  }else if(s[0] == 'R' || s[1] == 'R' || s[2] == 'R' ){
    cout << 1 << endl;
  }else{
    cout << 0 << endl;
  }
  
  return 0 ;
}

Measured memory_bytes: 3624
//ヘッダー
#include<bits/stdc++.h>
using namespace std;

//型定義
typedef long long ll;

//定数
const int INF=1e+9;
const int MOD=1e+9+7;

//REPマクロ
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define REPD(i,n) for(ll i=n-1;i>=0;i--)
#define REP2(i,a,b) for(ll i=a;i<(ll)(b);i++)
#define REPD2(i,a,b) for(ll i=a;i>(ll)(b);i--)

// 多次元 vector 生成
template<class T>
vector<T> make_vec(size_t a){
    return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

//vectorの扱い
#define ALL(x) (x).begin(),(x).end() //sortなどの引数省略
#define SIZE(x) ((ll)(x).size()) //size
#define MAX(x) *max_element(ALL(x)) //最大値
#define MIN(x) *min_element(ALL(x)) //最小値

int main(){
    int N;
    cin>>N;
    vector<pair<int,int>> A(N+1);
    
    REP(i,N){
        int a;
        cin>>a;
        A[i+1]=make_pair(a,i+1);
    }
    sort(ALL(A));
    int cnt=0;
    REP2(i,1,N+1){
        if(i%2!=A[i].second%2) cnt++;
    }
    cout<<cnt/2<<endl;

}

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

int main() {
  int n,m;
  string S;
  cin>>n>>m;
  //if(A%3==0||B%3==0||(A+B)%3==0) S="Possible";
  //else S="Impossible";
  
  cout<<(n-1)*(m-1)<<endl;
}

Measured memory_bytes: 256
import sys
from collections import deque

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main():
	n, x, y = IN()
	al=[]
	for i in range(n):
		ll=[0]*n
		for j in range(n-i):
			ll[i+j]=j
		for j in range(i+1):
			ll[i-j]=j
		
		df = x-1
		dt = y-1
		if ll[y-1] < ll[x-1]:
			df, dt = dt, df
		if ll[dt] < ll[df]+1:
			al.append(ll)
			continue
		ll[dt] = ll[df]+1
		for i in range(dt+1,n):
			if ll[i] < ll[i-1]+1:
				break
			ll[i]=ll[i-1]+1
		for i in range(dt-1,0,-1):
			if ll[i] < ll[i+1]+1:
				break
			ll[i]=ll[i+1]+1
		al.append(ll)
		
	#for l in al:
	#	print(l)
			
	rr=[0]*n
	for i in range(n):
		for j in range(i+1,n):
			rr[ al[i][j] ]+=1
	
	for v in rr[1:]:
		print(v)

def main2():
	n, x, y = IN()
	al=[[-1]*n for _ in range(n)]
	ol=queue.Queue()
	for i in range(n):
		al[i][i]=0
	
	for i in range(n-1):
		al[i][i+1]=1
		ol.put((i,i+1))
		al[i+1][i]=1
	al[x-1][y-1]=1
	ol.put((x-1, y-1))
	al[y-1][x-1]=1
	while not ol.empty():
		#print(ol.qsize())
		fp, tp = ol.get()
		for i, dist in enumerate(al[tp]):
			nd = al[fp][tp] + dist
			if dist > 0 and (al[fp][i] == -1 or al[fp][i] > nd) and fp < i:
				al[fp][i]=nd
				al[i][fp]=nd
				ol.put((fp,i))
			
		fp,tp=tp,fp
		for i, dist in enumerate(al[tp]):
			nd = al[fp][tp] + dist
			if dist > 0 and (al[fp][i] == -1 or al[fp][i] > nd) and fp < i:
				al[fp][i]=nd
				al[i][fp]=nd
				ol.put((fp,i))
			
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Measured memory_bytes: 76892
#include <bits/stdc++.h>
using namespace std;
long long w[100001],v[100001],dp[1000000];
int main() {
	
	ios_base::sync_with_stdio(false); 
    cin.tie(NULL); 
    
      long long n,i,j,k,sum=0,count=0,ans=0,x=0,y=0,l,r,m;
      cin>>n>>k;
      long long a[n];
      for(i=0;i<n;i++)
      {
          cin>>a[i];
          dp[a[i]]=1;
      }
      for(i=1;i<=k;i++)
      {
          for(j=0;j<n;j++)
          {
              if(i>=a[j])
              {
                  dp[i]=max(dp[i],(dp[i-a[j]]+1)%2);
              }
          }
      }
      if(dp[k]==1)
      cout<<"First";
      else
      cout<<"Second";
	return 0;
}

Measured memory_bytes: 4428
#include<stdio.h>
 
int main(void)
{
  int N,i,j,k,max = 0,count=0,flag = 0;
  char S[100],l;
  
  scanf("%d",&N);
  scanf("%s",S);

  for(i = 1;i<N;i++){
  	for(l='a';l<='z';l++){
      flag = 0;
      for(j = 0;j<i;j++){
        for(k=i;k<N;k++){
  				if(S[j] == S[k] && S[j]== l){
  					count++;
            flag = 1;
  					break;
  				}
          if(flag == 1)
            break;
        }
        if(flag == 1)
          break;
  		}
  	}
  	if(count>max)
  		max = count;
  	count = 0;
  }

  printf("%d\n",max);
  
  return 0;
}

Measured memory_bytes: 128
import java.util.*;

public class Main {
    public static void main(String[] args) {	
	Main main = new Main();
    	main.solveA();
    }
    
    private void solveA() {
    	Scanner sc = new Scanner(System.in);
    	int A = sc.nextInt();
    	int B = sc.nextInt();
	int C = sc.nextInt();
	int D = sc.nextInt();	
    	System.out.println(A+B > C+D ? "Left" : (A+B < C+D ? "Right" : "Balanced"));
    }

    private void solveB() {
    }
    private void solveC() {
    }
    private void solveD() {
    }
}


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

int main() {
    int n, a[2 * 100000];

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

    int b;
    long long sum = 0;
    sort(a, a + n);
    if(n % 2) b = a[n / 2];
    else b = (a[n / 2 - 1] + a[n / 2]) / 2;
    for(int i = 0; i < n; i++) {
        sum += abs(a[i] - b);
    }
    cout << sum << endl;
}


Measured memory_bytes: 1024
#include<iostream>
#include<string>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef unsigned long long ll;
int p[]={2,3,5,7,11,13,17,19,23,29,31};
int main(){
	int n,nfac[11];
	ll m,fac[11];
	string b;
	while(cin>>n>>b,n){
		m=0;
		rep(i,11)nfac[i]=fac[i]=0;
		rep(i,b.size())m*=n,m+=('0'<=b[i]&&b[i]<='9'?b[i]-'0':b[i]-'A'+10);
		ll ans=1184595334580404209LL;
		rep(i,11){
			while(n%p[i]==0)n/=p[i],nfac[i]++;
			if(nfac[i]){
				for(ll j=p[i];j<=m;j*=p[i])fac[i]+=m/j;
				if(ans>fac[i]/nfac[i])ans=fac[i]/nfac[i];
			}
		}
		cout<<ans<<endl;
	}
	return 0;
}

Measured memory_bytes: 928
#include <bits/stdc++.h>
using namespace std;
int main(){
  int r, D, x; cin >> r >> D >> x; //int入力
  vector<int> V(10);
  for(int i = 0; i < 10; i++){
    if(i == 0){
      V[i] = r*x - D;
    }else{
      V[i]= r*V[i-1] - D;
    }
    printf("%d\n", V[i]);
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>

using namespace std;

struct team {
	char name;
	int w;
	int l;
	int d;
};

int main() {
	int n;
	while (cin >> n) {
		if (n == 0) {
			break;
		}
		team t[10];
		int a;
		for (int i = 0; i < n; i++) {
			cin >> t[i].name;
			t[i].w = t[i].l = t[i].d = 0;
			for (int j = 0; j < n-1; j++) {
				cin >> a;
				if (a == 0) {
					t[i].w++;
				} else if (a == 1) {
					t[i].l++;
				} else {
					t[i].d++;
				}
			}
		}
		for (int i = n-1; i > 0; i--) {
			for (int j = 0; j < i; j++) {
				if (t[j].w < t[j+1].w) {
					swap(t[j], t[j+1]);
				} else if (t[j].w == t[j+1].w) {
					if (t[j].l > t[j+1].l) {
						swap(t[j], t[j+1]);
					}
				}
			}
		}
		for (int i = 0; i < n; i++) {
			cout << t[i].name << endl;
		}
	}
	return 0;
}

Measured memory_bytes: 1152
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
const int N = 200010;
int dist[N];
int k,n;

int main() {
    cin >> k >> n;
    for(int i = 1; i <= n; i ++) {
        cin >> dist[i];
    }
    dist[0] = dist[n];
    int curr = (dist[1] - dist[0] + k) % k;
    for(int i = 1; i <= n; i ++) {
        curr = max(curr, (dist[i] - dist[i-1] + k) % k);
    }
    cout << k - curr << endl;
    return 0;
}

Measured memory_bytes: 1024
#include<stdio.h>
#include<stdlib.h>
int D(int d,int dd);
typedef struct pp{
  int u;
  int k;
  int parent;
  int depth;
  int v[100];
  int J;
}P;
P *A;
int main(){
  int n,*G,i,j,p,count=0;
  
  scanf("%d\n",&n);
  
  A=(P*)malloc(n*n*sizeof(P));
  G=(int*)malloc(n*sizeof(int));
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      G[i*n+j]=0;
      }
  }
  for(i=0;i<n;i++){
    scanf("%d %d ",&A[i].u,&A[i].k);
    for(j=0;j<A[i].k;j++){
      scanf("%d",&A[i].v[j]);
    }
  }
  for(i=0;i<n;i++){
    for(j=0;j<A[i].k;j++){
      G[i*n+A[i].v[j]-1]=1;
    }
  }
  for(i=0;i<n;i++){
    for(j=0;j<n-1;j++) printf("%d ",G[i*n+j]);
    printf("%d\n",G[i*n+j]);
  }
  return 0;
}



Measured memory_bytes: 2112
import java.util.Scanner;

//Persistence
public class Main {

	static int[] w;
	static int n, m;
	
	static boolean f(int W){
		int need = 1;
		int s = 0;
		for(int i=0;i<n;i++){
			if(w[i]>W)return false;
			if(s+w[i]>W){
				need++;
				s = 0;
			}
			s+=w[i];
		}
		return need <= m;
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true){
			m = sc.nextInt();
			n = sc.nextInt();
			if((m|n)==0)break;
			w = new int[n];
			for(int i=0;i<n;i++)w[i]=sc.nextInt();
			int l = 1;
			int r = 1500000;
			while(r-l>1){
				int mid = (l+r)/2;
				if(f(mid))r = mid;
				else l = mid;
			}
			System.out.println(f(l)?l:r);
		}
	}
}

Measured memory_bytes: 29644
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>

using namespace std;

#define int long long

int A[100000] = {};
int B[100000] = {};
int C[100000] = {};
signed main() {
	int N;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
		cin >> B[i];
	}
	int cost = 0;
	for (int i = N - 1; i >= 0; i--) {
		A[i] += cost;
		if (A[i] % B[i] == 0) continue;
		else if (A[i] < B[i]) cost += B[i] - A[i];
		else  cost += B[i]*((A[i] / B[i]) + 1) - A[i];
	}

	cout << cost << endl;
	
}


Measured memory_bytes: 1792
#include <iostream>
using namespace std;
int main(void){
    int a, b, c, i, count = 0;
    
    cin >> a >> b >> c;
    
    for (i = a; i <= b; i++){
    
    if (c % i == 0) count++;
    
    }
    
    cout << count << endl;
    
}

Measured memory_bytes: 3104
#include <iostream> // cout, endl, cin
#include <iomanip>
#include <string>        // string, to_string, stoi
#include <vector>        // vector
#include <algorithm>     // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility>       // pair, make_pair
#include <tuple>         // tuple, make_tuple
#include <cstdint>       // int64_t, int*_t
#include <cstdio>        // printf
#include <map>           // map
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <deque>         // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset>        // bitset
#include <cctype>        // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <numeric>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
const int INF = 1001001001;

int main()
{
    int x, a, b;
    cin >> x >> a >> b;
    if(a - b >= 0)
    {
        cout << "delicious" << endl;
    }
    else if(b - a <= x)
    {
        cout << "safe" << endl;
    }
    else
    {
        cout << "dangerous" << endl;
    }
}


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

using namespace std;

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

typedef long long ll;

int main() {
    int dp[31];
    dp[0] = 1;
    dp[1] = 1;
    dp[2] = 2;
    for(int i=3;i<=30;i++){
        dp[i] = dp[i-1] + dp[i-2] + dp[i-3];
    }
    int n;
    while(cin >> n, n){
        cout << dp[n]/365/10+1 << endl;
    }
}


Measured memory_bytes: 3084
#include <stdio.h>
#include <string.h>

int main (){
  int a;
  char s[11];
  scanf("%s", s);
  a=strlen(s);

  for(int i=0; i<=a-1; i+=2){
    if(s[i] != 'h' || s[i+1] != 'i' ){
      printf("No\n");
      return 0;
    }
  }
  printf("Yes\n");
  return 0;
}


Measured memory_bytes: 128
//template code, C343, 2019
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Iterator;

class Main{
	public static void main(String argv[]) {
		Graph g = new Graph();
		g.loadAdjList();
		g.bfs(0); 
		//g.printAdjMatrix();
	}
}
class Graph {
	public int totalVertex;
	public LinkedList<LinkedList<Integer>> adjList;
	//adjacency list of edges
	
	public Graph() { totalVertex = 0; }
	
	public void loadAdjList() {
		Scanner in = new Scanner(System.in);
		totalVertex = in.nextInt();
		adjList = new LinkedList<LinkedList<Integer>>();
		for(int i = 0; i < totalVertex; i ++) {
			LinkedList<Integer> tmp = new LinkedList<Integer>();
			int idx1 = in.nextInt() - 1;
			int degree = in.nextInt();
			//System.out.println("mark idx1 = " + idx1 + " degree = " + degree);
			for(int j = 0; j < degree; j ++) {
				int idx2 = in.nextInt() - 1;
				tmp.add(idx2);
			}	
			adjList.add(tmp);
		}
		in.close();
	}
	
	
	public void bfs(int s) {
		
		LinkedList<Integer> toVisit = new LinkedList<Integer>();
		boolean[] seen = new boolean[totalVertex]; 
		int[] steps = new int[totalVertex]; 
		for(int i = 0; i < steps.length; i++) {
			steps[i] = -1; 
		}
		steps[s]=0; 
		
		toVisit.add(s); 
		seen[s] = true; 
		
		while(toVisit.size() != 0) {
			
			int u = toVisit.poll(); 
			
			
				for(int j = 0; j < adjList.get(u).size(); j++) {
					
					int n = adjList.get(u).get(j); 
					
					if( seen[n] == false)
					{
						toVisit.add(n); 
						seen[n] = true; 
						steps[n] = steps[u] + 1; 
					}
				}
			
		}
		for(int i = 0; i < totalVertex; i++) {
			System.out.println(i+1 + " " + steps[i]); 
		}
	}
	
	
	
	
	void topologicalSort(int s) {
		
	}
	
	
	
	
	
	
	
	
	
	
	
	public void printAdjMatrix() {
		Integer[][] adjMatrix = new Integer[totalVertex][totalVertex];
		for(int i = 0; i < totalVertex; i++) {
			for(int j = 0; j < totalVertex; j++) {
				adjMatrix[i][j] = 0;
			}
		}
		//complete the following
		for(int i = 0; i < adjList.size(); i++) {
			for(int j = 0; j < adjList.get(i).size(); j++) {
				adjMatrix[i][adjList.get(i).get(j)] = 1; 
			}
		}
		for(int i = 0; i < adjMatrix.length; i++) {
			for(int j = 0; j < adjMatrix[0].length; j++) {
				if(j == 3)
					System.out.println(adjMatrix[i][j]); 
				else
					System.out.print(adjMatrix[i][j] + " ");
			}
		}
	}
}

//change class name GraphRepresentation to Main() for submission to AIZU



Measured memory_bytes: 28616
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
#define rep(i,n) for((i)=0;(i)<(n);(i)++)
#define max(a,b) ((a)>(b) ? (a) : (b))
#define min(a,b) ((a)<(b) ? (a) : (b))
typedef long long ll;
typedef long double ld;
#define MOD1 1000000007
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int main(){
  ll n,m,k,i,j,result=0,l=0,r=0;
  ll a[200010];
  cin >> n >> k;
  rep(i,n) cin >> a[i];
  sort(a,a+n);
  r=n-1;
  if(k%2==1){
    result = a[r];
    r--;
    k--;
    m=(result<0 ? -1 : 1);
  }else{
    result = 1;
    m = 1;
  }
  while(k>0){
    i = a[l]*a[l+1];
    j = a[r]*a[r-1];
    //cout << i << " " << j << endl;
    if(i*m>j*m){
      result *= i % MOD1;
      result %= MOD1;
      l += 2;
    }else{
      result *= j % MOD1;
      result %= MOD1;
      r -= 2;
    }
    k -= 2;
  }
  cout << (result+MOD1) % MOD1 << endl;
  return 0;
}

Measured memory_bytes: 5160
import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)

def getN():
    return int(input())
def getList():
    return list(map(int, input().split()))
import math
from bisect import bisect_left

n = getN()

ans = n // 2 + (n % 2 == 1)

print(ans/n)

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

int main(){
  int A,B;
  cin >> A >> B;
  
  bool a=false;
  
  for(int i=1;i<1010;i++){
    if(floor(i*0.08)==A&&floor(i*0.1)==B){
      cout << i << endl;
      a=true;
      break;
    }
  }
  
  if(a==false){
    cout << -1 << endl;
  }
}
  
  
  

Measured memory_bytes: 256
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
# print(sum(a[:-1]))
if n==1:
    print(0)
else:
    ans=sum(a[1:n//2])+a[n//2-1]
    # print(ans)
    if n%2==1:
        ans+=a[n//2]
    # print(ans)
    ans+=sum(a[:n//2-1])
    print(ans)


Measured memory_bytes: 105044
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <limits>
#include <numeric>
#include <queue>
#include <set>
#include <bitset>
#include <map>

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<char> vc;
typedef vector<long long> vll;
typedef queue<int, int> qii;
typedef pair<int, int> pii;
typedef pair<string, int> psi;

#define PI 3.14159265359
#define rep(i,a,b) for(int i=a;i<b;i++)
#define repz(i,n) rep(i,0,n)

//int型を受け取ってその桁数を返す
int check_digits(int n) {
	int digits = 0;
	while (n != 0) {
		n /= 10;
		digits++;
	}
	return digits;
}

//int型を受け取ってその各桁の和を返す
int sum_digits(int n) {
	int sum=0;
	int N = n;
	while (N) {
		sum += N % 10;
		N /= 10;
	}
	return sum;
}


//最大公約数を返す関数
int gcd(int n, int m) {
	while (n != 0) {
		if (n < m) swap(n, m);
		n = n % m;
	}
	return m;
}


//2点間の距離を返す
float dis(int x1, int y1, int x2, int y2) {
	return sqrt(pow(abs(x1 - x2), 2) + pow(abs(y1 - y2), 2));
}


int main() {
	int n;
	cin >> n;
	vector<pair<pair<string, int>,int>> score(n);
	repz(i, n) {
		cin >> score[i].first.first >> score[i].first.second;
		score[i].first.second *= -1;
		score[i].second = i + 1;
	}

	sort(score.begin(), score.end());

	repz(i, n)cout << score[i].second << endl;	

	//system("pause");
	return 0;
}


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

int gcd(int a, int b)
{
   if (a%b == 0)
   {
       return(b);
   }
   else
   {
       return(gcd(b, a%b));
   }
}

int lcm(int a, int b)
{
   return a * b / gcd(a, b);
}

int main(){
    
    int n,X;
    cin >> n >> X;
    int num;
    vector<int> d(n);
    for(int i=0; i<n; i++){
        cin >> num;
        d.at(i)=abs(X-num);
    }

    int ans=d.at(n-1);
    for(int i=0; i<n-1; i++){
        ans=gcd(ans,d.at(i));
        //cout << ans << endl;
    }
    cout << ans << endl;
}

Measured memory_bytes: 3648
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <queue>
#include <math.h>
#include <string>

#define ll long long
#define vll vector<ll>
#define vvll vector<vll>
#define vb vector<bool>
#define vvb vector<vb>
#define vstr vector<string>
#define pll pair<ll, ll>
#define pb pair<bool, bool>
#define vpll vector<pll>
#define vpb vector<pb>

using namespace std;

int main()
{
	ll a, b;
	cin >> a >> b;
	cout << a * b << endl;
	return 0;
}

Measured memory_bytes: 3196
#include <stdio.h>
#define N 101
#define BLACK 2
#define GRAY 1
#define WHITE 0
#define NIL -1

int head,tail;
int Q[N];
int color[N];
int discover[N];
int adj[N][N];
int n;

void enqueue(int );
int dequeue();
void bfs(int );

int main(){
 
  //int n;
  int u,k,v;
  int i,j;
  int num;
  int uid;
  
  scanf("%d",&n);

  for(i=1;i<=n;i++){
    scanf("%d%d",&uid,&k);
    for(j=0;j<k;j++){
      scanf("%d",&num);
      adj[uid][num]=1;    
    }
  }
  /*
 for(i=1;i<=n;i++){
   printf("%d  ",i);
    for(j=1;j<=n;j++){
    printf("%d ",adj[i][j]);
  }
    printf("\n");
 }
 printf("\n");
 printf("\n");
 */
  bfs(1);

  for(i=1;i<=n;i++){
    printf("%d %d\n",i,discover[i]);
  }
  
  return 0; 
}

void enqueue(int num){
  
  Q[tail] = num;
  if(tail+1 == N) tail = 0;
  else tail++;
  
}

int dequeue(void){
  int x;
  x = Q[head];
  if(head+1 == N) head = 0;
  else head++;
  return x;
}

void bfs(int node){
  
  int i;
  int uid;

  for(i=1;i<=n;i++){
    color[i] = WHITE;
    discover[i] = NIL;
  }
  
  color[node] = GRAY;
  discover[node] = 0;

  enqueue(node);

  while((tail-head-1)>=0){

    uid = dequeue();

    for(i=1;i<=n;i++){
      if(adj[uid][i] == 1 && color[i] == WHITE){
	color[i] = GRAY;
	discover[i] = discover[uid]+1;
        enqueue(i);
      }
    }
    color[uid] =BLACK;
  }
}



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

using namespace std;

int main(){
  int n,m,k,ans=0;
  int i,j,l;
  vector <int> v;
  
  cin>>n>>m;
  
  for(i=0;i<m+1;i++)
    v.push_back(0);
  
  for(i=0;i<n;i++){
    cin>>k;
    for(j=0;j<k;j++){
      cin>>l;
      v[l]+=1;
    }
  }
  
  for(i=1;i<m+1;i++)
    if(v[i]==n)ans++;
  
  cout<<ans<<endl;
  return 0;
  }


Measured memory_bytes: 256
#include <stdio.h>
#include <math.h>
 
int main(void){
    double a, b, c, d, dis;
    scanf("%lf %lf %lf %lf", &a, &b, &c, &d);
    dis = sqrt((a-c)*(a-c) + (b-d)*(b-d));
    printf("%.4f\n", dis);
     
    return 0;
}


Measured memory_bytes: 2096
#include<bits/stdc++.h>
using namespace std;
main()
{
    int n,c,d;
    cin>>n;
      d=n%1000;
    if(d!=0)
       {
            c = (1000-d);
       cout<<c<<"\n";
       }
    else
     cout<<d<<endl;
}


Measured memory_bytes: 3628
import java.util.List;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import javax.swing.text.DefaultStyledDocument.ElementSpec;

import java.util.HashSet;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long count = 0;
        int n = sc.nextInt();
        int[] a = new int[n];
        int[] b = new int[n - 1];
        for (int i = 0; i < n - 1; i++) {
            b[i] = sc.nextInt();
        }
        for (int i = 0; i < n - 1; i++) {
            if (i == 0)
                a[0] = b[0];
            else {
                a[i] = Math.min(b[i], b[i - 1]);
            }
        }
        a[n - 1] = b[n - 2];
        for (int i = 0; i < n; i++) {
            count += a[i];
        }
        System.out.println(count);
    }
}

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

using namespace std;

#define forsn(i,s,n) for(int i = (s);i < (int)(n);i++)
#define forn(i,n) forsn(i,0,n)


bool EsPrimo (int n){
	int cont=0;
	for(int i=1;i*i<=n;i++){
		if(n%i==0){
			cont++;
		}
	}
	if(cont==1 && n != 1){
		return true;
	}
	return false;
}
using namespace std;
int main(){
	int n; cin >> n;
	vector<int> vv;
	vector<int> NumerosCongruentesCon1Mod5;
	forn(i, 11111){
		NumerosCongruentesCon1Mod5.push_back(5*i+1);
	}
	
	forn(i, 11111){
		if(EsPrimo(NumerosCongruentesCon1Mod5[i])){
			vv.push_back(NumerosCongruentesCon1Mod5[i]);
		}
	}

	forn(i, n){
		if(i){
			cout << " ";
		}
		cout << vv[i];
	}
	cout << endl;
}
   

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define FOR(i,l,r) for(i=l;i<r;i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(x) x.begin(),x.end()
#define P pair<ll,ll>
#define F first
#define S second
ll f(ll n){
  ll ans=1;
  for(ll x=2;x*x<=n;++x)if(n%x==0){
    ll c=0;
    while(n%x==0){n/=x;c++;}
    ans*=c+1;
  }
  if(n>1)ans*=2;
  return ans;
}
signed main(){
  ll N,i,ans=0;cin>>N;
  if(N>500000)ans+=1722125036282;
  if(N>1000000)ans+=5512937771084;
  if(N>1500000)ans+=9499861947730;
  if(N>2000000)ans+=13591488610189;
  if(N>2500000)ans+=17755581570386;
  if(N>3000000)ans+=21976790524609;
  if(N>3500000)ans+=26242735418711;
  if(N>4000000)ans+=30549028138813;
  if(N>4500000)ans+=34886918103679;
  if(N>5000000)ans+=39254974125209;
  if(N>5500000)ans+=43650106925225;
  if(N>6000000)ans+=48068775910024;
  if(N>6500000)ans+=52508580431588;
  if(N>7000000)ans+=56968921830520;
  if(N>7500000)ans+=61447068735440;
  if(N>8000000)ans+=65945737194645;
  if(N>8500000)ans+=70455013252192;
  if(N>9000000)ans+=74984870393344;
  if(N>9500000)ans+=79524588608243;
  FOR(i,(N-1)/500000*500000+1,N+1)ans+=i*f(i);
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 3612
#include<stdio.h>
int main(){
  int x,y;
  
  while(1){
    scanf("%d %d",&x,&y);
    if(x==0&&y==0){
      break;
    }
  if(x>y){
    printf("%d %d\n",y,x);
  }
  if(x==y){
    printf("%d %d\n",x,y);
  }
  if(x<y){
    printf("%d %d\n",x,y);
  }
  }
  return(0);
}

Measured memory_bytes: 1028
#input template
from sys import stdin, stdout
cin = stdin.readline
cout = stdout.write
mp = lambda: list(map(int, cin().split()))

def chars(): #function for taking string input as character array since string in python is immutable
    s = cin()
    return(list(s[:len(s) - 1]))

#print list	
def pl(a):	
	for val in a:
		cout(str(val) + ' ')
	cout('\n')
		
#main

n, = mp()
a = mp()
b = mp()

x = sum(b)
ans = a[0] + x
temp = ans
for i in range(1, n):
	temp += a[i] - b[i-1]
	ans = max(ans, temp)
cout(str(ans))

Measured memory_bytes: 9168
#include <bits/stdc++.h>
using namespace std;
const int MOD = (int)1e9 + 7;
const int INF = 100100100;
const double PI = 3.14159265358979323846;
typedef long long ll;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n, a, b;
  cin >> n;
  vector<int> v(n), p(n), q(n);
  REP(i, n) v[i] = i + 1;
  REP(i, n) cin >> p[i];
  REP(i, n) cin >> q[i];
  int pCount = 0, qCount = 0;
  do {
    pCount++;
    qCount++;
    int pj = 0, qj = 0;

    for (pj = 0; pj < n; pj++) {
      if (v[pj] != p[pj]) break;
    }
    for (qj = 0; qj < n; qj++) {
      if (v[qj] != q[qj]) break;
    }
    if (pj == n) a = pCount;
    if (qj == n) b = qCount;
  } while (next_permutation(v.begin(), v.end()));
  cout << abs(a - b) << endl;
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e5;
const ll mod=1e9+7;
int n;
ll a[maxn+10];
int main()
{
    while(cin>>n)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>a[i];
            a[i]+=a[i-1];
        }
        int l=1,r=3;
        ll ans=1e18;
        for(int i=2;i<n-1;i++)
        {
            while(l<i&&abs(a[i]-a[l]-a[l])>=abs(a[i]-a[l+1]-a[l+1]))
            {
                l++;
            }
            while(r<n&&abs(a[n]-a[r]-(a[r]-a[i]))>=abs(a[n]-a[r+1]-(a[r+1]-a[i])))
            {
                r++;
            }
            ans=min(ans,max(a[l],max(a[i]-a[l],max(a[r]-a[i],a[n]-a[r])))-min(a[l],min(a[i]-a[l],min(a[r]-a[i],a[n]-a[r]))));
        }
        cout<<ans<<endl;
    }
    return 0;
}

Measured memory_bytes: 1792
#include<cstdio>
#include<queue>
#include<utility>
#include<cstring>
#include<stack>
#include<algorithm>
#include<cmath>
#include<iostream>
#define MAX_N 100001
#define INF 2147483647
#define REP(i,n) for(int i=0;i<(int)(n);i++)
void init(int n);
int find(int n);
void unite(int x,int y);
bool same(int x, int y);
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};
struct data{
  int a;
  int b;
  int c;
  int key;
};

using namespace std;
int main()
{
  int Ma,Mb,res=55555555,N,a[101],b[101],c[101],dp[42][412][412];
  cin >> N >> Ma >> Mb;
  REP(i,N)cin >> a[i] >> b[i] >> c[i];
  
  REP(i,41)REP(j,401)REP(k,401)dp[i][j][k] = 55555555;
  dp[0][0][0] = 0;
  for(int i=0;i<=N;i++)
    for(int j=0;j<=400;j++)
      for(int k=0;k<=400;k++){
        dp[i+1][j][k] = min(dp[i+1][j][k],dp[i][j][k]);
        dp[i+1][j+a[i]][k+b[i]] = min(dp[i][j][k] + c[i],dp[i+1][j+a[i]][k+b[i]]);
      }
  for(int j=1;j<=400;j++)
    for(int k=1;k<=400;k++){
      if(j*Mb == k*Ma){
        res = min(res,dp[N][j][k]);
      }
    }
  if(res == 55555555)
    cout << -1 <<endl;
  else
    cout << res <<endl;
  return 0;
  
  
}




Measured memory_bytes: 28032
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
 
class Main {
  public static void main(String[] args)throws IOException{
      
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
  
    String[] strAry = str.split(" ");
    int x = Integer.parseInt(strAry[0]);
  	
  
    System.out.println((x/3600 )+ ":"+((x%3600)/60)+":"+ ((x%3600)%60) );
  } 
}

Measured memory_bytes: 24244
//
// Created by yamunaku on 2020/03/28.
//

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
#define repl(i, l, r) for(int i = (l); i < (r); i++)
#define per(i, n) for(int i = ((n)-1); i >= 0; i--)
#define perl(i, l, r) for(int i = ((r)-1); i >= (l); i--)
#define all(x) (x).begin(),(x).end()
#define MOD9 998244353
#define MOD1 1000000007
#define IINF 1000000000
#define LINF 1000000000000000000
#define SP <<" "<<
#define CYES cout<<"Yes"<<endl
#define CNO cout<<"No"<<endl
#define CFS cin.tie(0);ios::sync_with_stdio(false)
#define CST(x) cout<<fixed<<setprecision(x)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using mti = vector<vector<int>>;
using vl = vector<ll>;
using mtl = vector<vector<ll>>;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
template<typename T>
using heap = priority_queue<T, vector<T>, function<bool(const T, const T)>>;

int main(){
    //CFS;
    int k, n;
    cin >> k >> n;
    vi a(n);
    rep(i, n){
        cin >> a[i];
    }
    int ans = 0;
    rep(i, n - 1){
        ans = max(ans, a[i + 1] - a[i]);
    }
    ans = max(ans, k - a[n - 1] + a[0]);
    cout << k - ans << endl;
    return 0;
}

Measured memory_bytes: 1024
from collections import deque
h,w = map(int, input().split())
c = tuple(map(int, input().split()))
d = tuple(map(int, input().split()))
m = ['#' for _ in range(h+2)]
m[0] += '#'*w
m[h+1] += '#'*w
que = deque()
que.append(c)
warp = [[10**6 for _ in range(w+1)] for _ in range(h+1)]
for i in range(1,h+1):  m[i] += input()+'#'
up = lambda x,y: 1 if 0<x-1 and m[x-1][y]=='.' else 0
down = lambda x,y: 1 if x+1<=h and m[x+1][y]=='.' else 0
right = lambda x,y: 1 if y+1<=w and m[x][y+1]=='.' else 0
left = lambda x,y: 1 if 0<y-1 and m[x][y-1]=='.' else 0

def jump(x,y,cost):
  deq = deque()
  for i in range(x-2,x+3):
    if i<=0 or h<i:  continue
    for j in range(y-2,y+3):
      if j<=0 or w<j:  continue
      if m[i][j] == '.' and warp[i][j]>cost+1:
        warp[i][j] = cost+1
        deq.append((i,j))
  return deq
  
warp[c[0]][c[1]] = 0
while que:
  q = que.popleft()
  row = q[0]
  col = q[1]
  cost = warp[row][col]
  if up(row,col) and warp[row-1][col]>cost:
    que.appendleft((row-1,col))
    warp[row-1][col] = cost
  if down(row,col) and warp[row+1][col]>cost:
    que.appendleft((row+1,col))
    warp[row+1][col] = cost
  if right(row,col) and warp[row][col+1]>cost:
    que.appendleft((row,col+1))
    warp[row][col+1] = cost
  if left(row,col) and warp[row][col-1]>cost:
    que.appendleft((row,col-1))
    warp[row][col-1] = cost
  que.extend(jump(row,col,cost))

if warp[d[0]][d[1]] != 10**6:  print(warp[d[0]][d[1]])
else:  print(-1)


Measured memory_bytes: 120480
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
#include <map>
#include <set>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <forward_list>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <bitset>
#include <memory>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <cstdint>
#include <iomanip>
#include <tuple>
#include <random>
#include <map>
#include <queue>
#include <utility>

using namespace std;

typedef unsigned long long int ull;
typedef long long int ll;

ll N,K;

inline ll calc(const ll b) {
    ll p,r;
    p = N / b;
    r = N % b;
    ll ret = p * max((ll)0,b-K) + max((ll)0,r-K+1);
    return ret;
}

int main(void) {
    cin >> N >> K;
    ll res = 0;
    for(int b = 1;b <= N;b++) {
        res += calc(b);
    }
    if(K == 0) {
        res = res - N;
    }
    cout << res << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define PI 3.14159265358979323846
#define int long long
constexpr long long INF = numeric_limits<long long>::max() / 2;
constexpr int MOD = 1000000007;
using Graph = vector<vector<int>>;



signed main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    int Q;
    cin>>Q;
    int l[Q],r[Q];
    rep(i,Q){
        cin>>l[i]>>r[i];
    }
    int ma=100000;
    vector<int> dp{};
    vector<int> s{};
    s.push_back(2);
    s.push_back(3);
    s.push_back(5);
    s.push_back(7);
    dp.push_back(3);
    dp.push_back(5);
    for(int i=9;i<=ma;i+=2){
        bool z=true;
        rep(j,s.size()){
            if(i%s[j]==0){
                z=false;
                break;
            }else if(s[j]*s[j]>i) break;
        }
        if(z){
            s.push_back(i);
            int x=(i+1)/2;
            rep(j,s.size()){
                if(s[j]==x){
                    dp.push_back(i);
                    break;
                }else if(s[j]>x){
                    break;
                }
            }
        }

    }
    int w=0;
    int ch[ma+1]{};
    ch[0]=0;
    for(int i=1;i<=ma;i++){
        ch[i]=ch[i-1];
        if(i!=dp[w])continue;
        ch[i]++;
        w++;
    }
    rep(i,Q){
        cout << ch[r[i]]-ch[l[i]-1]<<endl;
    }


    



}


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

using namespace std;

const int maxn = 3000000;
int fac[maxn], invfac[maxn];

#define MOD 1000000007
long long qpow(long long a, long long b){
    long long s = 1;
    while (b > 0) {
        if (b & 1)s = s * a % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return s;
}
void init() {
    int n = maxn - 1;
    fac[0] = 1;
    for (int i = 1; i <= n; ++i)
        fac[i] = 1ll * fac[i - 1] * i % MOD;
    invfac[n] = qpow(fac[n], MOD - 2);
    for (int i = n - 1; i >= 0; --i) {
        invfac[i] = 1ll * invfac[i + 1] * (i + 1) % MOD;
    }
}
long long comb(int n, int m) {
    if (n < m) return 0;
    return 1ll * fac[n] * invfac[m] % MOD * invfac[n - m] % MOD;
}

int main(){
  init();
  long long r1, c1, r2, c2;
  cin >> r1 >> c1 >> r2 >> c2;
  long long g11 = 0;
  long long g12 = 0;
  long long g21 = 0;
  long long g22 = 0;
  for(int i = 0; i < r2+1; i++){
    g22 += comb(i+1+c2, c2);
    g22 %= MOD;
  }
  for(int i = 0; i < r2+1; i++){
    g21 += comb(i+1+c1-1, c1-1);
    g21 %= MOD;
  }
  for(int i = 0; i < r1; i++){
    g12 += comb(i+1+c2, c2);
    g12 %= MOD;
  }
  for(int i = 0; i < r1; i++){
    g11 += comb(i+1+c1-1, c1-1);
    g11 %= MOD;
  }

  cout << (g22 - g12 - g21 + g11 + MOD * 2) % MOD << endl;
}


Measured memory_bytes: 23680
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<cmath>
#include<string>
#include<queue>
#include<iomanip>
#include<bitset>
#include<math.h>
#include<stack>
#include<set>
using namespace std;
#define int long long
signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie();
	cout.tie();
	int k;
	cin >> k;
	int sum = 0;
	for (int i = 1; i <= k; i++) {
		for (int j = 1; j <= k; j++) {
			for (int z = 1; z <= k; z++) {
				sum += __gcd(__gcd(i, j), z);
			}
		}
	}
	cout << sum;
}
	

Measured memory_bytes: 3632
#include<iostream>
#include<string>

std::string s;
int N;
int ans;

int main()
{
	std::cin >> s >> N;
	
	for( int i = 0; i != N; ++i )
	{
		std::string t;
		std::cin >> t;

		for( int j = 0; j != t.size(); ++j )
		{
			bool f = true;

			for( int k = 0; k != s.size(); ++k )
			{
				if( s[k] != t[( j + k ) % t.size()] )
				{
					f = false;
					break;
				}
			}

			if( f )
			{
				++ans;
				break;
			}
		}
	}

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

	return 0;
}

Measured memory_bytes: 1208