akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

#define ull unsigned long long
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

const int INF = 1e9 + 7;

vector<int> adj[200005];
int p[200005];

int main(void)
{
	FAST
	
	int n, m; cin >> n >> m;
	
	for(int i = 0; i < m; i++)
	{
		int u, v; cin >> u >> v;
		adj[u].pb(v);
		adj[v].pb(u);
	}
	
	queue<int> q;
	
	p[1] = 1;
	q.push(1);
	int u;
	
	while(!q.empty())
	{
		u = q.front();
		q.pop();
		
		for(int v : adj[u])
		{
			if(!p[v])
			{
				p[v] = u;
				q.push(v);
			}
		}
	}
	
	for(int i = 2; i <= n; i++)
	{
		if(!p[i])
		{
			cout << "No";
			return 0;
		}
	}
	
	cout << "Yes\n";
	for(int i = 2; i <= n; i++) cout << p[i] << '\n';
	
		
	
	
	return 0;
}


Measured memory_bytes: 12860
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    int p, q, r;
    cin >> p >> q >> r;
    cout << min({p + q, p + r, q + r}) << endl;
    return 0;
}


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

using namespace std;

typedef pair<int, int> II;
typedef pair<long long, int> LLI;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<II> VII;

#define For(i,a,b) for(int i = a;i <= b; i++)
#define Rep(i,a,b) for(int i = a;i >= b; i--)
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, f) for(auto i : f)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define BUG(x) (cerr << #x << " = " << x << "\n")
#define ARRAY(f) {cerr << #f << " = "; FOR(i, f) cout << i << ' '; cout << "\n";}
#define sz(s) int(s.size())
#define reset(f, x) memset(f, x, sizeof(f))
#define all(x) x.begin(), x.end()
#define two(x) (1LL << x)
#define bit(x, i) ((x >> (i)) & 1LL)
#define onbit(x, i) (x | (1LL << (i)))
#define offbit(x, i) (x & ~(1 << (i)))

const int N = 18;
const int p = 37;

int n;
string s;
map<pair<LL, LL>, int> cnt;
LL res = 0;

int main() 
{
    ios_base::sync_with_stdio(false);
    srand(time(NULL));
    #ifndef ONLINE_JUDGE
        //freopen("in.txt","r",stdin);
        //freopen("","w",stdout);
    #endif
    cin >> n;
    cin >> s;
    string s1 = s.substr(0, n), s2 = s.substr(n, n);
    //BUG(s1); BUG(s2);
    REP(tt, two(n))
    {
        LL h1 = 0, h2 = 0;
        REP(i, n)
            if (!bit(tt, i)) h1 = h1*p + s1[i]-'a'+1;
        Rep(i, n-1, 0) 
            if (bit(tt, i)) h2 = h2*p + s1[i]-'a'+1;
        cnt[make_pair(h1, h2)]++;
    }
    REP(tt, two(n))
    {
        LL h1 = 0, h2 = 0;
        Rep(i, n-1, 0) 
            if (bit(tt, i)) h1 = h1*p + s2[i]-'a'+1;
        REP(i, n)
            if (!bit(tt, i)) h2 = h2*p + s2[i]-'a'+1; 
        if (cnt.find(mp(h1, h2)) != cnt.end()) 
        {
            //cerr << tt << ' ' << h1 << ' ' << h2 << ' ' << cnt[mp(h1, h2)] << "\n";
            res += cnt[mp(h1, h2)];
        }
    }
    cout << res << "\n";
}




Measured memory_bytes: 16640
#include <iostream>

using namespace std;

int main(void) {
    int k, s;
    cin>>k>>s;
    int cnt = 0;
    for (int x=0; x<=k; ++x) {
        for (int y=0; y<=k; ++y) {
            int diff = s - x - y;
            if (diff >= 0 && diff <= k) {
                cnt+=1;
            }
        }
    }
    cout<<cnt<<endl;
    return 0;
}


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


struct UnionFind {
    vector<int> par; // par[i]:iの親の番号　(例) par[3] = 2 : 3の親が2

    UnionFind(int n) : par(n) { //最初は全てが根であるとして初期化
        for(int i = 0; i < n; i++) par[i] = i;
    }

    int root(int x) { // データxが属する木の根を再帰で得る：root(x) = {xの木の根}
        if (par[x] == x) return x;
        return par[x] = root(par[x]);
    }

    void unite(int x, int y) { // xとyの木を併合
        int rx = root(x); //xの根をrx
        int ry = root(y); //yの根をry
        if (rx == ry) return; //xとyの根が同じ(=同じ木にある)時はそのまま
        par[rx] = ry; //xとyの根が同じでない(=同じ木にない)時：xの根rxをyの根ryにつける
    }

    bool same(int x, int y) { // 2つのデータx, yが属する木が同じならtrueを返す
        int rx = root(x);
        int ry = root(y);
        return rx == ry;
    }
};

int main() {
    int n,m,i,cur,ans;
    cin >> n >> m;

    UnionFind tree(n);

    for(i = 0; i < m; i++) {
        int x, y;
        cin >> x >> y;

        tree.unite(x, y); // xの木とyの木を併合する
    }
    
    vector<int> p(n);
    for(i=0; i<n; i++){
    	p[i]=tree.root(i);
    }
    
    sort(p.begin(), p.end());
    cur=1; ans=1;
    for(i=1; i<n; i++){
    	if(p[i]==p[i-1]){
    		cur++;
    		ans=max(cur,ans);
    	}else{
    		cur=1;
    	}
    }
	cout << ans << endl;
    return 0;
}

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

int main() {
	int n, h[100001]; cin>>n;
	for(int i=0;i<n;i++) scanf("%d",&h[i]);
	if(n==1){
		cout<<"Yes"; return 0;
	}
	if(n==2){
		if(h[1]>=h[0]){
			cout<<"Yes";
			return 0;
		}
		else{
			cout<<"No";
			return 0;
		}
	}
	if(n>=3){
		if(h[0]>=h[1]) h[0]-=1;
		if(h[0]>h[1]){
			cout<<"No"; return 0;
		}
	}
	for(int i=1;i<n-1;i++){
		if(h[i]>h[i+1]+1){
			cout<<"No"; return 0;
		}
		else if(h[i]==h[i+1]+1){
			if(h[i-1]==h[i]){
				cout<<"No"; return 0;
			}
			else{
				h[i]-=1;
			}
		}
		else if(h[i]>h[i-1]) h[i]-=1;
	}
	cout<<"Yes";
	return 0;
}

Measured memory_bytes: 640
n = int(input())
if n >= 1800:
    ans = 1
elif n >= 1600:
    ans = 2
elif n >= 1400:
    ans = 3
elif n >= 1200:
    ans = 4
elif n >= 1000:
    ans = 5
elif n >= 800:
    ans = 6
elif n >= 600:
    ans = 7
else:
    ans = 8
print(ans)

Measured memory_bytes: 9176
use std::collections::*;
use std::io::*;
use std::*;
// template {{{
macro_rules! get {
    ($($t:tt),*; $n:expr) => {
        {
            let stdin = std::io::stdin();
            let ret = std::io::BufRead::lines(stdin.lock()).take($n).map(|line| {
                let line = line.unwrap();
                let mut it = line.split_whitespace();
                _get!(it; $($t),*)
            }).collect::<Vec<_>>();
            ret
        }
    };
    ($($t:tt),*) => {
        {
            let mut line = String::new();
            std::io::stdin().read_line(&mut line).unwrap();
            let mut it = line.split_whitespace();
            _get!(it; $($t),*)
        }
    };
}
macro_rules! _get {
    ($it:ident; [char]) => {
        _get!($it; String).chars().collect::<Vec<_>>()
    };
    ($it:ident; [u8]) => {
        _get!($it; String).bytes().collect::<Vec<_>>()
    };
    ($it:ident; usize1) => {
        $it.next().unwrap().parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1
    };
    ($it:ident; [usize1]) => {
        $it.map(|s| s.parse::<usize>().unwrap_or_else(|e| panic!("{}", e)) - 1).collect::<Vec<_>>()
    };
    ($it:ident; [$t:ty]) => {
        $it.map(|s| s.parse::<$t>().unwrap_or_else(|e| panic!("{}", e))).collect::<Vec<_>>()
    };
    ($it:ident; $t:ty) => {
        $it.next().unwrap().parse::<$t>().unwrap_or_else(|e| panic!("{}", e))
    };
    ($it:ident; $($t:tt),+) => {
        ($(_get!($it; $t)),*)
    };
}
// }}}

fn main() {
    let (d, t, s) = get!(i64, i64, i64);
    println!("{}", if d <= t * s { "Yes" } else { "No" });
}


Measured memory_bytes: 2136
#include <iostream>
#include <vector>
#include <cmath>

typedef long long ll;
using namespace std;
int main() {
    ll n;
    cin >> n;
    ll a = 1;
    for(ll i=2;i*i<=n;i++) {
        if(n%i == 0)
            a = i;
    }
    int ans1 = 0, ans2 = 0;
    n = n/a;
    while(n>0) {
        ans1++;
        n /= 10;
    }
    while(a>0) {
        ans2++;
        a /= 10;
    }
    cout << max(ans1, ans2) << endl;
}


Measured memory_bytes: 256
/*
━━━━━┒ギリギリ♂ eye！
┓┏┓┏┓┃キリキリ♂ mind！
┛┗┛┗┛┃＼○／
┓┏┓┏┓┃ /
┛┗┛┗┛┃ノ)
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┛┗┛┗┛┃
┓┏┓┏┓┃
┃┃┃┃┃┃
┻┻┻┻┻┻
*/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
//#include <unordered_map>
using namespace std;
#define fr first
#define sc second
#define cl clear
#define BUG puts("here!!!")
#define W(a) while(a--)
#define pb(a) push_back(a)
#define Rint(a) scanf("%d", &a)
#define Rll(a) scanf("%I64d", &a)
#define Rs(a) scanf("%s", a)
#define Cin(a) cin >> a
#define FRead() freopen("in", "r", stdin)
#define FWrite() freopen("out", "w", stdout)
#define Rep(i, len) for(int i = 0; i < (len); i++)
#define For(i, a, len) for(int i = (a); i < (len); i++)
#define Cls(a) memset((a), 0, sizeof(a))
#define Clr(a, x) memset((a), (x), sizeof(a))
#define Full(a) memset((a), 0x7f7f7f, sizeof(a))
#define lrt rt << 1
#define rrt rt << 1 | 1
#define pi 3.14159265359
#define RT return
#define lowbit(x) x & (-x)
#define onenum(x) __builtin_popcount(x)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef pair<int, int> pii;
typedef pair<string, int> psi;
typedef pair<LL, LL> pll;
typedef map<string, int> msi;
typedef vector<int> vi;
typedef vector<LL> vl;
typedef vector<vl> vvl;
typedef vector<bool> vb;

const int maxn = 55;
const int maxm = 2550;
LL ret;
LL n, a;
LL x[maxn];
LL dp[maxn][maxn][maxm];

int main() {
	// FRead();
	while(cin >> n >> a) {
		Cls(dp); dp[0][0][0] = 1; ret = 0;
		Rep(i, n) cin >> x[i];
		For(i, 1, n+1) {
			Rep(j, n+1) {
				Rep(k, 2501) {
					dp[i][j][k] += dp[i-1][j][k];
					if(j >= 1 && k >= x[i-1]) {
						dp[i][j][k] += dp[i-1][j-1][k-x[i-1]];
					}
				}
			}
		}
		For(i, 1, n+1) ret += dp[n][i][a*i];
		cout << ret << endl;
	}
	RT 0;
}

Measured memory_bytes: 60544
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	
	int n;
	cin >> n;
	vector<long long>v(n, 1e12),m(n,0);
	v[0] = 0;
	for (int i = 0; i < n; i++)
		cin >> m[i];
	v[1] = abs(m[1] - m[0]);
	for (int i = 2; i < n; i++)
	{
		v[i] = min(abs(m[i] - m[i - 1]) + v[i - 1], abs(m[i] - m[i - 2]) + v[i - 2]);
	}
	cout << v[n - 1] << endl;
	return 0;
}

