akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <string>
#include <sstream>
#include <algorithm>
#include <random>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cmath>
#include <cassert>
#include <climits>
#include <bitset>
#include <functional>
#include <iomanip>
#include <random>

#include <atcoder/all>

#define FOR_LT(i, beg, end) for (int i = (int)(beg); i < (int)(end); i++)
#define FOR_LE(i, beg, end) for (int i = (int)(beg); i <= (int)(end); i++)
#define FOR_DW(i, beg, end) for (int i = (int)(beg); (int)(end) <= i; i--)
#define REP(n)              for (int repeat_index = 0; repeat_index < (int)n; repeat_index++)

using namespace std;


int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout << fixed << setprecision(20);

	int n, k; cin >> n >> k;

	int s = 2 * n;
	int t = 2 * n + 1;

	atcoder::mcf_graph<int32_t, int64_t> mcf(2 * n + 2);

	int64_t kBig = 1000000000;
	
	mcf.add_edge(s, t, n * k, kBig);
	FOR_LT(i, 0, n) {
		mcf.add_edge(s, i, k, 0);
		mcf.add_edge(n + i, t, k, 0);
	}

	FOR_LT(i, 0, n) {
		FOR_LT(j, 0, n) {
			int a; cin >> a;
			mcf.add_edge(i, n + j, 1, kBig - a);
		}
	}

	auto ret = mcf.flow(s, t, n * k);

	cout << kBig * k * n - ret.second << endl;

	auto edges = mcf.edges();

	vector<string> ans(n, string(n, '.'));
	for (auto& edge : edges) {
		if (edge.flow == 0) continue;
		if (edge.from == s) continue;
		if (edge.to == t) continue;

		ans[edge.from][edge.to - n] = 'X';
	}

	for (auto& a : ans) {
		cout << a << endl;
	}

	return 0;
}

Measured memory_bytes: 3876
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// 内部定数
#define D_VAL_MAX		100000									// 最大値数

// 内部構造体 - 値情報
typedef struct Val {
	int miVal;													// 値
	int miCnt;													// 数
} Val;

// 内部変数
static Val sz1Val[D_VAL_MAX];									// 値
static int siVCnt;												// 値数

// 内部変数 - テスト用
#ifdef D_TEST
	static int siRes;
	static FILE *szpFpT, *szpFpA;
#endif

// ソート関数 - 値昇順
int
fSortFnc(
	const void *pzpVal1			// <I> 値１
	, const void *pzpVal2		// <I> 値２
)
{
	Val *lzpVal1 = (Val *)pzpVal1;
	Val *lzpVal2 = (Val *)pzpVal2;

	// 値昇順
	if (lzpVal1->miVal > lzpVal2->miVal) {
		return(1);
	}
	else if (lzpVal1->miVal < lzpVal2->miVal) {
		return(-1);
	}

	return 0;
}

// 実行メイン
int
fMain(
	int piTNo					// <I> テスト番号 1～
)
{
	int i;
	char lc1Buf[1024], lc1Out[1024];

	// テストファイルオープン
#ifdef D_TEST
	sprintf(lc1Buf, ".\\Test\\T%d.txt", piTNo);
	szpFpT = fopen(lc1Buf, "r");
	sprintf(lc1Buf, ".\\Test\\A%d.txt", piTNo);
	szpFpA = fopen(lc1Buf, "r");
	siRes = 0;
#endif

	// 値数・K番目取得
	long long llKNo;
#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpT);
#else
	fgets(lc1Buf, sizeof(lc1Buf), stdin);
#endif
	sscanf(lc1Buf, "%d%lld", &siVCnt, &llKNo);

	// 値・数取得
	for (i = 0; i < siVCnt; i++) {
#ifdef D_TEST
		fgets(lc1Buf, sizeof(lc1Buf), szpFpT);
#else
		fgets(lc1Buf, sizeof(lc1Buf), stdin);
#endif
		sscanf(lc1Buf, "%d%d", &sz1Val[i].miVal, &sz1Val[i].miCnt);
	}

	// ソート
	qsort(sz1Val, siVCnt, sizeof(Val), fSortFnc);

	// K番目取得
	int liVal;
	long long llNo = 0;
	for (i = 0; i < siVCnt; i++) {
		llNo += (long long)sz1Val[i].miCnt;
		if (llKNo <= llNo) {
			liVal = sz1Val[i].miVal;
			break;
		}
	}

	// 結果セット
	sprintf(lc1Out, "%d\n", liVal);

	// 結果表示
#ifdef D_TEST
	fgets(lc1Buf, sizeof(lc1Buf), szpFpA);
	if (strcmp(lc1Buf, lc1Out)) {
		siRes = -1;
	}
#else
	printf("%s", lc1Out);
#endif

	// テストファイルクローズ
#ifdef D_TEST
	fclose(szpFpT);
	fclose(szpFpA);
#endif

	// テスト結果
#ifdef D_TEST
	if (siRes == 0) {
		printf("OK %d\n", piTNo);
	}
	else {
		printf("NG %d\n", piTNo);
	}
#endif

	return 0;
}

int
main()
{

#ifdef D_TEST
	int i;
	for (i = D_TEST_SNO; i <= D_TEST_ENO; i++) {
		fMain(i);
	}
#else
	fMain(0);
#endif

	return 0;
}



Measured memory_bytes: 1788
using System;
using System.Collections.Generic;
using System.Linq;
using static Input;
public class Hello
{
    private const int INF = 1000000007;
    public static void Main()
    {
        int N = NextInt();
        int M = NextInt();
        UnionFind UF = new UnionFind(M + 1);
        HashSet<int> Lang = new HashSet<int>();
        for (int i = 0; i < N; i++)
        {
            int K = NextInt();
            int L = NextInt();
            if (!Lang.Contains(L)) Lang.Add(L);
            for (int j = 1; j < K; j++)
            {
                int Li = NextInt();
                if (!Lang.Contains(Li)) Lang.Add(Li);
                UF.Unite(L, Li);
            }
        }
        int P = UF.Root(Lang.First());
        foreach (var L in Lang)
            if (P != UF.Root(L)) { Console.WriteLine("NO");return; }
        Console.WriteLine("YES");
    }
}
public class UnionFind
{
    public List<int> parent = new List<int>();

    public UnionFind(int N)
    {
        for (int i = 0; i < N; i++) parent.Add(i);
    }

    public int Root(int a)
    {
        if (parent[a] == a) return a;
        else return parent[a] = Root(parent[a]);
    }

    public void Unite(int a, int b)
    {
        a = Root(a);
        b = Root(b);
        if (a == b) return;
        parent[a] = parent[b];
    }

