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

int main() {
  int N, M, R;
  cin >> N >> M >> R;
  vector<int> r(R);
  for (auto& i : r) cin >> i, i--;
  vector<vector<int>> v(N, vector<int>(N, 1<<28));
  for (int i = 0; i < N; i++) v[i][i] = 0;
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    v[a][b] = v[b][a] = c;
  }

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      for (int k = 0; k < N; k++) {
	v[j][k] = min(v[j][k], v[j][i] + v[i][k]);
      }
    }
  }

  vector<vector<int>> dp(1 << R, vector<int>(R, 1<<28));
  for (int i = 1; i < R; i++) dp[0][i] = 0;
  for (int i = 0; i < (1<<R); i++) {
    for (int j = 0; j < R; j++) {
      if (i & (1<<j)) continue;
      for (int k = 0; k < R; k++) {
	if (i & (1<<k) == 0) continue;
	dp[i | (1<<j)][j] = min(dp[i | (1<<j)][j], dp[i][k] + v[r[k]][r[j]]);
      }
    }
  }

  int cost = 1<<28;
  for (int i = 0; i < R; i++) {
    cost = min(cost, dp[(1<<R)-1][i]);
  }

  cout << cost << endl;

  return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(ll n i=0; i<n; ++i)
#define rep1(i,n) for(ll n i=1; i<=n; ++i)
#define revrep(i,n) for(ll n i=n-1; n>=0; --i)
inline constexpr ll Inf = (1ULL << 62) -1;

template <class T>
void updatemax(T& a, T b) { if (b > a) a = b; }

int main() {
    ll ans=Inf;
    string s, t;
    cin >> s >> t;
    for (int i=0; i <= s.size()-t.size(); ++i) {
        ll d = 0;
        for (int j=0; j<t.size(); ++j) {
            if (t[j] != s[i+j])
                ++d;
        }
        ans = min(ans, d);
    }
    cout << ans << endl;
}


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

using namespace std;

int main(){

	int f, b, q, t, fg, tmp;
	string s;
	char c;
	bool ve=true;
	vector<char> v(800000);

	cin >> s;
	cin >> q;

	for (int i = 0;i < s.size();i++) {
		v[400000 + i] = s[i];
	}
	f = 400000 + s.size();
	b = 399999;

	for (int i = 0;i < q;i++) {
		cin >> t;
		if (t == 1) {
			if (ve) ve = false;
			else ve = true;
		}
		else {
			cin >> fg >> c;
			tmp = 0;
			if (ve) tmp++;
			if (fg == 1) tmp++;

			if (tmp % 2 == 0) {
				v[b] = c;
				b--;
			}
			else {
				v[f] = c;
				f++;
			}
		}
	}

	if (ve) {
		for (int i = b+1;i < f;i++) cout << v[i];
	}
	else {
		for (int i = f-1;i > b;i--) cout << v[i];
	}
	cout<< endl;


}


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

using ll = long long;
using Vi = vector<int>;
using Vl = vector<ll>;
using Pii = pair<int, int>;
using Pll = pair<ll, ll>;

constexpr int I_INF = numeric_limits<int>::max();
constexpr ll L_INF = numeric_limits<ll>::max();

//==================================

int main() {
#ifdef FASTIO
    cin.tie(0), cout.tie(0);
    ios::sync_with_stdio(false);
#endif
#ifdef FILEINPUT
    ifstream ifs("./in_out/input.txt");
    cin.rdbuf(ifs.rdbuf());
#endif

    ll N, M;
    cin >> N >> M;

    ll n_ac = 0;
    Vl aced(N);
    Vl n_wa(N);

    for (ll i = 0; i < M; i++) {
        ll p;
        string s;
        cin >> p >> s;
        --p;
        if (aced[p] == 0) {
            if (s == "AC") {
                ++n_ac;
                aced[p] = 1;
            }
            else {
                ++n_wa[p];
            }
        }
    }

    ll n_wa_total = 0;
    for (ll i = 0; i < N; i++) {
        if (aced[i] == 1) {
            n_wa_total += n_wa[i];
        }
    }

    cout << n_ac << " " << n_wa_total << endl;

    return 0;
}

Measured memory_bytes: 1792
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b,c,d,e,f,g,h;
    int i,j;
    cin>>a>>b;
    int ar[a];
    map<int,int>m;
    map<int,int>::iterator it;
    set<int>s;
    for(i=0;i<a;i++)
    {
        cin>>ar[i];
        m[ar[i]]++;

    }
    vector<int>v;
    c=m.size();
    for(it=m.begin();it!=m.end();it++)
    {
        v.push_back((*it).second);


    }
    sort(v.begin(),v.end());
    if(b>=c)
    {
        cout<<"0"<<endl;
    }
    else{
        int k=c-b;
        d=0;e=0;
        for(i=0;i<v.size();i++)
        {
            k-=1;
            d+=v[i];
            if(k<=0)
            {
                break;
            }
        }
        cout<<d<<endl;
    }

    m.clear();
    v.clear();
    return 0;


}


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

using namespace std;
int n,a[202020];
const int mod = 1e9+7;
long long sum = 0;
int main() {
scanf("%d",&n);
for (int i = 1;i <= n; i++) {
	scanf("%d",&a[i]);
	sum += a[i];
}
	sum %= mod;
	sum *= sum;
	sum %= mod;
	for (int i = 1;i <= n; i++) {
		sum -= (long long)a[i]*a[i];
		sum %= mod;
	}
	sum += mod;
	sum %= mod;
	sum *= (mod+1)/2;
	sum %= mod;
	printf("%lld\n",sum);
	return 0;
}
/*
5
6 14 15 7 12 16 5 4 11 9 3 10 8 2 13 1
4
1 2 3 4 5 6 7 8
*/

Measured memory_bytes: 4540
#include<stdio.h>
int main(){
  int n,d[100][100]={0};
  int i,j,max=-1000000000,k,l,p,q,s;
  scanf("%d",&n);
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      scanf("%d",&d[i][j]);
    }
  }
  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      for(k=0;i+k<n;k++){
	s=0;
	for(l=0;j+l<n;l++){ 
	  for(p=0;p<=k;p++){
	    s+=d[i+p][j+l];
	  }
	  if(max<s)max=s;
	}
      }
    }
  }
  printf("%d\n",max);
  return 0;
}
      

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

#define sync {ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);}
#define ll long long int
#define mod 1000000007
struct p{
    int a;
    int b;
};