Measured memory_bytes: 1792
#include <stdio.h>

int prime[1000000];

int main(void)
{
	int i, j;
	int n;
	int cnt;
	
	prime[0] = 0;
	prime[1] = 0;
	for (i = 2; i < 1000000; i++){
		prime[i] = 1;
	}
	
	for (i = 2; i * i < 1000000; i++){
		for (j = i * i; j < 1000000; j += i){
			prime[j] = 0;
		}
	}
/*	
	for (i = 0; i < 100; i++){
		printf("%d %d\n", i, prime[i]);
	}
*/	
	while (scanf("%d", &n) != EOF){
		cnt = 0;
		for (i = 0; i <= n; i++){
			if (prime[i] == 1){
				cnt += prime[i];
			}
		}
		
		printf("%d\n", cnt);
	}
	
	return (0);
}

Measured memory_bytes: 4496
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <cassert>
using namespace std;
int dx[5]={1,0,-1,0,0},dy[5]={0,1,0,-1,0};
long long gcd(long long a,long long b){if(a%b==0){return b;}else return gcd(b,a%b);}
long long lcm(long long a,long long b){if(a==0){return b;} return a/gcd(a,b)*b;}
#define INF 2000000000
#define MOD 1000000007
#define sym cout<<"---------"<<endl;
#define ll long long
#define mk make_pair
#define en endl
#define RE return 0
#define int ll
#define P pair<int,int>