    public bool Same(int a, int b) { return (Root(a) == Root(b)); }
}
public class Input
{
    private static Queue<string> queue = new Queue<string>();
    private static void Confirm() { if (queue.Count == 0) foreach (var s in Console.ReadLine().Split(' ')) queue.Enqueue(s); }
    public static int NextInt() { Confirm(); return int.Parse(queue.Dequeue()); }
    public static long NextLong() { Confirm(); return long.Parse(queue.Dequeue()); }
    public static string NextString() { Confirm(); return queue.Dequeue(); }
    public static double NextDouble() { Confirm(); return double.Parse(queue.Dequeue()); }
    public static int[] LineInt() { return Console.ReadLine().Split(' ').Select(int.Parse).ToArray(); }
    public static long[] LineLong() { return Console.ReadLine().Split(' ').Select(long.Parse).ToArray(); }
    public static string[] LineString() { return Console.ReadLine().Split(' ').ToArray(); }
    public static double[] LineDouble() { return Console.ReadLine().Split(' ').Select(double.Parse).ToArray(); }
}


Measured memory_bytes: 24664
#include <iostream>

using namespace std;

int main(int argc, char const *argv[])
{
    long long A, B, N;
    cin >> A >> B >> N;

    long long x;
    if (B - 1 <= N)
    {
        x = B - 1;
    }else
    {
        x = N;
    }
    
    cout << (A * x / B) << endl;
    
    return 0;
}

Measured memory_bytes: 3612
#include<bits/stdc++.h>
#define ll long long
#define mp make_pair
#define pb push_back
#define x first
#define y second
#define gc getchar_unlocked
#define cases int t;  cin>>t;   while(t--)
ll mod =1e9+7;
//template<typename T> 
inline ll pwr(ll base,ll n){ll ans=1;while(n>0){ if(n%2==1)ans=ans*base;  base=base*base; n/=2;} return ans;}
struct range{int l,h;}; 
using namespace std;
int main()
{
ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll int n;
    cin>>n;
    
    int a[n][n-1];
    for(int i=0;i<n;i++)
    {
      for(int j=0;j<n-1;j++)
        {
          cin>>a[i][j];
          a[i][j]--;
        }
    }

    int pointer[1000]={0};
    int days=0;
    int flag=0;
    int mark[1000]={0};
    int comp=0;
    while(true)
    {
      int check=0;
      memset(mark,0,sizeof(mark));
      for(int i=0;i<n;i++)
      {
          //cout<<i<<"\n";
          if(mark[i] || pointer[i]==n-1)continue;
          int player=a[i][pointer[i]];
          // cout<<i<<player<<"\n";
          if(a[player][pointer[player]]==i && mark[player]==0)
          {
            mark[i]=1;
            mark[player]=1;
            pointer[i]++;
            pointer[player]++;
            if(pointer[i]==n-1)comp++;
            if(pointer[player]==n-1)comp++;
            check=1;
          }
      }
      if(check)days++;
      else {flag=1;break;}
      if(comp==n)break;
    }
    if(flag)cout<<"-1";
    else cout<<days;
    return 0;
}

Measured memory_bytes: 4224
#include <bits/stdc++.h>
using namespace std;
int main()
{
    long long n,m;
    cin>>n>>m;
    long long mod=1000000007;
    char a[n][m];
    long long dp[n][m];
    for(long long i=0;i<n;i++)
    {
        for(long long j=0;j<m;j++)
        {
            cin>>a[i][j];
        }
    }
    for(long long i=0;i<n;i++)
    {
        for(long long j=0;j<m;j++)
            dp[i][j]=0;
    }
    for(long long i=0;i<n && a[i][0]!='#' ;i++)
    {
        dp[i][0]=1;
    }
    for(long long i=0;i<m && a[0][i]!='#' ;i++)
    {
        dp[0][i]=1;
    }

    for(long long i=1;i<n;i++)
    {
        for(long long j=1;j<m;j++)
        {
            if(a[i][j]!='#')
            dp[i][j]=dp[i-1][j]+dp[i][j-1];

            dp[i][j]%=mod;

            // cout<<dp[i][j]<<" ";
        }
        // cout<<endl;
    }

    // for(int i=0;i<n;i++)
    // {
    //     for(int j=0;j<m;j++)
    //     {
            
    //     }
    // }

    cout<<dp[n-1][m-1];
    return 0;
}

Measured memory_bytes: 9088
/****************************
/////  Shubham Singhal
/////  LNMIIT
*****************************/
#include <bits/stdc++.h>
#define ff first
#define ss second
#define mp make_pair
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define eb emplace_back
#define lwr lower_bound
#define upr upper_bound
#define pq priority_queue
#define umap unordered_map
#define rep(i,n) for(i=0; i<n;i++)
#define repp(i,a,n) for(i=a; i<n;i++)
#define precise(x) cout<<fixed<<setprecision(x)
#define all(x) x.begin(),x.end()
#define lcm(a,b) (a*b)/__gcd(a,b)
#define prn(x) cout<<x<<"\n"
#define prn2(x, y) cout<<x<<" "<<y<<"\n"
#define prn3(x, y, z) cout<<x<<" "<<y<<" "<<z<<"\n"
#define prn4(x, y, z, a) cout<<x<<" "<<y<<" "<<z<<" "<<a<<"\n"
#define prnv(x) for(auto i:x) cout<<i<<" "; cout<<"\n"
#define MOD 1000000007
#define MAX 300005
using namespace std;
 
typedef unsigned long long ull;
typedef long long ll;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd; 
typedef vector<int> vi;
typedef vector<long> vl;
typedef vector<char> vc;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef vector<string> vs;
typedef vector<pii> vpii;
typedef vector<pil> vpil;
typedef vector<pli> vpli;
typedef vector<pll> vpll;
const long long INF64 = 1e18;
clock_t time_p=clock();
void time()
{
    time_p=clock()-time_p;
    cerr<<"Time Taken : "<<(float)(time_p)/CLOCKS_PER_SEC<<"\n";
}
void doc()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
}
void fast()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
}
int fun(int n)
{
	return (n*(n-1))/2;
}
int main()
{
	fast();
	int ans=0,n,m;
	cin>>n>>m;
	ans+=(fun(n)+fun(m));
	cout<<ans;
}

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

int main() {
    int n;
    while (1){
        cin >> n;
        if (n == 0) break;
        double s[n], sum = 0, mean, std;
        for (int i = 0; i < n; i++){
            cin >> s[i];
            sum += s[i];
        }
        mean = sum / n;
        sum = 0;
        for (int i = 0; i < n; i++){
            sum += (s[i] - mean) * (s[i] - mean);
        }
        std = sqrt(sum / n);
        printf("%lf\n",std);
    }

    return 0;
}

