akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <algorithm>
#include <array>
#include <math.h>
#include <set>
#include <stdlib.h>
#include <string>
#include <vector>

#define INT_MAX 2000000000
#define MOD 1000000007
#define ll long long
#define rep(i,a,b) for(i = (a); i < (b); i++)
#define bitget(a,b) (((a) >> (b)) & 1)

using namespace std;

int i, j, k;

int main() {
	int h, w;
	cin >> h >> w;
	char a[100][101] = {};
	rep(i, 0, h) {
		cin >> a[i];
	}
	int b[10000];
	rep(i, 0, h) {
		rep(j, 0, w) {
			b[i*w + j] = a[i][j];
		}
	}
	sort(b, b + h*w);
	int c[5] = {};
	int d = 1;
	rep(i, 1, h*w) {
		if (b[i] != b[i - 1]) {
			c[d]++;
			d = 1;
		}
		else if(d==4){
			c[d]++;
			d = 1;
		}
		else {
			d++;
		}
	}
	c[d]++;
	int ans = 1;
	if (h % 2 == 1) {
		if (w % 2 == 1) {
			if (c[1] + c[3] > 1) {
				ans = 0;
			}
			else if (c[2] + c[3] > h / 2 + w / 2) {
				ans = 0;
			}
		}
		else {
			if (c[1] + c[3] > 1) {
				ans = 0;
			}
			else if (c[2] + c[3] > w / 2) {
				ans = 0;
			}
		}
	}
	else {
		if (w % 2 == 1) {
			if (c[1] + c[3] > 1) {
				ans = 0;
			}
			else if (c[2] + c[3] > h / 2) {
				ans = 0;
			}
		}
		else {
			if (c[1] + c[3] > 0) {
				ans = 0;
			}
			else if (c[2] + c[3] > 0) {
				ans = 0;
			}
		}
	}
	if (ans == 0) {
		cout << "No" << endl;
	}
	else {
		cout << "Yes" << endl;
	}

	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<typeinfo>
#include<string>

using namespace std;

// char transStr(){

// }

int main(){
  string s;
  string ans;
  getline(cin, s);
  for(string::size_type i = 0; i < s.length(); i++){
    if(s[i] >= 65 && s[i] <= 90){
      s[i] += 32;
    }else if(s[i] >= 97 && s[i] <= 122){
      s[i] -= 32;
    }
  }
  cout << s << endl;
  return 0;
}



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

int main() {
  int A,B;
  cin >> A >> B;
  int ans = 0;

  for (int N = A; N < B+1; N++) {
    int x;
    if (N / 10000 != N % 10)
      continue;
    int M, L;
    M = (N % 10000) / 1000;
    L = N % 100 / 10;
    if (M == L)
      ans ++ ;
    
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
//---------------------------
using namespace std;
//---------------------------
#define REP(i,n) for(int i = 0; i < (n); i++)
#define P(x) cout << (x) << "\n"

#define MOD 1e9+7
#define PI acos(-1.0)
#define ll long long
#define INF 1000000001

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

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

  // ifstream in("input.txt");
  // cin.rdbuf(in.rdbuf());

  int N;cin>>N;
  ll a=2;
  ll b=1;
  if(N==1){
    P(b);
    return 0;
  }
  ll c;
  for(int i=2;i<=N;i++){
    c=b+a;
    a=b;
    b=c;
  }
  P(c);
  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <math.h>
#include <algorithm>
using namespace std;
typedef int ll;

vector<ll> d(101,1);

void zz(int a,int b){
  if(a>0){
    if(a%b==0){
      d[b]++;
      a/=b;
      zz(a,b);
    }
  }
}

int main(){
  ll n;
  cin>>n;
  vector<int> res;
  vector<bool> is_prime(n + 1, true);
  for (int p = 2; p <= n; p++) {
      if (!is_prime[p]) continue;
      // p の倍数を全て消す。
      for (int k = p * 2; k <= n; k += p) is_prime[k] = false;
      res.push_back(p);
  }
  vector<vector<ll> > dp(n+1,vector<ll>(0,0));
  vector<ll> dpp(101,0);
  for(int i=2;i<=n;i++){
    for(int j=2;j<=i;j++){
      if(is_prime[j]){
        zz(i,j);
      }
    }
  }
  for(int i=1;i<=n;i++){
    if(d[i]>=75) dpp[75]++;
    if(d[i]>=25) dpp[25]++;
    if(d[i]>=15) dpp[15]++;
    if(d[i]>=5) dpp[5]++;
    if(d[i]>=3) dpp[3]++;
  }
  ll ans=dpp[75];
  ans+=(dpp[25]*(dpp[3]-1));
  ans+=(dpp[15]*(dpp[5]-1));
  if(dpp[5]>1) ans+=(dpp[5]*(dpp[5]-1)/2*(dpp[3]-2));
  cout<<ans<<endl;
}


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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))

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

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}
template<class S, class T>
ostream& operator<<(ostream& os, const pair<S,T>& p){
  return os << p.FF << " " << p.SS;
}
template<class T>
void maxi(T& x, T y){
  if(x < y) x = y;
}
template<class T>
void mini(T& x, T y){
  if(x > y) x = y;
}


const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

LL C[51][51];

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

  REP(i,51){
	C[i][0] = C[i][i] = 1;
	FOR(j,1,i)
	  C[i][j] = C[i-1][j] + C[i-1][j-1];
  }

  LL N, A, B;
  cin >> N >> A >> B;
  VL xs(N);
  REP(i,N) cin >> xs[i];
  RSORT(xs);
  cout << fixed << setprecision(9)
	   << accumulate(begin(xs), begin(xs)+A, 0ll) * 1. / A << endl;

  LL tot = 0;
  REP(i,N)
	if(xs[i] == xs[A-1])
	  ++tot;
  LL c = 0;
  REP(i,A)
	if(xs[i] == xs[A-1])
	  ++c;

  LL ans = 0;
  if(xs[0] == xs[A-1]){
	FOR(i,A,B+1)
	  ans += C[tot][i];
  }
  else{
	ans += C[tot][c];
  }
  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
import itertools
N=int(input())
point=[]
for _ in range(N):
    x,y=map(int,input().split())
    point.append([x,y])