#define MAX 200005

int dat[MAX];

struct BIT{
	int bit[MAX];
	void init(){
		memset(bit, 0, sizeof(bit));
	}
	void add(int i,int x){
		i++;
		while(i<MAX){
			bit[i]+=x;
			i+=i&-i;
		}
	}
	int get(int i){
		i++;
		int ret=0;
		while(i>0){
			ret+=bit[i];
			i-=i&-i;
		}
		return ret;
	}
};

BIT bit;

signed main(){
	int num; cin>>num;
	for(int i=0; i<num; i++) cin>>dat[i];
	
	int fir=0,last=11000000000;
	for(;;){
		if(fir==last) break;
		int mid=(fir+last)/2;
		int cnt=0,now=0;
		bit.init();
		bit.add(MAX/2, 1);
		for(int i=0; i<num; i++){
			now+=(dat[i]<=mid)?1:-1;
			cnt+=bit.get(MAX/2+now-1);
			bit.add(MAX/2+now, 1);
		}
		if(cnt<num*(num+1)/4+1) fir=mid+1;
		else last=mid;
	}
	cout<<fir<<en;
}




Measured memory_bytes: 2560
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def main():
    N,S,Q,*q = read().split()
    S = S.decode()
    tree = SegmentTree(initial_values=S, f=lambda x,y:x|y, converter=lambda c: 1 << (ord(c)-ord('a')))
    for t, a, b in zip(*[iter(q)]*3):
        if t == b'1': tree.update(int(a)-1, b)
        else: print(bin(tree.query(int(a)-1, int(b))).count('1'))

class SegmentTree:
    def __init__(self, n=None, f=max, zero_factory=int, converter=lambda x:x, initial_values=None):
        assert(n or initial_values)
        size = n if n else len(initial_values)
        self.__f, self.__z, self.__c = f, zero_factory, converter
        self.__n = 1 << ((size - 1).bit_length())
        self.__dat = [zero_factory() for _ in range(2 * self.__n)]
        if initial_values:
            zi = self.__get_leaf(0)
            for i, v in enumerate(initial_values): self.__dat[zi + i] = converter(v)
            for i in range(zi-1, -1, -1): self.__dat[i] = f(*self.__get_children(i))

    def update(self, index, value):
        i, v = self.__get_leaf(index), self.__c(value)
        if self.__dat[i] == v: return
        self.__dat[i] = v
        while i:
            i = self.__get_parent(i)
            self.__dat[i] = self.__f(*self.__get_children(i))

    def query(self, from_inclusive, to_exclusive):
        ans = self.__z()
        if to_exclusive <= from_inclusive: return ans
        l, r = self.__get_leaf(from_inclusive), self.__get_leaf(to_exclusive) - 1
        while r - l > 1:
            if self.__is_right(l): ans = self.__f(ans, self.__dat[l])
            if self.__is_left(r): ans, r = self.__f(ans, self.__dat[r]), r-1
            l, r = l//2, self.__get_parent(r)
        ans = self.__f(ans, self.__dat[l])
        if l != r: ans = self.__f(ans, self.__dat[r])
        return ans

    def __get_leaf(self, i): return self.__n + i - 1
    def __get_parent(self, i): return (i-1)//2
    def __get_children(self, i): return (self.__dat[2*i+1], self.__dat[2*i+2])
    def __is_left(self, i): return i&1 == 1
    def __is_right(self, i): return i&1 == 0

if __name__ == '__main__':
    main()


Measured memory_bytes: 112336
#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

//constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

//int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

bool func(int asum, int bsum, int csum, vector<int>sum) {
	if (asum > M || bsum > M || csum > M)return false;
	int aadd = sum[3];
	int badd = sum[2];
	int cadd = sum[1];
	if (asum + aadd > M) {
		int box = asum + aadd - M;
		aadd -= box;
		badd += box;
		cadd += box;
	}
	if (bsum + badd > M) {
		int box = bsum + badd - M;
		badd -= box;
		aadd += box;
		cadd += box;
	}
	if (csum + cadd > M) {
		int box = csum + cadd - M;
		cadd -= box;
		badd += box;
		aadd += box;
	}
	aadd += sum[4];
	badd += sum[4];
	cadd += sum[4];
	int amari = sum[4];
	if (asum + aadd > M) {
		int box = min(amari, asum + aadd - (int)M);
		aadd -= box;
		amari -= box;
	}
	if (bsum + badd > M) {
		int box = min(amari, bsum + badd - (int)M);
		badd -= box;
		amari -= box;
	}
	if (csum + cadd > M) {
		int box = min(amari, csum + cadd - (int)M);
		cadd -= box;
		amari -= box;
	}
	return asum + aadd <= M && csum + cadd <= M && bsum + badd <= M;
}

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

	while (cin >> N >> M, N) {
		string a, b, c;
		cin >> a >> b >> c;
		vector<int>num(N);
		vector<int>sum(5);
		for (int i = 0; i < N; i++) {
			if (a[i] == b[i] && a[i] != c[i])num[i] = 1;
			if (a[i] == c[i] && a[i] != b[i])num[i] = 2;
			if (b[i] == c[i] && b[i] != a[i])num[i] = 3;
			if (a[i] != b[i] && a[i] != c[i] && b[i] != c[i])num[i] = 4;
			sum[num[i]]++;
		}
		string ans = "-1";
		string ret;
		int asum = 0, bsum = 0, csum = 0;
		vector<char>check;
		for (char i = 'A'; i <= 'Z'; i++)check.push_back(i);
		for (char i = 'a'; i <= 'z'; i++)check.push_back(i);
		for (int i = 0; i < N; i++) {
			sum[num[i]]--;
			for (auto j : check) {
				asum += a[i] != j;
				bsum += b[i] != j;
				csum += c[i] != j;
			//	cout << j << " " << asum << " " << bsum << " " << csum << " " << endl;
			//	for (auto k : sum)cout << k << " ";
			//	cout << endl;
				if (func(asum, bsum, csum, sum)) {
					ret.push_back(j);
					break;
				}
				else {
					asum -= a[i] != j;
					bsum -= b[i] != j;
					csum -= c[i] != j;
				}
			}
		}
	//	cout << ret << endl;
		if (ret.size() == N)ans = ret;
		cout << ans << endl;
	}
}


