akhauriyash/Code-Regression huggingface dataset:
from sys import stdin
input = stdin.readline

N = 1000

def lcs(X, Y):
    c = [[0]*(N+1) for _ in range(N+1)]
    maxl = 0
    X = ' ' + X # 1オリジンにする
    Y = ' ' + Y
    pre_row = c[0]
    for i, x in enumerate(X):
        if i == 0:
            continue
        row = c[i]
        for j, y in enumerate(Y):
            if j == 0:
                continue
            if x == y:
                value = pre_row[j-1] + 1
                row[j] = value
                if maxl < value: # maxlの更新はこの時しか起こらない
                    maxl = value
            else:
                pre_row_j = pre_row[j]
                row_j_1 = row[j-1]
                value = row_j_1
                if pre_row_j > row_j_1:
                    value = pre_row_j # max関数だと、とても遅い
                row[j] = value
        pre_row = row # 今の列が次の列にとって一つ前の列になる

    return maxl

n = int(input())
for _ in range(n):
    s1 = input()[:-1]
    s2 = input()[:-1]
    print(lcs(s1, s2))


Measured memory_bytes: 30936
import math
n,d=map(int,input().split())

x=[[] for _ in range(n)]

for j in range(n):
    x[j] = input().split()
cnt=0
for i in range(n):
    for k in range(i+1,n):
        wa=0
        for j in range(d):
            wa +=(int(x[k][j])-int(x[i][j]))**2
        if math.sqrt(wa)%2==0 or math.sqrt(wa)%2==1:
            cnt+=1
print(cnt)           













Measured memory_bytes: 3188
# include "bits/stdc++.h"
using namespace std;
using LL = long long;
using ULL = unsigned long long;
const double PI = acos(-1);
template<class T>constexpr T INF() { return ::std::numeric_limits<T>::max(); }
template<class T>constexpr T HINF() { return INF<T>() / 2; }
template <typename T_char>T_char TL(T_char cX) { return tolower(cX); };
template <typename T_char>T_char TU(T_char cX) { return toupper(cX); };
const int vy[] = { -1, -1, -1, 0, 1, 1, 1, 0 }, vx[] = { -1, 0, 1, 1, 1, 0, -1, -1 };
const int dx[4] = { 0,1,0,-1 }, dy[4] = { 1,0,-1,0 };
int popcnt(unsigned long long n) { int cnt = 0; for (int i = 0; i < 64; i++)if ((n >> i) & 1)cnt++; return cnt; }
int d_sum(LL n) { int ret = 0; while (n > 0) { ret += n % 10; n /= 10; }return ret; }
int d_cnt(LL n) { int ret = 0; while (n > 0) { ret++; n /= 10; }return ret; }
LL gcd(LL a, LL b) { if (b == 0)return a; return gcd(b, a%b); };
LL lcm(LL a, LL b) { LL g = gcd(a, b); return a / g*b; };
# define ALL(qpqpq)           (qpqpq).begin(),(qpqpq).end()
# define UNIQUE(wpwpw)        sort(ALL((wpwpw)));(wpwpw).erase(unique(ALL((wpwpw))),(wpwpw).end())
# define LOWER(epepe)         transform(ALL((epepe)),(epepe).begin(),TL<char>)
# define UPPER(rprpr)         transform(ALL((rprpr)),(rprpr).begin(),TU<char>)
# define FOR(i,tptpt,ypypy)   for(LL i=(tptpt);i<(ypypy);i++)
# define REP(i,upupu)         FOR(i,0,upupu)
# define INIT                 std::ios::sync_with_stdio(false);std::cin.tie(0)

int n, k;
int a[202020];

int main(){
    cin >> n >> k;
    REP(i, n)cin >> a[i];
    int ans = 1, cnt = 0;
    bool flg = false;
    set<int>st;
    REP(i, n){
        if(i && a[i] > a[i - 1]){
            cnt++;
        }
        if(i >= k && a[i - k + 1] > a[i - k]){
            cnt--;
        }
        st.insert(a[i]);
        if(i >= k){
            if(a[i] != *st.rbegin() || a[i - k] != *st.begin()){
                if(cnt != k - 1 || !flg){
                    ans++;
                }
            }
            st.erase(a[i - k]);
        }
        if(cnt == k - 1)flg = true;
    }
    cout << ans << endl;
}

Measured memory_bytes: 10368
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n, m;
    int max;
    char s[110][15],t[110][15];
    cin >> n;
    for(int i = 0; i< n; i++){
        cin >>s[i];
    }
    
    cin >> m;
    for(int i = 0; i< m; i++){
        cin >>t[i];
    }
    
    map<string,int> mp;
    for(int i=0; i< n ; i++){
        if(mp[s[i]]==0){
            mp[s[i]] = 1;
        }else{
            mp[s[i]]++;
        }
    }
    
    for(int j=0; j< m ; j++){
        auto itr = mp.find(t[j]);
        if( itr != mp.end() ) {
            mp[t[j]]--;
        }
    }
    max = 0;
    for(int i=0; i< n ; i++){
        if(mp[s[i]] >max){
            max = mp[s[i]];
        }
    }
    cout << max << endl;
    
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue> 
using namespace std;
#define INF 0x3f3f3f3f
char p[110][110];
int n,m,k;
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		if(n+m<10)cout<<n+m<<endl;
		else cout<<"error"<<endl;
	}
	
	return 0;
}

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

int main() {
  string s, ans=""; cin >> s;
  int K; cin >> K;
  for(int i = 0; i < s.size(); ++i) {
    if('z'-s[i]+1 <= K && s[i] != 'a') {
      ans.push_back('a');
      K -= ('z'-s[i]+1);
    } else {
      ans.push_back(s[i]);
    }
  }
  int len = ans.size() - 1;
  if(K > 0) {
    ans[len] += K % 26;
  }
  cout << ans << endl;
  return 0;
}

// 整理
// 前から順番に見ていく
// s[i]にKを足した結果aにくり上がれるならaに繰り上げ
// そうでない時は最後の文字を残ったK回足してあげるのが妥当


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