int n;

void solve()
{
    cin>>n;
    
    vector<p> x(n);
    
    for(int i =0;i<n;i++)
        cin>>x[i].a>>x[i].b;
    
    
    for(int i = 0;i<n-2;i++)
    {
        if(x[i].a==x[i].b and x[i+1].a==x[i+1].b and x[i+2].a==x[i+2].b )
        {    cout<<"Yes";
            return;
        }
        
    }
    
    cout<<"No";
}

int main()
{
    sync;
    int t = 1;
    //cin>>t;
    while(t--){
        solve();
        cout<<"\n";
    }
    return 0;
}







Measured memory_bytes: 3648
n = int(input())
s_list = list(input() for _ in range(n))

alphabets = 'abcdefghijklmnopqrstuvwxyz'

ans = ""
for x in alphabets:
    min_count = float('inf')
    for s in s_list:
        count = s.count(x)
        min_count = min(min_count, count)
        if min_count == 0:
            break
    ans += x * min_count
print(ans)

Measured memory_bytes: 3060
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections

a,b,c = map(int,input().split())
ab = a + b
ac = a + c
bc = b + c
ans = min(ab,ac)
ans = min(ans,bc)
print(ans)

Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
#define int int64
#define rep(i, n) for(int i=0; i<n; i++)
#define FOR(i, a, b) for(int i=a; i<b; i++)
#define SORT(x) sort(x.begin(), x.end())
#define GSORT(x) sort(x.begin(), x.end(), greater<int>())
#define mk make_pair
#define fi first
#define se second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define V(T) vector<T>
typedef pair<int, int> P;
typedef pair<P, P> PP;
typedef vector<int> vi;
typedef vector<vi> vvi;
int max(int a, int b) {if(b>a) return b; else return a;}
int min(int a, int b) {if(b<a) return b; else return a;}