Measured memory_bytes: 3884
#include <bits/stdc++.h>
using namespace std;
//#define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define dbv(v) cerr << #v << "="; for (auto _x : v) cerr << _x << ", "; cerr << endl
#define dba(a, n) cerr << #a << "="; for (int _i = 0; _i < (n); ++_i) cerr << a[_i] << ", "; cerr << endl
template <typename A, typename B>
ostream& operator<<(ostream& os, const pair<A, B>& x) {
	return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
const int MOD = 924844033;
struct Mint {
	int val;
	Mint() { val = 0; }
	Mint(const ll& x) {
		val = (-MOD <= x && x < MOD) ? x : x % MOD;
		if (val < 0) val += MOD;
	}
	friend bool operator==(const Mint& a, const Mint& b) { return a.val == b.val; }
	friend bool operator!=(const Mint& a, const Mint& b) { return !(a == b); }
	friend bool operator<(const Mint& a, const Mint& b) { return a.val < b.val; }
	Mint& operator+=(const Mint& m) { if ((val += m.val) >= MOD) val -= MOD; return *this; }
	Mint& operator-=(const Mint& m) { if ((val -= m.val) < 0) val += MOD; return *this; }
	Mint& operator*=(const Mint& m) { val = (ll)val * m.val % MOD; return *this; }
	friend Mint modex(Mint a, ll p) {
		assert(p >= 0);
		Mint ans = 1;
		for (; p; p >>= 1, a *= a) if (p & 1) ans *= a;
		return ans;
	}
	Mint& operator/=(const Mint& m) { return *this *= modex(m, MOD - 2); }
	Mint& operator++() { return *this += 1; }
	Mint& operator--() { return *this -= 1; }
	Mint operator++(int) { Mint result(*this); *this += 1; return result; }
	Mint operator--(int) { Mint result(*this); *this -= 1; return result; }
	Mint operator-() const { return Mint(-val); }
	friend Mint operator+(Mint a, const Mint& b) { return a += b; }
	friend Mint operator-(Mint a, const Mint& b) { return a -= b; }
	friend Mint operator*(Mint a, const Mint& b) { return a *= b; }
	friend Mint operator/(Mint a, const Mint& b) { return a /= b; }
	friend ostream& operator<<(ostream& os, const Mint& x) { return os << x.val; }
};
const int MAXN = 2005;
Mint fac[MAXN];
int main() {
	fac[0] = 1;
	for (int i = 1; i < MAXN; ++i)
		fac[i] = fac[i - 1] * i;
	int n, k;
	scanf("%d%d", &n, &k);
	vector<vector<Mint>> dp(n + 1, vector<Mint>(3));
	dp[0][0] = 1;
	for (int i = 1; i <= min(n, 2 * k); ++i) {
		for (int j = i; j <= n; j += 2 * k) {
			vector<vector<Mint>> ndp(n + 1, vector<Mint>(3));
			for (int c = 0; c <= n; ++c) {
				ndp[c][0] = dp[c][0] + dp[c][1] + dp[c][2];
				if (c && j - k > 0) ndp[c][1] = dp[c - 1][0] + dp[c - 1][1];
				if (c && j + k <= n) ndp[c][2] = dp[c - 1][0] + dp[c - 1][1] + dp[c - 1][2];
			}
			swap(dp, ndp);
		}
		for (int c = 0; c <= n; ++c) {
			dp[c][0] += dp[c][1] + dp[c][2];
			dp[c][1] = dp[c][2] = 0;
		}
	}
	Mint ans = 0;
	for (int i = 0; i <= n; ++i) {
		Mint w = dp[i][0] + dp[i][1] + dp[i][2];
		w *= fac[n - i];
		if (i & 1) ans -= w;
		else ans += w;
	}
	printf("%d\n", ans.val);
}


Measured memory_bytes: 512
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <iterator>
#include <map>
#include <set>
#include <queue>
#include <list>
#include <regex>
using namespace std;
using pii  = pair<int,int>;
using ll=long long;
using ld=long double;
#define pb push_back
#define mp make_pair
#define sc second
#define fr first
#define stpr setprecision
#define cYES cout<<"YES"<<endl
#define cNO cout<<"NO"<<endl
#define cYes cout<<"Yes"<<endl
#define cNo cout<<"No"<<endl
#define rep(i,n) for(ll i=0;i<(n);++i)
#define Rep(i,a,b) for(ll i=(a);i<(b);++i)
#define rrep(i,n) for(int i=n-1;i>=0;i--)
#define rRep(i,a,b) for(int i=a;i>=b;i--)
#define crep(i) for(char i='a';i<='z';++i)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define ALL(x) (x).begin(),(x).end()
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define endl '\n'
int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}
ll gcd(ll a,ll b){return (b == 0 ? a : gcd(b, a%b));}
ll lcm(ll a,ll b){return a*b/gcd(a,b);}
constexpr ll MOD=1000000007;
constexpr ll INF=1000000011;
constexpr ll MOD2=998244353;
constexpr ll LINF = 1001002003004005006ll;
constexpr ld EPS=10e-8;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
template<typename T> istream& operator>>(istream& is,vector<T>& v){for(auto&& x:v)is >> x;return is;}
template<typename T,typename U> istream& operator>>(istream& is, pair<T,U>& p){ is >> p.first; is >> p.second; return is;}
template<typename T,typename U> ostream& operator>>(ostream& os, const pair<T,U>& p){ os << p.first << ' ' << p.second; return os;}
template<class T> ostream& operator<<(ostream& os, vector<T>& v){
  for(auto i=begin(v); i != end(v); ++i){
    if(i !=begin(v)) os << ' ';
    os << *i;
  }
  return os;
}

ll dp[107][100007];
ll V[100007],W[100007];

int main(){
  ll N,w,ma=0;
  cin >> N >> w;
  rep(i,105){
    rep(j,100005){
      dp[i][j]=INF;
    }
  }
  rep(i,N){
    cin >> W[i+1] >> V[i+1];
  }
  dp[0][0]=0;
  Rep(i,1,N+1){
    rep(j,100005){
      if(j>=V[i]){
        dp[i][j]=min(dp[i-1][j],dp[i-1][j-V[i]]+W[i]);
      }
      else{
        dp[i][j]=dp[i-1][j];
      }
    }
  }
  rep(j,100005){
    if(dp[N][j]<=w){
      ma=max(j,ma);
    }
  }
  cout << ma << endl;
}

Measured memory_bytes: 82688
N, K = map(int,input().split())
As = list(map(int,input().split()))

visited = [0] * N
path = []
now_town = 1
while(True):
    visited[now_town-1] = 1
    path.append(now_town)
    now_town = As[now_town-1]
    if visited[now_town-1] == 1:
        break
