akhauriyash/Code-Regression huggingface dataset:
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class Main {
	
	String INPUT = "";
	
	public static void main(String[] args) throws Exception {
		new Main().run();
	}
	
	void run() {
		Scanner sc = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(new ByteArrayInputStream(INPUT.getBytes()));
		PrintWriter out = new PrintWriter(System.out);
		
		solve(sc, out);
		out.flush();
	}
	
	void solve(Scanner sc, PrintWriter out) {
		int x,y;
		
		x = sc.nextInt();
		y = sc.nextInt();
		
		while (x != 0 || y != 0) {
			if (x > y) {
				System.out.println(y + " " + x);
			} else {
				System.out.println(x + " " + y);
			}
			x = sc.nextInt();
			y = sc.nextInt();
		}
	}
}



Measured memory_bytes: 35672
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <iomanip>

using namespace std;

typedef long long ll;

int main(){
    int N;
    cin >> N;
    vector<int> A(N), B(N);
    vector<pair<int, int>> BA(N);
    for(int i = 0; i < N; i++){
        cin >> A[i] >> B[i];
        BA[i] = make_pair(B[i], A[i]);
    }
    sort(BA.begin(), BA.end());
    
    int temp = 0;
    for(int i = 0; i < BA.size(); i++){
        temp += BA[i].second;
        if(temp > BA[i]
        .first){
            cout << "No\n";
            return 0;
        }
    }
    
    cout << "Yes\n";
}

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

using namespace std;

int main()
{
	string N;
	cin >> N;

	int sum = 0;
	for (size_t i = 0; i < N.size(); i++) {
		sum += N[i] - '0';
		sum %= 9;
	}

	if (sum == 0) {
		cout << "Yes" << endl;
	} else {
		cout << "No" << endl;
	}

	return 0;
}


Measured memory_bytes: 3772
#include <bits/stdc++.h>
using namespace std;
using namespace std;
 
#define ll long long
#define f(i,a,b) for(int i=a;i<b;i++)
#define endl '\n'
#define spc " "
#define pb push_back
#define mp make_pair

int main() 