nums=[i for i in range(1,N+1)]
perm=list(itertools.permutations(nums,N))
dist=0

for pattern in perm:
    tmp_dist=0
    for i in range(N-1):
        tmp_dist+=((point[pattern[i]-1][0]-point[pattern[i+1]-1][0])**2+(point[pattern[i]-1][1]-point[pattern[i+1]-1][1])**2)**0.5
    dist+=tmp_dist
print(dist/len(perm))
      
        
        

Measured memory_bytes: 8052
#include <bits/stdc++.h>
using namespace std;
const long long int maxn = 1e6+ 10;
long long n;
long long a[maxn];
long long maxx[maxn];
int main() {
	while(~scanf("%lld",&n)) {
		long long maxt = 0, sum = 0;
		for(int i = 1; i <= n; i++) {
			scanf("%lld",&a[i]);
			maxt = max(maxt, a[i]);
			maxx[i] = maxt;
			sum += (maxx[i] - a[i]);
		} 
		cout << sum << endl;
	}
	return 0;
} 

Measured memory_bytes: 6888
import java.io.PrintStream;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = null;
		PrintStream out = null;
		try {
			sc = new Scanner(System.in);
			out = System.out;
			exec(sc, out);
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		} finally {
			if(out != null) {
				out.close();
			}
			if(sc != null) {
				sc.close();
			}
		}
		System.exit(0);
	}
	public static void exec(final Scanner sc, final PrintStream out) throws Exception{
		final long S = sc.nextLong();
		final long M = 1000000000L;
		if(S == M*M) {
			out.print("0 0 "+M+" 0 0 "+M);
			return;
		}

		long x = M - S%M;
		long y = S/M + 1;
		out.print("0 0 "+M+" 1 "+x+ " " +y);
	}
}


Measured memory_bytes: 21844
N, X, Y = map(int, input().split())

ans = [0 for i in range(N-1)]
for i in range(1, N):
    for j in range(i+1, N+1):
        l = min(j-i, abs((j-Y))+1+abs((X-i)), abs((i-X))+1+abs((Y-j)))
        ans[l-1] += 1

for i in range(N-1):
    print(ans[i])

Measured memory_bytes: 3444
#include<cstdio>
#include<algorithm> 
#include<cstring>
#define ll long long
using namespace std;
const int inf=0x3f3f3f3f;
int n,base,a[100005];
ll ans;
int read()
{
	int x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
	while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
	return x*f;
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	sort(a+1,a+n+1);
	for(int i=n-1;i>=1;i--)
	{
		if(a[i]==a[i+1])
		{
			if(!base)base=i;
			else if(i+1!=base)ans=1ll*a[i]*a[base];
		}
		if(ans)break;
	}
	printf("%lld",ans);
	return 0;
}

Measured memory_bytes: 512
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <string>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (a); i > (b); i--)
#define ALL(a) (a).begin(), (a).end()

ll cnt[10000001];

int main() {
    ll N;
    cin >> N;
    FOR(i, 1, N + 1) {
        for (int j = i; j <= N; j += i) cnt[j]++;
    }
    ll ans = 0;
    FOR(i, 1, N + 1) ans += i * cnt[i];
    cout << ans << endl;
    return 0;
}

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

#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define repd(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define forr(i,a,b) for(ll i=(a);i<=(b);i++)
#define ford(i,a,b) for(ll i=(a);i>=(b);i--)
#define all(x) (x).begin(),(x).end() //sortなどの引数を省略したい
#define size(x) ((ll)(x).size()) //sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))
#define INF 1000000000000 //10^12
#define MOD 10000007 //10^9+7
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define MAXR 100000 //10^5:最大のrange(素数列挙などで使用)



int main(void) {
    int N,i,j;
    cin >> N;
    int n[210895];
    int m[210895];
    for(i=0;i<N-1;++i){
       cin >> n[i];
      
    }
     for(i=0;i<N-1;++i){
     
       m[n[i]-1]++;
    }
    for (j=0;j<N;++j){
        
         cout << m[j] << "\n";
    

    }   
    
  return 0;
}

Measured memory_bytes: 5192
#include <bits/stdc++.h>
#include <typeinfo>
#include <cxxabi.h>
#ifdef LOCAL
  #include "dbgtoki.hpp"
  #define DUMP(i) dump((string)TOSTRING(i), demangle(typeid(i).name()), __LINE__ , i) 
DbgTimer D_t;
  #define TSTART() D_t.start()
  #define TSTOP() D_t.stop()
#else
  #define DUMP(i) 
  #define TSTART()
  #define TSTOP()  
#endif
 
#define TOSTRING(x) #x
#define SZ(x) (int)(x).size()
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(s) (s).begin(), (s).end()
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()) , v.end());
using namespace std;
typedef long long unsigned int llu;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;

const int EPS = 1e-9;



int main (){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  while(cin >> n ){
    TSTART();
    
    int ans = n  * 800 - n / 15 * 200;
    
    cout << ans << endl;
    TSTOP();
  }
    return 0;
}



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

int main(){
    string S;
    const char *s = NULL;
    cin >> S;
    s = S.c_str();
    long long i;
    long long counta[2019];
    fill(counta,counta+2019,0);
    long long C[200001];
    C[0] = 0;
    counta[C[0]]++;
    long long inv = 202;
    for(i=1; i<=S.length(); i++){
        C[i] = (C[i-1]+(S[i-1]-'0')*inv)%2019;
        counta[C[i]]++;
        inv = (inv*202)%2019;
    }
    long long ans=0;
    for(i=0; i<2019; i++){
        ans += counta[i]*(counta[i]-1)/2;
    }
    cout << ans << endl;
    system("pause");
    return 0;
}

Measured memory_bytes: 5332
#include <iostream>
#include <vector>

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

int main()
{
	int answer = 0;
	
	auto check_is_all_odd = [](const int size, const vector<int> num)
	{
		for (size_t i = 0; i < size; ++i)
		{
			if (num[i] % 2 != 0) return false;
		}
		return true;
	};

	auto devide_by_two = [](const int size, vector<int> inputs)
	{
		vector<int> output;
		
		for (size_t i = 0; i < size; ++i)
			output.push_back(inputs[i] / 2);

		return output;
	};

	int n, input;
	cin >> n;

	vector<int> numbers;
	for (int i = 0; i < n; ++i)
	{
		cin >> input;
		numbers.push_back(input);
	}

	while (check_is_all_odd(n, numbers))
	{
		numbers = devide_by_two(n, numbers);
		answer++;
	}

	cout << answer << endl;
}