Measured memory_bytes: 3192
import sys
read = sys.stdin.buffer.read
input = sys.stdin.readline
input = sys.stdin.buffer.readline


#sys.setrecursionlimit(10**9)
#from functools import lru_cache

def RD(): return sys.stdin.read()
def II(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))
# rstrip().decode('utf-8')

#
#import numpy as np

from itertools import combinations

def main():
	s=input().decode()
	if s[2]==s[3] and s[4]==s[5]:
		print("Yes")
	else:
		print("No")



if __name__ == "__main__":
	main()

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

using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); ++i)
#define erep(i, n) for (ll i = 0; i <= (ll)(n); ++i)
#define FOR(i,a,b) for (ll i = (a); i < (ll)(b); ++i)
#define EFOR(i,a,b) for (ll i = (a); i <= (ll)(b); ++i)
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } }




int main() {
    int n; cin >> n;
    set<ll> a;
    rep(i, n) {
        ll in; cin >> in;
        if(a.find(in) == a.end()) {
            a.insert(in);
        } else {
            a.erase(in);
        }
    }
    cout << a.size() << endl;
}


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

int main()
{
ll n ;
cin >> n ;
deque<ll> v[n];
rep(i,n)rep(j,n-1){
  ll a;
  cin >> a;
  v[i].push_back(a-1);
}
ll num=0;
bool see[n]={};
vector<ll> s;
vector<ll> ss;
rep(i,n){
  s.push_back(i);
}
while(num<n*(n-1)/2){
  ans++;
  bool ng=1;
  rep(k,s.size()){
    ll i=s[k];
    if(v[i].size()>0&&see[i]==0){
      if(v[v[i][0]][0]==i&&see[v[i][0]]==0){
        ng=0;
        num++;
        see[i]=1;
        see[v[i][0]]=1;
        ss.push_back(i);
        ss.push_back(v[i][0]);
        v[v[i][0]].pop_front();
        v[i].pop_front();
      }
    }
  }
  sort(ss.begin(),ss.end());
  s=ss;
  ss.clear();
  rep(i,n)see[i]=0;
  if(ng){
    cout << -1 << endl;
    return 0;
  }
}
cout << ans << endl;
return 0;
}


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

using namespace std;

string s1, s2;
int dp[1001][1001];

int get_min(int x, int y, int z)
{
	int ret;
	if (x <= y && x <= z) {
		ret = x;
	} else if (y <= z) {
		ret = y;
	} else {
		ret = z;
	}
	return ret;
}

int main(void)
{
	cin >> s1;
	cin >> s2;
	for (int i = 1; i <= s1.length(); i++) {
		dp[i][0] = i;
	}
	for (int i = 1; i <= s2.length(); i++) {
		dp[0][i] = i;
	}
	for (int i = 1; i <= s1.length(); i++) {
		for (int j = 1; j <= s2.length(); j++) {
			if (s1[i - 1] == s2[j - 1]) {
				dp[i][j] = dp[i - 1][j - 1];
			} else {
				dp[i][j] = get_min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1;
			}
		}
	}
	cout << dp[s1.length()][s2.length()] << endl;
	return 0;
}
			

Measured memory_bytes: 7108
using System;
using System.Collections.Generic;
using System.Linq;

namespace C
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] In = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            int n = In[0];
            int m = In[1];
            var UF = new unionFind(n);
            for (int i=0; i < m; i++)
            {
                int[] In2 = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
                int a = In2[0];
                int b = In2[1];
                --a;--b;
                UF.Connect(a, b);
            }
            var Set = new HashSet<int>();
            for(int i = 0; i < n; i++)
            {
                Set.Add(UF.root(i));
            }
            Console.WriteLine(Set.Count - 1);
        }
    }
    public class unionFind
    {
        //親の番号を格納、親だった場合は-1 <-ふつうのunionfind
        //今回は、親だった場合　-(その集合のサイズ)にする
        int[] Parent;
        //まずすべて-1で初期化、
        public unionFind(int n)
        {
            Parent = new int[n];
            Parent = Enumerable.Repeat(-1, n).ToArray();
        }
        //aがどのグループに属しているか
        public int root(int a)
        {
            if (Parent[a] < 0) return a;
            return Parent[a] = root(Parent[a]);
        }
        //自分のいるグループの頂点数を調べる
        public int Size(int a)
        {
            return -Parent[root(a)];//親をとってきたい
        }
        //aとｂをくっつける
        public bool Connect(int a, int b)
        {
            //aとbをくっつけるのではなく、root(A)とroot(B)をくっつける
            a = root(a);
            b = root(b);
            //すでにくっついているとき
            if (a == b) return false;
            //大きい方(a)が小さいほう(b)をくっつけたい
            //大小が逆ならひっくり返す
            if (Size(a) < Size(b))
            {
                int tmp = a;
                a = b;
                b = tmp;
            }
            //aのサイズを更新する
            Parent[a] += Parent[b];
            //bの親をaに変更する
            Parent[b] = a;
            return true;
        }
    }
}


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

using namespace std;