vector<string> split(const string& s, char d)
{
  vector<string> r;
  for (string::const_iterator it = s.begin();;) {
    string::const_iterator jt = find(it, s.end(), d);
    if (jt == s.end()) {
      r.push_back(string(it, s.end()));
      return r;
    } else {
      r.push_back(string(it, jt));
      it = jt+1;
    }
  }
}
int main()
{
  string line;
  while (cin >> line && line != "#") {
    vector<string> clauses = split(line, '|');
    for (vector<string>::const_iterator it = clauses.begin(); it != clauses.end(); ++it) {
      vector<string> lits = split(it->substr(1, it->size()-2), '&');
      vector<int> v(100, 0);
      for (vector<string>::const_iterator jt = lits.begin(); jt != lits.end(); ++jt) {
        if ((*jt)[0] == '~') {
          v[(*jt)[1]-'A'] |= 1<<1;
        } else {
          v[(*jt)[0]-'A'] |= 1<<0;
        }
      }
      if (find(v.begin(), v.end(), 3) == v.end()) {
        cout << "yes" << endl;
        goto NEXT;
      }
    }
    cout << "no" << endl;
NEXT:
    ;
  }
  return 0;
}

Measured memory_bytes: 896
#include <bits/stdc++.h>
#define LLI long long int
#define FOR(v, a, b) for(LLI v = (a); v < (b); ++v)
#define FORE(v, a, b) for(LLI v = (a); v <= (b); ++v)
#define REP(v, n) FOR(v, 0, n)
#define REPE(v, n) FORE(v, 0, n)
#define REV(v, a, b) for(LLI v = (a); v >= (b); --v)
#define ALL(x) (x).begin(), (x).end()
#define RALL(x) (x).rbegin(), (x).rend()
#define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it)
#define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it)
#define EXIST(c,x) ((c).find(x) != (c).end())
#define fst first
#define snd second
#define popcount __builtin_popcount
#define UNIQ(v) v.erase(unique(ALL(v)), v.end())
#define bit(i) (1LL<<i)
#define sz(v) ((LLI)(v).size())

#ifdef DEBUG
#include <misc/C++/Debug.cpp>
#else
#define dump(...) ((void)0)
#endif

#define gcd __gcd

using namespace std;
template <class T> constexpr T lcm(T m, T n){return m/gcd(m,n)*n;}

template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;}
template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;}
template <typename T, typename U> istream& operator>>(istream &is, pair<T,U> &p){is >> p.first >> p.second; return is;}

template <typename T, typename U> bool chmin(T &a, const U &b){return (a>b ? a=b, true : false);}
template <typename T, typename U> bool chmax(T &a, const U &b){return (a<b ? a=b, true : false);}
template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);}

const LLI mod = 1e9+7;

LLI power(LLI n, LLI p, LLI m){
  LLI ret = 1;
  while(p>0){
    if(p&1) (ret *= n) %= m;
    (n *= n) %= m;
    p /= 2;
  }
  return ret;
}

LLI mod_inv(LLI n, LLI p){return power(n,p-2,p);}

LLI dp[5100][5100];

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

  int N;
  string s;
  while(cin >> N >> s){
    fill_array(dp, 0);

    dp[0][0] = 1;

    FOR(i,0,N){
      FORE(j,0,N){
	if(j+1<=N) (dp[i+1][j+1] += dp[i][j] * 2) %= mod;
	if(j-1>=0) (dp[i+1][j-1] += dp[i][j]) %= mod;
	if(j==0) (dp[i+1][j] += dp[i][j]) %= mod;
      }
    }

    LLI ans = dp[N][s.size()];

    (ans *= mod_inv(power(2,s.size(),mod),mod)) %= mod;
    

    cout << ans << endl;
  }
  
  return 0;
}


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


struct mint {
  ll x;
  mint(ll x=0) : x((x%MOD+MOD)%MOD) {}
  mint& operator+=(const mint a) { if ((x += a.x) >= MOD) x -= MOD; return *this; }
  mint operator+(const mint a) const { return mint(*this) += a; }
};
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }


const int M = 60;
mint dp[M+1][2][2][2];


int main() {
  ll L, R; cin >> L >> R;
  dp[0][0][0][0] = 1;

  rep(i, M) {
    int Li = L>>(M-1-i)&1;
    int Ri = R>>(M-1-i)&1;
    rep(j, 2) rep(l, 2) rep(r, 2) rep(xi, 2) rep(yi, 2) {
      int j2 = j, l2 = l, r2 = r;

      if (xi > yi) continue;

      if (!j && (xi^yi)) continue;
      if (xi&yi) j2 = 1;

      if (!l && (xi < Li)) continue;
      if (xi > Li) l2 = 1;

      if (!r && (yi > Ri)) continue;
      if (yi < Ri) r2 = 1;

      dp[i+1][j2][l2][r2] += dp[i][j][l][r];
    }
  }

  mint ans = 0;
  rep(l, 2) rep(r, 2) ans += dp[M][1][l][r];
  cout << ans << '\n';
}


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

int main() {
  int N, M;
  vector<int> A(N);
  
  cin >> N >> M;
  int total = 0;
  for (int i=0; i<N; i++){
    cin >> A.at(i);
    total += A.at(i);
  }
  
  int count = 0;
  for (int i=0; i<N; i++){
    if (A.at(i) >= (double) total/(4*M)) count++;
  }
  
  if (count >= M) cout << "Yes" << endl;
  else cout << "No" << endl;
}

Measured memory_bytes: 16640
#include"bits/stdc++.h"
#define INFLL        1e18
#define REP(i,n)     for(ll i=0,i##_len=(n);i<i##_len;++i)
#define REP1(i,n)    for(ll i=1,i##_len=(n);i<=i##_len;++i)
#define REPR(i,n)    for(ll i=(n)-1;i>=0;--i)
#define REPR1(i,n)   for(ll i=(n);i>0;--i)
#define REPC(i,obj)  for(auto i:obj)
#define ALL(obj)     (obj).begin(),(obj).end()
#define SETP(n)      cout<<fixed<<setprecision(n)
#define DELNL        cin.ignore(numeric_limits<streamsize>::max(),'\n')
#define VV(T,h,w)    vector<vector<T>>(h,vector<T>(w))
#define VVI(T,h,w,i) vector<vector<T>>(h,vector<T>(w,i))
using namespace std;
using ll=long long;
using ull=unsigned long long;
template<typename T>using pqg=priority_queue<T,vector<T>,greater<T>>;
template<typename T=ll>inline T in(){T ret;cin>>ret;return ret;}