signed main() {
    int sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;

    //int x=sx, y=sy;
    //cout << sx << " " << sy << endl;
    string res = "";
    rep(i, tx-sx) res += 'R';
    rep(i, ty-sy) res += 'U';
    rep(i, tx-sx) res += 'L';
    rep(i, ty-sy) res += 'D';
    res += 'D';
    rep(i, tx-sx+1) res += 'R';
    rep(i, ty-sy+1) res += 'U';
    res += "LU";
    rep(i, tx-sx+1) res += 'L';
    rep(i, ty-sy+1) res += 'D';
    res += 'R';

    /*rep(i, res.size()) {
        switch(res[i]) {
            case 'U':
                y++;
                break;
            
            case 'D':
                y--;
                break;

            case 'R':
                x++;
                break;

            case 'L':
                x--;
                break;
        }
    }*/

    //cout << x << " " << y << endl;

    cout << res << endl;
 
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
#include<cmath>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb(in,tmp) in.push_back(tmp)
#define all(in) in.begin(),in.end()
#define PI acos(-1)
using namespace std;
int tile[100][200]={0};
int co=100;
void ride(int i,int j,int tile[100][200],int& count){
  int tmp=tile[i][2*j];
  tile[i][2*j]=co;
  if(tile[tile[i][2*j+1]][2*tmp]==co){count++;return;}
  else if(tile[tile[i][2*j+1]][2*tmp]>100&&tile[tile[i][2*j+1]][2*tmp]<co)return;
  else ride(tile[i][2*j+1],tmp,tile,count);

}
int main(){
  int n;
  while(cin>>n,n){
    int count=0;
    rep(i,n){
      rep(j,2*n){
	cin>>tile[i][j];
      }
    }
    rep(i,n){
      rep(j,n){
	co++;
	if(tile[i][2*j]>100)continue;
	ride(i,j,tile,count);
      }
    } 
    cout<<count<<endl;
  }

}

Measured memory_bytes: 1236
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1000000000000000000
#define MOD 1000000007
#define all(v) v.begin(),v.end()
#define pb push_back
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int kaijo[2000010];
struct edge{int to,cost;};
int gcd(int a,int b){
    if(b==0)return a;
    return gcd(b,a%b);
}
int lcm(int a,int b){
    return a/gcd(a,b)*b;
}
bool prime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
void init_fact(){
    kaijo[0]=1;
    for(int i=1;i<=2000000;i++){
        kaijo[i]=kaijo[i-1]*i;
        kaijo[i]%=MOD;
    }
}
int modpow(int a,int b){
    if(b==0)return 1;
    if(b%2)return modpow(a,b-1)*a%MOD;
    int memo=modpow(a,b/2);
    return memo*memo%MOD;
}
int comb(int a,int b){
    if(!kaijo[0])init_fact();
    return kaijo[a]*modpow(kaijo[a-b],MOD-2)%MOD*modpow(kaijo[b],MOD-2)%MOD;
}
int inv(int x){
    x=modpow(x,MOD-2);
    return x;
}
bool kosa(double ax,double ay,double bx,double by,double cx,double cy,double dx,double dy){
    double ta=(cx-dx)*(ay-cy)+(cy-dy)*(cx-ax);
    double tb=(cx-dx)*(by-cy)+(cy-dy)*(cx-bx);
    double tc=(ax-bx)*(cy-ay)+(ay-by)*(ax-cx);
    double td=(ax-bx)*(dy-ay)+(ay-by)*(ax-dx);
    return tc*td<0&&ta*tb<0;
}
double dist(double ax,double ay,double bx,double by){
    return sqrt((bx-ax)*(bx-ax)+(ay-by)*(ay-by));
}
int n,a,b;
signed main(){
    cin>>n>>a>>b;
    n%=12;
    rep(i,n){
        if(i%2==0)a=a-b;
        else b=a+b;
    }
    cout<<a<<" "<<b<<endl;
    return 0;
}



Measured memory_bytes: 3120
import java.util.*;
import java.io.*;
import java.math.*;
class Main {

	public static void main(String[] args) {
		FastScanner I=new FastScanner();
		Output O=new Output();
		String s=I.next();
		Stack<Character> cur=new Stack<Character>();
		Stack<Character> ans=new Stack<Character>();
		for(int i=0;i<s.length();i++) {
			if(s.charAt(i)=='1') cur.push('1');
			else if(s.charAt(i)=='0') cur.push('0');
			else if(s.charAt(i)=='B' && cur.size()>0) cur.pop();
		}
		while(!cur.empty()) {
			ans.add(cur.pop());
		}
		while(!ans.empty()) {
			O.p(ans.pop());
		}
		
		System.out.println();
	}
	static class FastScanner {
		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st=new StringTokenizer("");
		String next() {
			while (!st.hasMoreTokens())
				try {
					st=new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());};
	}

	static class Output{
		PrintWriter w=new PrintWriter(System.out);
		void pln(int x) {w.println(x); w.flush();}
		void pln(long x) {w.println(x); w.flush();}
		void pln(String x) {w.println(x); w.flush();}
		void pln(char x) {w.println(x); w.flush();}
		void pln(StringBuilder x) {w.println(x); w.flush();}
		void p(int x) {w.print(x);w.flush();}
		void p(long x) {w.print(x);w.flush();}
		void p(String x) {w.print(x);w.flush();}
		void p(char x) {w.print(x);w.flush();}
		void p(StringBuilder x) {w.print(x);w.flush();}
	}
		
}


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

int main() {
    int N;  cin >> N;
    set<int> s;
    for (int i = 0; i < N; ++i) {
        int a;  cin >> a;
        s.insert(a);
    }
    cout << (s.size() & 1 ? s.size() : s.size() - 1) << endl;
}


Measured memory_bytes: 4992
#include<iostream>
#include<vector>
#include<string>
#include<cstdio>

using namespace std;

int main(void){
  int w,h;
  cin>>w>>h;
  vector<vector<int> > maze(w,vector<int>(h+1));
  vector<vector<int> > dp(w,vector<int>(h+1,100000000));
  //input
  for(int i = 0 ; i < w ; i ++){
    for(int j = 0 ; j < h ; j ++){
      char c;
      cin>>c;
      maze[i][j] = c-'0';
      //cout<<maze[i][j]<<" ";
    }
    //cout<<endl;
  }
  dp[0][0] = maze[0][0];
  for(int i = 1 ; i < w ; i ++){
     dp[i][0] = maze[i][0] + dp[i-1][0];
  }

  for(int i = 1 ; i < h ; i ++){
     dp[0][i] = maze[0][i] + dp[0][i-1];
  }
 
  
  for(int i = 1 ; i < w ; i ++){
    dp[i][0] = dp[i-1][0]+maze[i][0];
    
    for(int j = 1 ; j < h ; j ++){
      dp[i][j] = min(dp[i-1][j] + maze[i][j],dp[i][j-1] + maze[i][j]);
    }
  }
  /*
  cout<<h<<" "<<w<<endl;
  for(int i =  0 ; i < w ; i ++){
    for(int j = 0 ; j < h ; j ++){
      printf("%6d",dp[i][j]);
    }
    cout<<endl;
  }
  //*/
  
  cout<<dp[w-1][h-1]<<endl;

}

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

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::bitset;

#define rep(i,n)    for(int i=0; i<(int)n; i++)
#define reps(i,s,e) for(int i=s; i<(int)e; i++)
#define coutd(i) cout << std::fixed << std::setprecision(i)
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;

template<class T>
using Set = std::set<T>;
template<class T, class U>
using Map = std::map<T, U>;

template<class T>
using Graph = vector<vector<T>>;

const int INT_BIG = 1<<28;

int main() {
    ll N;
    cin >> N;
    ll answer = 0;
    ll tmpN = N;
    for (ll i = 2; i*i <= tmpN; i++) {
        ll counter = 0;
        while(true) {
            if (N % i == 0) {
                N /= i;
                counter++;
            } else {
                break;
            }
        }
        // if (counter != 0) {
        //     cout << i << ":" << counter << endl;
        // }
        answer += std::floor(0.5 * (-1 + std::sqrt(1 + 8 * counter)));
    }
    if (answer == 0 && tmpN != 1) {
        cout << 1 << endl;
    } else if (N != 1) {
        cout << answer + 1 << endl;
    } else {
        cout << answer << endl;
    }
}

Measured memory_bytes: 3656
#include<bits/stdc++.h>
#pragma warning(disable:4996)
#ifdef _MSC_VER
#  define __builtin_popcount __builtin_popcountll
#  define __builtin_popcountll __popcnt
#endif
#define int long long
using namespace std;
using ll = long long;
using ld = long double;
const long double PI = acos(-1.0);
const int dx[4] = { 1,0,-1,0 }, dy[4] = { 0,1,0,-1 };
inline void my_io() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout << fixed << setprecision(10);
}
int n, m;
vector < pair<int,int>>v;
bool my_comp(const vector<int>& firstElof, vector<int>& secondElof) {
	return secondElof[2]<firstElof[2];
}
signed main() {
	my_io();
	cin >> n >> m;
	v.resize(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i].first >> v[i].second;
	}
	struct sort_pred {
		bool operator()(const std::pair<int, int> &left, const std::pair<int, int> &right) {
			return left.first < right.first;
		}
	};

	std::sort(v.begin(), v.end(), sort_pred());
	int ans = 0;
	int nokori = m;
	int i = 0;
	while (nokori >= 0&&i<=n-1) {
		if (nokori - v[i].second >= 0) {
			nokori -= v[i].second;
			ans += v[i].first*v[i].second;
			i++;
		}
		else {
			break;
		}
	}
	if (i <= n - 1) {
		ans += nokori * v[i].first;
	}
	cout << ans << endl;
}

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

int main(){
    string s;
    cin>>s;
    int cnt=0;
    for(int i=0;i<(int)s.length();i++){
        if(s[i]=='0') cnt++;
    }
    cnt=min(cnt,(int)s.length()-cnt);
    cout<<cnt*2<<endl;
}

Measured memory_bytes: 3748
#include<iostream>
using namespace std;
int main(){
  int n,m;
  int i,j,k;
  int mat[100][100]={{}};
  int ve[100]={};
  cin >> n >> m;
  for(i=0;i<n;i++){
    for(j=0;j<m;j++){
      cin >> mat[i][j];
    }
  }
  for(j=0;j<m;j++) cin >> ve[j];
  for(i=0;i<n;i++){
    int x=0;
    for(j=0;j<m;j++) x+=mat[i][j]*ve[j];
    cout << x << endl;		       
  }
  
  return 0;
}



Measured memory_bytes: 3148
n = int(input())
a = list(map(int, input().split()))
aa = [0] + a + [0]
d = [0]*(n+1)
for i in range(n+1):
  d[i] = abs(aa[i]-aa[i+1])
s = sum(d)
for i in range(n):
  print(s - d[i] - d[i+1] + abs(aa[i+2]-aa[i]))