index = path.index(now_town)
loop = path[index:len(path)]
loop_len = len(loop)
if index < K:
    k = K - index
    r = k % loop_len
    print(loop[r])
else:
    print(path[K])


Measured memory_bytes: 103008
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <map>
#include <vector>
#include <math.h>
using namespace std;

#define rep(i,a) for(int i=0; i<a; i++)
#define rrep(i,a) for(int i=a; i>=0; i--)
#define loop3(i,j,k,a) for(int i=0; i<a; i++)for(int j=0; j<a; j++)if(i!=j)for(int k=0; k<a; k++)if(i!=k&&j!=k)
#define loop4(i,j,k,l,a) for(int i=0; i<a; i++)for(int j=0; j<a; j++)if(i!=j)for(int k=0; k<a; k++)if(i!=k&&j!=k)for(int l=0; l<a; l++)if(i!=l&&j!=l&&k!=l)
#define rep1(i,a) for(int i=1; i<=a; i++)

#define scnd1(a) scanf("%d", &a)
#define scnd2(a,b) scanf("%d%d", &a,&b)
#define scnd3(a,b,c) scanf("%d%d%d", &a,&b,&c)
#define scnd4(a,b,c,d) scanf("%d%d%d%d", &a,&b,&c,&d)

#define prtd1(a) printf("%d\n", a)
#define prtd2(a,b) printf("%d %d\n", a,b)
#define prtd3(a,b,c) printf("%d %d %d\n", a,b,c)
#define prtd4(a,b,c,d) printf("%d %d %d %d\n", a,b,c,d)

#define mem(a,n) memset( a, n, sizeof(a))

typedef long long ll;
ll N, C, dist[100009], cal[100009], turnL[100009][2], turnR[100009][2];

int main() {
    
    cin >> N >> C;
    
    mem( turnL, 0); mem( turnR, 0);
    
    rep( i, N) cin >> dist[i] >> cal[i];
    
    // 時計回りした時の寿司iまでの差し引きカロリーの最高値計算
    ll sumcal = 0;
    rep( i, N) {
        sumcal += cal[i];
        if(i==0 && cal[i] - dist[i] > 0 ) {
            turnR[i][0] = sumcal - dist[i];
            turnR[i][1] = dist[i];
        } else {
            if ( sumcal - dist[i] <= turnR[i-1][0] ) {
                turnR[i][0] = turnR[i-1][0];
                turnR[i][1] = turnR[i-1][1];
            } else {
                turnR[i][0] = sumcal - dist[i];
                turnR[i][1] = dist[i];
            }
        }
    }
    
    // 反時計時計回りした時の寿司iまでの差し引きカロリーの最高値計算
    sumcal = 0;
    rrep( i, N-1) {
        sumcal += cal[i];
        if(i==N-1) {
            if ( cal[i] - (C-dist[i]) > 0 ) {
                turnL[i][0] = sumcal - (C-dist[i]);
                turnL[i][1] = (C-dist[i]);
            }
        } else {
            if ( sumcal - (C-dist[i]) <= turnL[i+1][0] ) {
                turnL[i][0] = turnL[i+1][0];
                turnL[i][1] = turnL[i+1][1];
            } else {
                turnL[i][0] = sumcal - (C-dist[i]);
                turnL[i][1] = (C-dist[i]);
            }
        }
    }
    
    ll maxcal = max( turnR[N-1][0], turnL[0][0]);
    
    // 反時計回りにBまで行き折り返してAまで行く場合の差し引きカロリーの最大値を全探索
    for ( int i = 1; i < N; i++ ) {
        maxcal = max( maxcal, turnL[i+1][0] - turnL[i+1][1] + turnR[i][0] );
    }
    
    // 時計回りにAまで行き折り返してBまで行く場合の差し引きカロリーの最大値を全探索
    for ( int i = 1; i < N; i++ ) {
        maxcal = max( maxcal, turnR[i-1][0] - turnR[i-1][1] + turnL[i][0] );
    }
    
    cout << maxcal << endl;
}

Measured memory_bytes: 4992
from functools import partial
from itertools import islice


def take(n, iterable):
    return list(islice(iterable, n))


def chunked(iterable, n):
    return iter(partial(take, n, iter(iterable)), [])


def distance(x, y):
    return abs(x[0] - y[0]) + abs(x[1] - y[1])


def solve(s):
    h, w, d, *aqlr = map(int, s.split())
    a, q, lr = aqlr[:h * w], aqlr[h * w], aqlr[h * w + 1:]
    n = h * w
    pos = [0] * (n + 1)
    for i, _a in enumerate(a):
        pos[_a] = (i // w, i % w)
    cost = [0] * (n + 1)
    for i in range(d + 1, n + 1):
        cost[i] = cost[i - d] + distance(pos[i - d], pos[i])
    return "\n".join(["%d" % (cost[r] - cost[l]) for l, r in chunked(lr, 2)])


n, m, d = map(int, input().split())
l = '{} {} {}\n'.format(n, m, d) + '\n'.join([input() for _ in range(n)])
q, = map(int, input().split())
l += '\n{}\n'.format(q) + '\n'.join([input() for _ in range(q)])
print(solve(l))


Measured memory_bytes: 37112
#include <bits/stdc++.h>
//#include <tr1/unordered_map>
//#include "Bignum/bignum.h"
//#define big bignum
#define lowbit(x) (x & -x)
#define debug(x) (cout << "#x = " << (x) << endl)
#define Set(x, i) memset (x, i, sizeof(x))
#define R register
#define For(i, j, k) for(R int i = (j); i <= (int) (k); ++i)
#define Rep(i, j, k) for(R int i = (j); i >= (int) (k); --i)
#define Cross(i, j, k) for(R int i = (j); i; i = (k))
using namespace std;
typedef long long ll;
const ll N = 100011;
const ll INF = 5e16;

namespace IO {

	inline char gc() {
		static char buf[100000], *p1 = buf, *p2 = buf;
		return (p1 == p2) && (p2 = (p1 = buf) +
			fread(buf, 1, 100000, stdin), p1 == p2)? EOF: *p1++;
	}

	#define dd ch = getchar()
	inline ll read() {
		R ll x = 0; R int f = 0; R char dd;
		for (; !isdigit(ch); dd) f ^= (ch == '-');
		for (; isdigit(ch); dd)  x = x * 10 + (ch ^ 48);
		return f? -x: x;
	}
	#undef dd

	inline void write ( ll x ) {
		if (x < 0) x = -x, putchar('-');
		if (x > 9) write(x / 10); putchar(x % 10 | 48);
	}

	inline void wrn ( ll x ) { write(x), putchar(' '); }

	inline void wln ( ll x ) { write(x), putchar('\n'); }

	inline void wlnn ( ll x, ll y ) { wrn(x), wln(y); }

}

using IO::wln;
using IO::read;

int main()
{
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);

	ll n = read();
	For ( i, 1, 9 ) For ( j, 1, 9 ) 
		if (i * j == n) return puts("Yes"), 0;
	return puts("No"), 0;
}