{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen("in.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll n;
    cin>>n;
    set<string>s;
    while(n--){
        string str;
        cin>>str;
        s.insert(str);
    }
    cout<<s.size()<<endl;
}

Measured memory_bytes: 19144
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<cassert>
#include<cassert>
#include<unordered_map>
#include<unordered_set>
#include<functional>
#include<queue>
#include<stack>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<sstream>
#include<iomanip>
#include<cstdio>
#include<cstdlib>
#include<numeric>
#include<random>
#include<chrono>
#include<bitset>
using namespace std;

#define all(x) (x).begin(), (x).end()
#define pb push_back
#define xx first
#define yy second
#define sz(x) (int)(x).size()
#define gc getchar
#define IO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define mp make_pair

#ifndef ONLINE_JUDGE
#  define LOG(x) (cerr << #x << " = " << (x) << endl)
#else
#  define LOG(x) ((void)0)
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const double PI=acos(-1);
const ll INF = 1LL<<62;
const ll MINF = -(1LL<<62);

template<typename T> T getint() {
	T val=0;
	char c;
	
	bool neg=false;
	while((c=gc()) && !(c>='0' && c<='9')) {
		neg|=c=='-';
	}

	do {
		val=(val*10)+c-'0';
	} while((c=gc()) && (c>='0' && c<='9'));

	return val*(neg?-1:1);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); uniform_int_distribution<int>(0, n-1)(rng)


int main() {
	IO;
	int n;
	cin>>n;
	vector<ll> t(n+1);
	for(int i=0;i<=n;++i) cin>>t[i];
	vector<ll> suf(n+1);
	suf[n]=t[n];
	for(int i=n-1;i>=0;i--) suf[i]+=t[i]+suf[i+1];
	ll curr=1;
	bool ok=true;
	ll ans=0;
	for(int i=0;i<=n;++i) {
		ans+=curr;
		curr-=t[i];	
		ok&=curr>=0;
		curr=min(2*curr, (i==n?0:suf[i+1]));
	}
	
	cout<<(ok?ans:-1)<<"\n";
	return 0;
}


Measured memory_bytes: 4780
/**
 *  author: mahfuzz
 *  created: 08.12.2019
**/
 
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
#define debug(x) cout << #x << ':' << x << "\n";
//~ const double pi = 2*acos(0.0);
//~ const long long mod = 1e9+7;
//~ const int max_n = 1e6+5;
typedef long long ll;


int main(int argc, char* argv[]){
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    
    int k, s; cin >> k >> s;
    int cnt = 0;
    for(int i = 0; i <= k; i++){
        for(int j = 0; j <= k; j++){
            int z = s - i - j;
            if(z >= 0 && z <= k)
                cnt++;
        }
    }
        
    cout << cnt << "\n";
    
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define ok() puts(ok?"Yes":"No");
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef set<int> si;
typedef map<string, int> msi;
typedef greater<int> gt;
typedef priority_queue<int, vector<int>, gt> minq;
typedef long long ll;
typedef pair<ll,ll> pll;
const ll LINF = 1e18L + 1;
const int INF = 1e9 + 1;
//clang++ -std=c++11 -stdlib=libc++ 

int main() {
  int n,m; cin >> n >> m;
  // vb chosen(m+1, false);
  vi chosen(n+1, 1);
  bitset<100010> dp;
  // chosen[1] = 1;
  dp[1] = true;
  rep(i,m) {
    int x,y; cin >> x >> y;
    if (dp[x]) {
      dp[y] = true;
      if (chosen[x]==1) dp[x] = false;
    }
    chosen[y]++;
    chosen[x]--;
  }
  int ans =0;
  rep(i, n+1) {
    if (chosen[i]>0 && dp[i]) ans++;
  }
  cout << ans << endl;
  return 0;
} 

Measured memory_bytes: 640
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define MOD 1000000007
#define rep(i,n) for(i=0;i<n;i++)
#define loop(i,a,n) for(i=a;i<n;i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

typedef vector<int> vi;
typedef pair<int,int> pii;

int main(void) {
  int n,m;
  cin>>n>>m;
  int i;
  int path[10][10]={};
  rep(i,m){
    int a,b;
    cin>>a>>b;
    a--;b--;
    path[a][b]++;
    path[b][a]++;
  }

  vi v;
  rep(i,n)v.push_back(i);

  int ans=0;
  do{
    if(v[0]!=0)break;
    rep(i,n-1)
      if(path[v[i]][v[i+1]]==0)break;
    if(i==n-1)ans++;
  }while(next_permutation(all(v)));

  cout<<ans<<endl;
}


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

vector<string> v;

void solve(string s, int a, int b) {
  if ((a == 5 && b <= 3) ||
      (a <= 3 && b == 5) ||
      (a == 5 && b == 5) ||
      (a == 6 || b == 6)) {
    v.push_back(s);
    return;
  }
  solve(s + "A", a + 1, b);
  solve(s + "B", a, b + 1);
}

int main() {
  solve("",0,0);
  int a, b;
  while(cin >> a >> b) {
    set<string> ans;
    for (string s : v) {
      int k = a+b;
      if (k <= (int)s.size()) {
        string t = s.substr(0, k);
        if (count(t.begin(), t.end(), 'A') == a &&
            count(t.begin(), t.end(), 'B') == b) {
          ans.insert(t);
        }
      }
    }
    for (string s : ans) {
      cout << s << endl;
    }
  }
}

Measured memory_bytes: 1244
a,b,c = map(int,input().split(" "))
arr1 =[]
arr2 = []

for i in range(1,a+1):
	if(a%i==0):
		arr1.append(i)

for i in range(1,b+1):
	if(b%i==0):
		arr2.append(i)

set_int = set(arr1) & set(arr2)

set_int = list(set_int)
set_int.sort()
print(set_int[-c])


Measured memory_bytes: 3060
#pragma GCC optimize ("O3")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please



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


	int N;
	scanf("%d ", &N);
	int sL[1001] = {}, sR[1001] = {};
	pair<int, int> RR[500], LL[500];
	int rr = 0, ll = 0;
	int tot = 0;
	rep(i, N) {
		char c;
		int l = 0, r = 0;
		while((c = getchar_unlocked()) >= '(') {
			if (c == '(') r++;
			else {
				if (r > 0) r--;
				else l++;
			}
		}
		tot += r - l;
		if (l < r) {
			if (l <= 1000) sR[l] += r;
			else RR[rr++] = { l,r };
		}
		else {
			if (r <= 1000) sL[r] += l;
			else LL[ll++] = { r,l };
		}
	}

	if (tot != 0) {
		printf("No");
		return 0;
	}

	sort(RR, RR + rr);
	sort(LL, LL + ll);

	int OK = 1;
	int k = 0;
	rep(i, 1001) {
		if (sR[i]) {
			if (k < i) OK = 0;
			k += sR[i];
		}
	}
	rep(i, rr) {
		if (k < RR[i].first) OK = 0;
		k += RR[i].second - RR[i].first;
	}
	k = 0;
	rep(i, 1001) {
		if (sL[i]) {
			if (k < i) OK = 0;
			k += sL[i];
		}
	}
	rep(i, ll) {
		if (k < LL[i].first) {
			OK = 0;
		}
		k += LL[i].second - LL[i].first;
	}

	if (OK) printf("Yes");
	else printf("No");

	Would you please return 0;
}

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

#define MAX 500000
#define SENTINEL 2000000000

int L[MAX/2+2],R[MAX/2+2];
int cnt;

void merge(int A[],int n, int left,int mid, int right){
	int n1 = mid-left;
	int n2 = right-mid;
	
	for(int i=0;i<n1;i++) L[i]=A[left+i];
	for(int i=0;i<n2;i++) R[i]=A[mid+i];
	
	L[n1] = R[n2] = SENTINEL;
	int i=0,j=0;
	for(int k=left;k<right;k++){
		cnt++;
		if(L[i]<=R[j]){
			A[k]=L[i++];
		}else{
			A[k]=R[j++];
		}
	}
}

void mergeSort(int A[],int n,int left,int right){
	if(left+1<right){
		int mid=(left+right)/2;
		mergeSort(A,n,left,mid);
		mergeSort(A,n,mid,right);
		merge(A,n,left,mid,right);
	}
}

int main() {
	
	int A[MAX],n;
	cnt=0;
	
	cin>>n;
	for(int i=0;i<n;i++) cin>>A[i];
	
	mergeSort(A,n,0,n);
	
	for(int i=0;i<n;i++) {
		if(i) cout<<" ";
		cout<<A[i];
	}
	
	cout<<endl;
	
	cout<<cnt<<endl;
	
	return 0;
}

Measured memory_bytes: 6948
#include<stdio.h>

int main(void)
{
  int a,b,k;
  scanf("%d%d%d",&a,&b,&k);

  for(int i=a; i<=b; i++){
    if((i<a+k)||(i>b-k)){
      printf("%d\n",i);
    }
  }

    return 0;

}



Measured memory_bytes: 128
#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;

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

long long modinv(long long a, long long m=MOD) {
    long long b = m, u = 1, v = 0;
    while (b) {
        long long t = a / b;
        a -= t * b; swap(a, b);
        u -= t * v; swap(u, v);
    }
    u %= m; 
    if (u < 0) u += m;
    return u;
    /*MODの割り算の際に
    a/＝b;
    を
    a *= modinv(b,MOD);
    とする。
        */
}

#define MAXN 200000
#define MAXAB 1000000001

int N;

int A[MAXN],B[MAXN];

int solve_even() {
    sort(A,A+N);
    sort(B,B+N);

    return (B[N/2] + B[(N/2)-1])  - (A[N/2] + A[(N/2)-1]) + 1;

}

int solve_odd() {
    sort(A,A+N);
    sort(B,B+N);
    return B[N/2] - A[N/2] +1;
}

int main(){
    cin >> N;
    rep(i,N) cin >> A[i] >> B[i];
    int ans = N%2==1 ? solve_odd() : solve_even();
    cout << ans << endl;
    
    return 0;
}

Measured memory_bytes: 5192
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
ll a[200005],b[200005];
int cnt=0;
int vis[2000005];
vector<int>adj[2000005];
void dfs(int v)
{
    vis[v]=1;
    cnt++;
    for(auto u:adj[v])
    {
        if(!vis[u])
            dfs(u);
    }
}
int main()
{
    /*int  d,t,s;
    cin >> d >> t>> s;
    int dis = s*t;
    if(dis>=d)
        cout << "Yes\n";
    else
        cout << "No\n";*/
    /*string s,t;
    int mn = 1e9;
    cin >> s>> t;
    int n = (int)s.size();
    int m = (int)t.size();
    for(int i=0;i<n-m+1;i++)
    {
        int cur=0,lol=0;
        for(int j=i;j<i+m;j++)
        {
            if(s[j]!=t[cur])
                lol++;
            cur++;
        }
        mn = min(lol,mn);
    }
    cout << mn << endl;*/
    /*ll n;
    cin >> n;
    for(ll i=1;i<=n;i++)
        {
            cin >> a[i];
            b[i] = a[i];
            b[i]+=b[i-1];
        }
    ll sum=0;
    for(ll i=1;i<n;i++)
    {
        ll num = b[n]-b[i];
        //cout << num << endl;
        num = num%mod;
        sum = (sum+ num*a[i])%mod;
    }
    cout << sum << endl;*/
    int n,m;
    cin >> n >> m;
    for(int i=1;i<=m;i++)
    {
        int x,y;
        cin >> x>> y;
        adj[x].push_back(y);
        adj[y].push_back(x);
    }
    int mx=0;
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0)
        {
            dfs(i);
        }
        mx = max(mx,cnt);
        cnt=0;
    }
    cout << mx << endl;

}