int main()
{
    ll n=in(),sum=0;
    vector<ll>w(n);REP(i,n)sum+=w[i]=in();
    ll ans=INFLL,sum_tmp=0;
    REP(i,n){
        sum_tmp+=w[i];
        if(i==n-1||w[i]!=w[i+1])ans=min(ans,abs(sum_tmp*2-sum));
    }
    cout<<ans<<endl;
}

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

#define rep(i, a) for (int i = 0; i < (a); i++ )
#define FOR(i, m, n) for(int i = m; i < n; i++)
 
typedef pair<int,int> P;
typedef long long ll;
 
const int INF = 1001001001;
const int MOD = 1000000007;
const double PI=acos(-1);

int main(){
    ll N,K;
    cin >> N >> K;
    
    ll ans = 0;

    for (ll k=K; k<=N+1; k++) {
        ans += (N+(N-k+1))*k/2 - (k-1)*k/2 + 1;
        ans %= MOD;
        //cout << (N+(N-k+1))*k/2 <<" " <<  (k-1)*k/2  << endl;
    }
    cout << ans << endl;
}


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

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"
*/

const int WHITE=0, GRAY=1, BLACK=2;
const int INF = 10000;
/*
void bfs(int u, int d[], int color[], int N, int M[][]){
    d[u] = 0;
    color[u] = GRAY;
    queue<int> Q;
    Q.push(u);
    while(!Q.empty()){
        int current_u = Q.front(); Q.pop();
        rep(i,N){
            if(M[current_u][i] && color[i] == WHITE){
                Q.push(i); d[i] = d[u]+1;
            }
        }
    }
    rep(i,N){
        printf("%d %d\n", i+1, d[i]);
    }
}
*/

int main()
{
    int N; cin >> N;
    int d[N], M[N][N], color[N];
    // 初期化
    rep(i, N){
        d[i] = INF;
        color[i] = WHITE;
        rep(j,N) M[i][j] = false;
    }
    // 辺の情報を読み込む
    rep(i,N){
        int id; cin >> id;
        int k; cin >> k;
        rep(j,k){
            int vj; cin >> vj;
            M[i][vj-1] = true;
        }
    }
    d[0] = 0;
    color[0] = GRAY;
    queue<int> Q;
    Q.push(0);
    while(!Q.empty()){
        int current_u = Q.front(); Q.pop();
        // cout << current_u << endl;
        rep(i,N){
            if(M[current_u][i] && color[i] == WHITE){
                Q.push(i); d[i] = d[current_u]+1;
                color[i] = GRAY;
            }
        }
        color[current_u] = BLACK;
    }
    rep(i,N){
        if(d[i] == INF) printf("%d %d\n", i+1, -1);
        else printf("%d %d\n", i+1, d[i]);
    }
}


Measured memory_bytes: 3236
#define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>

using namespace std;

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

static const double eps = 10e-9;

int main(){
	int n;
	double table[]={165.0,170.0,175.0,180.0,185.0};
	int hist[] = {0,0,0,0,0,0};
	while(~scanf("%d",&n)){
		for(int i=0;i<n;i++){
			double tall;
			scanf("%lf",&tall);
			int idx=5;
			for(int j=0;j<sizeof(table)/sizeof(double);j++){
				if(table[j] > tall){
					idx = j;
					break;
				}
			}
			hist[idx]++;
		}

		for(int i=0;i<sizeof(hist)/sizeof(int);i++){
			printf("%d:",i+1);
			for(int j=0;j<hist[i];j++){
				cout << "*";
			}
			cout << endl;
		}
	}
}

Measured memory_bytes: 0
#include<stdio.h>
int main()
{
	char s[128];
	int a,b,i;
	while(scanf("%s",s),s[0]-'0')
	{
		for(a=b=i=0; s[i+1]; ++i)
			(s[i+1]-'B'?a:b)++;
		(a<b?b:a)++;
		printf("%d %d\n",a,b);
	}
	return 0;
}

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

using namespace std;

bool sortbysec(const pair<int,int> &a, 
              const pair<int,int> &b) { 
    return (a.second < b.second); 
} 

int partition(vector<pair<int, int>>& arr, int lo, int hi) {
    pair<int, int> pivot = arr[hi];
    int idx = lo;
    for (int i = lo; i < hi; i++) {
        if (arr[i].second < pivot.second) {
            std::swap(arr[idx], arr[i]);
            idx++;
        }
    }

    std::swap(arr[idx], arr[hi]);
    return idx;
}

void quickSort(vector<pair<int, int>>& arr, int lo, int hi) {
    if (lo >= hi) {
        return;
    }

    int p = partition(arr, lo, hi);
    quickSort(arr, lo, p - 1);
    quickSort(arr, p + 1, hi);
}

unsigned long f(int n, vector<int> a, vector<pair<int, int>> queries) {

//    quickSort(queries, 0, queries.size() - 1);

    sort(queries.begin(), queries.end(), sortbysec);

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

    unsigned long result = 0;
    int idx_a = a.size() - 1;
    int idx_q = queries.size() - 1;
    for (int i = 0; i < n; i++) {
        if (a[idx_a] >= queries[idx_q].second) {
            result += a[idx_a];
            idx_a--;
        } else {
            result += queries[idx_q].second;
            queries[idx_q].first--;
            if (queries[idx_q].first <= 0) {
                idx_q--;
            }
        }
    }

    return result;
}

int main() {

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

    vector<int> a;
    for (int i = 0; i < n; i++) {
        int tmp;
        cin >> tmp;
        a.push_back(tmp);
    }

    vector<pair<int, int>> queries;
    for (int i = 0; i < m; i++) {
        pair<int, int> p;
        cin >> p.first;
        cin >> p.second;
        queries.push_back(p);
    }

    unsigned long result = f(n, a, queries);
    cout << result << endl;

    return 0;
}


