akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
const int maxn=2e5+100;
LL a[maxn];
int n;
bool cmp(const LL x,const LL y){
    return x>y;
}
int main()
{
    //freopen("in.txt","r",stdin);
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
    sort(a+1,a+1+n,cmp);
    vector<LL>b;
    int i=1,j=n;
    while(i<j){
        b.push_back(a[i]);
        b.push_back(a[j]);
        i++,j--;
    }
    if(n%2){
        b.push_back(a[(n+1)/2]);
    }
    LL sum1=0;
    for(int i=1;i<b.size();i++){
        sum1+=abs(b[i]-b[i-1]);
    }
    int len=b.size()-1;
    LL sum=abs(b[0]-b[len]);
   // cout<<sum<<endl;
    for(int i=1;i<len;i++){
        sum+=abs(b[i]-b[i-1]);
    }
    LL sum2=0;
    for(int i=1;i<len;i++){
        sum2+=abs(b[i]-b[i-1]);
    }
    sort(a+1,a+1+n);
    vector<LL>c;

    i=1,j=n;
    while(i<j){
        c.push_back(a[i]);
        c.push_back(a[j]);
        i++,j--;
    }
    if(n%2){
        c.push_back(a[(n+1)/2]);
    }
    LL sum3=0;
    for(int i=1;i<c.size();i++){
        sum3+=abs(c[i]-c[i-1]);
    }
    len=c.size()-1;
    LL sum4=abs(c[0]-c[len]);
   // cout<<sum<<endl;
    for(int i=1;i<len;i++){
        sum4+=abs(c[i]-c[i-1]);
    }
    LL num=max(sum3,sum4);
    LL num1=max(sum,sum1);
    printf("%lld\n",max(num,num1));
    return 0;
}


Measured memory_bytes: 3316
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cstdio>
#include <cmath>
#include <cstring>

#define ll long long
#define scani(x) scanf("%i", &x)
#define scanll(x) scanf("%I64d", &x)
#define scanfl(x) scanf("%f", &x)
#define scanch(x) scanf("%c", &x)
#define scans(x) scanf("%s", x);
#define printi(x) printf("%i", x);
#define printll(x) printf("%I64d", x);
#define printfl(x) printf("%f", x);
#define printch(x) printf("%c", x);
#define prints(x) printf("%s", x);
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define INFI (1 << 29)
#define INFL (1LL << 62)
#define sz(x) int(x.size())
#define fi first
#define se second
#define forn(i, n) for(ll i = 0; i < n; i++)
#define for1(i, n) for(ll i = 1; i <= n; i++)
#define ford(i, n) for(ll i = (n - 1); i >= 0; i--)
#define fori(i, n) for(ll i = n; i >= 1; i--)
#define fore(i, l, r) for(ll i = l; i <= r; i++)

using namespace std;

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

    string s;

    cin >> s;

    ll n = sz(s), r = INFL;

    forn(i, n)
    {
        if((i + 2) < n)
        {
            ll sum = 0;

            fore(j, i, (i + 2))
                sum = ((sum * 10LL) + (s[j] - '0'));

            r = min(r, abs(753LL - sum));
        }
    }

    cout << r << "\n";

    return 0;
}


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

int main()
{
    int H, W, D;
    cin >> H >> W >> D;
    pair<int, int> list[H*W + 5];
    int d[H*W + 5];
    for(int i = 1; i <= H; ++i) {
        for(int j = 1; j <= W; ++j) {
            int tmp;
            cin >> tmp;
            list[tmp].first  = i;
            list[tmp].second = j;
        }
    }

    d[1] = 0;
    for(int i = 1; i <=D; ++i) {
        for(int j = i; j + D <= H * W; j += D) {
            int xprev = list[j].first,
                yprev = list[j].second,
                xcrnt = list[j+D].first,
                ycrnt = list[j+D].second,
                dprev = d[j];
            d[j+D] = dprev + abs(xcrnt - xprev) + abs(ycrnt - yprev);
        }
    }

    int Q;
    cin >> Q;
    int L[Q], R[Q];
    for(int i = 0; i < Q; ++i) cin >> L[i] >> R[i];
    for(int i = 0; i < Q; ++i) cout << d[R[i]] - d[L[i]] << endl;
    return 0;
}

Measured memory_bytes: 2944
#include <bits/stdc++.h>
       
using namespace std;
typedef long long ll;
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; }