int main() {
  unsigned long long int h, w;
  cin >> h >> w;
  if (h % 3 == 0 || w % 3 == 0) {
    cout << 0 << endl;
    return 0;
  }
  unsigned long long int ans = ULLONG_MAX;
  if (h % 3 != 0) {
    ans = min(ans, (w * (h / 3 + 1) - w * (h / 3)));
  }
  if (w % 3 != 0) {
    ans = min(ans, ((w / 3 + 1) * h - (w / 3) * h));
  }
  //vector<vector<int>> block = vector<vector<int>>(w, vector<int>(h, 0));

  unsigned long long int ma, mi;
  unsigned long long int a, b, c;

  const unsigned long long int x = w / 2;
  const unsigned long long int y = h / 2;

  // tate
  for (unsigned long long int i = 1; i < h; ++i) {
    a = i * x;
    b = i * (w - x);
    c = (h - i) * w;
    ma = max(a, max(b, c));
    mi = min(a, min(b, c));
    ans = min(ans, ma - mi);
  }

  // yoko
  for (unsigned long long int i = 1; i < w; ++i) {
    a = y * i;
    b = (h - y) * i;
    c = h * (w - i);
    ma = max(a, max(b, c));
    mi = min(a, min(b, c));
    ans = min(ans, ma - mi);
  }

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

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

using namespace std;

int main()
{
    int n, sum = 0;
    cin >> n;

    vector<int> a, b, c;

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        a.emplace_back(x);
    }

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        b.emplace_back(x);
    }

    for (int i = 0; i < n-1; ++i) {
        int x;
        cin >> x;
        c.emplace_back(x);
    }

    for (int i = 0; i < n; ++i) {
        sum += b[a[i] - 1];

        if (a[i] == a[i-1] + 1) {
            sum += c[a[i] - 2];
        }
    }

    cout << sum;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
                using namespace std;
                typedef long long ll;
                #define INPUT()              freopen("input.txt", "r", stdin)
                #define OUTPUT()             freopen("output.txt", "w", stdout)
                #define F                    first
                #define S                    second
            //Templatefunctions
                template<typename T>T gcd(T x, T y) { if (y == 0)return x; else return gcd(y, x%y);}
                template <class T> T euclideanDistance(T x1 , T x2 , T y1 , T y2){return sqrt(( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ));}
                template <class T> T digitSum(T n){T sum = 0;while(n > 0)sum += n % 10,n /= 10;return sum;}
            //datatypes
                typedef long long int ll;
                typedef unsigned long long int ull;
                typedef long double ld;
            //constants
                const double INF = 1e+9;
                const double INFLL = 1e+18;
                const double EPS = 1e-9;
                const double PI = acos(-1);
                const int knightDir[8][2] = {{-2,-1},{-2,1},{-1,2},{1,2},{2,-1},{2,1},{-1,-2},{1,-2}};
                       //const int dx[]={0,0,0,1,1,1,-1,-1,-1};
                       //const int dy[]={-1,0,1,-1,0,1,-1,0,1};
                const int dx[] = {0 , 1 , 0 , -1};
                const int dy[] = {1 , 0 , -1 , 0};
            //helpers
                //#define MOD         1000000007LL
                #define MAX         1000005
                const ll MOD = 998244353;
                int arr[MAX];
            //Global Variables



                int main()
                {
                        int n;
                        scanf("%d",&n);
                        int arr[n+1] , sum = 0;
                        for(int i = 1 ; i <= n ; i++)
                        {
                            scanf("%d",&arr[i]);
                            sum += arr[i];
                        }
                        int m;
                        scanf("%d",&m);
                        for(int i = 1 ; i <= m ; i++)
                        {
                            int p , x;
                            scanf("%d%d",&p,&x);
                            int temp = sum;
                            temp -= arr[p];
                            temp += x;
                            printf("%d\n",temp);
                        }
                }


Measured memory_bytes: 256
n = int(input())
nums = list(map(int, input().split()))
sum = 1
flag = 1
upper = pow(10, 18)
for num in nums:
  if flag == 1:
    sum = sum * num
    if sum > upper:
      flag = 2
  if num == 0:
    flag = 0
    break
if flag == 2:
  print(-1)
else:
  print(sum * flag)

Measured memory_bytes: 21644
#import math
import fractions as math

A, B, C, D = list(map(int, input().split()))
ans = []
x = int((C*D)/math.gcd(C,D))

for n in [A-1, B]:
    c = n // C
    d = n // D
    cd = n // x
    ans.append(n-c-d+cd)
print(ans[1]-ans[0])

Measured memory_bytes: 5048
#include <iostream>
#include <array>

using namespace std;

int main() {
    string s, t;
    cin >> s >> t;

    for (size_t i = 0; i < s.length(); ++i) {
        if (s.substr(i, s.length() - i) + s.substr(0, i) == t) {
            cout << "Yes" << endl;
            return 0;
        }
    }

    cout << "No" << endl;

    return 0;
}

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

int64_t	pow4(int64_t a)
{
	int64_t	result = 1;
	for (int i = 0; i < 4; ++i)
		result *= a;
	return result;
}

int64_t	pow5(int64_t a)
{
	int64_t	result = 1;
	for (int i = 0; i < 5; ++i)
		result *= a;
	return result;
}

int64_t	root5(int64_t a)
{
	int64_t	i = 0;
	int64_t	pow5_i;
	while ((pow5_i = pow5(i)) < a)
		++i;
	return i;
}

int	main()
{
	int64_t	X;
	cin >> X;
	int64_t	A, B;
	int64_t	pow5_A, pow5_B;
	for (A = 0; pow4(A) <= X; ++A) {
		pow5_A = pow5(A);
		pow5_B = -X + pow5_A;
		if (pow5_B < 0) 
			B = -root5(-pow5_B);
		else
			B = root5(pow5_B);
		if (pow5(B) == pow5_B)
			break;
	}
	cout << A << " " << B << endl;
}



Measured memory_bytes: 3620
#define _USE_MATH_DEFINES
#include<iostream>
#include<string>
#include<queue>
#include<cmath>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<random>
#include<functional>
#include<algorithm>
#include<stack>
#include<cstdio>
#include<bitset>
#include<unordered_map>
#include<climits>
#include<fstream>
using namespace std;
typedef long long ll;
typedef long double ld;
#define all(a) (a).begin(),(a).end()
#define EPS (1e-5)
#define bit(n,k) ((n>>k)&1)
const ll Mod = 9999971;
const ll mod = 998244353;
struct H {
	ll x, y;
	bool operator<(const H& b) const {
		if (x != b.x) return x < b.x;
		return y < b.y;
	}
	bool operator>(const H& b) const {
		if (x != b.x) return x > b.x;
		return y > b.y;
	}
	bool operator==(const H& b) const { return x == b.x && y == b.y; }
	bool operator!=(const H& b) const { return (*this) != b; }
};
struct P {
	ll pos, cost;
	bool operator<(const P& b) const { return cost < b.cost; }
	bool operator>(const P& b) const { return cost > b.cost; }
};
struct B {
	ll to, cost;
};
struct E {
	ll from, to, cost;
	bool operator<(const E& b) const { return cost < b.cost; }
	bool operator>(const E& b) const { return cost > b.cost; }
};
template<typename T, typename U>
void chmin(T& a, U b) {
	if (a > b) a = b;
}
template<typename T, typename U>
void chmax(T& a, U b) {
	if (a < b) a = b;
}
template<typename T>
T max_0(T a) {
	if (a < 0) return 0;
	return a;
}
template<typename T>
T min_0(T a) {
	if (a > 0) return 0;
	return a;
}
ll read() {
	ll u;
	ll k = scanf("%lld", &u);
	return u;
}
ll gcd(ll i, ll j) {
	if (i > j) swap(i, j);
	if (i == 0) return j;
	return gcd(j % i, i);
}
ll mod_pow(ll x, ll n, ll p) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % p;
		x = x * x % p;
		n >>= 1;
	}
	return res;
}//x^n%p
const ll Inf = 3023372036854775807;
const int inf = 1500000000;
#define int long long
//----------------------------------------------------
int n;
int a[200000];
int b[200000];
signed main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	int res = 0;
	for (int i = 0; i < n; i++) {
		cin >> b[i];
		if (a[i] != b[i]) res++;
	}
	priority_queue<P>p;
	//pos,cost
	for (int i = 0; i < n; i++) {
		if (b[i] != a[i])
			p.push(P{ i,b[i] });
	}
	int ans = 0;
	while (res) {
		int i = p.top().pos - 1, j = i + 1, k = j + 1;
		p.pop();
		if (i == -1) i = n - 1;
		if (k == n) k = 0;
		int r = b[j];
		b[j] -= b[i] + b[k];
		if (b[j] < a[j]) {
			cout << -1 << endl;
			return 0;
		}
		ans++;
		if (b[j] > a[j] && b[j] > b[i] && b[j] > b[k]) {
			r = r - b[j];
			int t = min(b[j] - max(b[i], b[k]), b[j] - a[j]);
			t = t / r;
			b[j] -= t * r;
			ans += t;
		}
		if (b[j] == a[j]) {
			res--;
			continue;
		}
		p.push(P{ j,b[j] });
	}
	cout << ans << endl;
}