Measured memory_bytes: 2548
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef long long Int;
typedef pair<Int, Int> P;

int main() {
    int a, b;
    cin >> a >> b;
    char ans = 'x';
    if (a + b == 15) ans = '+';
    if (a * b == 15) ans = '*';
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std ;
int N;
int main(){
    cin >> N;
    string S;
    cin >> S;
    int count=0;
    for(int i=0;i<N-2;i++){
        if(S[i]=='A'&&S[i+1]=='B'&&S[i+2]=='C'){
            count++;
        }
    }
    cout << count << "\n";
}

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

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second

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

int n, d;

bool issq(int x){
	int rx = (int)sqrt(x);
  	return rx*rx == x;
}

bool isgood(vi x, vi y){
	int ans = 0;
  	rep(i,d) ans += (x[i]-y[i])*(x[i]-y[i]);
  	//cout << "ans = " << ans << endl;
	return issq(ans);  	
}

void Main()
{
  	cin >> n >> d;
  	vvi x(n, vi(d));
  	rep(i,n) rep(j,d) cin >> x[i][j];
  	int cnt = 0;
	rep(i,n-1) {
    	rep3(j,i+1,n-1){
        	if(isgood(x[i], x[j])) {
              	cnt++;
            }
        }
    }  	
  	cout << cnt << endl;
  	return;
}

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
	return 0;
}

Measured memory_bytes: 256
import itertools

n,m,x = map(int, input().split(" "))
infolist = []
for i in range(n):
  infolist.append(list(map(int, input().split(" "))))
  
optionlist = list(itertools.product('AB', repeat=n))

moneylist = []
#各オプションにつき
for option in optionlist:
  knowledge = [0]*m
  money = 0
  #各本につき
  for i in range(n):
    if option[i]=="A":
      #各ナレッジにつき参考書から加点
      for k in range(m):
        knowledge[k] += infolist[i][k+1]
      money += infolist[i][0]
  #オプション評価
  if all([p >= x for p in knowledge]):
    moneylist.append(money)

if moneylist == []:
  print(-1)
else:
  print(min(moneylist))

Measured memory_bytes: 9740
import sys
from collections import defaultdict, deque
from heapq import heappush, heappop
import math
import bisect

# 再起回数上限変更
# sys.setrecursionlimit(1000000)

N = int(input())
a = int(N ** .5)

for num in range(a):
    num = a - num
    if N % num == 0:
        b = N // num
        # print(b, num)
        print(b + num -2)
        sys.exit()




Measured memory_bytes: 40044
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

const int INF = 100000000;

int main(){

	int dp[1000];
	fill(dp,dp+1000,INF);
	dp[0] = 0;

	int serve[] = {1,5,10,50,100,500};
	for(int i = 1; i < 1000; i++){
		for(int j = 0; j < 6; j++){
			if(i - serve[j] >= 0){
				dp[i] = min(dp[i],dp[i-serve[j]] + 1);
			}
			else
				break;
		}
	}

	int n;
	while(cin >> n && n != 0){
		int left = 1000 - n;
		cout << dp[left] << endl;
	}

	return 0;
}

Measured memory_bytes: 0
#include <iostream>
#include <fstream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#pragma comment(linker, "/STACK:400000000")

#define EPS 1e-9
#define INF MOD
#define MOD 1000000007LL
#define fir first
#define foreach(it,X) for(it=X.begin();it!=X.end();it++)
#define iss istringstream
#define ite iterator
#define ll long long
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<n;i++)
#define pi pair<int,int>
#define pb push_back
#define sec second
#define sst stringstream
#define sz size()
#define vc vector
typedef vc<int> vi;
typedef vc<ll> vl;
typedef vc<string> vs;

struct edge{int to,cap,rev,type;};

vector<edge> g[55];
bool used[55];

void addEdge(int from,int to,int cap){
	edge e;
	e.to=to;e.cap=cap;e.rev=g[to].size();e.type=1;
	g[from].push_back(e);
	e.to=from;e.cap=0;e.rev=g[from].size()-1;e.type=0;
	g[to].push_back(e);
}