int main() {
  int N, x;
  cin >> N >> x;
  vector<int> a(N);
  for (int i = 0; i < (int)N; ++i) cin >> a[i];

  ll ans = 0;
  for (int i = 0; i < (int)N - 1; ++i) {
    int c = a[i] + a[i + 1] - x;
    if (c > 0) {
      a[i + 1] = max(0, a[i + 1] - c);
      ans += c;
    }
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 640
#include"bits/stdc++.h"

using namespace std;

#define rep(i,n) for(int i=0;i<(int)(n);i++)
void sort2array(double *a,long *b,long n){
    for(int i=0;i<n;i++){
        b[i]=i;
    }
    sort(b,b+n,[a](long i,long j)->bool {return a[i]<a[j];} );
}
//bを何回足せばaを超えるか
int wtover(int a,int b){
    if(a%b>0)return (a/b)+1;
    else return a/b;
}
const int INF=100000001,ID_MAX=20;
int bi_e[ID_MAX]={0};
//2進数表示したときの最高桁
int bi_max(long n){
    int m=0;
    for(m;pow(2,m)<=n;m++);m=m-1;
    return m;
}
//bi_eに二進数表示したやつを代入
void bi_exs(long n){
    rep(i,ID_MAX){
        bi_e[i]=0;
    }
    if(n<pow(2,ID_MAX)){
        int m=bi_max(n);
        long l;
        for(m;m>-1;m--){
            l=pow(2,m);
            bi_e[m]=n/l;
            n=n%l;
        }
    }
}
//x^n mod m (nが負の時は０)
long myPow(long x, long n, long m){
  if(n<0)return 0;
  if(n == 0)
    return 1;
  if(n % 2 == 0)
    return myPow(x * x % m, n / 2, m);
  else
    return x * myPow(x, n - 1, m) % m;
}


int main(){
    int i;
    string s;
    //入力
    cin>>s;
    //処理
    //出力
    if(s=="ABC")cout<<"ARC"<<endl;
    else cout<<"ABC"<<endl;
}

Measured memory_bytes: 3612
import java.io.*;
public class Main {
	public static void main(String[] args) throws Exception{
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		int rec=0,rho=0;
		int a,b,d;
		String str;
		while((str=br.readLine())!=null){
			String[] s=str.split(",");
			a=Integer.parseInt(s[0]);
			b=Integer.parseInt(s[1]);
			d=Integer.parseInt(s[2]);
			if(a*a+b*b==d*d) rec++;
			else if(a==b) rho++;
		}
		System.out.println(rec+"\n"+rho);
	}
}

Measured memory_bytes: 16900
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
#define INF 2e9
typedef long long ll;
typedef pair<int, int> P;

int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    rep(i, n) cin >> v[i];

    sort(all(v));

    double ans = v[0];
    rep(i, n-1) {
        ans = (ans + v[i+1])/2.0;
    }

    printf("%.10f\n", ans);

}

Measured memory_bytes: 256
n = int(input())
a = list(map(int,input().split()))
p = 0
ans = 0
for i in range(1,n):
    if p == 0 and a[i-1] < a[i]:
        p = 1
    elif p == 0 and a[i-1] > a[i]:
        p = 2
    elif p == 1 and a[i-1] > a[i]:
        p = 0
        ans += 1
    elif p == 2 and a[i-1] < a[i]:
        p = 0
        ans += 1
print(ans+1)

Measured memory_bytes: 14252
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
using namespace std;
using ll = long long;
using P = pair<int, int>;
using vi = vector<int>;
using vc = vector<char>;
using vb = vector<bool>;
using vs = vector<string>;
using vll = vector<long long>;
using vp = vector<pair<int, int>>;
using vvi = vector<vector<int>>;
using vvc = vector<vector<char>>;
using vvll = vector<vector<long long>>;
template<class T> inline bool chmax(T &a, T b) { if (a<b) {a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, T b) { if (b<a) {a=b; return 1;} return 0;}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    ll n, m, d;
    cin >> n >> m >> d;
    double ans = 0;
    if (d == 0) {
        ans = (double)(m - 1) / n;
    } else {
        ans = 2.0 * (n - d) * (m - 1) / (n * n);
    }
    cout << fixed << setprecision(10) << ans << endl;
    
}


Measured memory_bytes: 256
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <vector>
#include <stack>
#include <set>
#include <map>
#define sf scanf
#define pf printf
#define show(x) cout<<"x:"<<x<<endl;
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int inf=1e9;
int n,k;
ll t[maxn];
int check(ll p)
{
    int i=0;
    for(int j=0;j<k;j++)
    {
        ll s=0;
        while(s+t[i]<=p)
        {
            s+=t[i];
            ++i;
            if(i==n)return n;
        }
    }
    return i;
}
ll solve()
{
    ll l=0,r=10000*100000;
    while(r-l>1)
    {
        ll m=(l+r)/2;
        ll v=check(m);
        if(v>=n)r=m;
        else l=m;
    }
    return r;
}
int main()
{
    cin>>n>>k;
    for(int i=0;i<n;i++)
        cin>>t[i];
    ll ans=solve();
    cout<<ans<<endl;
    return 0;
}



Measured memory_bytes: 3876
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする
#include <math.h> //数学関数と数学定数を利用する
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

int n,k;

int main(){
  cin >> n >> k;
  
  ll cnt = 0;
  
  if (k%2 == 0){
    cnt = pow(n/k,3) + pow(2*n/k - n/k,3);
  }else{
    cnt = pow(n/k,3);
  }
  
  cout << cnt;
  
  return 0;
}

Measured memory_bytes: 512
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <numeric>
#include <climits>
#include <iterator>
#include <iomanip>
#include <stack>
#include <bitset>
#include <functional>
using namespace std;
const constexpr int INF = 1e9;
//typedef std::pair<std::string,double> P;

typedef long long ll;
typedef vector<int> VI;

#define FOR(i, a, n) for (ll i = (ll)a; i<(ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)

//グラフの隣接リスト
VI g[100010];
//頂点の入次数を管理
int h[100010];
signed main(void) {
    int N; cin >> N;
    int cn2=0, cn4=0;
    REP(i, N){
        int a; cin >> a;
        if(a%2==0&&a%4!=0) cn2++;
        if(a%4==0) cn4++;
    }
    if(cn4+cn2/2>=N/2) cout << "Yes" << endl;
    else cout << "No" << endl;
	return 0;
}


Measured memory_bytes: 2560
#include <iostream>
#include <string>
#include <algorithm>
#define rep(i, n) for (int i=0; i<n; ++i)
using namespace std;

int main() {
    string s;
    int q;
    cin >> s >> q;

    bool is_r = false;
    string pre_s, post_s;
    rep(i, q) {
        int t;
        cin >> t;
        if (t == 1) {
            is_r = !is_r;
        } else if (t == 2) {
            int f;
            char c;
            cin >> f >> c;
            if (f == 1) {
                if (is_r) {
                    post_s += c;
                } else {
                    pre_s = c + pre_s;
                }
            } else if (f == 2) {
                if (is_r) {
                    pre_s = c + pre_s;
                } else {
                    post_s += c;
                }
            }
        }
    }

    if (is_r) {
        rep(i, post_s.size()) cout << post_s[post_s.size()-i-1];
        rep(i, s.size()) cout << s[s.size()-i-1];
        rep(i, pre_s.size()) cout << pre_s[pre_s.size()-i-1];
    } else {
        cout << pre_s;
        cout << s;
        cout << post_s;
    }
    cout << endl;

    return 0;
}

Measured memory_bytes: 1232
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <iterator>
#include <climits>
#include <unordered_map>
#include <queue>


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

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

inline int ctoi(char c) {
    return (int)(c -'0');
}

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

typedef pair<ll, P> Q;

bool field[60][60];

int main() {
    rep(i, 60) {
        rep(j, 60) {
            field[i][j] = false;
        }
    }

    ll h,w;
    cin >> h >> w;
    ll white_count = 0;
    rep(i, h) {
        rep(j, w) {
            char c;
            cin >> c;
            if (c == '.') {
                field[i+1][j+1] = true;
                ++white_count;
            }
        }
    }

    priority_queue<Q> que;
    que.push(Q(-1,P(1,1)));
    ll distance = 0;
    while(!que.empty()) {
        const Q this_q = que.top();
        que.pop();
        const P this_p = this_q.second;
        if (this_p.first == h && this_p.second == w) {
            distance = this_q.first;
            break;
        }
        if (!field[this_p.first][this_p.second]) {
            continue;
        }

        field[this_p.first][this_p.second] = false;
        que.push(Q(this_q.first-1, P(this_p.first-1, this_p.second)));
        que.push(Q(this_q.first-1, P(this_p.first+1, this_p.second)));
        que.push(Q(this_q.first-1, P(this_p.first, this_p.second+1)));
        que.push(Q(this_q.first-1, P(this_p.first, this_p.second-1)));

    }

    ll ans = -1;
    if (distance != 0) {
        ans = white_count + distance;
    }
    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < n; ++i)
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())
using namespace std;