Measured memory_bytes: 86400
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <queue>
#include <cmath>
using namespace std;

int main() {  
    long long int n, t;
    cin >> n >> t;
    long long int res = 0;
    long long int last = 0;
    for (int i = 0; i < n; i++) {
        long long int tt;
        cin >> tt;
        res += min(t, tt + t - last);
        last = tt + t;
    }
    cout << res << endl;
}




Measured memory_bytes: 3632
#include "bits/stdc++.h"
#define ll long long
#define fi first
#define se second
#define pb push_back
#define endl '\n'
#define all(v) (v).begin(), (v).end()
#define fastio ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

int main() {
    int n;
    cin >> n;
    vector<int> a(n+1), cnt(n+1, 0);
    for(int i = 2; i <= n; i++) {
        int x;
        cin >> x;
        cnt[x]++;
    }
    for(int i = 1; i <= n; i++) {
        cout << cnt[i] << endl;
    }

    return 0;
}


Measured memory_bytes: 4792
// package Contest1#177;

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        PrintWriter out = new PrintWriter(System.out);

        StringTokenizer st = new StringTokenizer(br.readLine());
        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        HashMap<Integer, Set<Integer>> map = new HashMap<>();
        for(int i=1; i<=n; i++){
            map.put(i, new HashSet<Integer>());
        }

        for(int i=0; i<m; i++){
            StringTokenizer pair = new StringTokenizer(br.readLine());
            int v1 = Integer.parseInt(pair.nextToken());
            int v2 = Integer.parseInt(pair.nextToken());

            map.get(v1).add(v2);
            map.get(v2).add(v1);
        }

        int max = 0;
        boolean[] vis = new boolean[n+1];

        for(int i=1; i<=n; i++){
            if(!vis[i]){
                max = Math.max(max, dfs(i, vis, map));
            }
        }

        System.out.println(max);
    }

    static int dfs(int i, boolean[] vis, HashMap<Integer, Set<Integer>> map){
        vis[i] = true;
        int size = 1;

        Set<Integer> fr = map.get(i);
        
        for(int next: fr){
            if(!vis[next]){
                size += dfs(next, vis, map);
            }
        }

        return size;
    }
}

Measured memory_bytes: 167740
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<cmath>
#include<utility>
#include<queue>
#include<set>
using namespace std;

int g[100000];

int dfs(int x){
    if(g[x]==x){
        return x;
    }
    else{
        return x=dfs(g[x]);
    }
}
int main(){
    int n,x,y;
    vector<pair<int,int> > a,b;
    int p=0;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        a.push_back({x,i});
    }
    for(int i=0;i<n;i++){
        scanf("%d",&x);
        b.push_back({x,i});
    }
    sort(a.begin(),a.end());
    sort(b.begin(),b.end());
    for(int i=0;i<n;i++){
        if(a[i].first>b[i].first){
            printf("No\n");
            return 0;
        }
    }
    p=0;
    for(int i=0;i<n;i++){
        if(i==p){
            while(p<n&&a[p].first<=b[i].first){
                p++;
            }
            for(int j=i;j<p;j++){
                a[j].first=b[i].first;
                b[j].first=b[i].first;
            }
        }
    }
    for(int i=0;i<n;i++){
        if(a[i].second==b[i].second){
            printf("Yes\n");
            return 0;
        }
    }
    for(int i=1;i<n;i++){
        if(a[i].first==a[i-1].first||b[i].first==b[i-1].first){
            printf("Yes\n");
            return 0;
        }
    }
    for(int i=0;i<n;i++){
        g[i]=i;
    }
    for(int i=0;i<n;i++){
        x=dfs(a[i].second);
        y=dfs(b[i].second);
        if(x!=y){
            g[max(x,y)]=g[min(x,y)];
        }
    }
    for(int i=0;i<n;i++){
        x=dfs(i);
        if(x){
            printf("Yes\n");
            return 0;
        }
    }
    printf("No\n");
}

Measured memory_bytes: 2548
n,k = map(int,input().split())
A = [int(i) for i in input().split()]

Visit = [False for i in range(n+1)]

loop_st,loop_end,loop_len = 0,0,0
st = 1
releki = []
loop_list = []
for i in range(10000000000000):
    if Visit[st] == True:
        loop_key = st
        loop_st = releki.index(loop_key)
        loop_end = len(releki)-loop_st
        loop_list = releki[loop_st:]
        loop_len = len(loop_list)
        break
    releki.append(st)
    Visit[st] = True
    st = A[st-1]
if k <loop_st:
    print(releki[k])
    exit()
k -= loop_st

print(loop_list[k%loop_len])

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

int main() {
  int a, b, k, c = 0;
  cin >> a >> b >> k;
  for (int i = min(a, b); i >= 1; --i) {
    if (a % i == 0 && b % i == 0) {
      if (++c == k) {
        cout << i << endl;
        return 0;
      }
    }
  }
  return 0;
}

Measured memory_bytes: 256
/*
Author : farhan132
*/
#include <bits/stdc++.h>
 
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> ii;
typedef pair<ll, int> li;
typedef pair<int, ll> il;
typedef pair<ll , ll> lli;
typedef vector<ii> vii;
typedef vector<il> vil;
typedef vector<li> vli;
typedef vector<lli> vlli;
typedef pair < ll , pair < ll , ll > > cm;