int dfs(int v,int t,int f){
	if(v==t)return f;
	used[v]=1;
	rep(i,g[v].size()){
		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 maxFlow(int s,int t){
	int flow=0;
	for(;;){
		fill(used,used+55,0);
		int f=dfs(s,t,INF);
		if(!f)return flow;
		flow+=f;
	}
}

int N,M;
int from[510],to[510],a[510][55];

int main(){
	while(cin>>N>>M && N){
		rep(i,M)rep(j,51)a[i][j]=0;
		rep(i,M){
			int u,v;
			string p;
			cin>>from[i]>>to[i]>>p;
			from[i]--,to[i]--;
			rep(j,p.sz){
				if(p[j]=='+')p[j]=' ';
			}
			iss is(p);
			string s;
			while(is>>s){
				int A=0,B=0;
				int x=0;
				while(x<s.sz && isdigit(s[x])){
					A=A*10+s[x]-'0';
					x++;
				}
				if(x==0)A=1;
				if(x==s.sz){
					a[i][0]=A;
					continue;
				}
				assert(s[x]=='x');
				x++;
				if(x==s.sz){
					a[i][1]=A;
					continue;
				}
				assert(s[x]=='^');
				x++;
				while(x<s.sz && isdigit(s[x])){
					B=B*10+s[x]-'0';
					x++;
				}
				a[i][B]=A;
			}
		}
		
		vi ans(51);
		int higherDeg[55][510]={};
		for(int i=50;i>=0;i--){
			rep(j,N)g[j].clear();
			rep(j,M){
				if(higherDeg[from[j]][g[from[j]].sz]){
					addEdge(from[j],to[j],INF);
				}else{
					addEdge(from[j],to[j],a[j][i]);
				}
				if(higherDeg[to[j]][g[to[j]].sz]){
					addEdge(to[j],from[j],INF);
				}else{
					addEdge(to[j],from[j],a[j][i]);
				}
			}
			ans[i]=maxFlow(0,N-1);
			rep(j,N)rep(k,g[j].sz){
				if(g[j][k].type==1 && g[j][k].cap>0){
					higherDeg[j][k]=1;
				}
			}
		}
		sst ss;
		for(int i=50;i>=0;i--)if(ans[i]>0){
			if(i>1){
				if(ans[i]>1)ss<<ans[i]<<"x^"<<i<<"+";
				else ss<<"x^"<<i<<"+";
			}
			else if(i==1){
				if(ans[i]>1)ss<<ans[i]<<"x+";
				else ss<<"x+";
			}
			else ss<<ans[i]<<"+";
		}
		string out=ss.str();
		if(out!="")cout<<out.substr(0,out.sz-1)<<endl;
		else cout<<0<<endl;
	}
}

Measured memory_bytes: 1548
import math

def prime_factorize(n):
    ret = []
    # 小さい素数から順に試し割りしていく
    for i in range(2, int(math.sqrt(n))+1):
        if n % i != 0:
            continue
        e = 0
        while n % i == 0:
            e += 1
            n //= i
        ret += [(i, e)]

    if n != 1:
        ret += [(n, 1)]

    return ret


def extgcd(a, b):
    x,y, u,v = 0,1, 1,0
    while a != 0:
        q, r = b//a, b%a
        m, n = x-u*q, y-v*q
        b,a, x,y, u,v = a,r, u,v, m,n
        g = b
    return x, y, g


def modinv(a, m):
    x, y, g = extgcd(a, m)
    if g != 1:
        print ("[+]Inverse does not exist.")
    else:
        return ((m + x) % m) % m


def calc_comb(n, r, mod):
    ans_mul, ans_div = 1, 1

    for i in range(r):
        ans_mul *= (n-i)
        ans_div *= (i+1)
        ans_mul %= mod
        ans_div %= mod

    return ans_mul * modinv(ans_div, mod) % mod


def main():
    N, M = map(int, input().split())
    v = prime_factorize(M)
    MOD = int(10**9+7)
    ans = 1

    for i in v:
        ans *= calc_comb(N+i[1]-1, i[1], MOD)
        ans %= MOD
    print (ans)

main()


Measured memory_bytes: 3192
#include<bits/stdc++.h>
using namespace std;
 
void __print(int x) {cout << x;}
void __print(long x) {cout << x;}
void __print(long long x) {cout << x;}
void __print(unsigned x) {cout << x;}
void __print(unsigned long x) {cout << x;}
void __print(unsigned long long x) {cout << x;}
void __print(float x) {cout << x;}
void __print(double x) {cout << x;}
void __print(long double x) {cout << x;}
void __print(char x) {cout << '\'' << x << '\'';}
void __print(const char *x) {cout << '\"' << x << '\"';}
void __print(const string &x) {cout << '\"' << x << '\"';}
void __print(bool x) {cout << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), __print(i); cout << "}";}
void _print() {cout << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cout << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
 
#define int long long int
#define fi first
#define se second
#define FIO {ios::sync_with_stdio(0);cin.tie(nullptr);cout.tie(nullptr);}
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define all(x) (x).begin(), (x).end()
#define pow10(n) stoll("1"+string(n,'0'))
//fill(all(arr),1)
//if(map.count(key)){}
//bitset<20>(n);
//int mini=min({a,b,c,d,e});
 
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef long double ld;

int32_t main(){
	FIO
	int x;
	cin>>x;
	for(int i=-120;i<=120;i++)
	{
		for(int j=-120;j<=120;j++)
		{
			if(pow(i,5)-pow(j,5)==x)
			{
				cout<<i<<" "<<j;
				return 0;
			}
		}
	}
	return 0;
}

Measured memory_bytes: 4064
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
#define REP(i, n) FOR(i, 0, n)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define UM unordered_map
#define ALL(a) (a).begin(), (a).end()
typedef vector<ll> vi;
typedef vector<vector<ll>> vvi;
typedef pair<ll, ll> pii;
const ll INF = 1LL << 58;
const ll MOD = 1000000007;
struct Edge
{
    ll s, t, d;
};
typedef vector<vector<Edge>> Graph;
typedef vector<pii> vpii;
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;
}

signed main()
{
    int N;
    cin >> N;
    vi ans;
    if (N == 0)
        ans.push_back(0);

    int tmp = 1;
    while (N != 0)
    {
        if (N % (tmp * -2) == 0)
            ans.push_back(0);
        else
        {
            ans.push_back(1);
            N -= tmp;
        }
        tmp *= -2;
    }
    REP(i, ans.size())
    {
        cout << ans[ans.size() - 1 - i];
    }
    cout << endl;
    // system("pause");
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
int n;
int A[maxn], B[maxn]; 
int main()
{
	scanf("%d", &n);
	for(int i = 1; i <= n; ++i) scanf("%d", &A[i]);
	for(int i = n; i; --i) scanf("%d", &B[i]);
	int j = 1;
	for(int i = 1; i <= n; ++i)
		if(A[i] == B[i])
		{
			bool f = 0; 
			for(; j <= n; ++j)
				if(B[j] != A[i] && B[i] != A[j])
				{
					swap(B[i], B[j]);
					f = 1;
					break;
				} 
			if(!f)
			{
				puts("No");
				return 0;
			}
		}
	puts("Yes");
	for(int i = 1; i <= n; ++i) printf("%d ", B[i]);
	return 0;
}

Measured memory_bytes: 5328
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <string>
#include <numeric>


using std::cout;
using std::cin;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;


ll gcd(ll a, ll b) {
    while (b != 0) {
        auto tmp = a;
        a = b;
        b = tmp % a;
    }
    return a;
}



void oneCase () {
    string a;
    cin >> a;
    for (auto x : a) {
        cout << "x";
    }
    cout << '\n';
}




int main() {
    int k = 1;
//    cin >> k;
    for (int i = 0; i < k; ++i) {
        oneCase();
    }

    return 0;
}

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

int main() {
	int a, b, c, x; cin >> a >> b >> c >> x;

	int ans = 0;
	rep(i, 0, a + 1) {
		rep(j, 0, b + 1) {
			rep(k, 0, c + 1) {
				if (x == (500 * i + 100 * j + 50 * k)) ans++;
			}
		}
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n,m) for(int i=n;i<(int)(m);i++)
#define rrep(i,n,m) for(int i=((int)(n)-1);i>=m;i--)
#define all(x) (x).begin(),(x).end()
typedef long long ll;
const int MOD=1000000007;
using namespace std;
int main(){
    int n;
    cin>>n;
    int r=n;
    
    vector<ll> vec(n,1);
    rep(i,1,n){
        vec[i]=vec[i-1]+i+1;
    }
    
    while(r>0){
        int k=lower_bound(all(vec),r)-vec.begin();
        cout<<k+1<<endl;
        r-=k+1;
    }
    
}


Measured memory_bytes: 78336
#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 FOR(i, m, n) for(int i = m;i < n;i++)
#define itrfor(itr,A) for(auto itr = A.begin(); itr !=A.end();itr++)
typedef long long llong;
char moji[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char moji2[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char moji3[10]={'0','1','2','3','4','5','6','7','8','9'};
#define Sort(a) sort(a.begin(),a.end());
#define Reverse(a) reverse(a.begin(),a.end());
#define print(a) cout << a << endl;
#define n_max int(2*1e5+5)


struct UnionFind{
    int A[n_max];
    UnionFind( int n){
        REP(i,n) A[i]=i;
    }

    int root(int a){
        if(A[a] == a)    return a;
        int tmp;
        tmp = root(A[a]);
        A[a] = tmp;
        return tmp;
    }

    bool same(int a, int b){
        if(root(a) == root(b)) return true;
        return false;
    }

    void merge(int a,int b){
        A[root(a)] = root(b);
    }
};


struct town{
    int k,l;
    int id;
};


bool comp(town a,town b){
    if(a.k != b.k) return a.k < b.k;
    else return a.l < b.l;
}


int main(){
    int n,k,l;
    cin >> n >> k >> l;
    UnionFind K(n),L(n);

    int a,b;
    REP(i,k){
        scanf("%d%d",&a,&b);
        K.merge(a-1,b-1);
    }
    REP(i,l){
        scanf("%d%d",&a,&b);
        L.merge(a-1,b-1);
    }

    town T[n_max];
    REP(i,n){
        T[i].id=i;
        T[i].k = K.root(i);
        T[i].l = L.root(i);
    }

    sort(T,T+n,comp);

    int ans[n_max]={};

    int i=0;
    while(1){
        pair<int,int> p;
        p.first = T[i].k;
        p.second = T[i].l;
        vector<int> ind;
        ind.push_back(T[i].id);
        while(1){
            i+=1;
            if(i < n and T[i].k == p.first and T[i].l == p.second){
                ind.push_back(T[i].id);
            }
            else{
                itrfor(itr,ind) ans[*itr] = ind.size();
                break;
            }
        }
        if(i >=n) break;
    }

    REP(i,n) cout << ans[i] << " ";

}

Measured memory_bytes: 5524
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <stdio.h>
#include <iomanip>
#include <limits>
#include <list>
#include <queue>
#include <tuple>
#include <map>
#include <sstream>
using namespace std;
#define MOD (long long int)(1e9+7)
#define ll long long int
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define reps(i,n) for(int i=1; i<=(int)(n); i++)
#define REP(i,n) for(int i=n-1; i>=0; i--)
#define REPS(i,n) for(int i=n; i>0; i--)
#define FOR(i,a,b) for(int i=a; i<(int)(b); i++)
#define ALL(x) (x).begin(),(x).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define SORT(c) sort(ALL(x))
#define CLR(a) memset((a), 0 ,sizeof(a))
#define PB push_back
#define MP make_pair
#define SP << " " <<
const int INF = 1001001001;
const ll LINF = 100100100100100100;
const double EPS = 1e-10;
const double PI  = acos(-1.0);
typedef pair<int,int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}

__attribute__((constructor))
void initial(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cout << fixed << setprecision(15);
}

signed main(){
  string s; cin>>s;
  int n = s.size();
  cout << 2018;
  rep(i,n){
    if(i<4) continue;
    cout << s[i];
  }
  cout << endl;
  return 0;
}


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

using namespace std;

int main() {
    int a, b, d, r;
    double f;
    cin >> a >> b;
    d = a / b;
    r = a % b;
    f = (double)a / (double)b;
    printf("%d %d %.5f\n", d, r, f);
    return 0;
}



Measured memory_bytes: 3232
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0) 
#define setbit0(n, i) ((n) & (~(1LL << (i)))) 
#define setbit1(n, i) ((n) | (1LL << (i))) 
#define togglebit(n, i) ((n) ^ (1LL << (i))) 
#define lastone(n) ((n) & (-(n))) 
char gap = 32;
template<typename T> 
ostream& operator<<(ostream &os, const vector<T> &v) { 
    os << '{'; 
    for (const auto &x : v) os << gap << x;
        return os << '}'; 
}
template<typename A, typename B> 
ostream& operator<<(ostream &os, const pair<A, B> &p) {
        return os << '(' << p.first << gap << p.second << ')';
}

template <class T>
void read(T &x) {
    char c;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        ;
    for (x = 0; c <= '9' && c >= '0'; c = getchar()) x = x * 10 + (c & 15);
}
#define ll long long 
#define lll __int128_t
#define pb push_back
#define mp make_pair
typedef pair < int,int> ii;
typedef vector<ii> vii;
typedef vector<ll>vl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll hashPrime = 1610612741;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll n,i,j,k,l,m,d;
    cin >> n >> d;
    ll ans = 0;
    for (i = 1;i<=n;i++){
        ll x, y;
        cin >> x >> y;
        ll dis = x * x + y * y;
        if(dis<=d*d)
            ans++;
    }
    cout << ans << "\n";
    return 0;
}

Measured memory_bytes: 3652
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main(void){
	int n;
	while(cin >> n && n){
		vector<pair<int,int> >v(n);
		bool possible=true;
		int tmp=0;
		for(int i=0;i<n;i++){
			cin >> v[i].second >> v[i].first;
		}
		sort(v.begin(), v.end());
		for(int i=0;i<n;i++){
			tmp += v[i].second;
			if(tmp > v[i].first) possible = false;
		}
		possible?cout<<"Yes"<<endl:cout<<"No"<<endl;
	}
}

Measured memory_bytes: 0
import java.util.*; 
import java.io.*;

public class Main {
	
	public static void solve(InputReader in) {
		int n = in.readInt(); 
		long a[] = new long[n+1]; 
		for(int i = 1; i<=n; i++ ) 
			for(int j = i; j<=n; j+=i) 
				 a[j]++; 
			
		long ans = 0; 
		for(int i = 1; i<=n; i++) {
			ans += i * a[i]; 
		}
		System.out.println(ans); 
	}
	public static void main(String[] args) {
		InputReader in = new InputReader(System.in); 
		int t = 1; 
		while(t-- > 0) {
			solve(in); 
		}
	}
}

class InputReader{
	private InputStream stream;
	private byte[] buf = new byte[1024];
	private int curChar;
	private int numChars;
	private SpaceCharFilter filter;

	 public InputReader(InputStream stream) {
		this.stream = stream;
	}

     public int read() {
		if (numChars == -1)
			throw new InputMismatchException();
		if (curChar >= numChars) {
			curChar = 0;
			try {
				numChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (numChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

     public int readInt() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			if (c < '0' || c > '9')
				throw new InputMismatchException();
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

     public String readString() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isSpaceChar(c));
		return res.toString();
	}

     public long readLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			if (c < '0' || c > '9') {
				throw new InputMismatchException();
			}
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}
	
     public boolean isSpaceChar(int c) {
		if (filter != null)
			return filter.isSpaceChar(c);
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}
     
     public String next() {
		return readString();
	}
    
     public interface SpaceCharFilter {
		public boolean isSpaceChar(int ch);
	}
}


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

int main(){
  ll n,k;
  cin >> n >> k;
  if( n < k ){
    cout << 1 << endl;
    return 0;
  }
  ll ans = 0;
  for(ll i=k;i<=n;i++){
    ll fr = (i-1)*i/2;
    ll bc = n*(n+1)/2 - (n-i)*(n-i+1)/2;
    ans += bc-fr+1;
    if( i == n ) ans++;
    ans %= 1000000007;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 3632
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define pb push_back
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
using ll=long long;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
#define MOD  1000000007
const ll INF=1e18;
template<class T>void show(vector<T>v){for (int i = 0; i < v.size(); i++){cerr<<v[i]<<" ";}cerr<<endl;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b){ a = b; return 1; } return 0; }



int main(int argc, char const *argv[]) {
  ll x, k, d;
  cin >> x >> k >> d;
  x = abs(x);
  if (x / d >= k) {
    cout << abs(x - k * d) << endl;
    return 0;
  }
  k -= x / d;
  if (k % 2 == 0) {
    cout << abs(x-x/d*d) << endl;
  }
  else cout << abs(x-x/d*d-d) << endl;

  return 0;
}

Measured memory_bytes: 3604
#include<bits/stdc++.h>
using namespace std;
 
typedef long long   ll;
typedef long double ld;
const int N  = 5e5+10;
const ll mod = 1e9+7;
const ll inf = 1e18;
 
#define fast            ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define pb              push_back
#define mp              make_pair
#define ub              upper_bound                 // first element >  val(itr)
#define lb              lower_bound                 // first element >= val(itr) 
#define all(x)          (x).begin(),(x).end()
#define reset(a,x)      memset(a,x,sizeof(a))       // x can be 0 or -1
#define leadzero(a)     __builtin_clz(a)            // count leading zeroes
#define trailzero(a)    __builtin_ctz(a)            // count trailing zeroes
#define bitcount(a)     __builtin_popcount(a)       // count set bits
#define iceil(n,x)      ((n)+(x)-1)/(x)
#define test            int t; cin>>t; while(t--)
#define forn(i,a,e)     for(ll i = a; i < e; i++)
#define pii             pair<int,int>
#define pll             pair<ll,ll>
#define sp              <<" "<<
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }

void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a;    }
ll powm(ll base,ll exp){ll ans=1;while(exp){if(exp&1) ans=(ans*base)%mod;exp>>=1,base=(base*base)%mod;}return ans;}
ll add(ll a,ll b)  { return (a%mod+b%mod)%mod;        }
ll sub(ll a, ll b) { return (a%mod+mod-b%mod)%mod;    }
ll mult(ll a,ll b) { return ((a%mod)*(b%mod))%mod;    }
ll choose2(ll n)   { ll ans=(n*(n-1))/2; return ans;  }
ll dist2(ll x1,ll y1,ll x2,ll y2){ ll ans=(x2-x1)*(x2-x1)+(y2-y1)*(y2-y1); return ans;}

int main()
{
    fast
    double w,h,x,y;
    cin>>w>>h>>x>>y;

    if(w==h)
    {
       if(x==y && x==(w/2))
        cout<<fixed<<setprecision(9)<<(w*h)/(2.0)<<" "<<1;
       else
        cout<<fixed<<setprecision(9)<<(w*h)/(2.0)<<" "<<0;
    }
    else
    {
       if(x==(w/2) && y==(h/2))
        cout<<fixed<<setprecision(9)<<(w*h)/(2.0)<<" "<<1;
       else
        cout<<fixed<<setprecision(9)<<(w*h)/(2.0)<<" "<<0;
    }

    return 0;
}

Measured memory_bytes: 384
/**
 *    author:  [itmo] enot.1.10
 *    created: 24.09.2016 15:01:41       
**/
#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back
#define mp make_pair
#define forn(i, n) for(int i = 0 ; (i) < (n) ; ++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__),fflush(stderr)
#define sz(a) ((int)(a).size())
#define all(a) (a).begin(),a.end()
#define pw(x) (1LL<<(x))

using namespace std;

typedef long long ll;
typedef double dbl;
typedef vector<int> vi;
typedef pair<int, int> pi;

const int inf = 1.01e9;
const dbl eps = 1e-9;

/* --- main part --- */

const int N = 1e5 + 10;
int a[N];

int main()
{
    #ifdef home
        assert(freopen("1.in", "r", stdin));
        assert(freopen("1.out", "w", stdout));
    #endif
    int n;
    scanf("%d", &n);
    forn(i, n) scanf("%d", a + i);
    int res = 0;
    forn(i, n) a[i]--;
    forn(i, n) if (i < a[i] && a[a[i]] == i) res++;
    printf("%d\n", res);
    #ifdef home
        eprintf("time = %d ms\n", (int)(clock() * 1000. / CLOCKS_PER_SEC));
    #endif
    return 0;
}


Measured memory_bytes: 640
#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 main(){
    int k;
    cin >> k;
    int ans = 0;
    for(int i = 1;i <= k;i++){
        for(int j = 1;j <= k;j++){
            for(int l = 1;l <= k;l++){
                ans += gcd(gcd(i,j),l);
            }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3632
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <utility>
using namespace std;

int main(){
    int N, M;
    cin >> N >> M;
    vector<pair<int, int> > cities[100001];
    for(int i=0; i<M; i++){
        int p, y;
        cin >> p >> y;
        cities[p].push_back(make_pair(y, i));
    }

    pair<int, int> ans[100001];
    for(int i=1; i<=N; i++){
        sort(cities[i].begin(), cities[i].end());
        for(int j=0; j<cities[i].size(); j++){
            ans[cities[i][j].second] = make_pair(i, j+1);
        }
    }

    for(int i=0; i<M; i++){
        printf("%06d%06d\n", ans[i].first, ans[i].second);
    }
    return 0;
}


Measured memory_bytes: 7808
function Main(input){
    input = input.split("\n");
    for(var i = 0;1;i++){
        input[i] = input[i].split(" ");
        var r0 = parseInt(input[i][0],10);
        var w0 = parseInt(input[i][1],10);
        var c = parseInt(input[i][2],10);
        var r = parseInt(input[i][3],10);
        if(r0 == 0 && w0 == 0 && c == 0 && r == 0)
            break;

        var a = c * w0;
        if(a <= r0)
            console.log("0");
        else{
            console.log(Math.floor((a - r0 + r - 1) / r));
        }
    }
}
Main(require("fs").readFileSync("/dev/stdin","utf8"));

Measured memory_bytes: 15704
#include<bits/stdc++.h>
using namespace std;
    
#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#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 int long long
#define double long double
    
#define all(a)      (a).begin(),(a).end()
#define sz(x)       (int)x.size()
    
#define ff first
#define ss second
#define mp make_pair
         
#define pb push_back
#define endl "\n" 
         
#define f(i,l,r) for(int i=l;i<=r;i++)
#define rf(i,r,l) for(int i=r;i>=l;i--)    

#define bp __builtin_popcountll
#define inf 1e18
    
const int N=1e6+5;
const int M=1e9+7;
void solve()
{
	int h1,m1,h2,m2,k;
	cin>>h1>>m1>>h2>>m2>>k;
	int tot=((h2-h1+24)%24)*60;
	tot+=(m2-m1);
	tot-=k;
	tot=max(0LL,tot);
	cout<<tot<<endl;
}
signed main()  
{
	FAST
    int t=1;
    // cin>>t;
    for(int tc=1;tc<=t;tc++)
    {
    	// cout<<"Case #"<<tc<<": ";
    	solve();
    }
}


Measured memory_bytes: 3616
import java.util.*;

public class Main{
	public static void main(String args[]) {
		Scanner input  = new Scanner(System.in);
		int n = input.nextInt();
		int W = input.nextInt();
		long arr[][] = new long[n][3];
		for(int i=0;i<n;i++) {
			long w = input.nextInt();
			long v = input.nextInt();
			arr[i] = new long[]{w,v};
		}
		System.out.println(dp(arr,n,W));
	}
	
	static long knap_sack(long arr[][],int n,long W) {
		if(n==-1 || W==0) {
			return 0;
		}
		
		long w = arr[n][0];
		long v = arr[n][1];
		if(W<w) {
			return knap_sack(arr,n-1,W);
		}
		long r_max = v+knap_sack(arr,n-1,W-w);
		long l_max = knap_sack(arr,n-1,W);
		return Math.max(r_max, l_max);
	}
	static long dp(long arr[][],int n,int W) {
		long dp[][] = new long[n+1][W+1];
		for(int i=0;i<=n;i++) {
			for(int j=0;j<=W;j++) {
				if(i==0 || j==0) {
					dp[i][j] = 0;
				}
				else if(j>=arr[i-1][0]) {
					dp[i][j] = Math.max(dp[i-1][(int)(j-arr[i-1][0])]+arr[i-1][1], dp[i-1][j]);
				}
				else {
					dp[i][j] = dp[i-1][j];
				}
			}
		}
		return dp[n][W];
	}
}


Measured memory_bytes: 127572
#!/usr/bin/python3
# -*- coding:utf-8 -*-

class UnionFindTree:
  def __init__(self, n):
    self.nodes = [-1]*n
    
  def find(self, x):
      parent = self.nodes[x]
      if parent < 0:
        return x
      else:
        self.nodes[x] = self.find(self.nodes[x])
        return self.nodes[x]
  
  def unite(self, x, y):
    xp = self.find(x)
    yp = self.find(y)
    if self.nodes[xp] > self.nodes[yp]:
      xp, yp = yp, xp
    self.nodes[xp] += self.nodes[yp]
    self.nodes[yp] = xp  
  
  def size(self, x):
    return -self.nodes[self.find(x)]


def main():
  n, m = map(int, input().split())
  edges = []
  
  for _ in range(m):
    a, b = map(lambda x:int(x)-1, input().split())
    edges.append((a, b))
  uftree = UnionFindTree(n)
  scores = []
  
  for a, b in edges[::-1]:
    ap = uftree.find(a)
    bp = uftree.find(b)
    if  ap != bp:
      scores.append(uftree.size(ap) * uftree.size(bp))
      uftree.unite(a, b)    
    else:
      scores.append(0)
      
  sum = 0
  for score in scores[::-1]:
    sum += score
    print(sum)

if __name__=='__main__':
  main()



Measured memory_bytes: 23200