Measured memory_bytes: 256
import sys
input = sys.stdin.readline

N, K = map(int, input().split())
nums = []
for i in range(N):
    t, d = map(int, input().split())
    nums.append((d, t))
nums.sort(reverse=True)
se = set()
doubled = []
rest = []
points = 0
for i, n in enumerate(nums):
    if i < K:
        if not n[1] in se:
            se.add(n[1])
        else:
            doubled.append(n)
        points += n[0]
    else:
        rest.append(n)
ans = points + pow(len(se), 2)
idx = len(doubled)-1
for d, t in rest:
    if idx < 0: break
    if t in se: continue
    points -= doubled[idx][0]
    idx -= 1
    points += d
    se.add(t)
    ans = max(ans, points+pow(len(se), 2))
print(ans)


Measured memory_bytes: 55284
import java.util.*
 
fun main(args: Array<String>) {
  val sc = Scanner(System.`in`)
  val s = sc.next()
  
  var ans = ""
  if (s == "Sunny") {
    ans = "Cloudy"
  }
  if (s == "Cloudy") {
    ans = "Rainy"
  }
  if (s == "Rainy") {
    ans = "Sunny"
  }
  
  println(ans)

}

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

using namespace std;

typedef long long ll;

const int INF = 1e9;
const ll LLINF = 1e18;
const ll MOD = (ll)1e9 + 7;