/*

*/






Measured memory_bytes: 256
def insertionSort(array, amount):
    for i in range(1, amount):
        v = array[i]
        j = i - 1
        while j >= 0 and array[j] > v:
            array[j + 1] = array[j]
            j = j - 1

        array[j + 1] = v

        for i in range(amount - 1):
            print(array[i], end=" ")
        print(array[amount - 1])

amount = int(input())
array = [0 for i in range(amount)]
array = list(map(int, input().split()))

for i in range(amount - 1):
    print(array[i], end=" ")
print(array[amount - 1])

insertionSort(array, amount)

Measured memory_bytes: 6756
N,M = map(int,input().split())
A = list(map(int,input().split()))
num = [0,2,5,5,4,5,6,3,7,6]

dp = [0]*(N+1)

for i in range(N+1):
	for j in range(len(A)):
		if i-num[A[j]]<0:
			continue
		if i-num[A[j]]>0 and dp[i-num[A[j]]]==0:
			continue
		dp[i] = max(dp[i],dp[i-num[A[j]]]+1)

A.sort()
A.reverse()
ans = ""
now = N
while now!=0:
	for i in range(len(A)):
		if now-num[A[i]]<0:
			continue
		if dp[now-num[A[i]]]==dp[now]-1:
			if dp[now-num[A[i]]]==0 and now-num[A[i]]!=0:
				continue
			ans = ans+str(A[i])
			now -= num[A[i]]
			break
print(ans)

Measured memory_bytes: 42348
#include<stdio.h>