using ll = long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline void dump(vector<T> v) { for (auto& x : v) cerr << x << " "; cerr << endl; }
template<class T> inline void dump(vector<pair<T, T>> v) { for (auto& p : v) cerr << p.first << " " << p.second << endl; }
template<class T> inline void dump(vector<vector<T>> vv) { for (auto& v : vv) {for (auto& x : v) cerr << x << " "; cerr << endl;} }

constexpr int INF = 1e9+5;
constexpr long long INFLL = 1LL<<60;

class UnionFind {
public:
  // 親の番号を格納する。親だった場合は-(その集合のサイズ)
  vector<int> parent;

  // 作るときは parent の値を全て -1 にする
  // こうすると全てバラバラになる
  UnionFind(const int& n = 0) : parent(n, -1) {}

  // x がどのグループに属しているか調べる
  int find(const int& x) {
    if (parent[x] < 0) return x;
    return parent[x] = find(parent[x]);
  }

  // x と y をくっつける  O(log n)
  bool unite(int x, int y) {
    // x と y を直接くっつけるのではなく，root(x) に root(y) をくっつける
    x = find(x);
    y = find(y);
    if (x == y) return false;  // すでにくっついているからくっつけない

    // 大きい方(x) に小さい方(y)をくっつけたい 大小が逆だったらひっくり返す
    if (size(x) < size(y)) swap(x, y);

    parent[x] += parent[y];  // x のサイズを更新する
    parent[y] = x;  // y の親を x に変更する
    return true;
  }

  // 自分のいるグループの頂点数を調べる
  int size(const int& x) { return -parent[find(x)]; }

  // x と y が同じグループに属しているか否か
  bool same(const int& x, const int& y) { return find(x) == find(y); }
};

using P = pair<int, int>;

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

  ll n, m;
  cin >> n >> m;

  vector<P> edges;
  rep(i, m) {
    int a, b; cin >> a >> b; a--; b--;
    edges.push_back({a, b});
  }

  vector<ll> ans;
  ans.push_back((n * (n - 1)) / 2);

  UnionFind uf(n);

  for (auto it = edges.rbegin(); it != edges.rend(); it++) {
    if (uf.same(it->first, it->second)) {
      ans.push_back(ans.back());
    } else {
      ans.push_back(ans.back() - ll(uf.size(it->first)) * ll(uf.size(it->second)));
      uf.unite(it->first, it->second);
    }
  }
  ans.pop_back();

  reverse(ALL(ans));

  for (auto& x : ans) cout << x << endl;

  return 0;
}


Measured memory_bytes: 5824
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
inline int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }

int pow(long long x, int n, int m){
    long long ret = 1;
    while (n > 0) {
        if (n & 1) ret *= x, ret %= m;
        x *= x, x %= m;
        n >>= 1; 
    }
    return ret % m;
}

int euler_phi(int n) {
   int ret = n;
   for(int i = 2; i * i <= n; ++i){
       if(n % i == 0) {
           ret -= ret / i;
           while(n % i == 0) n /= i;
       }
   }
   if(n > 1) ret -= ret / n;
   return ret;
}

int main()
{
    int K;
    scanf("%d", &K);

    K /= gcd(K, 7);
    K *= 9;

    int res = euler_phi(K);

    if(gcd(K, 10) != 1) {
        printf("%d\n", -1);
        return 0;
    }

    int ans = 1000000;
    for(int i = 1; i * i <= res; ++i){
        if(res % i == 0) {
            if(pow(10, i, K) == 1){
                cerr << pow(10, i, K) << '\n';
                printf("%d\n", i);
                return 0;
            }
            if(pow(10, res / i, K) == 1) ans = min(ans, res / i);
        }
    }
    printf("%d\n", ans);

    return 0;
}