int main()
{
  int N; scanf("%d", &N);
  char buf[64] = { '\0' };
  vector<int> freqs(64, 0);
  for (int i = 0; i < N; ++i) {
    scanf("%s", buf);
    ++freqs[buf[0] - 'A'];
  }

  vector<char> I = { 'M', 'A', 'R', 'C', 'H' };
  ll ans = 0;
  for (int i = 0; i < 5; ++i) {
    for (int j = i + 1; j < 5; ++j) {
      for (int k = j + 1; k < 5; ++k) {
        ll val = 1LL * freqs[I[i] - 'A'] * freqs[I[j] - 'A'] * freqs[I[k] - 'A'];
        ans += val;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}


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

int main() {
  int A,B;
  cin >> A >> B;
  int M,m;
  M=max(A,B);
  m=min(A,B);
  for(int i=0;i<M;i++){
    cout << m;
  }
  cout << endl;
}


Measured memory_bytes: 3564
using namespace std;

#include <iostream>
#include <vector>
#include <set>
#include <string>
#include <algorithm>
#include <utility>
#include <functional>
#include <queue>
#include <cmath>
#include <numeric>
#include <map>
using namespace std;


int main(void){
	string N;
	map<pair<string, int>, int> SP;
  int nwin = 0;

	cin >> N;

	
	for (auto i : N)
      if (i == 'o')
        nwin++;
	cout << ((15 - N.length() + nwin >= 8) ? "YES" : "NO") << endl;

	return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

char str[100001];
 int main(void)
 {
 	int res=0;
 	scanf("%s",str);
 	for(int i=1;str[i]!='\0';i++){
 		if(str[i-1]!=str[i]) res++;
 	}
 	printf("%d\n",res);
 	return 0;
 }

Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <iterator>
#include <algorithm>
#include <map>
#include <string.h>
#include <vector>
#include <cmath>
#include <deque>
#include <iomanip>

using namespace std;
#define range(a) a.begin(), a.end()
#define rep(i, c) for(int i = 0; i < (int)c; i++)
typedef long long ll;

const string YES = "Yes";
const string NO = "No";


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

  string s;
  string ans;
  cin >> s;
  rep(i, 5){
    ans += "hi";
    if(ans == s){
      cout << YES << endl;
      return 0;
    }
  }
  cout << NO << endl;




  return 0;

}


Measured memory_bytes: 256
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input! {
        n: i32,
        s_vec: [String; n]
    }

    let mut ac_count = 0;
    let mut wa_count = 0;
    let mut tle_count = 0;
    let mut re_count = 0;

    for result in s_vec.iter() {
        if result.as_str() == "AC" {
            ac_count += 1;
        } else if result.as_str() == "WA" {
            wa_count += 1;
        } else if result.as_str() == "TLE" {
            tle_count += 1;
        } else {
            re_count += 1;
        }
    }
    println!("AC x {}", ac_count);
    println!("WA x {}", wa_count);
    println!("TLE x {}", tle_count);
    println!("RE x {}", re_count);
}


Measured memory_bytes: 7492
N = int(input())
A = []
rem = N * (N-1) // 2
for _ in range(N):
    *temp, = map(int, input().split())
    A.append(temp[::-1])
day = 0
next_opponent = [-1]*N
played_yesterday = list(range(N))
while rem:
    day += 1
    impossible = True
    for p in played_yesterday:
        if A[p]: next_opponent[p] = A[p].pop() - 1
    played_today = []
    for p in played_yesterday:
        candidate = next_opponent[p]
        if candidate == -1: continue # player p had already finished all the game
        elif next_opponent[candidate] == p: # matched
            impossible = False
            rem -= 1
            played_today += [p, candidate]
            next_opponent[p], next_opponent[candidate] = -1, -1
    if impossible: print(-1); break
    played_yesterday = played_today
else:
    print(day)

Measured memory_bytes: 34816
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Sort(a) sort(a.begin(), a.end())
const int INF = 1<<30;
const ll MOD = 1000000007;

int pcnt(int x) {
    return __builtin_popcount(x);
}

int f(int x) {
    if (x == 0) return 0;
    return f(x%pcnt(x))+1;
}

int main(void) {
    int n; cin >> n;
    string s; cin >> s;
    vi x(n);
    rep(i, n) x[i] = s[i]-'0';
    int pc = 0;
    rep(i, n) pc += x[i];
    vi ans(n);
    rep(b,2) {
        int npc = pc;
        if (b == 0) npc++; else npc--;
        if (npc <= 0) continue;
        int r0 = 0;
        rep(i, n) {
            r0 = (r0*2)%npc;
            r0 += x[i];
        }
        int k = 1;
        for (int i = n-1; i>= 0; --i){
            if (x[i] == b) {
                int r = r0;
                if (b == 0) r = (r+k)%npc;
                else r = (r-k+npc)%npc;
                ans[i] = f(r)+1;
            }
            k = (k*2)%npc;
        }
    }

    rep(i, n) cout << ans[i] << endl;

    return 0;
}

Measured memory_bytes: 5244
#include<bits/stdc++.h>
#define il inline
#define stt static
#define rg register
#define ll long long
#define db double
#define pii pair<int,int>
#define mp make_pair
#define fst first
#define snd second
#define pb push_back
#define y1 substitute_of_y1
#define DEBUG(x) cerr<<#x" = "<<x<<endl;
using namespace std;

template<typename T> il T rd(){
	T nmb=0;int sgn=0;char chr=getchar();
	while(!isdigit(chr)){if(chr=='-')sgn=1;chr=getchar();}
	while(isdigit(chr)){nmb=(nmb<<3)+(nmb<<1)+chr-'0';chr=getchar();}
	return sgn?-nmb:nmb;
}
template<typename T> void wt(T nmb){
	if(nmb>9)wt(nmb/10);
	putchar(nmb%10+'0');
}
template<typename T> il void cmax(T &x,T y){x=x>y?x:y;}
template<typename T> il void cmin(T &x,T y){x=x<y?x:y;}
inline void proc_status(){
	ifstream t ("/proc/self/status");
	cerr<<string(istreambuf_iterator<char>(t),istreambuf_iterator<char>())<<endl;
	/*freopen("/proc/self/status","r",stdin);
	char ch[100];
	while(scanf("%s",ch)!=EOF)cerr<<ch<<endl;*/
}

const int L=610;
const int P=1e9+7;

int a[L],sum[L];

int g[L];
il void getg(int lim){
	g[0]=1;
	for(int i=2,val=1;i<=lim;i+=2)
		val=(ll)val*(i-1)%P,g[i]=val;
}

int dp[L][L];

int main(){
	int n=rd<int>(),p=rd<int>();
	while(p--){
		int x=rd<int>(),y=rd<int>();
		a[x]=y,a[y]=x;
	}
	int lim=n*2;
	for(int i=1;i<=lim;++i)sum[i]=sum[i-1]+!a[i];
	getg(lim);
	for(int i=1;i<=lim;++i)
		for(int j=i;j<=lim;++j){
			if((j-i+1)&1)continue;
			int val=0;
			for(int k=i;k<=j;++k)
				if(a[k]&&(a[k]<i||a[k]>j))goto END;
			for(int k=i+1;k<j;++k)
				(val+=(ll)dp[i][k]*g[sum[j]-sum[k]]%P)%=P;
			dp[i][j]=(g[sum[j]-sum[i-1]]-val+P)%P;
			END:;
	}
	int Ans=0;
	for(int i=1;i<=lim;++i)
		for(int j=i+1;j<=lim;++j)
			(Ans+=(ll)dp[i][j]*g[sum[i-1]+sum[lim]-sum[j]]%P)%=P;
	printf("%d\n",Ans);
	return 0;
}

Measured memory_bytes: 1664
#include <bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>

#define IOS ios :: sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define AfterDecimal(n) cout << fixed << setprecision(n);
#define ll long long int
#define ld long double
#define all(a) a.begin(), a.end()
#define pii pair<int, int>
#define pll pair<ll, ll>
#define bits(x) __builtin_popcount(x)

using namespace __gnu_pbds;
using namespace std;

// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")

//template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef tree<int, null_type, less<int>, rb_tree_tag,
            tree_order_statistics_node_update>
    ordered_set;

#ifdef LOCAL
#define debug(...) __f(#__VA_ARGS__, __VA_ARGS__)
template<typename Arg1>
void __f(const char* name, Arg1&& arg1){
    cout << "# "<< name << " = " << arg1 << '\n';
}
template<typename Arg1, typename... Args>
void __f(const char* names, Arg1 && arg1, Args &&... args){
        const char* comma = strchr(names + 1 ,',');
        cout << "# ";
        cout.write(names, comma - names)<< " = " << arg1 << " | ";
        __f(comma + 1, args...);
}
#else
#define debug(...) 36
#endif


std::mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
inline int ri(int a, int b) {
    return uniform_int_distribution<int>(a, b)(rng);
}

ll power(ll x, ll y, ll p){
   ll res = 1;
   x = x % p;
   while (y > 0) {
      if (y & 1)
         res = (res * x) % p;
      y = y >> 1;
      x = (x * x) % p;
   }
   return res;
}

const ll INF = (ll)(8e18);
const ll MOD = (ll)(1e9 + 7);
const ll HMOD = (ll)4222234727;
const ll Hashprime = (ll)31;

const int maxn = 100100;

int32_t main(){
    IOS;
    #ifndef ONLINE_JUDGE
    //  freopen("input.txt", "r", stdin);
    //  freopen("output.txt", "w", stdout);
    #endif
    int t = 1; // cin >> t;
    while(t--){
       ll n; cin >> n;
       ll ans = 0;
       ll tans = 0;
       for(ll i = 1; i <= n; i++){
          ll x; cin >> x;
          ans += x * tans;
          ans %= MOD;
          tans += x;
          tans %= MOD;
       }
       cout << ans << '\n';
    }
    #ifdef LOCAL
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
    #endif
}
//g++ -DLOCAL -std=c++17 -Wshadow -Wall -o "c" "comp.cpp" -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG -g


Measured memory_bytes: 3544
MAX_PRIME = 10**6
is_prime = [1] * MAX_PRIME
primes = []
is_prime[0] = 0
is_prime[1] = 0
for i in range(MAX_PRIME):
    if is_prime[i]:
        primes.append(i)
        for j in range(2, ((MAX_PRIME-1)//i) + 1):
            is_prime[i*j] = 0

x = int(input())
while not is_prime[x]:
    x += 1
print(x)

Measured memory_bytes: 14016
#include "bits/stdc++.h"
using namespace std;
#define ff first
#define ss second
typedef pair<pair<int,int>,pair<int,int>> P;
vector<P> v;
P mx(P m, P x){
    if(m.ff == x.ff)
        return {x.ff, max(m.ss, x.ss)};
    if(m.ff < x.ff)
        return {x.ff, max(m.ff, x.ss)};
    else
        return {m.ff, max(m.ss, x.ff)};
}
signed main(){
    int n, x;
    cin >> n;
    int N = 1 << n;
    for(int i = 0; i < N; i++){
        cin >> x;
        v.push_back({{x,i},{0,-1}});
    }
    for(int j = 1; j < N; j <<= 1)
        for(int i = 0; i < N; i++)
            v[i|j] = mx(v[i|j], v[i]);
    int m = 0;
    for(int i = 0; i < N; i++){
        m = max(m, v[i].ff.ff+v[i].ss.ff);
        if(i)cout << m << endl;
    }
}


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

int main(){ 
  Int N;
  cin >> N;
  Int ans = 0;
  for (Int i = 1; i * i <= N; i++) {
    ans = i * i;
  }
  
  cout << ans << endl;

}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int n, m, q, a[210000], b[210000], c, dist[110000], T[110000], cnt, U[110000];
vector<pair<int, int> >x[110000], y[110000]; bool used[210000];
int main() {
	scanf("%d%d%d", &n, &m, &q);
	for (int i = 0; i < m; i++) { scanf("%d%d", &a[i], &b[i]); x[a[i]].push_back(make_pair(b[i], i)); x[b[i]].push_back(make_pair(a[i], i)); }
	fill(dist, dist + n + 1, 999999); dist[1] = 0; queue<int> Q; Q.push(1);
	while (!Q.empty()) { int a1 = Q.front(); Q.pop(); for (pair<int, int> i : x[a1]) { if (dist[i.first] > dist[a1] + 1) { dist[i.first] = dist[a1] + 1; Q.push(i.first); } } }
	for (int i = 1; i <= n; i++) { for (pair<int, int> j : x[i]) { if (dist[i] < dist[j.first]) { y[i].push_back(j); T[j.first]++; } } }
	for (int i = 0; i < q; i++) {
		scanf("%d", &c); c--;
		queue<pair<int, int> > Q1;
		int ss = -1;
		if (dist[a[c]] < dist[b[c]])ss = b[c];
		if (dist[a[c]] > dist[b[c]])ss = a[c];
		if (ss >= 1 && U[ss] == 0 && used[c] == false) {
			Q1.push(make_pair(ss, c));
			while (!Q1.empty()) {
				int a1 = Q1.front().first, a2 = Q1.front().second; Q1.pop();
				if (U[a1] == 1 || used[a2] == true) continue; T[a1]--; used[a2] = true;
				if (T[a1] != 0) continue; cnt++; U[a1] = 1;
				for (pair<int, int> j : y[a1]) {
					if (U[j.first] == 0) Q1.push(j);
				}
			}
		}
		printf("%d\n", cnt);
	}
	return 0;
}

Measured memory_bytes: 20008
import sys

N,K,Q=map(int,sys.stdin.readline().split())
points=[0]*N
for i in range (Q):
  points[int(sys.stdin.readline().rstrip())-1]+=1
for p in points:
  if (K-(Q-p)) > 0:
    print('Yes')
  else:
    print('No')


Measured memory_bytes: 47832
#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <bitset>
#include<vector>
#include<utility>
#include<fstream>
#include<queue>
#include <iomanip>
#include<numeric>
#include<set>
using namespace std;
typedef long long ll;
#define rep(i,k) for(i=0;i<k;i++)
#define P pair<ll,ll>
ll mod = 1000000007;
P p[200005];

ll i, b[1005], c, k, j, n, m, z = 0, mi = 0, t, cc, th;
ll a;
vector<ll> va, vb, vc, vj;
string s;
char ca[10000];
priority_queue<ll> q;




int main() {
	cin >> n>>s;
	z++;
	rep(i, n-1) {
		if (s[i] != s[i + 1])z++;

	}

	cout <<z ;


	return 0;
}

Measured memory_bytes: 512
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<set>
#include<map>

using namespace std;
typedef long long ll;
#define i_7 1000000007
#define i_5 1000000005

ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    else return c+i_7;
}
typedef pair<int,int> i_i;
typedef pair<ll,ll> l_l;
#define inf 100000000/*10^8*/

/////////////////////////////////



int main(){
    int n,k;cin>>n>>k;
    int a[2*k+1][2*k+1];memset(a,0,sizeof(a));
    for(int i=1;i<=n;i++){
        int x,y;cin>>x>>y;char c[2];cin>>c;
        if(c[0]=='W'){
            a[x%(2*k)][y%(2*k)]++;
        }else{
            if(x%(2*k)<=k-1){
                a[x%(2*k)+k][y%(2*k)]++;
            }else{
                a[x%(2*k)-k][y%(2*k)]++;
            }
        }
    }
    for(int i=0;i<=2*k-1;i++){
        for(int j=1;j<=2*k-1;j++){
            a[i][j]+=a[i][j-1];
        }
    }
    for(int i=0;i<=2*k-1;i++){
        for(int j=1;j<=2*k-1;j++){
            a[j][i]+=a[j-1][i];
        }
    }
    for(int j=2*k-1;j>=0;j--){
        for(int i=2*k-1;i>=0;i--){
            a[i+1][j+1]=a[i][j];
        }
    }
    for(int i=0;i<=2*k;i++){a[i][0]=0;a[0][i]=0;}
    int ans=0;
    for(int x=0;x<=k;x++){
        for(int y=0;y<=k;y++){
            int cou=2*a[k-y][k-x]-2*a[k-y][2*k-x]+a[k-y][2*k]-2*a[2*k-y][k-x]+2*a[2*k-y][2*k-x]-a[2*k-y][2*k]+a[2*k][k-x]-a[2*k][2*k-x]+a[2*k][2*k];
            cou=max(cou,n-cou);
            if(cou>ans)ans=cou;
        }
    }
    cout<<ans<<endl;
    
    return 0;
}

Measured memory_bytes: 15872
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int main(){
    int n,m,Q;
    scanf(" %d %d %d", &n, &m ,&Q);

    vector<vector<int>> L(n), R(n);
    rep(i,m){
        int l,r;
        scanf(" %d %d", &l, &r);
        --l;
        --r;
        L[l].pb(i);
        R[r].pb(i);
    }

    vector<vector<int>> ans(n,vector<int>(n));

    rep(i,n){
        vector<bool> v(m);
        int ct = 0;
        for(int j=i; j<n; ++j){
            for(int k:L[j]) v[k] = true;
            for(int k:R[j])if(v[k]) ++ct;
            ans[i][j] = ct;
        }
    }

    rep(i,Q){
        int p,q;
        scanf(" %d %d", &p, &q);
        --p;
        --q;
        printf("%d\n", ans[p][q]);
    }
    return 0;
}


Measured memory_bytes: 4352
#include <bits/stdc++.h>
#define For(i,a,b) for(int i=a;i<=b;i++)
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define sz(x) ((int)x.size())
#define MOD (ll)(INF)
#define INF 1e17
#define int ll
using namespace std;
using ll=long long;
using pii=pair<int,int>;

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

int fpow(int b,int p)
{
	if(p==0) return 1;
	int h=fpow(b,p/2);
	if(p%2) return h*b%MOD*h%MOD;
	return h*h%MOD;
}

int32_t main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	int n; cin>>n;
	vector<int> v(n+1);
	for(auto &i:v) cin>>i;
	int t;
	int ans=0;
	For(i,0,n-1){
		cin>>t;
		int a=min(t,v[i]);
		v[i]-=a; t-=a; ans+=a;
		a=min(t,v[i+1]);
		v[i+1]-=a; t-=a; ans+=a;
	}
	cout<<ans<<"\n";
	return 0;
}




















Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>
#include <functional>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <deque>
#include <sys/timeb.h>
#include <fstream>

using namespace std;

#define repr(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repr(i,0,n)
#define reprrev(i,a,b) for(int i=(int)(b)-1;i>=(int)(a);i--)
#define reprev(i,n) reprrev(i,0,n)
#define repi(itr,ds) for(auto itr=ds.begin();itr!=ds.end();itr++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define mp make_pair
#define mt make_tuple
#define INF 1050000000
#define INFL 1100000000000000000LL
#define EPS (1e-10)
#define MOD 1000000007
#define PI 3.1415926536
#define RMAX 4294967295

typedef long long ll;
typedef pair<int, int> Pi;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<Pi> vPi;
typedef vector<vector<int> > vvi;
typedef vector<vector<bool> > vvb;
typedef vector<vector<ll> > vvll;
typedef vector<vector<char> > vvc;
typedef vector<vector<string> > vvs;
typedef vector<vector<double> > vvd;
typedef vector<vector<Pi> > vvPi;
typedef priority_queue<int, vector<int>, greater<int> > pqli;
typedef priority_queue<ll, vector<ll>, greater<ll> > pqlll;
typedef priority_queue<Pi, vector<Pi>, greater<Pi> > pqlP;
struct Edge {
	int from, to, cost;
	bool operator<(Edge e) {
		return cost < e.cost;
	}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
template <class T>
using vec = vector<T>;
template<class T>
using pql = priority_queue<T, vector<T>, greater<T>>;
string debug_show(Pi a) {
	return "(" + to_string(a.first) + "," + to_string(a.second) + ")";
}

template<class T>
struct augEdge {
	T from, to;
	int cost;
	bool operator<(augEdge e) { return cost < e.cost; }
	bool operator>(augEdge e) { return cost > e.cost; }
};
template<class T>
using augGraph = vector<augEdge<T>>;



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

	int n, m;
	cin >> n >> m;
	vs a(n), b(m);
	rep(i, n) {
		string s;
		cin >> s;
		a[i] = s;
	}
	rep(j, m) {
		string s;
		cin >> s;
		b[j] = s;
	}

	bool f = false, ff = false;
	rep(i, n) {
		rep(j, n) {
			if (i + m - 1 >= n || j + m - 1 >= n) continue;

			f = true;
			rep(k, m) {
				rep(l, m) {
					if (a[i + k][j + l] != b[k][l]) f = false;
				}
			}

			if (f) ff = true;
		}
	}

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

Measured memory_bytes: 256
S = input()

n = S.count("N")
w = S.count("W")
s = S.count("S")
e = S.count("E")

if (n == 0 and s > 0) or (s == 0 and n > 0) or (w == 0 and e > 0) or (e == 0 and w > 0):
    print("No")
else:
    print("Yes")

Measured memory_bytes: 3060
#pragma region
#include<bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = n; i > 0; i--)
#define repc(i, m, n) for (int i = m; i < n; i++)
#define reprc(i, m, n) for (int i = m; i > n; i--)
#define all(v) v.begin(), v.end
#define INF 1e9
#define EPS 1e-10
using namespace std;
typedef long long ll;
#pragma endregion

int main()
{
    int N;
    cin >> N;
    vector<int> A(N);

    repc(i, 1, N){
        int a;
        cin >> a;
        A[a - 1]++;
    }

    rep(i, N){
        cout << A[i] << endl;
    }
}

Measured memory_bytes: 4040
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define space " "
#define MOD (long long) 1000000007
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename T> void __f(const char* variable_name, T&& value){cout << variable_name << ": " << value << endl;}
template <typename T1, typename... T2> void __f(const char* names, T1&& arg1, T2&&... args){const char* comma = strchr(names + 1, ',');cerr.write(names, comma - names) << ": " << arg1 << " |";__f(comma + 1, args...);}

const ll N = 5e5 + 5;
ll *v;
ll n, d;

void solve(){
    cin >> n >> d;
    vector<bool> arr(n, false);
    while(d--){
        ll p, q;
        cin >> p;
        while(p--){
            cin >> q;
            arr[q-1] = true;
        }   
    }
    ll ans = 0;
    for(auto i : arr)
        if(!i)
            ans++;
    cout << ans << endl;
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    ll t = 1;
    // cout << "\n\nBLAH BLAH BLAH\n\n";
    // cin >> t;
    while(t--){
        solve();
    }
    return 0;
}

Measured memory_bytes: 3656
N = int(input())
A = [int(input()) for _ in range(N)]
 
M = max(A)
M_index = A.index(M)
sub_M = 0
for i in range(N):
  if i != M_index: sub_M = max(sub_M, A[i])

for i in range(N):
  if i == M_index: print(sub_M)
  else: print(M)

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

typedef long long ll;

//const ll INF = 1LL<<60;


long double dis(int dx, int dy){
  dx *= dx;
  dy *= dy;
  return pow(dx+dy,0.5);
}



int main(){

  int N;
  cin>>N;

  int x[N],y[N];

  rep(i,N) cin>>x[i]>>y[i];
  long double ans = 0;

  rep(i,N) for(int j = i+1; j<N; j++) ans += dis(x[i]-x[j],y[i]-y[j]);


  cout<<fixed<<setprecision(10)<< 2*ans/N;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 1005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
    	if(c == '-') f = -1;
    	c = getchar();
    }
    while(c >= '0' && c <= '9') {
    	res = res * 10 +c - '0';
    	c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
	out(x / 10);
    }
    putchar('0' + x % 10);
}
int N,K;
void Solve() {
    read(N);read(K);
    out(N - K + 1);enter;
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <climits>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <map>
#include <math.h>
#include <algorithm>
#include <vector>
#include <string.h>
#include <random>
#include <iomanip>
#include <limits>
 
using namespace std;
 
typedef long long ll;
 
typedef pair<ll,ll> P;
long long int INF = 1e18;
double Pi = 3.141592653589;
const int mod = 1000000007;
// memset(a,0,sizeof(a)); →全部０にする
 
vector<int> G[100005];
std::vector<P> tree[100010];
 
int dx[8]={1,0,-1,0,1,1,-1,-1};
int dy[8]={0,1,0,-1,1,-1,-1,1};
 
#define p(x) cout<<x<<endl;
#define el cout<<endl;
#define pe(x) cout<<x<<" ";
#define re(i,a,b) for(i=a;i<=b;i++)
 
ll i,j,k,l,ii,jj;
ll n,m,a,b,c,d,ans=INF,num,ddx,ddy;
ll x[100005],y[100005];
char s[300004];



ll de(ll x){
	ll num=0;
	while(true){
		num += x%10;
		x = x/10;
		if(x==0)break;
	}
	return num;
}


int main(){
	cin>>n;
	for(i=1;i<n;i++){
		ans = min(ans,de(i)+de(n-i));
	}
	p(ans);
	return 0;
}

Measured memory_bytes: 4992
import sys
N, Y = map(int, input().split())

num = min(N, Y//10000)
if 10000*N < Y:
    print('-1 -1 -1')
    sys.exit()

for i in reversed(range(0, num+1)):
    for j in reversed(range(0, N-i+1)):
        if 10000*i + 5000*j > Y:
            continue
        if 10000*i + 5000*j + 1000*(N-i-j) == Y:
            print('%d %d %d' % (i, j, N-i-j))
            sys.exit()
print('-1 -1 -1')


Measured memory_bytes: 8948
n = int(input())

grid = []
for i in range(3):
    array = list(map(int, input().strip().split()))
    grid.append(array)

k = sum(grid[1])
for i in range(n-1):
    if grid[0][i]+1 == grid[0][i+1]:
        k += grid[2][grid[0][i]-1]
        
print(k)  

Measured memory_bytes: 3064
import java.util.*;

public class Main {

    static final int NONE = -1;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int M = sc.nextInt();

        List<List<Integer>> out = new ArrayList<>();
        boolean[][] in = new boolean[N][N];
        for (int i = 0; i < N; i++) out.add(new ArrayList<>());

        for (int i = 0; i < M; i++) {
            int u = sc.nextInt() - 1;
            int v = sc.nextInt() - 1;
            out.get(u).add(v);
            in[v][u] = true;
        }

        List<Integer> sol = null;

        for (int u = 0; u < N; u++) {
            List<Integer> cycle = shortestCycle(u, out, in[u]);
            if (cycle != null && (sol == null || cycle.size() < sol.size())) sol = cycle;
        }

        if (sol == null) System.out.println(-1);
        else {
            System.out.println(sol.size());
            for (int u : sol) System.out.println(u + 1);
        }
    }


    static List<Integer> shortestCycle(int s, List<List<Integer>> adj, boolean[] in) {
        int[] pred = new int[in.length];
        Arrays.fill(pred, NONE);
        Deque<Integer> deque = new ArrayDeque<>();
        deque.add(s);

        while (!deque.isEmpty()) {
            int u = deque.pollLast();

            for (int v : adj.get(u)) {
                if (pred[v] != NONE) continue;
                if (in[v]) {
                    List<Integer> cycle = new ArrayList<>();
                    while (pred[u] != NONE) {
                        cycle.add(u);
                        u = pred[u];
                    }
                    cycle.add(s);
                    cycle.add(v);
                    return cycle;
                }
                pred[v] = u;
                deque.addFirst(v);
            }
        }

        return null;
    }
}

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


def solve(N, C, AB):
    def edges_to_array(A, B, N, M):
        head = [-1] * (N + 1)
        nxt = [0] * (M + M)
        to = [0] * (M + M)
        for i in range(M):
            a = A[i]
            b = B[i]
            nxt[i << 1] = head[a]
            to[i << 1] = b
            head[a] = i << 1
            nxt[i << 1 | 1] = head[b]
            to[i << 1 | 1] = a
            head[b] = i << 1 | 1
        return head, nxt, to

    def EulerTour(head, nxt, to, root=1):
        N = len(head) - 1
        parent = [0] * (N + 1)
        ind_L = [0] * (N + 1)
        ind_R = [0] * (N + 1)
        i = -1
        tour = []
        stack = [-root, root]
        stack[0] = -root
        stack[1] = root
        while stack:
            v = stack.pop()
            tour.append(v)
            i += 1
            if v > 0:
                ind_L[v] = i
                p = parent[v]
                k = head[v]
                while k != -1:
                    w = to[k]
                    if w == p:
                        k = nxt[k]
                        continue
                    parent[w] = v
                    stack.append(-w)
                    stack.append(w)
                    k = nxt[k]
            else:
                ind_R[-v] = i
        return tour, ind_L, ind_R, parent
    head, nxt, to = edges_to_array(AB[::2], AB[1::2], N, N - 1)
    tour, ind_L, ind_R, parent = EulerTour(head, nxt, to)
    removed = [0] * (N + 1)
    answer = [N * (N + 1) // 2] * (N + 1)
    memo = [0] * (N + 1)
    for v in tour:
        if v > 0:
            memo[v] = removed[C[parent[v]]]
        else:
            v = -v
            removed[C[v]] += 1
            p = parent[v]
            x = (ind_R[v] - ind_L[v]) // 2 + 1  # subtree size
            x -= removed[C[p]] - memo[v]
            answer[C[p]] -= x * (x + 1) // 2
            removed[C[p]] += x
    for i, x in enumerate(removed):
        x = N - x
        answer[i] -= x * (x + 1) // 2
    return answer


N = int(readline())
C = (0,) + tuple(map(int, readline().split()))
AB = tuple(map(int, read().split()))
answer = solve(N, C, AB)
print('\n'.join(map(str, answer[1:])))


Measured memory_bytes: 94016
#include <bits/stdc++.h>
using namespace std;
/*{{{*/  //template
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
constexpr int INF = numeric_limits<int>::max()/2;
constexpr long long LINF = numeric_limits<long long>::max()/3;
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define sz(x) (int)(x).size()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
//struct fin{ fin(){ cin.tie(0); ios::sync_with_stdio(false); } } fin_;
struct Double{ double d; explicit Double(double x) : d(x){} };
ostream& operator<<(ostream& os,const Double x){ os << fixed << setprecision(20) << x.d; return os; }
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os, const map<T,U>& mp){ os << "{"; for(auto& p : mp){ os << p << ","; } os << "}"; return os; }
template<typename T,typename U> ostream& operator<<(ostream& os,const pair<T,U>& p){ os << "(" << p.first << ","<< p.second <<")"; return os; }
template<typename T> ostream& operator<<(ostream& os,const set<T>& st){ os<<"{"; for(T v:st) os<<v<<","; os <<"}"; return os; }
template<typename T,typename U> inline void chmax(T &x,U y){ if(y>x) x = y; }
template<typename T,typename U> inline void chmin(T &x,U y){ if(y<x) x = y; }
using ll = long long;
using ull = unsigned long long;
using pii = std::pair<int,int>;
using vi = std::vector<int>;
typedef vector<vi> vvi;
ll gcd(ll a,ll b){ if(b==0) return a; else return gcd(b,a%b); }
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
//constexpr double eps = 1e-14; 
constexpr double eps = 1e-10; 
constexpr ll mod = 1e9+7;
const int dx[]={1,0,-1,0} ,dy[] = {0,1,0,-1};
/*}}}*/

int main(){
    int N,M; cin >> N >> M;
    string s; cin >> s;
    reverse(all(s));

    vector<int> L(N+1);
    rep(i,N+1){
        if(s[i] == '0'){
            L[i] = i;
        }else{
            L[i] = L[i-1];
        }
    }

    vector<int> ans;

    int now = 0;
    while(1){
        if(now == N) break;

        int d = min(M, N-now);
        int nxt = L[now+d];
        if(nxt == now){
            cout << -1 << endl;
            return 0;
        }

        ans.emplace_back(nxt-now);
        now = nxt;
    }
    reverse(all(ans));
    for(auto v : ans){
        cout << v << " ";
    }
    cout << endl;
}


Measured memory_bytes: 4404
#include <cstdio>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <utility>
#include <list>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <string>
#include <numeric>
#include <functional>
#include <ctime>
#include <bitset>
#include <map>
#include <set>
#include <complex>

using namespace std;

typedef long long ll;
typedef long long LL;
typedef unsigned long long ull;
typedef unsigned long long ULL;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<ll> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<string> vs;

typedef pair<int, int> pii;
typedef pii PII;

#define endl "\n"

#ifdef WIN32
#define debug(x) cerr << #x << " : " << x << "\n"
#else
#define debug(x)
#endif

static const int INF = 1 << 29;
static const double PI = acos(-1.0);
static const double EPS = 1e-10;

struct DisjointSet {
	vector<int> rank, p;

	DisjointSet(){}

	DisjointSet(int size) {
		rank.resize(size, 0);
		p.resize(size, 0);
		for (int i = 0; i < size; i++){
			makeSet(i);
		}
	}

	void makeSet(int x) {
		p[x] = x;
		rank[x] = 0;
	}

	bool same(int x, int y){
		return findSet(x) == findSet(y);
	}

	void unite(int x, int y){
		link(findSet(x), findSet(y));
	}

	void link(int x, int y){
		if (rank[x] > rank[y]) {
			p[y] = x;
		}
		else{
			p[x] = y;
			if (rank[x] == rank[y]){
				rank[y]++;
			}
		}
	}

	int findSet(int x){
		if (x != p[x]){
			p[x] = findSet(p[x]);
		}
		return p[x];
	}
};

struct Edge{
	int source, target, cost;
	Edge(int source = 0, int target = 0, int cost = 0) :
		source(source), target(target), cost(cost) {}
	bool operator < (const Edge &e) const {
		return cost < e.cost;
	}
};

int kurskal(int N, vector<Edge> edges) {
	int totalCost = 0;
	sort(edges.begin(), edges.end());
	DisjointSet dset = DisjointSet(N + 1);
	for (int i = 0; i < N; i++){
		dset.makeSet(i);
	}
	int source, target;
	for (int i = 0; i < edges.size(); i++){
		Edge e = edges[i];
		if (!dset.same(e.source, e.target)){
			//MST.push_back(e);
			totalCost += e.cost;
			dset.unite(e.source, e.target);
		}
	}
	return totalCost;
}

int main(){
	int N, M, cost;
	int source, target;

	cin >> N >> M;
	vector<Edge> edges;
	for (int i = 0; i < M; i++){
		cin >> source >> target >> cost;
		edges.push_back(Edge(source, target, cost));
	}
	cout << kurskal(N, edges) << endl;
	return 0;
}

Measured memory_bytes: 3616