Measured memory_bytes: 9584
import sys
input=sys.stdin.readline
n,q=map(int,input().split())
G=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
imos=[0]*n
count=[-1]*n
for i in range(q):
    p,x=map(int,input().split())
    imos[p-1]+=x
stack=[(0,0)]
while len(stack)>0:
    now=stack.pop()
    x=now[0]
    y=now[1]
    c=y+imos[x]
    count[x]=c
    for i in G[x]:
        if count[i]==-1:
            stack.append((i,c))
for i in count:
    print(i,end='')
    print(' ',end='')


Measured memory_bytes: 61284
#!/usr/bin/env python3

import sys
import math

def f(h):
    return 1 if h == 1 else 1 + 2 * f(int(math.floor(h / 2)))

def main():
    h = int(sys.stdin.readline())
    print(f(h))

if __name__ == '__main__':
    main()

Measured memory_bytes: 3060
W, H, N = map(int,input().split())
x = 0
y = 0
a = 0
X = []
Y = []
A = []
for i in range(N):
    x, y, a = map(int,input().split())
    X.append(x)
    Y.append(y)
    A.append(a)
P = [[0 for i in range(W)] for j in range(H)]
for i in range(N):
    if A[i] == 1:
        for j in range(X[i]):
            for k in range(H):
                P[k][j] = 1
    elif A[i] == 2:
        for j in range(X[i],W):
            for k in range(H):
                P[k][j] = 1
    elif A[i] == 3:
        for j in range(Y[i]):
            for k in range(W):
                P[j][k] = 1
    elif A[i] == 4:
        for j in range(Y[i],H):
            for k in range(W):
                P[j][k] = 1
ans = 0
for i in range(W):
    for j in range(H):
        if P[j][i] == 0:
            ans += 1
print(ans)


Measured memory_bytes: 9352
#include <bits/stdc++.h>
using std::cout;
using std::cin;
using std::endl;

namespace my_space
{
    int even(int a, int b)
    {
        int x = 0;
        if((a+b)%2 == 0)
            x = 1;
        return x;
    }
}
int main()
{
    int H, W;

    while(1)
    {
        int i;
        cin>>H>>W;

        if (H==0 & W==0)
            break;

        for (int i = 1; i <= H; i++)
        {
            for (int j = 1; j <= W; j++)
            {
                if(my_space::even(i, j))
                    cout<<"#";
                else
                    cout<<".";
            }
            cout<<'\n';
        }
        cout<<endl;
    }

    return 0;
}


Measured memory_bytes: 3100
#include<iostream>
using namespace std;
int main() {
	int n, ans = 0, a[114514]; cin >> n; for (int i = 1; i <= n; i++)cin >> a[i];
	for (int i = 1; i <= n; i++) if (a[i] == i) { ans++; i++; }
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
#define Maxn 200010

int n, a[Maxn], b[Maxn], c[Maxn], P[Maxn];
set<int >S;
int main()
{
	scanf("%d", &n);
	for (int i=1; i<=n; i++)
	{
		scanf("%d", &a[i]);
		P[a[i]]++;
		S.insert(a[i]);
	}
	for (int i=1; i<=n; i++)
	{
		scanf("%d", &b[i]);
		P[b[i]]++;
		S.insert(b[i]);
	}
	bool flag = 0; for (auto p:S) if (P[p]>n) { flag = 1; break; }
	if (flag) puts("No");
	else
	{
		puts("Yes");
		for (int i=1; i<=n; i++) c[i]=b[n-i+1];
		int cur = n, top = 1;
		for (int i=1; i<=n; i++)
			while ((a[i]==c[i]) && (cur >= top))
                if(c[cur] != c[i] && a[cur] != c[i]){ swap(c[i], c[cur]);}
                else if(c[top] != c[i] && a[top] != c[i]) {swap(c[i], c[top]);}
                else cur--, top++;
		for (int i=1; i<=n; i++) printf("%d ", c[i]); puts("");
	}
}


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

int main()
{
    int n;
    cin >> n;
    string s = to_string(n);
    for (int i = 0; i < 3; i++) {
        if (s[i] == '7') {
            cout << "Yes" << endl;
            return 0;
        }
    }
    cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 3696
#include<bits/stdc++.h>
#define ll long long
using namespace std;
bool pal(string s){
    int l=0,r=s.length()-1;
    if(r+1==1)return 1;
    while(l<r){
        if(s[l]!=s[r])return 0;
        l++;r--;
    }
    return 1;
}
ll comb(int n,int r){
    if(n<r)return 0;
    ll ans=1;
    for(int i=1;i<=r;i++){
        ans*=n;
        ans/=i;
        n--;
    }
    return ans;
}
int main(){
    ll n;cin>>n;
    ll a[n];
    map<ll,ll> mp;
    map<ll,ll> cnt;
    for(int i=0;i<n;i++){
        cin>>a[i];
        cnt[a[i]]++;
    }
    ll sum=0;
    for(auto it: cnt){
        ll v=comb(it.second,2);
        mp[it.first]=v;
        sum+=v;
    }
    for(int i=0;i<n;i++){
        ll v=cnt[a[i]];
        v--;
        ll val=comb(v,2);
        ll s=sum-mp[a[i]]+val;
        cout<<s<<endl;
    }
}

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

int main()
{
	int a, b, c, x, i, j, k,s=0;
    cin>>a>>b>>c;
    cin>>x;

    for (int i=0; i<=a; i++)
      for (int j=0; j<=b; j++)
        for (int k=0; k<=c; k++)
        {
    	    if (i*500+j*100+k*50==x) s++;
        }

    cout << s << endl;
    return 0; 
}

Measured memory_bytes: 256
N = int(input())
A = input().split(" ")
B = input().split(" ")
C = input().split(" ")


satisfaction = 0

for i in range(0, N):
  satisfaction += int(B[int(A[i])-1])
  if i == 0:
    continue
  if int(A[i]) - int(A[i-1]) == 1:
    satisfaction += int(C[int(A[i-1])-1])
    
print(satisfaction)

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

public class Main {
	public static void main(String[] args) { new Main(); }
	FS in = new FS();
	PrintWriter out = new PrintWriter(System.out);

	
	
	Main() {
		int x = in.nextInt();
		while (true) {
			int cnt = 0;
			for (int i = 2; i * i <= x; i++)
				if (x %i == 0)
					cnt++;
			if (cnt == 0)
				break;
			x++;
		}
		
		out.println(x);
		out.close();
	}

	
	
	int abs(int x) { if (x < 0) return -x; return x; }
	long abs(long x) { if (x < 0) return -x; return x; }
	int max(int x, int y) { if (x < y) return y; return x; }
	int min(int x, int y) { if (x > y) return y; return x; }
	long max(long x, long y) { if (x < y) return y; return x; }
	long min(long x, long y) { if (x > y) return y; return x; }

	class FS {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens()) {
				try { st = new StringTokenizer(br.readLine()); }
				catch (Exception e) {}
			} return st.nextToken();
		}
		int nextInt() { return Integer.parseInt(next()); }
		long nextLong() { return Long.parseLong(next()); }
		double nextDouble() { return Double.parseDouble(next()); }

		void intArr(int sz, int[] x) { for (int i = 0; i < sz; i++) x[i] = nextInt(); }
		void longArr(int sz, long[] x) { for (int i = 0; i < sz; i++) x[i] = nextLong(); }
		void doubleArr(int sz, double[] x) { for (int i = 0; i < sz; i++) x[i] = nextDouble(); }
	}
}