int main(){
  
  while(1){
    
    int n,cnt=0;
  
  scanf("%d",&n);
  
  if(n==0){break;}

  n=1000-n;
  
  while(n>=500){
    n-=500;
    cnt++;
  }
  while(n>=100){
    n-=100;
    cnt++;
  }
  while(n>=50){
    n-=50;
    cnt++;
  }
  while(n>=10){
    n-=10;
    cnt++;
  }
  while(n>=5){
    n-=5;
    cnt++;
  }
  while(n!=0){
    n--;
    cnt++;
  }

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

Measured memory_bytes: 1028
#include<bits/stdc++.h>
typedef long long int ll;
using namespace std;
ll n,i,m,k,x,y,z;
vector<ll>le,ri,v;
int main()
{
    scanf("%lld %lld %lld",&n,&m,&k);
    string c;
    cin >> c;
    for(i=0; i<n; i++)
    {
        if(c[i]=='o')
        {
            if(x==0)
                le.push_back(++x),y=i;
            else
            {
                if(i-y>k)
                    le.push_back(++x),y=i;
                else
                    le.push_back(x);
            }
        }
    }
    le.pop_back();
    x=0;
    for(i=n-1; i>=0; i--)
    {
        if(c[i]=='o')
        {
            if(x==0)
                ri.push_back(++x),y=i;
            else
            {
                if(y-i>k)
                    ri.push_back(++x),y=i;
                else
                    ri.push_back(x);
            }
        }
    }
    ri.pop_back();
    z=-1;
    for(i=0; i<n; i++)
    {
        if(c[i]=='o')
        {
            if(z==-1)
            {
                if(ri.size()==0 || ri.back()<m)
                    v.push_back(i);
                ri.pop_back();
                z++;
            }
            else if(ri.size()==0)
            {
                if(le[z]<m)
                    v.push_back(i);
            }
            else
            {
                x=ri.back()+le[z];
                ri.pop_back();
                z++;
                if(x<m)
                    v.push_back(i);
            }
        }
    }
    for(i=0;i<v.size();i++)
        printf("%lld\n",v[i]+1);
}



Measured memory_bytes: 6644
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define vi vector<int>
#define vll vector<ll>
#define vvi vector < vi >
#define pii pair<int,int>
#define pll pair<long long, long long>
#define vpll vector < pair<long long,long long> >
#define fill(a,val)  memset(a,val, sizeof (a))
#define sort_unique(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define all(cont) cont.begin(), cont.end()
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define MAX   1e9
#define MIN  -1e9
typedef map<int,int> mpi;
typedef set<int> seti;
typedef multiset<int> mseti;
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define rep(i, n)    for(long long int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define repn(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define trav(a, x) for(auto& a : x)
#define dbg(x) cout << #x << " = " << x << '\n'
#define dbg2(x,y) cout<<#x<<" = "<<x<<" & "<<#y<<" = "<<y<<endl
#define inp(a,start,len) for(long long int i = start;i < len;i++) cin>>a[i]
struct Interval{ll start,end;};
bool compare(Interval i1, Interval i2) {return (i1.start < i2.start);}


int main(){
	fio;
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	#endif
  ll k;
  cin>>k;
  rep(i,k){
    cout<<"ACL";
  }
  return 0;
}


Measured memory_bytes: 3640
import java.util.*;

public class Main{
	public static void main(String args[]){
		Scanner sc = new Scanner(System.in);
		int n, judge, taro = 0, hanako = 0;
		String s, s2;
		
		n = sc.nextInt();
		
		for(int i = 0; i < n; i++){
			s = sc.next();
			s2 = sc.next();
			
			judge = s.compareTo(s2);
			
			if(judge < 0)
				hanako += 3;
			else if(judge > 0)
				taro += 3;
			else{
				taro ++;
				hanako ++;
			}
		}
		
		System.out.println(taro + " " + hanako);
	}
}

Measured memory_bytes: 29760
#include <cstdio>
#include <cstring>
#include <cmath>
#include <utility>
#include <iostream>
#include <functional>
#include <bitset>
#include <algorithm>
#include <vector>
#include <forward_list>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <tuple>
#include <numeric>
#define rep(i, s, g) for ((i) = (s); (i) < (g); ++(i))
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = (1ll << 60);

int main(void)
{
    ll n, k;

    cin >> n >> k;

    vector<ll> a(n);

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

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

    ll l = -INF, r = INF;

    while (r - l > 1)
    {
        ll mid = (r + l) / 2;
        ll count = 0;

        for (int i = 0; i < n; i++)
        {
            if (a[i] > 0)
            {
                ll l2 = -1, r2 = n;

                while (r2 - l2 > 1)
                {
                    ll mid2 = (r2 + l2) / 2;

                    if (a[i] * a[mid2] <= mid)
                    {
                        l2 = mid2;
                    }
                    else
                    {
                        r2 = mid2;
                    }
                }

                count += r2;
            }
            else if (a[i] < 0)
            {
                ll l2 = -1, r2 = n;

                while (r2 - l2 > 1)
                {
                    ll mid2 = (r2 + l2) / 2;

                    if (a[i] * a[mid2] <= mid)
                    {
                        r2 = mid2;
                    }
                    else
                    {
                        l2 = mid2;
                    }
                }

                count += n - r2;
            }
            else if (mid >= 0)
            {
                count += n;
            }

            if (a[i] * a[i] <= mid)
            {
                count--;
            }
        }

        if (k <= count / 2)
        {
            r = mid;
        }
        else
        {
            l = mid;
        }
    }

    cout << r << endl;
}


Measured memory_bytes: 1792
#include <iostream>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <math.h>
#include <stdlib.h>
#include <algorithm>

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

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; }

//INT_MAX	2,147,483,647 = 2*1.0e9

int main(){
  int N, M; cin >> N >> M;

  priority_queue<int> jobStock;

  vector<vector<int>> allJobs(M);
  //何日前までに終えないと行けないかで整理
  for(int i = 0; i < N; i++){
    int a, b; cin >> a >> b;
    if(a > M) continue;
    allJobs[M-a].push_back(b);
  }

  int ans = 0;
  for(int i = M-1; i >= 0; i--){
    for(auto j : allJobs[i]){
      jobStock.push(j);
    }

    if(!jobStock.empty()){
      ans += jobStock.top();
      jobStock.pop();
    }
  }

  cout << ans << endl;

  return 0;
}



Measured memory_bytes: 5632
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
ll a[N];
int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin>>n;
    ll t;
    cin>>t;
    ll ans=t,p=t;
    cin>>a[1];
    for(int i=2;i<=n;i++)
    {
        cin>>a[i];
        if(a[i]<=p)
        {
        //    cout<<p<<endl;
            ans+=(t-(p-a[i]));
            p=a[i]+t;
        }
        else
        {
            p=a[i]+t;
            ans+=t;
        }
    }
    cout<<ans<<endl;
    return 0;
}


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

using namespace std;

long long d[200005][3];

int main() {
	ios::sync_with_stdio(false);
	long long n, k;
	long long r, s, p;
	cin>>n>>k;
	cin>>r>>s>>p;
	string t;
	cin>>t;
	for (long long i = 0; i < t.size(); i++) {
		if (i >= k) {
			d[i][0] = (i - k >= 0) * max(d[i - k][1], d[i - k][2]) + (t[i] == 's') * r;
			d[i][1] = (i - k >= 0) * max(d[i - k][0], d[i - k][2]) + (t[i] == 'p') * s;
			d[i][2] = (i - k >= 0) * max(d[i - k][0], d[i - k][1]) + (t[i] == 'r') * p;
		} else {
			d[i][0] = (t[i] == 's') * r;
			d[i][1] = (t[i] == 'p') * s;
			d[i][2] = (t[i] == 'r') * p;
		}
	}
	/*for (long long i = 0; i < t.size(); i++)
		cout<<d[i][0]<<" "<<d[i][1]<<" "<<d[i][2]<<endl;*/
	long long res = 0;
	for (long long i = t.size() - 1; i >= 0 && k > 0; k--, i--)
		res += max(d[i][0], max(d[i][1], d[i][2]));
	cout<<res;
}


Measured memory_bytes: 4560
// In the name of God

#include <bits/stdc++.h>

using namespace std;

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

	int k;
	cin >> k;
	
	string s;
	cin >> s;

	cout << s.substr(0, min(k, (int)s.length()));

	if(s.length() > k) cout << "...";

	return 0;
}


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

using namespace std;

#define int long long
#define endl '\n'
#define MOD 1000000007
#define maxn 100010

vector<pair<int, int>> g[maxn];
int dist[maxn], parent[maxn], n, m, src;


void dijkstra(int src) {
    memset(dist, 63, sizeof dist);
    int inf = dist[0];
    dist[src] = 0;
    set<pair<int, int>> q;

//    dist , node
    q.insert({0, src});
    while (!q.empty()) {
        int len = q.begin()->first;
        int node = q.begin()->second;

        q.erase({len, node});
        for (auto x: g[node]) {
            if (dist[x.first] > dist[node] + x.second) {
                q.erase({dist[x.first], x.first});
                parent[x.first] = node;
                dist[x.first] = x.second + dist[node];
                q.insert({dist[x.first], x.first});
            }
        }
    }
    for (int i = 0; i < n; i++)
        if (dist[i] == inf)
            cout << "INF" << endl;
        else
            cout << dist[i] << endl;
}

vector<pair<pair<int, int>, int>> ed;

void bellman(int src) {
    memset(dist, 63, sizeof dist);
    int inf = dist[0];

    dist[src] = 0;
    int xx;
    for (int i = 0; i < n; i++) {
        xx = -1;
        for (auto x: ed) {
            if (dist[x.first.first] < inf and dist[x.first.first] + x.second < dist[x.first.second]) {
                dist[x.first.second] = x.second + dist[x.first.first];
                xx = 2;
            }
        }
    }
    if (xx == -1) {
        for (int i = 0; i < n; i++)
            if (dist[i] == inf)
                cout << "INF" << endl;
            else
                cout << dist[i] << endl;
    } else
        cout << "NEGATIVE CYCLE" << endl;
}

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

int sz[maxn], p[maxn];

void init(int n) {
    for (int i = 0; i <= n; i++)
        p[i] = i, sz[i] = 1;
}

int find(int n) {
    return (n == p[n]) ? n : p[n] = find(p[n]);
}

bool unite(int a, int b) {
    a = find(a);
    b = find(b);

    if (a == b)
        return false;
    if (b > a)
        swap(a, b);
    sz[a] += sz[b];
    p[b] = a;
    return true;
}

void mst() {
    sort(ed.begin(), ed.end(), comp);
    int ans = 0;
    for (auto x:ed) {
        if (unite(x.first.first, x.first.second))
            ans += x.second;
    }
    cout << ans << endl;
}

void solve() {
    cin >> n >> m;

    for (int i = 0; i < m; ++i) {
        int a, b, c;
        cin >> a >> b >> c;

        ed.push_back({{a, b}, c});
    }
//    dijkstra(src);
//    bellman(src);
    init(n + 1);
    mst();
}

int32_t main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
//    cin >> t;

    while (t--)
        solve();
    return 0;
}


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