#define ff first
#define ss second
#define pb push_back
#define in insert
#define f0(b) for(int i=0;i<(b);i++)
#define f00(b) for(int j=0;j<(b);j++)
#define f1(b) for(int i=1;i<=(b);i++)
#define f11(b) for(int j=1;j<=(b);j++)
#define f2(a,b) for(int i=(a);i<=(b);i++)
#define f22(a,b) for(int j=(a);j<=(b);j++)
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld", &a , &b)
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n", a , b)
#define PI 3.14159265359
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clzll(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcountll(n)
#define clean cout.flush()

const ll mod = (ll)1e9+7;
const ll maxn = (ll)1e5+10;
const int nnn = 664579+500;
const int inf = numeric_limits<int>::max()-1;
//const ll INF = numeric_limits<ll>::max()-1;
const ll INF = 1e18;

ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dxx[]={0,1,0,-1,1,1,-1,-1};
ll dyy[]={1,0,-1,0,1,-1,1,-1};

//ofstream wr ("txt.txt");
//ifstream rr ("input.txt");


void solve(){
	
	ll k;
	cin >> k;
	ll num = 0;
	ll tot = 0;
	ll ten = 1;
	for(ll i = 1; i <= k; i++){
		tot += 7*(ten);
		ten *= 10;
		ten %= k;
		tot%=k;
		if(tot == 0){
			cout << i << '\n';
			return;
		}
	}
	cout << "-1\n";
	
 	return;
}

int main() {
   
   ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
   //freopen("input.txt", "r", stdin);
   //freopen("output.txt", "w", stdout);
    ll T;
    T=1;
    //cin >> T;
    //scanf("%lld",&T);
    ll CT = 0;
    
    //work();
    while(T--){
    	//cout << "Case #" << ++CT <<": " ;
    	//printf("Case %lld: ",++CT);
		solve();
	}
	
    return 0;
}
/*


*/

Measured memory_bytes: 3644
#include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;


int main(){

	int N,K,row,col,tmp;
	scanf("%d",&N);
	scanf("%d",&K);

	for(int loop = 0; loop < K; loop++){
		scanf("%d %d",&col,&row);
		col = min(col,N+1-col);
		row = min(row,N+1-row);
		tmp = min(col,row);
		if(tmp%3 == 1){
			printf("1\n");
		}else if(tmp%3 == 2){
			printf("2\n");
		}else{
			printf("3\n");
		}
	}

	return 0;
}

Measured memory_bytes: 2548
from sys import stdin

N, K = [int(x) for x in stdin.readline().rstrip().split()]
input_str = stdin.readline().rstrip()
if(K==1):
  str = input_str[0].lower()
else:
  str = input_str[0]
for i in range(1,N):
  if(i==K-1):
    str = str + input_str[i].lower()
  else:
    str = str + input_str[i]
print(str)

Measured memory_bytes: 3060
#include <iostream>
#include <string>
#include <queue>
#include <vector>
#include <algorithm>
#include <math.h>
#include <map>
#include <iomanip>
#include <queue>
#include <numeric>
#define repl(i, l, r) for (ll i = l; i < r; i++)
#define rep(i, n) repl(i, 0, n)

using namespace std;
using ll = long long;

int main() {

    ll N;

    cin >> N;

    vector<ll> A(N);


    rep(i, N) {
        cin >> A[i];
    }

    sort(A.begin(), A.end(), greater<>());

    ll ans = 0, count1 = 0, count2 = 0, i = 0;
    
    while (count1 != N - 2) {
        if (i == 0) {
            ans += A[i];
            i++;
        }
        if (i > 0) {
            if (count2 == 1) {
                ans += A[i];
                i++;
                count2 = 0;
            }
            else {
                ans += A[i];
                count2++;
            }
        }
        count1++;
    }

    if (N == 2) {
        ans = max(A[0], A[1]);
    }

    cout << ans;

    return 0;
}

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



int main(void){

    int N;
    cin>>N;
    string A[N];
    int i,j;
    rep(i,N){
        cin>>A[i];
    }
    int a=0;
    rep(i,N){
        if(i<(N-1)){
            if(A[i][A[i].size()-1]!=A[i+1][0]){//次の最初の言葉と同じ
                a=1;
                break;
            }
        }
        //同じ言葉を使ってる
        rep(j,i){
            if(A[j]==A[i]){
                a=1;
                break;
            }
        }

    }


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

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

typedef long long LL;
const int MK=105;
const int M=70005; // sqrt(1e9)*2
const int MO=1e9+7;
int cnt[M];
int dp[MK][M];

inline int add(int x,int y)
{
	x+=y;
	if(x>=MO) x-=MO;
	return x;
}
inline int mul(int x,int y)
{
	return 1LL*x*y%MO;
}

int main()
{
	int n,k;
	cin>>n>>k;
	int l,r,m=0;
	for(int i=1;i<=n;)
	{
		l=i;
		r=n/(n/i);
		//cout<<l<<" "<<r<<endl;
		cnt[++m]=(r-l+1);
		i=r+1;
	}
	dp[0][0]=1;
	for(int i=1;i<=k;i++)
	{
		int sum=dp[i-1][0];
		for(int j=m;j>=1;j--)
		{
			sum=add(sum,dp[i-1][m+1-j]);
			dp[i][j]=mul(sum,cnt[j]);
		}
	}
	int ans=0;
	for(int i=1;i<=m;i++)
		ans=add(ans,dp[k][i]);
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 28800
#pragma target("avx")
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> P;
typedef vector<ll> V;
typedef unordered_map<ll, ll> U_MAP;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> rpq;
const int INF = 1e9, MOD = 1e9 + 7, ohara = 1e6 + 10;
const ll LINF = 1e18;

#define rep(i, n) for (ll(i) = 0; (i) < (int)(n); (i)++)
#define rrep(i, a, b) for (ll i = (a); i < (b); i++)
#define rrrep(i, a, b) for (ll i = (a); i >= (b); i--)
#define all(v) (v).begin(), (v).end()
#define Size(n) (n).size()
#define Cout(x) cout << (x) << endl
#define doublecout(a) cout << fixed << setprecision(15) << a << endl;
#define fi first
#define se second
#define m_p make_pair
#define p_b push_back
string to_string(string s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

//------ Believe yourself as a genius!!!!!! ------

int dy[] = {1, 0, -1, 0};
int dx[] = {0, 1, 0, -1};
// int dy[]={-1,0,1,-1,1,-1,0,1};int dx[]={-1,-1,-1,0,0,1,1,1};
string alph("abcdefghijklmnopqrstuvwxyz"), s[ohara], co;
ll n, cnt, ans, a[ohara], b, c, d, tmp, m, h, w, x, y, sum, k, q;

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

  cin >> n;
  rep(i, n) cin >> s[i] >> a[i];
  cin >> co;
  rep(i, n) {
    if (s[i] == co) {
      rrep(j, i + 1, n) ans += a[j];
      break;
    }
  }
  Cout(ans);
  return 0;
}

Measured memory_bytes: 9728
#include<iostream>
#include<string>
#include<vector>
#include<utility>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<map>
#include<complex>
#define INF 2147483647
#define llINF 9223372036854775807
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long

using namespace std;
int main(){
  string s;cin>>s;
  int mi=INF;
  for(int i=0;i<s.size()-1;i++){
    if(s[i]!=s[i+1])
      mi=min(mi,max(i+1,(int)s.size()-i-1));
  }
  if(mi==INF)
    if(s[0]=='0')
      cout<<s.size()<<endl;
    else
      cout<<s.size()<<endl;
  else
    cout<<mi<<endl;
  return 0;
}


Measured memory_bytes: 512
s = input()
l = []
n = len(s)
mod = 2019
s = list(s)
s = list(map(int, s))
l.append(s[-1])
for i in range(n-2, -1, -1):
    tmp = l[-1]
    l.append((tmp+s[i] * pow(10, n - i - 1, 2019))%2019)
cnt = [0 for i in range(2019)]
cnt[0] += 1
for i in range(len(l)):
    cnt[l[i]] += 1
ans = 0
for i in range(len(cnt)):
    ans += cnt[i]*(cnt[i]-1)//2
print(ans)

Measured memory_bytes: 100156
#include <iostream>
#include <cstring>
#include <string>    
#include <algorithm>
#include <cmath>
#include <numeric>
using namespace std;
typedef long long ll;
ll n, m, a, can[10], buf[10], out[10], dp[10010][10];
int need[] = {0, 2, 5, 5, 4, 5, 6, 3, 7, 6};

ll SUM(ll l[]){
	ll r = 0;
	for(ll i = 1; i <= 9; i++){r += l[i];}
	return r;
}

bool compare(ll big[], ll small[]){
	if(SUM(big) > SUM(small)){return true;}
	if(SUM(big) < SUM(small)){return false;}
	if(SUM(big) == SUM(small)){
		for(ll i = 9; i >= 1; i--){
			if(big[i] > small[i]){return true;}
			if(big[i] < small[i]){return false;}
		}
	}
	return false;
}

int main(){
	cin >> n >> m;
	memset(can, 0, sizeof(can));
	for(ll i = 1; i <= m; i++){
		cin >> a;
		if(a > can[need[a]]){can[need[a]] = a;}
	}
	//for(ll i = 0; i <= 9; i++){cout << can[i];}cout << endl;
	
	memset(dp, 0, sizeof(dp));
	dp[0][0] = 1;
	for(ll i = 0; i <= n; i++){
		if(dp[i][0] == 1){
			for(ll j = 2; j <= 7; j++){
				if(can[j] != 0){
					for(ll k = 0; k <= 9; k++){buf[k] = dp[i][k];}
					buf[can[j]]++;
					if(compare(buf, dp[i+j])){
						for(ll k = 0; k <= 9; k++){dp[i+j][k] = buf[k];}
					}
				}
			}
		}
	}
	for(ll i = 9; i >= 1; i--){
		for(ll j = 1; j <= dp[n][i]; j++){
			cout << i;
		}
	}
	cout << endl;
	return 0;
}

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


int main(){
    int n, k;
    cin >> n >> k;
    vector<int> v(n);
    for(int i=0; i<n; i++) cin >> v[i];

    int ans = 0;

    for(int h=0; h<n; h++){
        vector<int> u;
        for(int i=0; i<h; i++) u.push_back(v[i]);
        for(int t=n; t>=0; t--){
            if((t == n && h < k) || h + (n-t-1) < k && h <= t){
                if(t != n) u.push_back(v[t]);
                int sum = 0;
                if(u.size() != 0){
                    for(int x:u) sum += x;
                    sort(u.begin(), u.end());
                    for(int i=0; i<(k - (int)u.size()); i++) if(i < (int)u.size() && u[i] < 0) sum -= u[i];
                }
                ans = max(ans, sum);
            }
        }
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
import heapq

n = int(input())
a = [0] + list(map(int, input().split())) #index調整
selectleft, selectright= [], []
leftscore, rightscore = [], []
for i in range(1,n+1): heapq.heappush(selectleft, a[i]) # selectleftからは最小値を取り出す
leftscore.append(sum(selectleft)) # 左のn個中n個選ぶとき
for i in range(n+1,2*n+1): # n+1~n+n個の中から選ぶとき
    heapq.heappush(selectleft, a[i])
    tmpscore = leftscore[-1] + a[i]
    poped = heapq.heappop(selectleft)
    tmpscore -= poped
    leftscore.append(tmpscore)
for i in range(2*n+1,3*n+1):
    heapq.heappush(selectright, (-1)*a[i]) # selectrightからは最大値を取り出す
rightscore.append(sum(selectright)*(-1))
for i in range(2*n,n,-1):
    heapq.heappush(selectright, (-1)*a[i])
    tmpscore = rightscore[-1] + a[i]
    poped = heapq.heappop(selectright) * (-1)
    tmpscore -= poped
    rightscore.append(tmpscore)
rightscore.reverse()
print(max(map(lambda x,y:x-y, leftscore, rightscore)))

Measured memory_bytes: 37212
//  cout << char(i+48) << endl;//なぜかaは47と得る時がある。+48で出力もaにできる。
//  cout << char(97) << endl;//アスキーコードでaを出力
// sort(b.begin(), b.end());//bという配列を小さい方からソート
// reverse(b.begin(), b.end());//bという配列をリターン
// 10桁でdouble出力するパターン１ 
// printf("%.10f\n", ma);
// 10桁でdouble出力するパターン２
//cout << fixed << setprecision(10);
//  cout << ma << endl;

/*01 02 03 12 13 23　と６回見ていくパターン 
for(int i=0;i<n-1;i++){
  for(int j=i+1;j<n;j++){
    }
  }
*///vector<vector<int>> data(3, vector<int>(4));//int型の2次元配列(3×4要素の)の宣言
//int64_t a;
//10のi乗pow(10, i);
/*string s; stringでの文字列を数字型に変える方法
  cin >> s;
  rep(i,s.size()-2) {
  int a= (s.at(i)-'0')*100 + (s.at(i+1)-'0')*10+ s.at(i+2) -'0';
    */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main() {
  vector<int>a(5);
  rep(i,5) cin >> a.at(i);
  rep(i,5) if(a.at(i)==0) cout<<i+1<<endl;
}

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


void solve(long long N, std::vector<long long> A){
    long long prev = A[0];
    long long result = A[0];
    if (find(A.begin(), A.end(), 0) != A.end()) {
            cout << 0 << endl;
            return;
    }
    for (int i = 1; i < N; i++) {
        result *= A[i];
        if (prev > result && A[i] != 0) {
            cout << -1 << endl;
            return;
        }
        if (result > 1000000000000000000LL) {
            cout << -1 << endl;
            return;
        }
        prev = result;
    }
    cout << result << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    long long N;
    scanf("%lld",&N);
    std::vector<long long> A(N);
    for(int i = 0 ; i < N ; i++){
        scanf("%lld",&A[i]);
    }
    solve(N, std::move(A));
    return 0;
}


Measured memory_bytes: 4072
#include<iostream>
#include<string>
using namespace std;
 
int main(){
    string N;
    int K;
    cin >> N;
    cin >> K;
    
    int n[N.size()];
    long ones[101][2][101]={}; // #digit, smaller flag, #nonzero
 
    long ans;
 
    for(int i=0; i<N.size(); i++){
        n[i] = (int) N[i] - '0';
        //cout << n[i] << endl;
    }
 
    ones[0][0][0] = 1;

    for(int i=0; i<N.size(); i++){
        for(int smaller=0; smaller<2; smaller++){
            for(int j=0; j<=K; j++){
                for(int x=0; x<= (smaller ? 9 : n[i]); x++){
                    if( x==0 ){
                        ones[i+1][ smaller | (x<n[i]) ][j] += ones[i][smaller][j];
                    }else{
                        ones[i+1][ smaller | (x<n[i]) ][j+1] += ones[i][smaller][j];                        
                    }
 
                }
                //cout << "total: ones[ " << i+1 << " ][ " <<  smaller  << " ][ " << j << " ] = " << ones[i+1][ smaller ][j] << endl; 
            }
        }
    }
 
    ans += ones[N.size()][0][K] + ones[N.size()][1][K];
 
    cout << ans << endl;
 
    return 0;
}

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

int main(void){
  long long int ans = 1;
  int n;
  while(cin>>n,n){
  ans = 1;
  int t , f;
    t = f = 0;
  for(int i = 1 ; i <= n ; i ++){
    int use = i;
    bool how = false;
    while(use%2==0){
      use/=2;
      t++;
      how = true;
    }
    while(use%5==0){
      use/=5;
      f++;
      how = true;
    }
    /*
    if(i%2==0){t++;
    }
    if(i%5==0){f++;
    }
    //cout<<f<<" "<<t<<" "<<i<<endl;
    */
  }
  cout<<min(t,f)<<endl;
  }
}

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


void f(int t,int d,int l){
  int ans=0,c=0;

  while(t--){
    int x;
    cin>>x;
    if(t==0) break;
    if(x>=l){
      c=d;
    }else{
      c=max(0,c-1);
    }
    if(c>0){
      ans++;
    }
  }

  cout<<ans<<endl;
}


main(){
  int t,d,l;
  while(true){
    cin>>t>>d>>l;
    if(t==0) break;
    f(t,d,l);
  }

}



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


int main() {

    string s;
    cin >> s;
    for (int i = 0; i < s.size(); i += 2) {
        cout << s[i];
    }
    cout << endl;
    return 0;
}

Measured memory_bytes: 512
#include <iostream>
#include <climits>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <deque>
#define INF_INT (INT_MAX / 2)
#define INF_LONG (LONG_MAX / 2)
//#define DEBUG true
#define DEBUG false
using namespace std;

const int MAX = 100001;
const int MOD = 1000000007;

typedef long long ll;
typedef pair< int, int > pii;
typedef pair< ll, ll > pll;
typedef pair< ll, int > pli;
typedef pair< int, ll > pil;

int ceil(int x, int y){
  return (x % y == 0) ? x / y : x / y + 1;
}

int gcd(int x, int y){
  return y ? gcd(y, x % y) : x;
}

int lcm(int x, int y){
  return x / gcd(x, y) * y;
}

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

  int x, a;
  cin >> x >> a;

  if(x < a) cout << "0" << endl;
  else cout << "10" << endl;

  return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
int main(void)
{
int n,a[55]={0},i,j,sum=0;
scanf("%d",&n);
  for(i=0;i<n;i++)
  {
  scanf("%d",&a[i]);
  }
  for(i=0;i<n;i++)
  {
  for(j=i+1;j<n;j++)
    sum+=a[i]*a[j];
  }
  printf("%d",sum);
 
return 0;
}

Measured memory_bytes: 128
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100010;
int head[maxn],vis[maxn],n,tot,b[maxn],cnt;
struct Edge{
    int to,next;
}edge[maxn*2];
void add(int x,int y){
    edge[++tot].to=y;
    edge[tot].next=head[x];
    head[x]=tot;
}
#define p edge[i].to
void dfs(int x,int fa){
    if(x==n){
        vis[x]=1;b[++cnt]=x;return;}
    for(int i=head[x];i;i=edge[i].next){
        if(p==fa)continue;
        dfs(p,x);
        if(vis[p]){
            vis[x]=1;
            b[++cnt]=x;
            return;
        }
    }
}
void getflag(int x){
    for(int i=head[x];i;i=edge[i].next){
        if(!vis[p]){
            vis[p]=vis[x];
            getflag(p);
        }
    }
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<n;i++){
        int x,y;
        scanf("%d%d",&x,&y);
        add(x,y);
        add(y,x);
    }
    dfs(1,0);
    vis[n]=2;vis[1]=1;
    int temp1=2,temp2=cnt-1,now=1;
    while(temp1<=temp2){
        if(now==1){
            vis[b[temp2]]=now;
            temp2--;
        }
        else vis[b[temp1]]=now,temp1++;
        now=3-now;
    }
    for(int i=1;i<=cnt;i++)getflag(b[i]);
    int cnt1=0,cnt2=0;
    for(int i=1;i<=n;i++)if(vis[i]==1)cnt1++;
    else cnt2++;
    if(cnt1<=cnt2)printf("Snuke");
    else printf("Fennec");
    return 0;
}

Measured memory_bytes: 7680
#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<string> s(n);
    long long ans=0;
    long long suc=1;

    for(int i=0;i<n;i++){
        cin>>s[i];
        sort(s[i].begin(),s[i].end());
    }
    sort(s.begin(),s.end());
    for(int i=0;i<n-1;i++){
        if(s[i]==s[i+1]){
            suc++;
        }
        else{
            ans+=suc*(suc-1)/2;
            suc=1;
        }
    }
    if(s[n-2]==s[n-1]){
        ans+=suc*(suc-1)/2;
    }
    cout<<ans<<endl;
}

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

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

  sort(x, x + a);
  for (int i = 0; i < a - 1; i++) sum += x[i];
  if (x[a - 1] < sum) cout << "Yes" << endl;
  else cout << "No" << endl;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
	int a,b;
	bool first = true;
	while(cin >> a >> b){
		bool flag = false;
		if(a==0 && b==0) break;
		if(!first) cout << endl;
		else first = false;
		for(int i=a; i<=b; i++){
			if(i%4==0 && ( i%100!=0 || i%400==0) ){
				cout << i << endl;
				flag = true;
			}
		}
		if(flag == false) cout << "NA" << endl;
	}
	return 0;
}

Measured memory_bytes: 0
#include<bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = [](){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0); return 0;
}();


int main() {
    int n, a, b;
    cin >> n;
    unordered_map<int, vector<int>> e;
    vector<int> color(n+1, 0);
    for(int i = 0; i < n-1; i++) {
        cin >> a >> b;
        e[a].push_back(b);
        e[b].push_back(a);
    }
    color[1] = 1;
    color[n] = -1;
    vector<int> dq = {1, n};
    while(!dq.empty()) {
        vector<int> new_dq;
        for(int node: dq) {
            for (int neibor: e[node]) {
                if (color[neibor] == 0) {
                    color[neibor] = color[node];
                    new_dq.push_back(neibor);
                }
            }
        }
        dq.swap(new_dq);
    }
    int pos = 0, neg = 0;
    for(int i = 1; i <= n; i++) {
        if (color[i] == 1)
            pos += 1;
        else if (color[i] == -1)
            neg += 1;
    }
    if (neg >= pos)
        cout << "Snuke" << endl;
    else
        cout << "Fennec" << endl;

    return 0;
}




Measured memory_bytes: 9636
#include <bits/stdc++.h>
#define LL long long
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
using namespace std;

int main(){
	int n,mn=1e9,mx=0,x;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>x;
		mn = min(mn, x);
		mx = max(mx, x);
	}
	cout<<mx-mn<<endl;
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main(){
  int N,X;cin>>N>>X;
  vector<int> a(N+1);
  a.at(0)=0;
  int L,count=0;
  for(int i=0;i<N;i++){
    cin>>L;
    a.at(i+1)=a.at(i)+L;
  }
  for(int i=0;i<N+1;i++){
    if(a.at(i)<=X)count++;
    else break;
  }
  cout<<count<<endl;
}

Measured memory_bytes: 256
#include <stack>
#include <string>
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    stack<int> D;
    
    string S;
    
    while (cin >> S)
    {
        if (S == "+" || S == "-" || S == "*")
        {
            int B = D.top();
            
            D.pop();
            
            int A = D.top();
            
            D.pop();
            
            if (S == "+")
            {
                D.push(A + B);
            }
            if (S == "-")
            {
                D.push(A - B);
            }
            if (S == "*")
            {
                D.push(A * B);
            }
        }
        else
        {
            D.push(atoi(S.c_str()));
        }
    }
    
    cout << D.top() << endl;
    
    return 0;
}