Measured memory_bytes: 63724
#include <bits/stdc++.h>
#include <typeinfo>
#include <cxxabi.h>
#ifdef LOCAL
#include "dbgtoki.hpp"
#define DUMP(i) dump((string)TOSTRING(i), demangle(typeid(i).name()), __LINE__ , i) 
#else
#define DUMP(i)
#endif

using namespace std;
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(s) (s).begin(), (s).end()
#define dis distance
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
#define uni(v) v.erase( unique(ALL(v)) , v.end());
#define pub push_back
#define sec second
#define fir first

typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vector <int> > vvi;
const int EPS = 1e-9;
const int MOD = 1e9+7;
const int INF = (1 << 30);
const double PI = acos(-1);

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	vi A(3);
	REP(i,3)cin >> A[i];
	so(A);
	int ans = min(A[1]-A[0]+A[2]-A[1],A[2]-A[1]+A[1]-A[0]);
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <limits>
#include <cmath>
#include <iomanip>
#include <functional>
#include <random>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using ll = long long;

int main()
{
    // 整数の入力
    vector<ll> X1(5);
    for (size_t i = 0; i < 5; i++)
    {
        cin >> X1[i];
    }

    ll ans = -1;
    for (size_t i = 0; i < 5; i++)
    {
        if(X1[i] == 0){
            ans = i;
            break;
        }
    }
    cout << ans+1 << endl;
   return 0;
}

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

int main()
{
    long long n, k;
    cin >> n >> k;
    vector<long long> a(n), b(n);
    rep(i, n) cin >> a.at(i) >> b.at(i);

    map<long long, long long> mp;
    rep(i, n)
    {
        mp[a.at(i)] += b.at(i);
    }

    long long cnt = k;
    for (auto i = mp.begin(); i != mp.end(); i++)
    {
        long long key = i->first, value = i->second;
        // cout << key << "->" << value << " " << cnt << endl;
        cnt -= value;
        if (cnt <= 0)
        {
            cout << key << endl;
            break;
        }
    }
}

Measured memory_bytes: 5760
#include <bits/stdc++.h>
#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 INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;

void Main()
{
    int k;
    cin >> k;

    vector<int> p;
    for (int i = 1; i <= k; i++)
    {
        for (int q = 1; q <= k; q++)
        {
            for (int r = 1; r <= k; r++)
            {
                int minmin = min({i, q, r});
                int mi = 1;
                for (int s = 1; s <= minmin; s++)
                {

                    if (i % s == 0 && q % s == 0 && r % s == 0)
                    {
                        mi = s;
                    }
                }
                p.push_back(mi);
            }
        }
    }
    ll total = accumulate(ALL(p), 0.0);
    cout << total << endl;
}
int main()
{
    std::cout << std::fixed << std::setprecision(15);
    Main();
}

Measured memory_bytes: 36180
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <queue>
#include <sstream>
#include <utility>
#include <vector>

#define REP(i, n) for(int i = 0; i < (n); i++)
#define ALL(a) (a).begin(), (a).end()
#define AALL(a, n) (a), a + n
#define FILL(a, n) memset((a), n, sizeof(a))
#define FILLZ(a) FILL(a, 0)

using namespace std;

typedef long long ll;

const double EPS = 1e-10;
const double PI = acos(-1.0);

inline int toInt(string s) {
	int v;
	istringstream sin(s);
	sin >> v;
	return v;
}