int main(){
  int n,m;
  int sum=0;
  cin >> n;
  vector<int> t(n);
  for(int i=0;i<n;i++){
    cin >> t[i];
    sum += t[i];
  }
  cin >> m;
  vector<int> p(m),x(m);
  for(int i=0;i<m;i++){
    cin >> p[i] >> x[i];
  }
  for(int i=0;i<m;i++){
    cout << sum-t[p[i]-1]+x[i] << endl;
  }
}

Measured memory_bytes: 256
# -*- coding: utf-8 -*-

import sys
sys.setrecursionlimit(10 ** 9)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

def upd_dpth(node, depth):
    par, sib, deg, dep, hei, l, r = nodes[node]
    height = 0
    if l != -1:
        height = max(height, upd_dpth(l, depth+1))
    if r != -1:
        height = max(height, upd_dpth(r, depth+1))
    nodes[node][3] = depth
    nodes[node][4] = height
    return height+1

N = INT()

# node: [parent, sibing, degree, depth, height, l, r]
nodes = [[-1, -1, 0, 0, 0, -1, -1] for i in range(N)]

for i in range(N):
    node, l, r = MAP()
    if l != -1:
        # 次数の更新
        nodes[node][2] += 1
        # 親の更新
        nodes[l][0] = node
        # 兄弟の更新
        nodes[l][1] = r
        # 左の子の更新
        nodes[node][5] = l
    if r != -1:
        nodes[node][2] += 1
        nodes[r][0] = node
        nodes[r][1] = l
        nodes[node][6] = r


for i in range(N):
    # 深さと高さの更新
    if nodes[i][0] == -1:
        upd_dpth(i, 0)

for i in range(N):
    par, sib, deg, dep, hei, l, r = nodes[i]
    if par == -1:
        _type = 'root'
    elif deg == 0:
        _type = 'leaf'
    else:
        _type = 'internal node'
    print('node {node}: parent = {parent}, sibling = {sibling}, \
degree = {degree}, depth = {depth}, height = {height}, {type}'.format(
        node=i, parent=par, sibling=sib, degree=deg, depth=dep, height=hei, type=_type))



Measured memory_bytes: 5640
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define stoi stoll
using ll=long long;
using vi=vector<int>;
using pii=pair<int,int>;
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)
#define ITR(i,b,e) for(auto i=(b);i!=(e);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##len=(int)(n);i<i##len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define SZ(c) ((int)c.size())
#define CONTAIN(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) ((y)<0||(x)<0||(y)>=(h)||(x)>=(w))
#define dump(...)
const signed INF_=1001001001; const long long INF=1001001001001001001LL;
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
template<class T> ostream& operator<<(ostream &os,const vector<T> &v) {
    ITR(i,begin(v),end(v))os<<*i<<(i==end(v)-1?"":" ");return os;}
template<class T> istream& operator>>(istream &is,vector<T> &v) {
    ITR(i,begin(v),end(v)) is>>*i;return is;}
template<class T,class U> istream& operator>>(istream &is, pair<T,U> &p) {
    is>>p.first>>p.second;return is;}
template<class T, class U> bool chmax(T &a,const U &b){return a<b?a=b,1:0;}
template<class T, class U> bool chmin(T &a,const U &b){return a>b?a=b,1:0;}
template<class T> using heap=priority_queue<T,vector<T>,greater<T>>;
struct{template<class T> operator T(){T x;cin>>x;return x;}} IN;
struct before_main_function {
    before_main_function() {
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
        #define endl "\n"
    }
} before_main_function;
//------------------8<------------------------------------8<--------------------

signed main() {
    int N, C;
    cin >> N >> C;
    vector<vector<int>> D(C, vector<int>(C));
    vector<vector<int>> A(N, vector<int>(N));
    cin >> D >> A;

    vector<vector<int>> cost(3, vector<int>(C, 0));
    REP(i, N) {
        REP(j, N) {
            REP(c, C) {
                cost[(i + j + 2) % 3][c] += D[A[i][j]-1][c];
            }
        }
    }

    int ans = INF;
    REP(a, C) {
        REP(b, C) {
            REP(c, C) {
                if (a == b || b == c || a == c) continue;
                chmin(ans, cost[0][a] + cost[1][b] + cost[2][c]);
            }
        }
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 2304
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include<bits/stdc++.h>
using namespace std;
#define rep(i,s,t) for(ll i = (ll)(s); i < (ll)(t); i++)
#define rrep(i,s,t) for(ll i = (ll)(s-1);(ll)(t) <= i; i--)
#define all(x) (x).begin(), (x).end()
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> Pll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
constexpr ll INF = numeric_limits<ll>::max()/4;
constexpr ll n_max = 2e5+10;
#define int ll

template <typename A, typename B>
string to_string(pair<A, B> p);
string to_string(const string &s) {return '"' + s + '"';}
string to_string(const char *c) {return to_string((string) c);}
string to_string(bool b) {return (b ? "true" : "false");}
template <size_t N>
string to_string(bitset<N> v){
    string res = "";
    for(size_t i = 0; i < N; i++) res += static_cast<char>('0' + v[i]);
    return res;
}
template <typename A>
string to_string(A v) {
    bool first = true;
    string res = "{";
    for(const auto &x : v) {
        if(!first) res += ", ";
        first = false;
        res += to_string(x);
    }
    res += "}";
    return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p){return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}

void debug_out() {cerr << endl;}
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << " " << to_string(H);
    debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

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

signed main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    rep(i,0,5){
        ll n; cin >> n;
        if(n == 0){
            cout << i + 1 << endl;
        }
    }
}

Measured memory_bytes: 3608
#include<iostream>
using namespace std;
int x,ans;
int main(){
  cin>>x;
  for(int i=x;;i++){
      bool p=1;
      for(int j=2;j*j<=x;j++){
          if(i%j==0){
              p=0;
              break;
          }
      }
      if(p){
          ans=i;break;
      }
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
using namespace std;
struct ss{
    int rank;
    int p;
    int y;
}a[100005];
int cmp(ss x,ss y){
    if(x.p!=y.p) return x.p<y.p;
    else return x.y<y.y;
}
int main()
{
    int n ,m;
    scanf("%d%d",&n,&m);
    int b[100005];
    for(int i=0;i<m;i++){
        a[i].rank=i;
        scanf("%d%d",&a[i].p,&a[i].y);
        b[i]=a[i].p;
    }
    sort(a,a+m,cmp);
    int f=a[0].p,flag=1;
    int x[100005];
    
    for(int i=0;i<m;i++){
        if(a[i].p!=f) {flag=1;f=a[i].p;}
        x[a[i].rank]=flag++;
    }

    for(int i=0;i<m;i++)
        printf("%06d%06d\n",b[i],x[i]);
    return 0;

}


Measured memory_bytes: 3704