Measured memory_bytes: 23124
// Problem : B - Counting Roads
// Contest : AtCoder - AtCoder Beginner Contest 061
// URL : https://atcoder.jp/contests/abc061/tasks/abc061_b
// Memory Limit : 256 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>

#define spc ' '
#define endl '\n'

using namespace std;


int main(){
	ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int n, m, x, y;
	cin >> n >> m;
	vector<int> a(n);
	for(int i=0; i<m;i++){
	    cin >> x >> y;
	    a[x-1]++;
	    a[y-1]++;
	}
	for(int i:a){
	    cout << i << endl;
	}
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define pb push_back
#define all(x) x.begin(),x.end()
#define cmin(a,b) (a>b?a=b:a)
#define cmax(a,b) (a<b?a=b:a)
#define mem(a,k) memset(a,k,sizeof(a))
#define lop(i,s,t) for(int i=s;i<(t);++i)
#define rep(i,s,t) for(int i=s;i<=(t);++i)
#define dec(i,s,t) for(int i=s;i>=(t);--i)
#define fore(i,v) for(int i=g[v],d=es[i].d;i;i=es[i].nxt,d=es[i].d)
using namespace std;

#define Pr(f,...) //fprintf(stderr,f,##__VA_ARGS__),fflush(stderr)

typedef long long ll;

template<typename T>
void read(T &x){
	x=0; 
	char c;
	for(c=getchar();!isdigit(c);c=getchar()); 
	for(;isdigit(c);c=getchar())x=x*10+c-'0'; 
}

const int N=1e5+50;

int n,m,vis[N]; 
vector<int> g[N],vec;

void dfs(int v){
	vec.pb(v); 
	vis[v]=true;
	for(int d:g[v])if(!vis[d]){
		dfs(d); 
		break;
	}
}

int main(int argc,char *argv[]){
#ifdef CURIOUSCAT
	//freopen("dat.in","r",stdin);
	//freopen("my.out","w",stdout);
#endif
	read(n),read(m); 
	rep(i,1,m){
		int u,v; 
		read(u),read(v); 
		g[u].pb(v),g[v].pb(u); 
	}
	dfs(1); 
	reverse(all(vec)); 
	vec.pop_back(); 
	dfs(1);
	printf("%d\n",(int)vec.size()) ;
	for(int x:vec)printf("%d ",x);
	return 0;
}

Measured memory_bytes: 9976
N = int(input())
B_list = list(map(int,input().split()))

A = 0
for i in range(N-1):
    if i == 0:
        A += B_list[0]
    else:
        A += min(B_list[i-1],B_list[i])

A += B_list[N-2]
print(A)

Measured memory_bytes: 2940
#include <iostream>
#include <string>
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

int main() {
    int a, b; cin >> a >> b;
    
    if(b*2 >= a) cout << 0 << endl;
    else cout << a-2*b;
	return 0;
}

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

int main() {
	int n;
	cin >> n;
	vector<int> as(n);
	for (auto &a : as) {
		cin >> a;
	}
	bitset<4000001> s;
	s[0] = true;
	for (auto &a : as) {
		s |= (s << a);
	}
	for (int i = (accumulate(as.begin(), as.end(), 0) + 1) / 2; ; i++) {
		if (s[i]) {
			cout << i << endl;
			break;
		}
	}
	return 0;
}

Measured memory_bytes: 1664
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

func min(nums ...int) int {
	ret := nums[0]
	for _, v := range nums {
		if v < ret {
			ret = v
		}
	}
	return ret
}

func main() {
	sc.Split(bufio.ScanWords)
	q, h, s, d := nextInt(), nextInt(), nextInt(), nextInt()
	n := nextInt()
	puts(min(n*4*q, n*2*h, n*s, (n/2)*d+(n%2)*min(4*q, 2*h, s)))
	wt.Flush()
}

var (
	sc  = bufio.NewScanner(os.Stdin)
	rdr = bufio.NewReaderSize(os.Stdin, 1000000)
	wt  = bufio.NewWriter(os.Stdout)
)

func next() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	i, _ := strconv.Atoi(next())
	return i
}

func nextFloat64() float64 {
	f, _ := strconv.ParseFloat(next(), 64)
	return f
}

func nextInts(n int) []int {
	slice := make([]int, n)
	for i := 0; i < n; i++ {
		slice[i] = nextInt()
	}
	return slice
}

func nextFloat64s(n int) []float64 {
	slice := make([]float64, n)
	for i := 0; i < n; i++ {
		slice[i] = nextFloat64()
	}
	return slice
}

func nextMega() string {
	buf := make([]byte, 0, 1000000)
	for {
		l, p, _ := rdr.ReadLine()
		buf = append(buf, l...)
		if !p {
			break
		}
	}
	return string(buf)
}

func putf(format string, a ...interface{}) {
	fmt.Fprintf(wt, format, a...)
}

func puts(a ...interface{}) {
	fmt.Fprintln(wt, a...)
}


Measured memory_bytes: 640
n = int(input())
ab = [list(map(int,input().split())) for _ in range(n)]
ab = sorted(ab, key=lambda x:(x[1],x[0]))
time = 0
for l in ab:
  time += l[0]
  if time > l[1]:
    print("No")
    quit()