Measured memory_bytes: 3220
import java.util.Scanner;
import java.awt.Point;
public class Main{
	static boolean r;
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		Point[] g=new Point[m];
		long[] ans=new long[m];
		ans[m-1]=(long)n*(n-1)/2;
		for(int i = 0; i<m; i++){
			int a = sc.nextInt()-1;
			int b = sc.nextInt()-1;
			g[i]=new Point(a,b);
		}
		Unionfind uf=new Unionfind(n);
		for(int i=m-1; i>=0; i--){
			if(i!=m-1){
				if(!r){
					ans[i]=ans[i+1]-uf.size(g[i+1].x);
				}else{
					ans[i]=ans[i+1];
				}
			}
			r=false;
			uf.unite(g[i].x,g[i].y);
		}
		for(int i = 0; i<m; i++){
			System.out.println(ans[i]);
		}
	}
	static class Unionfind{
		int[] parent;
		long[] ans;
		long[] size;
		public Unionfind(int n){
			this.parent=new int[n];
			this.size=new long[n];
			this.ans=new long[n];
			for(int i = 0; i<n; i++){
				parent[i]=i;
				size[i]=1;
			}
		}
		public int root(int t){
			if(parent[t]==t){
				return t;
			}else{
				return parent[t]=root(parent[t]);
			}
		}
		public void unite(int x,int y){
			if(same(x,y)){
				r=true;
				return;
			}
			int xroot = root(x);
			int yroot = root(y);
			parent[yroot]=xroot;
			ans[xroot]=size[yroot]*size[xroot];
			size[xroot]+=size[yroot];
		}
		public boolean same(int x,int y){
			return root(x)==root(y);
		}
		public long size(int x){
			return ans[root(x)];
		}
	}
}