Measured memory_bytes: 3828
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P)
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T> ostream& operator << (ostream &s, set<T> P)
{ for(auto it : P) { s << "<" << it << "> "; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
{ for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s << endl; }


const int INF = 1<<29;
string S;

int rev(int bit, int i) {
    return bit ^ (1<<i);
}

long long solve() {
    int N = S.size();

    vector<int> dp(N+1, INF);
    vector<int> mval(1<<26, INF);
    dp[0] = 0;
    mval[0] = 0;
    int par = 0;
    for (int i = 1; i <= N; ++i) {
        par = rev(par, S[i-1] - 'a');
        chmin(dp[i], mval[par] + 1);
        for (int j = 0; j < 26; ++j) {
            par = rev(par, j);

            chmin(dp[i], mval[par] + 1);
            
            par = rev(par, j);
        }

        //COUT("----------------"); COUT(i); COUT(par); COUT(dp[i]);

        chmin(mval[par], dp[i]);
    }       
    return dp[N];
}

int main() {
    while (cin >> S) cout << solve() << endl;
}


















Measured memory_bytes: 263556
#include <bits/stdc++.h>
using namespace std;
#define push_back pb
#define make_pair mp
using ll =  long long;
const int INF = 0x3f3f3f3f;
const int MAX_N = 25;
int N;
int V[MAX_N],C[MAX_N];

void solve()
{
    int ans = 0;
	for(int i=0; i<(1<<N);++i){
		int x = 0, y = 0;
		for(int j=0;j<N;++j){
			if(i>>j&1){
				x += V[j];
				y += C[j];
			}
		}
		ans = max(ans, x-y);
	}
	cout << ans << "\n";
}

int main()
{
	cin >> N;
	for(int i=0;i<N;++i)cin >> V[i];
	for(int i=0;i<N;++i)cin >> C[i];
	solve();
}

Measured memory_bytes: 256
from collections import defaultdict
class UnionFind:
    def __init__(self, n):
        class KeyDict(dict):
            # 辞書にないときの対応
            def __missing__(self,key):
                self[key] = key
                return key
        self.parent = KeyDict()
        self.rank = defaultdict(int)
        self.weight = defaultdict(int)

    # 根を探す
    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            # 経路圧縮
            # 自分自身じゃない場合は、上にさかのぼって検索(再帰的に)
            y = self.find(self.parent[x])           
            self.weight[x] += self.weight[self.parent[x]]   #圧縮時にweightを更新(和)
            self.parent[x] = y      #親の置き換え(圧縮)
            return self.parent[x]

    # 結合
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        # 低い方を高い方につなげる(親のランクによる)
        if self.rank[x] < self.rank[y]:
            self.parent[x] = y
        else:
            self.parent[y] = x
        
        if self.rank[x] == self.rank[y]:
            self.rank[x] += 1

    ### 重み付き
    def weighted_union(self, x, y, w):
        # print("unite",x,y,w,self.weight)
        px = self.find(x)
        py = self.find(y)
        # 低い方を高い方につなげる(親のランクによる)
        # if px == py: return 0
        if self.rank[px] < self.rank[py]:
            self.parent[px] = py
            self.weight[px] = - w - self.weight[x] + self.weight[y]
        else:
            self.parent[py] = px
            self.weight[py] =  w + self.weight[x] - self.weight[y]
        if self.rank[px] == self.rank[py]:
            self.rank[px] += 1
        return 0

    # 判定
    def judge(self, x, y):
        return self.find(x) == self.find(y)

N, M = map(int, input().split())
# unionfindで同じクラスタに属すか判定
# 同じクラスタ内では拘束条件を適用する
# 重み付きunionfindを使おう
uf = UnionFind(N)
for i in range(M):
    l, r, d = map(int, input().split())
    if not uf.judge(l,r):
        uf.weighted_union(l,r,d)
    else:
        if uf.weight[r]-uf.weight[l]!=d:
            print("No")
            exit()

print("Yes")

Measured memory_bytes: 30576
#include <stdio.h>
#define MIN(a,b) ((a) < (b) ? (a) : (b))
int main(void){
	char c[2];
	int fr[1000][1000],n,i,j,x,ans;
	while(scanf("%d",&n)){
		if(n==0)break;
		ans=0;
		for(i=0;i<n;i++){
			for(j=0;j<n;j++){
				scanf("%1s",&c);
				if(c[0]=='.'){
					if(i==0 || j==0)fr[i][j]=1;
					else {
						x=MIN(MIN(fr[i][j-1],fr[i-1][j]),fr[i-1][j-1])+1;
						if(ans<x)ans=x;
						fr[i][j]=x;
					}
				}
				else fr[i][j]=0;
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}

Measured memory_bytes: 4508
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
                int quotient = a / b;
                int remainder = a % b;
                double floatno = (double) a / (double) b;
                System.out.printf("%d %d %.5f",quotient,remainder,floatno);
    }

}


Measured memory_bytes: 26636
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ll long long

ll Mypow(ll a,ll b){
    ll i,ans=1;
    for (i=0; i<b; i++) ans*=a;
    return ans;
}

int main(){
    ll x,a=0,b=0;
    scanf("%lld",&x);
    for (a=-118; a<=119; a++) {
        for (b=-118; b<=119; b++) {
            if (x==(Mypow(a, 5)-Mypow(b, 5))) {
                printf("%lld %lld",a,b);
                return 0;
            }
        }
    }
    return 0;
}




Measured memory_bytes: 1708
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<string>
#include<cstring>
#include<climits>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<deque>
#include<tuple>
#include<list>
#include<unordered_map>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i,n) for(ll i=0;i<n;i++)
#define rep2(i,a,b) for(ll i=a;i<b;i++)
#define repr(i,n) for(ll i=n-1;i>=0;i--)
#define all(x) x.begin(),x.end()
#define INF (1 << 30) - 1
#define LLINF (1LL << 61) - 1
// #define int ll
using namespace std;
const int MOD = 1000000007;
const int MAX = 510000;


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,k,a[1000],sum=0,ans;
    vector<ll> div;
    cin>>n>>k;
    rep(i,n){
        cin>>a[i];
        sum+=a[i];
    }
    for(ll i=1;i*i<=sum;i++){
        if(sum%i==0){
            div.pb(i);
            div.pb(sum/i);
        }
    }
    sort(all(div));
    reverse(all(div));
    for(ll d:div){
        ll r[1000]={},need=INF;
        ll sumr[1000]={};
        rep(i,n){
            r[i]=(a[i]%d);
        }
        sort(r,r+n);
        rep(i,n){
            sumr[i+1]=r[i]+sumr[i];
        }
        rep(i,n){
            ll a,b;
            a=sumr[i];
            b=d*(n-i)-(sumr[n]-sumr[i]);
            // cout<<d<<" "<<a<<" "<<b<<endl;
            if(a==b){
                need=min(need,a);
            }
        }
        if(need<=k){
            ans=d;
            break;
        }
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
#define rep(i,n) for (ll i=0; i<n; ++i)
#define all(c) begin(c),end(c)
#define PI acos(-1)
#define oo LLONG_MAX
template<typename T1, typename T2>
bool chmax(T1 &a,T2 b){if(a<b){a=b;return true;}else return false;}
template<typename T1, typename T2>
bool chmin(T1 &a,T2 b){if(a>b){a=b;return true;}else return false;}
/*

10 - 10000
9991個
真ん中取ればいい

3 - 5
3,4,5=3個

*/
int main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  
  ll N; cin >> N;
  vector<ll> A(N, 0), B(N, 0);
  ll a = 0, b = 0, ans = 0;
  rep(i, N) cin >> A[i] >> B[i];
  sort(all(A));
  sort(all(B));
  ll mid = N/2;
  if (N%2 == 0){
    a += A[mid] + A[mid - 1];
    b += B[mid] + B[mid - 1];
    ans = b - a + 1;
  }
  else
    ans = B[mid] - A[mid] + 1;
  cout <<ans << endl;
}


Measured memory_bytes: 6372
#include <iostream>
int gcd(int a , int b){
    return (b == 0)? a : gcd(b , a % b);
}
int main(){
    int a , b;
    while(std::cin >> a >> b){
        int x = gcd(a , b);
        std::cout << x << " " << x * (int)(a / x) * (int)(b / x) << "\n";
    }
}


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

int A;
int B;

int main(){
  cin >> A >> B;
  if(A + B == 3){
    cout << "3";
  }
  if(A + B == 5){
    cout << "1";
  }
  if(A + B == 4){
    cout << "2";
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
static const int MAX = 100000;
static const int NIL = -1;

int n;
vector<int> G[MAX];
int color[MAX];

void dfs(int r, int c)
{
  stack<int> S;
  S.push(r);
  color[r] = c;
  while (!S.empty())
  {
    int u = S.top();
    S.pop();
    for (int i = 0; i < G[u].size(); i++)
    {
      int v = G[u][i];
      if (color[v] == NIL)
      {
        color[v] = c;
        S.push(v);
      }
    }
  }
}

void assignColor()
{
  int id = 1;
  for (int i = 0; i < n; i++)
  {
    color[i] = NIL;
  }
  for (int u = 0; u < n; u++)
  {
    if (color[u] == NIL)
    {
      dfs(u, id++);
    }
  }
}

int main()
{
  int s, t, m, q;
  cin >> n >> m;

  for (int i = 0; i < m; i++)
  {
    cin >> s >> t;
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  cin >> q;

  for (int i = 0; i < q; i++)
  {
    cin >> s >> t;
    if (color[s] == color[t])
    {
      cout << "yes" << endl;
    }
    else
    {
      cout << "no" << endl;
    }
  }

  return 0;
}


Measured memory_bytes: 8956
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n;
  cin>>n;
  int k=1,i;
  long long p=1,c=1e18,d,q[n];
  for(i=1;i<=n;i++)
  {
	  cin>>q[i-1];
	  if(q[i-1]==0)
	  	k=0;
  }
  if(k==0)
  {
	  cout<<"0\n";
	  return 0;
  }
  for(i=0;i<n;i++)
  {
	  d=c/p;
	  if(q[i]>d)
	  {
		  cout<<"-1\n";
		  return 0;
	  }
	  p*=q[i];

  }
  cout<<p<<endl;
  return 0;
  
}


Measured memory_bytes: 4400
from itertools import permutations
import math

N = int(input())
A = []
for i in range(N):
    A.append(list(map(int,input().split())))
#print(A)

B = []
for i in permutations(A,2):
    B.append(i)
#print(B)

ans = 0
for i in range(len(B)):
    ans += math.sqrt((B[i][0][0] - B[i][1][0])**2 + (B[i][0][1] - B[i][1][1])**2)

print(ans/N)

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

int main() {
	int H, W;
	static long long U[2000][2000] = {}, D[2000][2000] = {}, L[2000][2000] = {}, R[2000][2000] = {}, ANS = 0;
	string S[2000];
	cin >> H >> W;
	for (int i = 0; i < H; i++) cin >> S[i];
	for (int i = 0; i < H; i++) {
		int COUNT = 0;
		for (int j = 0; j < W; j++) {
			if (S[i][j] == '.') {
				COUNT++;
			}
			else {
				COUNT = 0;
			}
			L[i][j] = COUNT;
		}
	}
	for (int i = 0; i < H; i++) {
		int COUNT = 0;
		for (int j = W - 1; j >= 0; j--) {
			if (S[i][j] == '.') {
				COUNT++;
			}
			else {
				COUNT = 0;
			}
			R[i][j] = COUNT;
		}
	}
	for (int i = 0; i < W; i++) {
		int COUNT = 0;
		for (int j = 0; j < H; j++) {
			if (S[j][i] == '.') {
				COUNT++;
			}
			else {
				COUNT = 0;
			}
			U[j][i] = COUNT;
		}
	}
	for (int i = 0; i < W; i++) {
		int COUNT = 0;
		for (int j = H - 1; j >= 0; j--) {
			if (S[j][i] == '.') {
				COUNT++;
			}
			else {
				COUNT = 0;
			}
			D[j][i] = COUNT;
		}
	}
	for (int i = 0; i < H; i++) {
		for (int j = 0; j < W; j++) {
			if (S[i][j] == '.') {
				ANS = max(ANS, L[i][j] + R[i][j] + U[i][j] + D[i][j] - 3);
			}
		}
	}
	cout << ANS;
}

Measured memory_bytes: 129408
import itertools

n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

l = [i+1 for i in range(n)]
ll = []
p_= 0
q_= 0
for i in itertools.permutations(l,n):
    ll.append(i)

for i in ll:
    if list(i) == p:
        p_ = ll.index(i)+1
    if list(i) == q:
        q_ = ll.index(i)+1

print(abs(p_ - q_))

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

using namespace std;

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

    string s;

    cin >> s;

    int q;

    cin >> q;

    bool rev = 0;

    deque<char> ans;

    for(int i=0; i<s.size(); i++) ans.push_back(s[i]);

    while(q--){
        int op;

        cin >> op;

        if(op == 1){
            rev ^= 1;
        }
        else{
            int f;
            char c;

            cin >> f >> c;

            f--;

            if(f^rev) ans.push_back(c);
            else ans.push_front(c);
        }
    }

    if(rev) reverse(ans.begin(), ans.end());

    for(int i=0; i<ans.size(); i++) cout << ans[i];

    cout << endl;

    return 0;
}


Measured memory_bytes: 976
n,m=map(int,input().split())
A=reversed(sorted(map(int,input().split())))
M=[2,5,5,4,5,6,3,7,6]
D={}
E={}
for a in A:
    if M[a-1] in D:
        continue
    else:
        D[M[a-1]]=a
        E[a]=M[a-1]
S=list(reversed(sorted(E)))
        
dp=[0]*(n+2)
for i in range(1,n+2):
    cnt=-1
    for d in D:
        if i-d>=0:
            if dp[i-d]!=-1:
                cnt=max(dp[i-d]+1,cnt)
    dp[i]=cnt    
k=dp[n]
ans=""

while k>0:
    for s in S:
        x=E[s]
        if n-x<0:
            continue
        if dp[n-x]==k-1:
            ans+=str(s)
            k-=1
            n-=x
            break
print(ans)

Measured memory_bytes: 3412
#include <iostream>
#define rep(i,a) for(int i=0;i<(a);i++)

using namespace std;
int f[30][30]={},x,y,size;

void n(){
    int dx[13]={0,1,-1,0,0,-1,-1,1,1,0,0,2,-2},dy[13]={0,0,0,1,-1,1,-1,1,-1,2,-2,0,0};
    rep(i,4*size+1)f[y+dy[i]+10][x+dx[i]+10]+=1;
    
}

int main(){
    char z;
    while(cin>>x>>z>>y>>z>>size){
        n();
    }
    int m=0,c=0;
    rep(i,10)
        rep(j,10)
            if(f[i+10][j+10]==0)c++;
                else m=max(m,f[i+10][j+10]);
    cout<<c<<endl<<m<<endl;
    return 0;
}

Measured memory_bytes: 1164
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long  ll;
typedef unsigned long long ul;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef double ld;
typedef complex<ld> Point;
const ll INF = mod * mod;
typedef pair<int, int> P;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>b;i--)
#define rep0(i,n) for(int i=0;i<n;i++)
#define per0(i,n) for(int i=n-1;i>=0;i--)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
const ld eps = 1e-6;
const ld pi = acos(-1.0);
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
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 dp[3001][3001];
void solve() {
    string s; cin >> s;
    string t; cin >> t;
    int s_size = s.length();
    int t_size = t.length();

    rep0(i,s_size){
        rep0(j,t_size){
            if(s[i] == t[j]) {
                chmax(dp[i+1][j+1], dp[i][j]+1);
            }
            chmax(dp[i+1][j+1], dp[i][j+1]);
            chmax(dp[i+1][j+1], dp[i+1][j]);
        }
    } 
    string res = "";
    int i = s.size(), j = t.size();
    while (i > 0 && j > 0)
    {
        if (dp[i][j] == dp[i-1][j]) {
            i--; 
        }

        else if (dp[i][j] == dp[i][j-1]) {
            j--; 
        }

        else {
            res = s[i-1] + res; 
            i--, j--; 
        }
    }
    cout << res << endl;
}
int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	solve();
	return 0;
}

Measured memory_bytes: 35456
#include<iostream>
#include<cmath>
using namespace std;
int co(int n)
{
    int c=0;
    while(n)
    {
        n=n/10;
        c++;
    }
    return c;
}
int main()
{
    long long int n,a,b,sum,c,p;
    double d;
    cin>>a>>b;
    c=co(b);
    p=co(a);
   if(c==1)
   {
       sum=(a*10)+b;

   }
   else if(c==2)
   {
       sum=(a*100)+b;
   }
   else
   {
       sum=(a*1000)+b;
   }

   p=sqrt(sum);
   d=sqrt(sum);
  if(p==d)
  {
      cout<<"Yes"<<endl;
  }
  else
  {
      cout<<"No"<<endl;
  }

}


Measured memory_bytes: 256
import sys
def input():
  return sys.stdin.readline().rstrip()

N,K=map(int,input().split())

num_=[0]*K
for i in range(1,N+1):
  num_[i%K]+=1
ans=0
for a in range(K):
  b=(K-a)%K
  c=(K-a)%K
  if (b+c)%K!=0:
    continue
  ans+=num_[a]*num_[b]*num_[c]

print(ans)


Measured memory_bytes: 66992
#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>
#include<limits.h>
#include<cassert>
#include<iterator>
#include<complex>
#include<stack>
#include<sstream>
using namespace std;
typedef  long long int lint;
typedef pair<int, int> P;
typedef pair<lint, lint> LLP;
typedef pair<char, char>CP;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define sort(v) sort((v).begin(), (v).end())
#define reverse(v) reverse((v).begin(), (v).end())
#define upper(v,hoge) upper_bound(v.begin(),v.end(),hoge)
#define lower(v,hoge) lower_bound(v.begin(),v.end(),hoge)
#define llower(v,hoge) *lower_bound(v.begin(), v.end(), hoge)
#define lupper(v,hoge) *upper_bound(v.begin(), v.end(), hoge)
/*
vector<char>al(26);
al = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
vector<char>AL(26);
AL = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
vector<char>NUM(10);
	NUM = { '0','1','2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' };
*/



int main() {
	int H, W;
	cin >> H >> W;
	vector<vector<char>>S(H, vector<char>(W));
	rep(i, H) {
		rep(j, W) {
			cin >> S[i][j];
		}
	}
	vector<vector<vector<int>>>sum(H, vector<vector<int>>(W, vector<int>(4)));
	rep(i, H) {
		sum[i][0][0] = 0;
		for (int j = 1; j < W; j++) {
			sum[i][j][0] += sum[i][j - 1][0];
			if (S[i][j - 1] == '.') {
				sum[i][j][0]++;
			}
			else {
				sum[i][j][0] = 0;
			}
		}
	}
	rep(i, H) {
		sum[i][W - 1][1] = 0;
		for (int j = W - 2; j >= 0; j--) {
			sum[i][j][1] += sum[i][j + 1][1];
			if (S[i][j + 1] == '.') {
				sum[i][j][1]++;
			}
			else {
				sum[i][j][1] = 0;
			}
		}
	}
	rep(j, W) {
		sum[0][j][2] = 0;
		for (int i = 1; i < H; i++) {
			sum[i][j][2] += sum[i - 1][j][2];
			if (S[i - 1][j] == '.') {
				sum[i][j][2]++;
			}
			else {
				sum[i][j][2] = 0;
			}
		}
	}
	rep(j, W) {
		sum[H - 1][j][3] = 0;
		for (int i = H - 2; i >= 0; i--) {
			sum[i][j][3] += sum[i + 1][j][3];
			if (S[i + 1][j] == '.') {
				sum[i][j][3]++;
			}
			else {
				sum[i][j][3] = 0;
			}
		}
	}
	/*rep(i, H) {
		rep(j, W) {
			if (S[i][j] == '#') {
				cout << 0<<' ';
			}
			else {
				cout <<sum[i][j][0]+ sum[i][j][1] + sum[i][j][2] + sum[i][j][3]<<' ';
				//cout << sum[i][j][2] << ' ';
			}
		}
		cout << endl;
	}*/
	int ans = 0;
	rep(i, H) {
		rep(j, W) {
			if (S[i][j] == '.') {
				ans = max(ans, sum[i][j][0] + sum[i][j][1] + sum[i][j][2] + sum[i][j][3]);
			}
		}
	}
	cout << ans+1 << endl;
}


Measured memory_bytes: 223232
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll mod = 998244353;
const ll INF = 1001001001;
const ll LINF = 1001001001001001001;

void prvec(vector<ll> vec){
    ll n = vec.size();
    rep(i,n) cout << i << " " << vec.at(i) << "\n";
}

void pr2d(vector<vector<ll>> vvec){
    ll h = vvec.size();
    ll w = vvec.at(0).size();
    rep(i,h){
        rep(j,w){
            cout << vvec.at(i).at(j) << " ";
        }
        cout << "\n";
    }
}

int main(){
    ll n ; cin >> n;
    ll a = 0, b = 0 , c = 0;
    ll cnt = 0;
    ll mx = 0;
    rep(i,n){
        ll x,y; cin >> x >> y;
        if(x==y) cnt++; else cnt = 0;
        mx = max(mx,cnt);
    }
    if(mx>=3) cout << "Yes"; else cout << "No";
    cout << endl;
    return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
// const bool debug=true;
const bool debug=false;
#define DEBUG if(debug==true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;

int main(void) {
    ll n,c;
    cin >> n >> c;
    vector<pair<ll, ll> > xv;
    rep(i,n){
        ll tx,tv;
        cin >> tx >> tv;
        xv.push_back(make_pair(tx,tv));
    }
    vector<pair<ll, ll> > xv_rev;
    for(int i=n-1;i>=0;i--){
        xv_rev.push_back(make_pair(c-xv[i].first,xv[i].second));
    }
    vector<pair<ll,ll> > max_a,max_b;
    ll tmp=0,dist=0,now=0;
    max_a.push_back(make_pair(0,0));
    max_b.push_back(make_pair(0,0));
    rep(i,n){
        now = now + xv[i].second - (i==0 ? xv[i].first : (xv[i].first - xv[i-1].first));
        if(tmp<now){
            tmp=now;
            dist=xv[i].first;
        }
        max_a.push_back(make_pair(tmp,dist));
    }
    tmp=0;dist=0;now=0;
    rep(i,n){
        now = now + xv_rev[i].second - (i==0 ? xv_rev[i].first : (xv_rev[i].first - xv_rev[i-1].first));
        if(tmp<now){
            tmp=now;
            dist=xv_rev[i].first;
        }
        max_b.push_back(make_pair(tmp,dist));
    }
    ll res=0;
    rep(i,n+1){
        tmp = max_a[i].first+max_b[n-i].first - min(max_a[i].second,max_b[n-i].second);
        tmp = max(tmp, max(max_b[n-i].first, max_a[i].first));
        res = max(res,tmp);
    }
    cout << res << endl;
    return 0;
}


Measured memory_bytes: 8048
N,M,Q = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(Q)]

def dfs(arr,i = 0):
    if i == N:
        score = 0
        for q in range(Q):
            score += A[q][3] if arr[A[q][1]-1] - arr[A[q][0]-1] == A[q][2] else 0
        return score

    max_score = 0
    for n in range(arr[i-1] if i >= 0 else 1, M+1):
        x = [arr[k] if k != i else n for k in range(N)]
        max_score = max(max_score, dfs(x,i+1))
    
    return max_score
        
print(dfs([1 for _ in range(N)]))

Measured memory_bytes: 9236
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct union_find{
    vector<int> par, rank;
    union_find(int n){
        par = vector<int>(n);
        rank = vector<int>(n);
        init(n);
    }
    void init(int n){
        for(int i=0;i<n;i++){
            par[i]=i;
            rank[i]=0;
        }
    }
    int find(int x){
        if(par[x]==x){
            return x;
        }else{
            return par[x] = find(par[x]);
        }
    }
    void unite(int x, int y){
        x = find(x);
        y = find(y);
        if(x==y) return;
        if(rank[x]<rank[y]){
            par[x]=y;
        }else{
            par[y]=x;
            if(rank[x]==rank[y]) rank[x]++;
        }
    }
    bool same(int x, int y){
        return find(x) == find(y);
    }
};

int main(){
    int N,M;
    cin>>N>>M;
    union_find UF(N);
    vector<int> A(N);
    for(auto &a:A)cin>>a;
    for(int i=0;i<M;i++){
        int x,y;
        cin>>x>>y;
        UF.unite(x,y);
    }
    vector<vector<int>> V(N);
    for(int i=0;i<N;i++){
        V[UF.find(i)].push_back(A[i]);
    }
    for(auto &v:V) sort(v.begin(), v.end());
    vector<int> VV;
    int T=0;
    int64_t ans=0;
    for(auto &v:V){
        if(!v.empty()){
            T++;
            ans+=v.front();
            for(int i=1;i<v.size();i++) VV.push_back(v[i]);
        }
    }
    sort(VV.begin(), VV.end());
    if(T==1){
        cout<<0<<endl;
        return 0;
    }
    if(VV.size()<T-2){
        cout<<"Impossible"<<endl;
        return 0;
    }
    for(int i=0;i<T-2;i++){
        ans+=VV[i];
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 6912
// Bismillaahirrahmaanirrahiim
/* Author	: Jehian Norman Saviero (@Reiva5) */
#include <bits/stdc++.h>

/* MACROS SAMPAH */
#define jehian using
#define mau namespace
#define libur std
#define cepet {ios_base::sync_with_stdio(0);cin.tie(0);}

jehian mau libur;
/* MACROS FOR TYPE */
typedef long l;
typedef long double ld;
typedef long long ll;
typedef unsigned long ul;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<l> vl;
typedef vector<ll> vll;
typedef pair<int, int> pi;
typedef pair<l,l> pl;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pld;

/* MACROS FOR PRINT */
#define nl printf("\n");
#define sp printf(" ");

/* MACROS FOR FUNCTION AND METHOD */
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define RESET(x) memset((x), 0, sizeof(x))
#define SET(x,y) memset((x), (y), sizeof(x))

/* MACROS FOR ATTRIBUTE */
#define fi first
#define se second

/* MACROS FOR SCAN */
#define scc(x) scanf("%c", &x)
#define scd(x) scanf("%Lf", &x)
#define scf(x) scanf("%f", &x)
#define sci(x) scanf("%d", &x)
#define scl(x) scanf("%ld", &x)
#define scll(x) scanf("%lld", &x)
#define scld(x) scanf("%Lf", &x)
#define scui(x) sci(x)
#define scul(x) scl(x)
#define scull(x) scll(x)

ll cnt = 0;
ll data[110];
ll N;
bool benar;

void dfs(ll idx){
	if (idx == N){
		benar = false;
		for (ll i = 0; i < N && !benar; ++i){
			benar = (data[i]%2 == 0);
		}
		cnt += benar;
	} else {
		data[idx] -= 1;
		dfs(idx+1);
		data[idx] += 1;
		dfs(idx+1);
		data[idx] += 1;
		dfs(idx+1);
		data[idx] -= 1;
	}
}

int main(){
	scll(N);
	for (ll i = 0; i < N; ++i){
		scll(data[i]);
	}
	dfs(0);
	cout << cnt << endl;
	return 0;
}

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

int main(){
  int n,ans=0,i1,i2;
  cin >> n;
  if (n<105) cout << 0 << endl;
  else {
  int num[n/2-51];
  for (i1=105;i1<=n;i1+=2){
    num[i1]=0;
    for (i2=1;i2<=i1;i2+=2){
      if (i1%i2==0) num[i1]++;
    }
    if (num[i1]==8) ans++;
  }
  cout << ans << endl;
  }
}

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

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

map<ll,ll> mp;

// 一回きりの素因数分解を行う．
void factorization_onetime(const ll &n){

    ll now = n;
    for(ll i = 2; i*i <= n; i++){
        ll times = 0;
        while(now%i==0){
            times++;
            now/=i;
        }
        if(times>=1){
            mp[i] += times;
        }
        if(now==1){
            break;
        }
    }
    if(now > 1){
        mp[now]++;
    }
    return;
}

int main(){
    ll n;
    cin >> n;
    if(n==1){
        cout << 0 << endl;
        return 0;
    }
    factorization_onetime(n);

    ll ans = 0;
    for(auto it:mp){
        ll t = it.second;
        // cout << t << endl;
        ll tmp = 1;
        while(t-tmp >= 0){
            ans++;
            t -= tmp;
            tmp ++;
        }
    }

    cout << ans << endl;


    return 0;
}


Measured memory_bytes: 3680
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")

#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <sstream>
#include <fstream>
#include <functional>
#include <array>
#include <climits>
//#include "debug.h"
using namespace std;
 
#define A first
#define B second
 
typedef long long ll;
constexpr int MOD = 1000000007;
 
template<typename T>
void print(const vector<T> &v, bool add_one = false, int start = -1, int end = -1) {
  if (start < 0) start = 0;
  if (end < 0) end = int(v.size());
 
  for (int i = start; i < end; i++)
    cout << v[i] + (add_one ? 1 : 0) << (i < end - 1 ? ' ' : '\n');
} 

void solve() {
  string S, T; cin >> S >> T;
  int ans = T.size();
  for(int i = 0; i <= (int)S.size() - T.size(); i++){
    int cnt = 0;
    for(int j = 0; j < T.size(); j++){
      if(S[i + j] == T[j]) cnt++;
    }
    ans = min(ans, (int)T.size() - cnt);
  }
  cout << ans;
}
 
int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int T;
  T = 1;
  //cin >> T;
  while(T--){
    solve();
  }
}

Measured memory_bytes: 3648
//NAME-DEEPESH BHATT
//Institute-IIT Dhanbad
//MOTTO-THINK FIRST,CODE LATER

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll MOD = 1000000007  ;
#define endl '\n'
#define pb push_back
#define mp make_pair

//Check for signed int overflow
// use __builtin_popcount(num) to calculate no.of setbits in num
//s.lower_bound() works in O(logn) whereas lower_bound(s.begin(),s.end(),x) works in O(n)
//      if(!lazy)
//         solve 1600+(rating) problems in codeforces.
//      else
//         solve 1400+(rating) problems in codeforces.

ll gcd(ll a,ll b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}
ll lcm(ll a ,ll b)
{
    return a/gcd(a,b)*b;
}
ll power(ll a,ll n){
    if (n == 0)
        return 1;
    ll p = power(a, n / 2);
    p = (p * p)%MOD;
    if (n % 2)
        return (p * a)%MOD;
    else
        return p%MOD;
}

/*vector<bool>is_prime(1000001,true);
void sieve(){
    is_prime[0]=is_prime[1]=false;
    is_prime[2]=true;
    for(int i=2;i*i<=1000000;i++){
        if(is_prime[i]){
            for(int j=i*i;j<=1000000;j+=i){
                is_prime[j]=false;
            }
        }
    }
}*/

int32_t main()
{
    ios_base::sync_with_stdio(false);    //for fast I/O operation
    cin.tie(nullptr);
    ll test=1;
    /*cin>>test;*/
    while(test--) {
        ll n;
        cin>>n;
        ll change=(1000-(n%1000))%1000;
        cout<<change<<endl;
    }
    return 0;
}

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

const int mod = 1'000'000'007;
struct mint {
  ll x;
  mint(ll x=0):x(x%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint a) const {
    mint res(*this);
    return res+=a;
  }
  mint operator-(const mint a) const {
    mint res(*this);
    return res-=a;
  }
  mint operator*(const mint a) const {
    mint res(*this);
    return res*=a;
  }
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
  
  mint inv() const {
    return pow(mod-2);
  }
  mint& operator/=(const mint a) {
    return (*this) *= a.inv();
  }
  mint operator/(const mint a) const {
    mint res(*this);
    return res/=a;
  }
};

mint d[100001];

int main() {
  int n, k;
  cin >> n >> k;
  mint sum = 0;
  for (int i=k; i>0; i--) {
    mint num = k/i;
    d[i] = num.pow(n);
    for (int j=2; j*i<=k; j++) {
      d[i] -= d[j*i];
    }
    sum += d[i] * i;
  }
  cout << sum.x << endl;
  return 0;
}


Measured memory_bytes: 4388
#include "bits/stdc++.h"

using namespace std;

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

	int ans = 0;

	for (int i = 0; i < 4; i++)
	{
		if (s[i] == '+') ans++;
		else ans--;
	}

	cout << ans << endl;

	return 0;
}

Measured memory_bytes: 256