print("Yes")

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

int main() {
    int n;
    cin >> n;
    int cnt = 0;
    for(int i = 1; i <= n; i++){
        string s = to_string(i);
        if(s.size() % 2 == 1){
            cnt++;
        }
    }
    cout << cnt << endl;
}
    


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

typedef long double ld;

int main(){
  ld M, rD, rR, cD, cR;
  cin >> M >> rD >> rR >> cD >> cR;
  ld j1, j2;
  
  j1 = ceil( (100*cD) / rD);
  j2 = ceil( (100*cR) / rR);
  
  long long ans = M - j1 - j2;
  cout << ((ans >= 0) ? ans : -1) << endl;  
  return 0;
}

Measured memory_bytes: 1224
#include<bits/stdc++.h>//lg
using namespace std;
const int mod=1000000007;
int f[42][1<<18];
int n,x,y,z,p;
int qp(int x,int k)
{
	int res=1;
	while(k)
	{
		if(k&1)res=1ll*res*x%mod;
		x=1ll*x*x%mod;
		k>>=1;
	}
	return res;
}
int ans,sum;
int main()
{
	scanf("%d%d%d%d",&n,&x,&y,&z);
	p=(1<<z-1)+(1<<y+z-1)+(1<<x+y+z-1);
	f[0][0]=1;
	sum=(1<<x+y+z)-1;
	for(int i=0;i<n;i++)
		for(int j=0;j<=sum;j++)
			if(f[i][j])
				for(int k=1;k<=10;k++)
				{
					int now=(j<<k)|(1<<k-1);
					now&=sum;
					if((now&p)!=p)
						(f[i+1][now]+=f[i][j])%=mod;
				}
	for(int i=0;i<=sum;i++)
		(ans+=f[n][i])%=mod;
	printf("%d\n",(qp(10,n)-ans+mod)%mod);
	return 0;
}


Measured memory_bytes: 41216
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
ll powmod(ll a, ll k){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k>>=1;
    }
    return ans;
}
ll inv(ll a){
	return powmod(a, MOD-2);
}
ll f[200001];
ll invf[200001];
ll calc(int x, int y){
	return f[x+y]*invf[x]%MOD*invf[y]%MOD;
}
int main()
{
	int h, w, n;
	cin>>h>>w>>n;
	f[0]=1;
	for(ll i=1; i<=h+w; i++) f[i]=f[i-1]*i%MOD;
	invf[h+w]=inv(f[h+w]);
	for(ll i=h+w-1; i>=0; i--) invf[i]=invf[i+1]*(i+1)%MOD;
	P p[3002]; p[0]=P(1, 1), p[n+1]=P(h, w);
	for(int i=1; i<=n; i++){
		int r, c;
		cin>>r>>c;
		p[i]=P(r, c);
	}
	sort(p, p+n+2);
	ll dp[2][3002]={};
	dp[0][0]=1;
	for(int i=1; i<=n+1; i++){
		int x=p[i].first, y=p[i].second;
		for(int j=0; j<i; j++){
			int x2=p[j].first, y2=p[j].second;
			if(x2>x || y2>y) continue;
			dp[0][i]+=dp[1][j]*calc(x-x2, y-y2);
			dp[0][i]%=MOD;
			dp[1][i]+=dp[0][j]*calc(x-x2, y-y2);
			dp[1][i]%=MOD;
		}
	}
	cout<<(dp[1][n+1]-dp[0][n+1]+MOD)%MOD<<endl;
	return 0;
}

Measured memory_bytes: 3456
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<math.h>
#include<iomanip>
#include<set>
#include<numeric>
#include<cstring>
#include<cstdio>
#include<functional>
#include<bitset>

#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 REVERSE(v,n) reverse(v,v+n);
#define VREVERSE(v) reverse(v.begin(), v.end());
#define ll long long
#define pb(a) push_back(a)
#define m0(x) memset(x,0,sizeof(x))
#define print(x) cout<<x<<endl;
#define pe(x) cout<<x<<" ";
#define lb(v,n) lower_bound(v.begin(), v.end(), n);
#define ub(v,n) upper_bound(v.begin(), v.end(), n);
#define int long long
#define all(x) (x).begin(), (x).end()

using namespace std;

const int MAX = 1010000;
const int MOD = (ll)1000000000 + 7;
const ll INF = 1e17;
const double pi = acos(-1);
const double EPS = 1e-10;

//typedef pair<int, int> P;

bool visited[100010];
int a[100010];
vector<int> G[100010];
vector<int>cost[100010];

void dfs(int n, int cnt) {
	visited[n] = true;
	cost[cnt].pb(a[n]);
	for (auto x : G[n]) {
		if (!visited[x]) {
			dfs(x, cnt);
		}
	}
}

vector<int>rem;
signed main() {
	int N, M; cin >> N >> M;
	REP(i, N)cin >> a[i];
	REP(i, M) {
		int a, b; cin >> a >> b;
		G[a].pb(b);
		G[b].pb(a);
	}
	m0(visited);
	int cnt = 0;
	REP(i, N) {
		if (visited[i])continue;
		else {
			dfs(i, cnt);
			cnt++;
		}
	}
	//print(cnt);
	if (cnt == 1) {
		cout << 0 << endl;
		return 0;
	}
	if (N - 2 * cnt + 2 < 0) {
		print("Impossible");
		return 0;
	}
	/*REP(i, cnt) {
		REP(j, cost[i].size()) {
			pe(cost[i][j]);
		}
		cout << endl;
	}*/
	int ans = 0;
	REP(i, cnt) {
		VSORT(cost[i]);
		ans += cost[i][0];
		FOR(j, 1, cost[i].size()) {
			rem.pb(cost[i][j]);
		}
	}
	VSORT(rem);
	REP(i, cnt - 2) {
		ans += rem[i];
	}
	
	print(ans);
}


Measured memory_bytes: 11768
N = int(input())
L = list(map(int,input().split()))
L.sort()
ans = 0
import bisect
for i in range(N-2):
    a = L[i]
    for j in range(i+1,N-1):
        b = L[j]
        k = bisect.bisect_left(L,a+b)
        l = bisect.bisect_left(L,b-a)
        ans += k-max(j+1,l)
print(ans)

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

typedef long long ll;

#define MAX 26
#define NIL -1

struct Node{
    int p;
    int l,r;
};


Node tree[MAX];
int dep[MAX],hei[MAX];
int n;

void setdep(int r,int d){
    dep[r]=d;
    if(tree[r].l!=NIL)setdep(tree[r].l,d+1);
    if(tree[r].r!=NIL)setdep(tree[r].r,d+1);
}