int main() {
	int n;
	scanf("%d", &n);
	vector<double> v(n);
	for(auto&& e : v) {
		scanf("%lf", &e);
	}

	double result = 0.0;
	for(auto&& e : v) {
		result += 1 / e;
	}

	printf("%lf", 1 / result);
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
struct Node
{
    double x, y;
    Node(double xx = 0, double yy = 0){
        x = xx; y = yy;
    }

    Node operator + (Node b){
        return Node(x+b.x, y+b.y);
    }
    Node operator - (Node b){
        return Node(x-b.x, y-b.y);
    }
    Node operator * (double a){
        return Node(a*x, a*y);
    }
    Node operator / (double a){
        return Node(x/a, y/a);
    }

    double norm(){return x*x+y*y;}
    double len(){
        return sqrt(norm());
    }

    bool operator < (const Node &p) const{
        return x!=p.x ? x<p.x : y<p.y;
    }

    bool operator == (const Node &p) const {
        return fabs(x-p.x)<eps && fabs(y-p.y)<eps;
    }
};

bool equals(double a, double b) {
    return fabs(a)-fabs(b) < eps;
}

typedef Node vtr;

struct line{
    Node a, b;
    line(Node x, Node y){
        a = x, b = y;
    }
};

//向量点积
double dot(vtr a, vtr b){
    return a.x*b.x+a.y*b.y;
}

//向量叉积
double cross(vtr a, vtr b){
    return a.x*b.y-a.y*b.x;
}

//判断向量a,b是否正交(垂直)
bool isOrthogonal(vtr a, vtr b){
    return equals(dot(a, b), 0.0);
}

bool isOrthogonal(Node a1, Node a2, Node b1, Node b2){
    return isOrthogonal(a1-a2, b1-b2);
}

bool isOrthogonal(line s1, line s2){
    return equals(dot(s1.b-s1.a, s2.b-s2.a), 0.0);
}

//判断向量a,b是否平行
bool isParallel(vtr a, vtr b){
    return equals(cross(a, b), 0.0);
}

bool isParallel(Node a1, Node a2, Node b1, Node b2){
    return isParallel(a1-a2, b1-b2);
}

bool isParallel(line s1, line s2){
    return equals(cross(s1.b-s1.a, s2.b-s2.a), 0.0);
}

//投影 求垂足坐标
//点p在直线p1p2上的投影
Node project(line s, Node p)
{
    vtr base = s.b-s.a;
    //算出比例
    double r = dot(p-s.a, base)/base.norm();
    return s.a+base*r;
}

//映象 求p以s为对称轴的对称点
Node reflect(line s, Node p){
    return p+(project(s, p)-p)*2.0;
}

int ccw(Node p0, Node p1, Node p2)
{
    vtr a = p1-p0, b = p2-p0;
    //b在a的逆时针方向
    if(cross(a, b) > eps) return 1;
    //顺时针方向
    if(cross(a, b) < -eps) return -1;
    //共线但反向
    if(dot(a, b) < -eps) return 2;
    //共线同向
    //b更长一点
    if(a.norm() < b.norm()) return -2;
    //a更长一点
    return 0;
}

//判断两直线是否相交
//bool intersect(Node p1, Node p2, Node p3, Node p4)
//bool intersect


int main()
{
    Node p0, p1, p2, p3;
    int q; cin >> q;
    while(q--)
    {
        cin >> p0.x >> p0.y >> p1.x >> p1.y >> p2.x >> p2.y >> p3.x >> p3.y;
        if(isOrthogonal(p0, p1, p2, p3)) puts("1");
        else if(isParallel(p0, p1, p2, p3)) puts("2");
        else puts("0");
    }
    return 0;
}



Measured memory_bytes: 3220
#if defined(LOCAL)
// #define RANDOM_TEST
#define PROBLEM_NAME "c"
const double _max_double_error = 1e-9;
#include "testutils.h"
#endif
#include <bits/stdc++.h>

using namespace std;

using vi = vector<int>; using vvi = vector<vi>;
using ii = pair<int,int>; using vii = vector<ii>;
using l = long long; using vl = vector<l>; using vvl = vector<vl>;
using ll = pair<l,l>; using vll = vector<ll>; using vvll = vector<vll>;
using lu = unsigned long long;
using vb = vector<bool>; using vvb = vector<vb>;
using vd = vector<double>; using vvd = vector<vd>;
using mll = unordered_map<l, l>;
const l INF = numeric_limits<int>::max();
const double EPS = 1e-10; static constexpr auto PI = acos(-1);
const l e0=1, e3=1000, e5=100000, e6=10*e5, e7=10*e6, e8=10*e7, e9=10*e8;
const char lf = '\n';
#define all(x) begin(x), end(x)
#define F(a,b,c) for (l a = l(b); a < l(c); a++)
#define B(a,b,c) for (l a = l(c) - 1; a >= l(b); a--)

#if defined(LOCAL)
bool local = true;
#define L(x...) debug(x)
#else
bool local = false;
#define L(x, ...) (x)
#endif
struct VoidStream { void operator&(std::ostream&) { } };
#define LOG !(local) ? (void) 0 : VoidStream() & cerr

void solve(istream& cin, ostream& cout) {
  l n, q;
  cin >> n >> q;
  vl v(n);
  vl counts(n + 1);
  vl frq(n + 1);
  F(i, 0, n) cin >> v[i];
  l answer = 0;
  F(i, 0, n) {
    l t = max(v[i] - counts[v[i]], (l) 1);
    counts[v[i]]++;
    frq[t]++;
    if (frq[t] > 1) answer++;
  }
  F(i, 0, q) {
    // L(counts);
    // L(frq);
    l p, x; cin >> p >> x; p--;
    // remove current
    counts[v[p]]--;
    l t = max(v[p] - counts[v[p]], (l)1);
    frq[t]--;
    if (frq[t]) answer--;
    v[p] = x;
    t = max(l(1), x - counts[x]);
    counts[x]++;
    frq[t]++;
    if (frq[t] > 1) answer++;
    cout << answer << lf;
  }
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cout << fixed << setprecision(15);
#if defined(LOCAL)
  maybe_run_tests(cin, cout);
#else
  solve(cin, cout);
#endif
}


Measured memory_bytes: 6272
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;

const lld N = 200043;
const lld MOD = 1000000007;

lld add(lld x, lld y)
{
    x =((x%MOD)+(y%MOD))%MOD;
    while(x >= MOD) x -= MOD;
    while(x < 0) x += MOD;
    return x;
}

lld mul(lld x, lld y)
{
    return ((x%MOD)*(y%MOD))% MOD;
}

lld binpow(lld x, lld y)
{
    lld z = 1;
    while(y)
    {
        if(y & 1) z = mul(z, x);
        x = mul(x, x);
        y >>= 1;
    }
    return z;
}

lld inv(lld x)
{
    return binpow(x, MOD - 2);
}

lld divide(lld x, lld y)
{
    return mul(x, inv(y));
}

// Combinations

/*
lld fact[N];

void precalc()
{
    fact[0] = 1;
    for(lld i = 1; i < N; i++)
        fact[i] = mul(fact[i - 1], i);
}

lld C(lld n, lld k)
{
    return divide(fact[n], mul(fact[k], fact[n - k]));
}
*/
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    lld t,i,j;
    //cin>>t;
    t=1;
    while(t--)
    {
        lld a,b,c;
        cin>>a>>b>>c;
        lld x;
        if(a%b==0)
            x=a/b;
        else
            x=a/b+1;
        cout<<x*c<<endl;
    }
}




Measured memory_bytes: 3612
n,m,k = gets.chomp.split(" ").map(&:to_i)
a = gets.chomp.split(" ").map(&:to_i)
b = gets.chomp.split(" ").map(&:to_i)

a_sum = [0]
sum = 0
a.each do |i|
  if sum+i > k
    break
  end
  sum += i
  a_sum << sum
end

b_sum = [0]
sum = 0
b.each do |i|
  if sum+i > k
    break
  end
  sum += i
  b_sum << sum
end