Measured memory_bytes: 98268
#include <bits/stdc++.h>
using namespace std;
int dp[110000],n,m;
vector <int> edge[110000];
int rec(int cur)
{
	if(dp[cur]>=0)
		return dp[cur];
	if(edge[cur].size()==0)
		return dp[cur]=0;
	for(int i=0;i<edge[cur].size();i++)
	{
		int to=edge[cur][i];
		dp[cur]=max(dp[cur],rec(to)+1);
	}
	return dp[cur];
}
int ans;
int main()
{
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int x,y;
		cin>>x>>y;
		edge[x].push_back(y);
	}
	fill(dp,dp+n+1,-1);
	for(int i=1;i<=n;i++)
	{
		rec(i);
		ans=max(ans,dp[i]);
	}
	cout<<ans<<"\n";
	return 0;
}

Measured memory_bytes: 8320
import sys

input = sys.stdin.readline


def read_values():
    return map(int, input().split())


def read_list():
    return list(read_values())


def func(N, mod):
    F = [1]
    for i in range(1, N + 1):
        F.append(F[-1] * i % mod)
    return F


INV = {}


def inv(a, mod):
    if a in INV:
        return INV[a]
    r = pow(a, mod - 2, mod)
    INV[a] = r
    return r


def C(F, a, b, mod):
    return F[a] * inv(F[b], mod) * inv(F[a - b], mod) % mod


def main():
    mod = 10 ** 9 + 7
    N, M = read_values()
    X = read_list()
    Y = read_list()

    res_x = 0
    for i in range(N - 1):
        res_x += (i + 1) * (N - i - 1) * (X[i + 1] - X[i]) % mod
        res_x %= mod

    res_y = 0
    for i in range(M - 1):
        res_y += (i + 1) * (M - i - 1) * (Y[i + 1] - Y[i]) % mod
        res_y %= mod
    print(res_x * res_y % mod)


if __name__ == "__main__":
    main()



Measured memory_bytes: 19308