int seth(int r){
    if(tree[r].l==tree[r].r){
        return hei[r]=0;
    }else{
        return hei[r]=max(seth(tree[r].r),seth(tree[r].l))+1;
    }
}

int getsib(int r){
    int p=tree[r].p;
    if(p==NIL)return NIL;
    else if(tree[p].l!=NIL && tree[p].l!=r)return tree[p].l;
    else if(tree[p].r!=NIL && tree[p].r!=r)return tree[p].r;
    else return NIL;
}

void print(){
    for(int i=0;i<n;i++){
        cout << "node "<<i;
        cout << ": parent = "<<tree[i].p;
        cout << ", sibling = " << getsib(i);
        int deg=0;
        if(tree[i].l!=NIL)deg++;
        if(tree[i].r!=NIL)deg++;
        cout << ", degree = " << deg;
        cout << ", depth = " <<dep[i];
        cout << ", height = " <<hei[i];
        cout <<", ";
        if(tree[i].p==NIL)cout << "root\n";
        else if(deg==0)cout << "leaf\n";
        else cout << "internal node\n";
    }
}

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

    cin >> n;
    for(int i=0;i<n;i++)tree[i].p=tree[i].l=tree[i].r=NIL;

    int id,l,r;
    for(int i=0;i<n;i++){
        cin >> id >> l >> r;
        tree[id].l=l;
        tree[id].r=r;
        tree[l].p=tree[r].p=id;;
    }

    int root;
    for(int i=0;i<n;i++){
        if(tree[i].p==NIL)root=i;
    }
    setdep(root,0);
    int maxh=seth(root);
    print();

}




Measured memory_bytes: 3172
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
int a[4][4];
bool tag[4][4];
bool check() {
	for (int i = 1; i <= 3; i++) {
		if (tag[i][1] && tag[i][2] && tag[i][3])
			return true;
	}	
	for (int i = 1; i <= 3; i++) {
		if (tag[1][i] && tag[2][i] && tag[3][i])
			return true;
	}
	if (tag[1][1] && tag[2][2] && tag[3][3]) {
		return true;
	}
	if (tag[3][1] && tag[2][2] && tag[1][3]) {
		return true;
	}
	return false;
}
void in(int b) {
	for (int i = 1; i <= 3; i++) {
		for (int j = 1; j <= 3; j++) {
			if (b == a[i][j])
				tag[i][j] = true;
		}
	}
}
int main() {
	for (int i = 1; i <= 3; i++) {
		for (int j = 1; j <= 3; j++) {
			cin >> a[i][j];
		}
	}
	int n;
	int b;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> b;
		in(b);
	}
	if (check()) {
		cout << "Yes" << endl;
	}
	else cout << "No" << endl;
	return 0;
}

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define each(itr,v) for(auto itr:v)
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF INT_MAX/3

int n[2];
int card[2][55];
int dp[51][51][51][51][4][2];

int dfs(ll ai,ll aj,ll bi,ll bj,ll pass,ll turn){
  if(pass==2)return 0;
  if(dp[ai][aj][bi][bj][pass][turn]!=-INF)return dp[ai][aj][bi][bj][pass][turn];
  int res=-INF;
  if(aj<n[turn]){
    if(card[turn][aj]==-1){
      maxch(res,-dfs(bj,bj,ai,aj+1,0,1-turn));
    }else{
      maxch(res,-dfs(bi,bj,ai,aj+1,0,1-turn));
    }
  }
  if(ai==aj&&bi==bj){
    maxch(res,-dfs(bi,bj,ai,aj,pass+1,1-turn));
  }else{
    ll s1=0,s2=0;
    repl(i,ai,aj){
      if(card[turn][i]!=-1)s1+=card[turn][i];
    }
    repl(i,bi,bj){
      if(card[1-turn][i]!=-1)s2+=card[1-turn][i];
    }
    maxch(res,-dfs(bj,bj,aj,aj,0,1-turn)+(s1-s2));
  }
  return dp[ai][aj][bi][bj][pass][turn]=res;
}

int main(){
	cin.sync_with_stdio(false);
  cin>>n[0]>>n[1];
  rep(k,2)rep(i,n[k])cin>>card[k][i];
  rep(i,51)rep(j,51)rep(k,51)rep(l,51)rep(x,4)rep(y,2)dp[i][j][k][l][x][y]=-INF;
  cout<<dfs(0,0,0,0,0,0)<<endl;
	return 0;
}

Measured memory_bytes: 214596
#
# File    : A.rb
# Author  : Kazune Takahashi
# Created : 12/6/2018, 11:05:22 PM
# Powered by Visual Studio Code
#

s = gets.chomp.split(" ").map{|i| i.chomp}
puts s[0][0] + s[1][0] + s[2][0]


Measured memory_bytes: 1788
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define F first
#define S second
#define ll long long
using namespace std;
ll n,k;
int main(){
	ll ans=0;
	cin>>n>>k;
	while(n!=0)
	{
		ans++;
		n/=k;
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
N, K = map(int, input().split())
P = list(map(lambda x: int(x)-1, input().split()))
C = list(map(int, input().split()))
INF = 10**18
ans = -INF
for i in range(N):
    arr = []
    tot = 0
    cnt = 0
    nex = i
    while cnt < K:
        nex = P[nex]
        tot += C[nex]
        arr.append(tot)
        cnt += 1
        if nex == i:
            break
    if cnt < K:
        cycle = cnt
        cycle_score = tot
        loop, amari = K//cycle, K%cycle
        for j in range(cycle):
            if j < amari:
                arr.append(cycle_score*loop+arr[j])
            else:
                arr.append(cycle_score*(loop-1)+arr[j])
    ans = max(ans, max(arr))
print(ans)

Measured memory_bytes: 108920
#include<iostream>
#include<cmath>
#include<vector>
using namespace std;

int main(){
while(true){
        cin.tie(0);
        ios::sync_with_stdio(false);
        int m,n;
        cin >>m>>n;
        if(m==0&&n==0) break;
        vector<vector<int> >comb(m+1,vector<int>(n+1));
        int temp;
        cin >> temp;
        comb.at(m-1).at(n) = 1;
        for(int i = 0;i<temp;++i){
                int x,y;
                cin >>x>>y;
                comb.at(x-1).at(y-1) = -1;
        }
        for(int i = m-1;i>=0;--i){
                for(int j = n-1; j>=0;--j){
           if(comb.at(i).at(j)==-1)comb.at(i).at(j)=0;
                        else
                        comb.at(i).at(j)=comb.at(i+1).at(j)+comb.at(i).at(j+1);
                        
                }
        }
        cout << comb.at(0).at(0)<<endl;
        }
        return 0;
}



Measured memory_bytes: 3124