s = a_sum.size-1
t = b_sum.size-1
max = [s,t].max
t = 0

while(true)
  red = k-a_sum[s]
  if red >= b_sum[t]
    if s+t > max
      max = s+t
    end
    t += 1
    if t > b_sum.size-1
      break
    end
  else
    s -= 1
    if s == 0
      break
    end
  end
end

p max

Measured memory_bytes: 46172
#include<bits/stdc++.h>
#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 FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999
using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

int main(void){
    int N;
    bool flg=false;
    char s;
    cin>>N;
    REP(i,N){
      cin>>s;
      if(s=='Y'){
        flg=true;
      }
    }
    if(flg){
      cout<<"Four"<<endl;
    }
    else{
      cout<<"Three"<<endl;
    }
    return 0;
}


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

int main () {
    int n, a, b, count = 0;
    string ans = "No";
    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> a >> b;
        if (a==b) {
            count ++;
        }
        else if (count >= 3) { 
            ans = "Yes";
            break;
        }
        else {
            count = 0;
        }
        if (i == n-1 && count >= 3) {
            ans = "Yes";
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 3624
n= int(input())
al = list(map(int, input().split()))
bl = list(map(int, input().split()))
cl = list(map(int, input().split()))

lal = sorted(al)
lbl = sorted(bl)
lcl = sorted(cl)


from bisect import bisect_left, bisect_right
 

res = 0

for i in range(n):
    bx = lbl[i]
    ax = bisect_left(lal,bx)
    cx = n - bisect_right(lcl,bx)
    
    res += ax*cx

print(res)

Measured memory_bytes: 23328
#include <iostream>
#include<string>
#include<math.h>
#include<vector>
#include<algorithm>
#include<array>
#include<functional>
#include<iomanip>
#include<ios>
using namespace std;
#define FOR(i,n) for(int (i)=0;(i)<(n);(i)++)
const long long int mod=1e9+7;
typedef long long ll;

ll gcd(ll a, ll b) {
  if(a < b) return gcd(b, a);
  ll r;
  while ((r=a%b)) {
    a = b;
    b = r;
  }
  return b;
}

bool issq(int n){
    int i=1;
    while(i*i<=n){
        if(i*i==n){
            return true;
        }
        i++;
    }
    return false;
}
int main(){
    int N,D;
    cin >>N>>D;

    int X[N][D];
    FOR(i,N){
        FOR(j,D){
            cin>>X[i][j];
        }
    }
    int ans=0;
    for(int i=0;i<N-1;i++){
        for(int j=i+1;j<N;j++){
            int s=0;
            FOR(k,D){
                s+=pow((X[i][k]-X[j][k]),2);
            }
            if(issq(s)){
                ans++;
            }
        }
    }

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




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

string S;


signed main()
{
	cin >> S;

	rep(i, S.length() - 1)
	{
		string sub2 = S.substr(i,2);
		if (sub2[0] == sub2[1])
		{
			cout << i + 1 << " " << i + 2 << endl;
			return 0;
		}

		if (i != S.length() - 2)
		{
			string sub = S.substr(i, 3);
			if (sub[0] == sub[1] || sub[0] == sub[2] || sub[1] == sub[2])
			{
				cout << i + 1 << " " << i + 3 << endl;
				return 0;
			}
		}
	}

	cout << "-1 -1" << endl;
}


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

int main() {
  int n;
  string s;
  cin >> n >> s;
  int ans = 0;
  rep(i,n){
    vector<bool>a(26);
    vector<bool>b(26);
    int c = 0;
    rep(j,n){
      if(j <= i) a[s[j]-'a'] = true;
      else b[s[j]-'a'] = true; 
    }
    rep(j,26){
      if(a[j] && b[j]) c++;
    }
    ans = max(ans,c);
  }
  cout << ans << endl;
}

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

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;

const ll LongINF=1e13+7;
const int INF=1e9+7;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template <class T, class Y>T GCD(T a, Y b){if(a<b){int c=a;a=b;b=c;}if (a % b == 0){return b;}return GCD(b, a % b);}
template<class T,class Y>T LCM(T a,Y b){return (a*b)/GCD(a,b);}
void clear(queue<pair<int, int>> &q){queue<pair<int, int>> empty;swap(q, empty);} //queueの中身の型は適時変更を忘れない

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

#define REP(i,a,b) for(ll i=(a);i<(b);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define rv reverse
#define ALL(a) (a).begin(),(a).end()
#define decimal(x) fixed<<setprecision(x)

int main(){
    int x,ans;
    cin>>x;
    if(400<=x&&x<600){
        ans=8;
    }else if(600<=x&&x<800){
        ans=7;
    }else if(800<=x&&x<1000)ans=6;
    else if(1000<=x&&x<1200)ans=5;
    else if(1200<=x&&x<1400)ans=4;
    else if(1400<=x&&x<1600)ans=3;
    else if(1600<=x&&x<1800)ans=2;
    else if(1800<=x&&x<2000)ans=1;
    cout<<ans<<endl;
    return 0;
}

//a=97,z=122

Measured memory_bytes: 3628
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string.h>
#include <queue>
#include <math.h>
#include <string>
#include <iostream>
using namespace std;
typedef long long ll;

int main()
{
	int H, W, N;
	scanf("%d %d %d", &H, &W, &N);

	if (H > W) swap(H, W);
	int i;
	for (i = 0; W * i < N; i++) {}
	printf("%d", i);
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
vector<int> num;
 
//素因数分解は不要で、単にMの約数を求めれば良い。
//Mの約数のうち、
int main()
{
    int N, M;
 
    cin >> N >> M;
 
    int ans = 0;
    for (int i = 1; i * i <= M; i++)
    {
        if (M % i == 0)
        {
            num.push_back(i);
            num.push_back(M/i);
        }
    }
    sort(num.begin(),num.end());
    
    for(int i = 0; i < num.size(); i++)
    {
       // printf("%d",num[i]);
        if (num[i] >= N) {
            ans = M / num[i];
            break;
        }
    }
    
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int inf = 1e9;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};

struct state{
    int cost;
    int y;
    int x;
    state(int c, int y, int x):cost(c),y(y),x(x){}
    state(){}
    bool operator<(const state &a) const{
        return cost > a.cost;
    }
};

int main(){
    while(1){
        int h,w,n;
        cin >> h >> w >> n;
        if(h == 0) break;

        string s;
        cin >> s;
        vector<string> grid(h+2, string(w+2, '#'));
        for(int i=1; i<=h; i++){
            cin >> grid[i];
            grid[i] = "#" + grid[i] + "#";
        }

        vector<int> dir(n+1, 0);
        for(int i=0; i<n; i++){
            dir[i+1] = (s[i]=='L')? (dir[i]-1+4)%4: (dir[i]+1)%4;
        }
        vector<vector<int>> next(n+1, vector<int>(4, inf));
        next[n][dir[n]] = n;
        for(int i=n-1; i>=0; i--){
            for(int j=0; j<4; j++){
                next[i][j] = next[i+1][j];
            }
            next[i][dir[i]] = i;
        }
        int sy=-1,sx=-1;
        int gy=-1,gx=-1;
        for(int i=1; i<=h; i++){
            for(int j=1; j<=w; j++){
                if(grid[i][j] == 'S'){
                    sy = i;
                    sx = j;
                }
                if(grid[i][j] == 'G'){
                    gy = i;
                    gx = j;
                }
            }
        }

        priority_queue<state> wait;
        wait.push(state(0, sy, sx));
        vector<vector<int>> dp(h+2, vector<int>(w+2, inf));
        dp[sy][sx] = 0;
        while(!wait.empty()){
            int c = wait.top().cost;
            int y = wait.top().y;
            int x = wait.top().x;
            wait.pop();
            if(c > dp[y][x]) continue;

            for(int d=0; d<4; d++){
                int ny = y+dy[d];
                int nx = x+dx[d];
                int nc = next[c][d];
                if(grid[ny][nx] == '#') continue;
                if(nc == inf) continue;
                if(nc < dp[ny][nx]){
                    dp[ny][nx] = nc;
                    wait.push(state(nc, ny, nx));
                }
            }
        }
        if(dp[gy][gx] != inf){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }
    return 0;
}


Measured memory_bytes: 9556
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cstring>
#include <math.h>
#include <bitset>
#include <queue>
#include <set>
#include <iomanip>
#include <math.h>
#include <assert.h>
// #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr long long int INFLL = 1001001001001001LL;
constexpr long long int infll = 1001001001001001LL;
constexpr int INF = 1000000007;
constexpr int inf = 1000000007;
const int mod = 1000000007;

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

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

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

inline double nCr(const int n, int r)
{
    if (n == 0)
    {
        return 0;
    }
    if (r == 0)
    {
        return 1;
    }
    if (r == 1)
    {
        return n;
    }
    if (n == r)
    {
        return 1;
    }

    if (r > n / 2)
    {
        r = n / 2;
    }

    double result = 1;
    for (double i = 1; i <= r; i++)
    {
        result *= (n - i + 1) / i;
    }

    return (result);
}

template <typename T>
T seinomi(T a)
{
    if (a > 0)
    {
        return a;
    }
    else
    {
        return 0;
    }
}

template <typename T>
map<T, T> soinsuubunkai(T n) //連想配列[素因数f.first][個数f.second]
{
    map<T, T> ret;
    for (T i = 2; i * i <= n; i++)
    {
        while (n % i == 0)
        {
            ret[i]++;
            n /= i;
        }
    }
    if (n != 1)
        ret[n] = 1;
    return ret;
}

template <typename T>
T ketasuu(T num)
{
    return std::to_string(num).length();
}

int kaizyou(int k)
{
    int sum = 1;
    for (int i = 1; i <= k; ++i)
    {
        sum *= i;
    }
    return sum;
}

template <class ForwardIt, class T>
void iota(ForwardIt first, ForwardIt last, T value)
{
    while (first != last)
    {
        *first++ = value;
        ++value;
    }
}

//nの階乗を計算
int factorial(int n)
{
    int answer = 1;
    while (n > 1)
    {
        answer *= n;
        n--;
    }
    return answer;
}
int comb(int n, int r)
{
    return factorial(n) / (factorial(r) * factorial(n - r));
}

long long modpow(long long a, long long n, long long mod)
{
    long long res = 1;
    while (n > 0)
    {
        if (n & 1)
            res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}


template <typename T>
T amarinasi(T a,T b){
    if(a%b==0)
{
    return a/b;
}
else if(a%b>0)
{
    return a/b+1;
}
else
{
    return a/b-1;
}
}

//小数点以下10桁テンプレート（main関数内の最初に貼付け）
//std::cout << std::fixed << std::setprecision(10);

//----------------------------------------------------------------

int main(){
ll n;
cin>>n;
ll ans=1;
for(int i=1;i<n;i++){
    if(pow(2,i)<=n){
        chmax(ans,(ll)pow(2,i));
    }
    else{
        break;
    }
}
cout<<ans<<endl;
}












Measured memory_bytes: 256
#include<iostream>
#include<math.h>
using namespace std;
int main(){
	int a,b,c,d;
	cin>>a>>b>>c>>d;
	if((abs(a-b)<=d && abs(b-c)<=d )|| abs(c-a)<=d)
	    cout<<"Yes";
	else
	    cout<<"No";
}

Measured memory_bytes: 256
import math

def main():
    N, K = map(int, input().split())

    p = 0.0
    for i in range(1, K):
        if i > N:
            break
        m = math.ceil(math.log2(K / i))
        p += 1 / N * 0.5 ** m

    for i in range(K, N + 1):
        p += 1 / N

    print(p)

if __name__ == '__main__':
    main()


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

int main() {
  string s;
  cin >> s;
  if(s == "AAA" || s == "BBB") cout << "No" << endl;
  else cout << "Yes" << endl;
  return 0;
}

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

T read(T)() { return readln.chomp.to!T; }
T[] reads(T)() { return readln.split.to!(T[]); }
alias readint = read!int;
alias readints = reads!int;

long calc(int a, int b, int c) {
    long ans = 0;

    // 毒入り(美味しい) -> 解毒剤(美味しい)の順に食べる
    int x = min(b, c);
    ans += x * 2;
    b -= x;
    c -= x;

    // 毒入り(美味しい) -> 解毒剤(美味しくない)の順に食べる
    x = min(a, c);
    ans += x;
    a -= x;
    c -= x;

    // 毒入り(美味しい)が余っているなら 1 枚だけ食べる
    if (c > 0) ans++;

    // 余りの解毒剤(美味しい)を食べる
    ans += b;
    return ans;
}

void main() {
    auto abc = readints;
    int a = abc[0], b = abc[1], c = abc[2];
    writeln(calc(a, b, c));
}


Measured memory_bytes: 256
// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

template<typename String> bool isPalindrome(const String &s) {
  int n = s.size();
  rep(i, n / 2) if (s[i] != s[n - 1 - i]) return false;
  return true;
}

main {
  string s;
  cin >> s;
  cout << (isPalindrome(s) ? "Yes" : "No") << endl;
}


Measured memory_bytes: 256
import sys
# from collections import defaultdict, deque
import math
# import copy
# from bisect import bisect_left, bisect_right
import heapq

# sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline

getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = 10 ** 20
MOD = 1000000007


def dijkstra(graph, start, n):
    h = []
    heapq.heappush(h, (0, start))
    costs = [INF for _ in range(n)]
    while h:
        cost, cur = heapq.heappop(h)
        if costs[cur] != INF:
            continue
        costs[cur] = cost
        for edge in graph[cur]:
            ecost, tgt = edge
            if costs[tgt] == INF:
                heapq.heappush(h, (cost + ecost, tgt))

    return costs

def solve():
    n = getN()
    graph = [[] for _ in range(n)]
    for i in range(n-1):
        a,b,c = getList()
        a,b = a-1, b-1

        graph[a].append([c, b])
        graph[b].append([c, a])
    q, k = getList()
    costs = dijkstra(graph, k-1, n)
    # print(costs)
    for i in range(q):
        s, e = getZList()

        print(costs[s] + costs[e])

    return

def main():
    n = getN()
    for _ in range(n):
        solve()
if __name__ == "__main__":
    # main()
    solve()


Measured memory_bytes: 114936
// in case of multiple test cases and N=1e5 don't intialize arrays as global except for graph vector-array
// always check whether or not you are doing mod of a negative number
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
 
using namespace __gnu_pbds;
using namespace std;

#define double      	long double
#define int         	long long
#define pb          	push_back
#define pii         	pair<int,int>
#define vi          	vector<int>
#define vii         	vector<pii>
#define mi          	map<int,int>
#define mii         	map<pii,int>
#define all(a)      	(a).begin(),(a).end()
#define sz(x)       	(int)x.size()
#define endl        	"\n"
#define repp(i,a,b) 	for(int i=a;i<b;i++)
#define rep(i,a,b) 		for(int i=a;i<=b;i++)
#define brep(i,a,b)     for(int i=a;i>=b;i--)
#define deb1(x)      	cout << #x << "=" << x << endl
#define deb2(x, y)  	cout << #x << "=" << x << "," << #y << "=" << y << endl
#define deb3(x, y, z)  	cout << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define trace(v) 		for(auto it=v.begin();it!=v.end();it++)cout<<*it<<" ";cout<<endl;
#define tracearr(a,l,r) for(int iii=l;iii<=r;iii++)cout << a[iii] << " ";cout << endl;
#define PI          	3.1415926535897932384626
#define F 				first
#define S 				second
#define clr(x,y) 		memset(x, y, sizeof(x))
#define IOS         	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);


typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
// *s.find_by_order(1) - gives the 2nd samllest element in set
// s.order_of_key(x)   - gives the number of elements in the set which are strictly smaller than x


const int N= 5000 + 5;
const int MOD=1e9 + 7;


int val[N];
int a[N];
int n,k;

int solve(int idx)
{
	int mp[N];
	clr(mp,0);
	int cost = 0;
	int len = 0;
	vi v;
	while(mp[idx]==0)
	{
		mp[idx]=1;
		idx = a[idx];
		cost += val[idx];
		v.pb(val[idx]);
		len++;
	}
	int ans  = 0;
	int times = k/len;
	int left = k%len;
	int flag = 0;
	if(cost>0)
	{
		flag = 1;
		ans = cost*times;
	}
	if(flag==1)
	{
		int cur = 0;
		int sum = 0;
		int temp = ans;
		while(left>0)
		{
			idx = a[idx];
			sum += val[idx];
			cur = max(cur,sum);
			left--;
		}
		ans += cur;
		sum = 0;
		cur = INT_MAX;
		for(int j=v.size()-1;j>=0;j--)
		{
			sum += v[j];
			cur = min(sum,cur);
		}
		if(cur<0)
			temp -= cur;
		ans = max(ans,temp);
	}
	else
	{
		clr(mp,0);
		int kk = k;
		int cur = INT_MIN;
		int sum = 0;
		while(kk>0 && mp[idx]==0)
		{
			mp[idx] = 1;
			idx = a[idx];
			sum += val[idx];
			cur = max(cur,sum);
			kk--;
		}
		ans += cur;
	}
	return ans;
}

int32_t main()
{
	IOS;
	int T=1;
	//cin >> T;
	// int numOfSetBits = __builtin_popcountll(n);
	std::cout << std::fixed << std::setprecision(12);
	while(T--)
	{
		cin >> n >> k;
		rep(i,1,n)
			cin >> a[i];
		rep(i,1,n)
			cin >> val[i];
		int ans = INT_MIN;
		rep(i,1,n)
			ans = max(ans,solve(i));	
		cout << ans;
	}
}

Measured memory_bytes: 3860
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
#include <functional>
#include <string>
#include <stack>
#include <set>
#include <sstream>
#include <iomanip>
#include <limits>
#include <cstring>

using namespace std;
using ll = long long;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef pair<string, string> sP;
typedef pair<ll, pair<ll, ll>> PP;

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll INF = 1 << 30;
const ll INF2 = 9e18;
const double INF3 = 9e14;
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
const int px[6] = { 0,1,-1,1,0,1 }, py[6] = { -1,-1,0,0,1,1 };
const int tx[8] = { -1,0,1,-1,1,-1,0,1 }, ty[8] = { -1,-1,-1,0,0,1,1,1 };
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()
#define pb push_back
#define eb emplace_back
#define fr first
#define sc second

bool dpx[32000][8010], dpy[32000][8010];

int main() {
	string s;
	int x, y, now = 16000, fl = 2, fx = 0, fy = 0;
	cin >> s >> x >> y;
	x += 16000, y += 16000;
	int i = 0, b = 0;
	for (;i < s.size();i++) {
		if (s[i] == 'T')break;
		b++;
	}
	i++;
	dpx[16000 + b][fx] = 1, dpy[16000][0] = 1,fl = 1;
	while (i < (int)s.size()) {
			int cnt = 0;
			for (;i < s.size();i++) {
				if (s[i] == 'T')break;
				else cnt++;
			}
			if (fl) {
				fl = 0;
				fy++;
				for (int j = 8000;j <= 24000;j++) {
					if (dpy[j - cnt][fy - 1])dpy[j][fy] = 1;
					else if (dpy[j + cnt][fy - 1])dpy[j][fy] = 1;
					else dpy[j][fy] = 0;
				}
			}
			else {
				fl = 1;
				fx++;
				for (int j = 8000;j <= 24000;j++) {
					if (dpx[j - cnt][fx - 1])dpx[j][fx] = 1;
					else if (dpx[j + cnt][fx - 1])dpx[j][fx] = 1;
					else dpx[j][fx] = 0;
				}
			}
			i++;
	}
	if (dpx[x][fx] && dpy[y][fy])cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}

Measured memory_bytes: 254336
#include <iostream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <vector>
#include <utility>
#include <numeric>
#include <string>
#include <set>
#include <map>
#include <tuple>
#include <queue>
#include <stack>
#define rep(i, a, n) for(int (i) = (int)(a);(i) < (int)(n);++(i))
#define repe(i, a, n) for(int (i) = (int)(a);(i) <= (int)(n);++(i))
#define repr(i, a, n) for(int (i) = (int)(a);(i) > (int)(n);--(i))
#define reper(i, a, n) for(int (i) = (int)(a);(i) >= (int)(n);--(i))
#define SIZE(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define RNG(x, n) &((x)[0]), &((x)[n])
#define SUBIF(a, b, x) ((x) ? (b) : (a))
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl;
#define no cout << "no" << endl;
#define pb push_back
#define EPS (1e-20)
#define INF 2147483647LL
#define MOD 1000000007LL
using llint = long long;
using VI = std::vector<int>;
using PII = std::pair<int, int>;
using SI = std::set<int>;
using MII = std::map<int, int>;
using TIII = std::tuple<int, int, int>;
using namespace std;


int n, s;


int cntBit(int bit){
	int cnt = 0;
	do {
		cnt += bit&1;
	} while(bit/=2);
	return cnt;
}


VI enumComb(){
	VI comb;
	rep(i, 0, 1<<10)
		if(cntBit(i) == n) comb.pb(i);
	return comb;
}


int solve(){
	int ans = 0;
	VI comb = enumComb();
	for(int x : comb){
		int sum = 0;
		repe(i, 0, 9)
			if((x>>i)&1) sum += i;
		if(sum == s) ans++;
	}
	return ans;
}


int main(){
	while(cin >> n >> s, n || s) cout << solve() << endl;

	return 0;
}


Measured memory_bytes: 3160
#include <bits/stdc++.h>
     
    using namespace std;
     
    #define FOR(i,a,b)		for(int i=(a),_b=(b);i<(_b);++i)
    #define FORD(i,a,b)		for(int i=(a),_b=(b);i>(_b);--i)
    #define pb			push_back
    #define mp			make_pair
    #define	all(c)			(c).begin(),(c).end()
    #define	present(c,x)		((c).find(x) != (c).end())
    #define	cpresent(c,x)		(find(all(c),x) != (c).end())
    #define	endl			'\n'
     
    typedef long long		ll;
    typedef unsigned long long	ull;
    typedef unsigned char	 	byte;
    typedef vector<int>		vi;
    typedef pair<int, int>		pii;
    typedef pair<ll, ll>		pll;
    typedef vector<pii>		vpii;
     
    const int MX = 25;
     
    int main(int argc, char *argv[])
    {
    #ifdef	HTRINH_UNIT_TEST
    	freopen(argv[1],"r",stdin);
    	ifstream cin(argv[1]);
    #endif
    #if 0
    	ofstream cout(argv[2]);
    #endif
    	ios :: sync_with_stdio(false);
    	cin.tie(NULL);
     
    	int n,k;
    	cin >> n >> k;
		int h[n],cst[n];
		cin >> h[0];
		cst[0] = 0;
		FOR(i,1,n) {
			cin >> h[i];
			cst[i] = 1000000009;
			int cnt = k;
			FORD(j,i-1,-1) {
				cst[i] = min(cst[i],cst[j]+abs(h[i]-h[j]));
				--cnt;
				if (cnt == 0) break;
			}
		}
    	cout << cst[n-1] << endl;
    	return 0;
    }


Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int NSIZ=1000100;
const int mxint=-1u/2;
const long long mod=1e9+7;
typedef pair<int,int> pii;
int n, m, o, re=0;
int a[NSIZ], b[NSIZ];
long long fac[NSIZ], inv[NSIZ];
bool chk[NSIZ];
long long findinv(long long x){
    long long ss=1;
    for(long long d=x, e=1; e<=mod-2; e*=2, d=(d*d)%mod){
        if((mod-2)&e)ss=(ss*d)%mod;
    }
    return ss;
}
long long calc(long long x, long long y){
    return ((fac[x]*inv[y])%mod*inv[x-y])%mod;
}
int main(){
    int i, j, k, l;
    scanf("%d %d %d %d", &n, &m, &k, &l);
    fac[0]=1;
    inv[0]=1;
    for(i=1; i<NSIZ; i++){
        fac[i]=fac[i-1]*i;
        fac[i]%=mod;
        inv[i]=findinv(fac[i]);
    }
//    printf("=%lld %lld\n", fac[n+m], inv[n]);
    long long res=calc(n+m-2,n-1);
//    printf("%lld\n", res);
    for(i=1; i<=k; i++){
        long long ss=calc(i-1+m-l-1,i-1)*calc(n-i+l-1,l-1);
        ss%=mod;
//        printf("%d %d %d %d\n", i-1, m-l-1, n-i, l-1);
//        printf("%lld %lld\n", calc(i-1+m-l,i-1), calc(n-i+l-1,l-1));
        res-=ss;
        res+=mod;
        res%=mod;
    }
    printf("%lld", res);
    return 0;
}


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

int main(){
  int a,b;
    
    while(1){
      cin >> a >> b;
      if( a == 0 && b == 0 )break;
        
      if( a < b ){
        cout << a << " " << b << endl;
      }else{
        cout << b << " " << a << endl;
      }
     }

    return 0;
}

Measured memory_bytes: 1160