akhauriyash/Code-Regression huggingface dataset:
import java.util.*;
 
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[] d = new int[N];
        for (int i = 0; i < N; i++) {
            d[i] = sc.nextInt();
        }
        int ans = 0;
        for (int i = 0; i < N-1; i++) {
            for (int j = i+1; j < N; j++) {
                ans += d[i]*d[j];
            }
        }
        System.out.println(ans);
    }
}


Measured memory_bytes: 21716
#include <bits/stdc++.h>
#define int long long
using namespace std;
using Graph = vector<vector<int>>;
const int MOD = 1000000007;

struct UnionFind{
  vector<int> Par;
 
  //N個の独立した根として初期化
  UnionFind(int N) : Par(N) {
    for( int i = 0; i < N; i++ ) Par.at(i) = -1;
  }
 
  //どこの根に属するか得る再帰関数
  int root(int X){
    if( Par.at(X) < 0 ) return X;
    else return Par.at(X) = root(Par.at(X));
  }
 
  //XとYが同じ根に属するか判定
  bool same(int X, int Y){
    return root(X) == root(Y);
  }
 
  //XとYの木を合体　サイズを持つ
  bool merge(int X, int Y){
    X = root(X);
    Y = root(Y);
    if( X == Y ) return false;
    if( Par.at(X) > Par.at(Y) ) swap(X, Y); //小さいほうから大きいほうに繋ぎ変える
    Par.at(X) += Par.at(Y);
    Par.at(Y) = X;
    return true;
  }
 
  //所属する木のサイズを取得
  int size(int X){
    return -Par.at(root(X));
  }
};

signed main() {
  int N, M, K;
  cin >> N >> M >> K;
  
  //フレンドかどうか
  vector<int> F(N);
  UnionFind T(N);
  for( int i = 0; i < M; i++ ){
    int A, B;
    cin >> A >> B;
    A--;
    B--;
    F.at(A)++;
    F.at(B)++;
    T.merge(A, B);
  }
  
  //誰が誰をブロックしてるか
  Graph BLO(N);
  for( int i = 0; i < K; i++ ){
    int C, D;
    cin >> C >> D;
    C--;
    D--;
    BLO.at(C).push_back(D);
    BLO.at(D).push_back(C);
  }

  for( int i = 0; i < N; i++ ){
    int ans = T.size(i) - F.at(i);
    for( int j = 0; j < BLO.at(i).size(); j++ ){
      if( T.same(i, BLO.at(i).at(j)) ) ans--;
    }
    cout << ans-1 << " ";
  }
  
  
  
}


Measured memory_bytes: 8192
#include<bits/stdc++.h>
const int N = 100010;

char s[N];

int main()
{
	scanf("%s", s);
	int cnt = 0, len = strlen(s);
	for (int i = 1; i < len; i++)
		if (s[i] != s[i - 1])
			cnt ++;
	printf("%d\n", cnt);
	return 0;
}

Measured memory_bytes: 384
import java.util.Scanner;

public class Main {

	public static int[] uf;
	public static long[] dis;
	public static int[] dep;

	public static int Root(int n) {
		if(uf[n] == n)
			return n;
		else
			return Root(uf[n]);
	}

	public static long RootDis(int n) {
		if(uf[n] == n)
			return 0;
		else
			return dis[n] + RootDis(uf[n]);
	}

	public static boolean Add(int l, int r, long d) {
		boolean ret = true;
		int lroot = Root(l);
		int rroot = Root(r);
		long tmp1 = RootDis(l);
		long tmp2 = RootDis(r);
		if(lroot != rroot) {
			if(dep[lroot] > dep[rroot]) {
				dis[rroot] = -d + tmp1 - tmp2;
				uf[rroot] = lroot;
			} else if(dep[lroot] < dep[rroot]) {
				dis[lroot] = d - tmp1 + tmp2;
				uf[lroot] = rroot;
			} else {
				dis[rroot] = -d + tmp1 - tmp2;
				uf[rroot] = lroot;
				dep[lroot]++;
			}
		} else {
			ret = (tmp1 - tmp2) == d;
		}
		return ret;
	}

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		uf = new int[n];
		dis = new long[n];
		dep = new int[n];
		for(int i = 0; i < n; i++) {
			uf[i] = i;
			dis[i] = 0;
			dep[i] = 0;
		}
		boolean ans = true;
		for(int i = 0; i < m; i++) {
			ans = ans && Add(sc.nextInt() - 1, sc.nextInt() - 1, sc.nextLong());
		}
		if(ans)
			System.out.println("Yes");
		else
			System.out.println("No");
	}

}


Measured memory_bytes: 153632
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<functional>
#include<math.h>

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

int main() {
	int a, b;
	cin >> a >> b;

	int soc = 1;
	int cnt = 0;

	while (soc < b) {
		soc--;
		soc += a;
		cnt++;
	}

	cout << cnt << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;

int main(int argc, const char * argv[]) {
    
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    double a, b, c, d, e, f;
    cin >> a >> b >> c >> d >> e >> f;
    
    double ans_water = 100 * a;
    double ans_sugar = 0;
    
    for(int ia = 0; ia <= f / 100 / a; ia++){
        for(int ib = 0; ib <= f / 100 / b; ib++){
            if ( ia == 0 && ib == 0) break;
            double water = 100 * (a * ia + b * ib);
            for(int ic = 0; ic <= f / c; ic++){
                for(int id = 0; id <= f / d; id++){
                    double sugar = ic * c + id * d;
                    if(water + sugar > f || sugar > e * water / 100) break;
                    if(ans_sugar / (ans_sugar + ans_water) < sugar / (sugar + water) ){
                        ans_water = water;
                        ans_sugar = sugar;
                    }
                }
            }
        }
    }
    
    cout << ans_sugar + ans_water << " " << ans_sugar << endl;
    
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
typedef std::pair<int,int> ipair;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; };
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; };

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

    vector<vector<int>> dp(n+1, vector<int>(3, 0));
    rep(i,n) {
        rep(j,3) {
            rep(k,3) {
                if (j!=k) {
                    //jは昨日の選択、kは今日の選択
                    if (k==0) {
                        chmax(dp[i+1][k], dp[i][j]+a[i]);
                    } else if (k==1) {
                        chmax(dp[i+1][k], dp[i][j]+b[i]);
                    } else if (k==2) {
                        chmax(dp[i+1][k], dp[i][j]+c[i]);
                    }
                }
            }
        }
    }
    
    long long res = 0;
    rep(i,3) if(res < dp[n][i]) res=dp[n][i];
    cout << res << endl;

    return 0;
}

Measured memory_bytes: 6912
#include <iostream>
#include <string>
#include <vector>
#include <map>

#include <algorithm>
#include <queue>
#include <cctype>
#include <numeric>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)
#define debugos cerr
#define debug(v) do {debugos << "L" << __LINE__ << " " << #v << " > ";debugos<<(v)<<newl;} while (0)
#define debugv(v) do {debugos << "L" << __LINE__ << " " << #v << " > ";for(auto e:(v)){debugos<<e<<" ";}debugos<<newl;} while (0)
#define debuga(m,w) do {debugos << "L" << __LINE__ << " " << #m << " > ";for(int x=0;x<(w);x++){debugos<<(m)[x]<<" ";}debugos<<newl;} while (0)
#define debugaa(m,h,w) do {debugos << "L" << __LINE__ << " " << #m << " > \n";for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){debugos<<(m)[y][x]<<" ";}debugos<<newl;}} while (0)

int N;
string S;
string alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int ans;

int main() {
    cin >> N;
    cin >> S;
    rep(i,S.length()) {
        int p = alpha.find(S[i]) + N;
        if (25 < p) {
            p = p - 26;
        }
        S[i] = alpha[p];
    }
    cout << S << '\n';
}

Measured memory_bytes: 256
using System;
using static System.Console;
using System.Linq;

namespace Main
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] tmp = ReadLine().Split(' ').Select(int.Parse).ToArray();
            Array.Sort(tmp);
            WriteLine(tmp[0] + tmp[1]);
        }
    }
}

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

struct edge {
  int u, v;
};

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

  int rest = (N-1)*(N-2)/2 - K;
  if(rest < 0) {
    cout << "-1" << endl;
    return 0;
  }

  vector<edge> e;
  for(int i = 2; i <= N; i++) {
    e.push_back(edge{1,i});
  }

  int i = 2;
  int j = 3;
  while(rest > 0) {
    if(j > N) {
      i++;
      j = i + 1;
    }
    e.push_back(edge{i,j});
    j++;
    rest--;
  }

  int M = e.size();
  cout << M << endl;
  rep(i,M) {
    cout << e[i].u << " " << e[i].v << endl;
  }

  return 0;
}


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

int w;

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

  cin >> w; cout << w * 32 << "\n";
}


Measured memory_bytes: 3184
n = int(input())
a = list(map(int, input().split()))
s = sum(a)
k = 1 - a[0]
s -= a[0]
ans = 1

for i in range(1,n + 1):
    if k * 2 <= s:
        k *= 2
    else:
        k = s
    ans += k
    k -= a[i]
    s -= a[i]

if k == 0 and s == 0:
    print(ans)
else:
    print(-1)


Measured memory_bytes: 20124
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <math.h>
#include <string>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    cin >> s;
    for(int i = 0 ; i < s.length() ; i++){
        if( s[i] == s[i + 1] ){
            cout << i + 1 << " " << i + 2;
            return 0;
        }
        if( i < s.length() - 1 && s[i] == s[i + 2] ){
            cout << i + 1 << " " << i + 3;
            return 0;
        }
    }
    cout << "-1 -1";
}


Measured memory_bytes: 512
s=gets.chomp.split("")
flag=Array.new(4){false}
s.each do|c|
  case(c)
    when "W" then
      flag[0]=true
    when "E" then
      flag[1]=true
    when "N" then
      flag[2]=true
    when "S" then
      flag[3]=true
  end
end
if (flag[0] and flag[1]) or not(flag[0] or flag[1])
  if (flag[2] and flag[3]) or not(flag[2] or flag[3])
    puts "Yes"
  else
    puts "No"
  end

else
  puts "No"
end


Measured memory_bytes: 1788
#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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)*}
    };
}

#[allow(unused_macros)]
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, bytes) => {
        read_value!($next, String).into_bytes()
    };

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

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

fn main() {
    input! {
        s: chars
    }

    let mut ans = 0i64;
    let mut w = 0;

    for i in (0..s.len()).rev() {
        if s[i] == 'W' {
            w += 1;
        } else {
            ans += w;
        }
    }

    println!("{}", ans);
}


Measured memory_bytes: 4352
n, m = map(int,input().split())
A = [list(map(int,input().split())) for i in range(m)]

for i in range(m):
    A[i][2] = -A[i][2]

P = [float("inf")] * (n+1)
P[1] = 0
F = [[0] * 2 for i in range(n+1)]
F[1][0] = 1
F[n][1] = 1

for i in range(n):
    for j in range(m):
        if F[A[j][0]][0] == 1 :
            F[A[j][1]][0] = 1
        if F[A[j][1]][1] == 1 :
            F[A[j][0]][1] = 1
        P[A[j][1]] = min(P[A[j][1]], P[A[j][0]] + A[j][2])

ans0 = P[:]

for i in range(n):
    for j in range(m):
        if F[A[j][0]][0] == 1 :
            F[A[j][1]][0] = 1
        if F[A[j][1]][1] == 1 :
            F[A[j][0]][1] = 1
        P[A[j][1]] = min(P[A[j][1]], P[A[j][0]] + A[j][2])

ans1 = P[:]

#print(F)

def d():
    for i in range(n+1):
        if F[i] == [1, 1] and ans1[i] < ans0[i]:
            return "inf"
    return -ans0[n]

print(d())

Measured memory_bytes: 51672
#include <bits/stdc++.h>
using namespace std;
int main(){
    unsigned long long  n,k;
    cin >> n >> k;
    vector <unsigned long long> a(n);
    for(unsigned long long i = 0; i < n; i++){
        cin >> a[i];
    }
    for(unsigned long long i = k; i < n; i++){
        if(a[i]>a[i-k]){
            cout << "Yes" << endl;
        }else{
            cout << "No" << endl;
        }
    }
    return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n)-1; i >= 0; i--)

using namespace std;
using ll = long long;
typedef pair<int, int> Pair;

int main() {
  int n;

  cin >> n;
  int ans = pow(n,3);
  cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
 
int main(){
    vector<string> input(1000);
    int cnt = 0;
    for(int i = 0; i < 1000; i++) {
        string tmp;
        cin >> tmp;
        if(tmp == "0") {
            cnt = i ;
            break;
        }
        input[i] = tmp;
    }

    for(int i = 0; i < cnt; i++) {
        string tmp = input[i];
        int aaa = 0;
        for(int j = 0; j < tmp.length(); j++) {
            int intest = tmp[j] - '0';
            aaa += intest;
        }
        cout << aaa << endl;
    }
    return 0;
}


Measured memory_bytes: 3204
#include<iostream>
#include<cstdio>

using namespace std;

#define SIZE 110
#define ll long long int

ll stk[SIZE], top;

void push(ll x){
    if(top == 100) return;
    stk[++top] = x;
}

ll pop(){
    if(top == 0) return 0;
    return stk[top--];
}

int main(){
    int f = 0;
    ll sum = 0;
    char ch;
    while((ch = getchar()) != '\n'){
        if('0' <= ch && ch <= '9') { sum = sum * 10 + ch - '0'; f = 1; }
        else if(ch == ' ') { if(f) push(sum); f = 0; sum = 0; }
        else{
            ll a, b;
            b = pop(); a = pop();
            switch(ch){
                case '+': push(a + b); break;
                case '-': push(a - b); break;
                case '*': push(a * b); break;
            }
        }
    }
    cout << pop() << endl;
    return 0;
}


Measured memory_bytes: 3112
/* Murad Eynizade */

#include <bits/stdc++.h>
#define intt long long
#define FAST_READ ios_base::sync_with_stdio(0);cin.tie(0);
#define SIZE 100001
#define INF INT_MAX
#define F first
#define S second
#define in(a) scanf("%d",&a);
#define outn(a) printf("%d\n",&a);
#define outs(a) printf("%d ",&a);


using namespace std;

int a[100005];

int main()
{
    FAST_READ;
    int n , k;
    cin>>n>>k;
    for (int i = 0;i<n;i++)cin>>a[i];
    int in = 0;
    vector< int >neg,pos;
    while (a[in] <= 0 && in < n) {
        if (a[in] == 0) {
            k--;
            in++;
            break;
        }
        neg.push_back(abs(a[in]));
        in++;
    }
    reverse(neg.begin(),neg.end());
    while (in < n) {
        pos.push_back(a[in]);
        in++;
    }
    int sz = neg.size();
    int sz1 = pos.size();
    if (k == 0) {
        cout<<0<<endl;
        return 0;
    }
    if (sz == 0)cout<<pos[k - 1];
    else if (sz1 == 0)cout<<neg[k - 1]<<endl;
    else {
        int ans = INF;
        for (int i = 0;i<=k;i++) {
            if (i == 0 && sz1 >= k)ans = min(ans,pos[k - 1]);
            else if (i == k && sz >= k)ans = min(ans,neg[k - 1]);
            if (i > sz1 || k - i > sz)continue;
            else {
                ans = min(ans,pos[i - 1] + neg[k - i - 1] + min(pos[i - 1],neg[k - i - 1]));
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}


Measured memory_bytes: 1404
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main { // クラス名はMain
  public static void main(String[] args) throws Exception {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String s = reader.readLine();
    int min = Integer.MAX_VALUE;
    for (int i = 0; i < s.length() - 2; i++) {
      int tmp = Math.abs(753 - Integer.parseInt(s.substring(i,  i + 3)));
      if (tmp < min) {
        min = tmp;
      }
    }
    System.out.println(min);
  }
}


Measured memory_bytes: 22996
import numpy as np
num=int(input())
#valid=["M","A","R","C","H"]
arr=np.zeros(5)
#print(arr)

for i in range(num):
    chara=list(input())[0]
    if chara == "M":
        arr[0]+=1
    if chara == "A":
        arr[1]+=1
    if chara == "R":
        arr[2]+=1
    if chara == "C":
        arr[3]+=1
    if chara == "H":
        arr[4]+=1

ans=0
for i in range(3):
    for k in range(3-i):
        for j in range(3-k-i):
            ans+=arr[i]*arr[1+i+k]*arr[2+i+k+j]
            #print(arr[i]*arr[1+i+k]*arr[2+i+k+j])
print(int(ans))


Measured memory_bytes: 21504
class Array
  def abs; map{|k| k.abs } end
end

n = gets.to_s.to_i
a = gets.to_s.split.map{|t|t.to_i}

t = a.abs
m = t.min

ans = t.reduce(0){|s,t|s+t}
if m == 0
  puts ans
  exit
end

if a.count{|k| k < 0}.odd?
  puts ans - 2 * m
else
  puts ans
end

Measured memory_bytes: 9484
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define RNG(i,from,to) for(int i=(from); i<(int)(to); i++)
#define gcd(i,j) __gcd((i), (j))
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vecll;
template<typename S, typename T> string to_string(pair<S, T> p) { return "("+to_string(p.first)+","+to_string(p.second)+")"; }
template<typename T> string to_string(T p[2]) { return "["+to_string(p[0])+","+to_string(p[1])+"]"; }
template<typename T> void println(T e){ cout << to_string(e) << endl; }
template<typename T> void println(const vector<T>& v){ cout << "[";for(const auto& e : v){ cout << to_string(e) << ","; }cout << "]";cout << endl; }
template<typename T> void println(const vector<vector<T>>& vv){ for(const auto& v : vv){ println(v); } }
template<typename Iter> void println(Iter first, Iter last){ for(auto it=first; it != last; it++){ cout << *it << " "; }; cout << endl; }
template<typename T> T mod_pow(T x, T n, const T &p) { T ret = 1; while(n > 0) { if(n & 1) (ret *= x) %= p; (x *= x) %= p; n >>= 1; } return ret; }
template<typename T> T mod_inv(T x, const T &p) { return mod_pow(x, p-2, p); }
const ll DVSR = 1e9+7;


int main(int argc, char const *argv[])
{
  ll a,b,c,d; cin >> a >> b >> c >> d;
  if (((a-b)*(a-b) <= d*d && (b-c)*(b-c) <= d*d) || (a-c)*(a-c) <= d*d) {
    cout << "Yes" << endl;
  } else {
    cout << "No" << endl;
  }
  return 0;
}


Measured memory_bytes: 256
using System;

class Program
{
    static void Main()
    {
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j <= 9; j++)
            {
                Console.WriteLine("{0}x{1}={2}", i, j, i * j);
            }
        }
    }
}

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

using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)

int main(){
	char b;
	cin >> b;
	std:: map<char,char>mp;
	mp['A']='T';
	mp['T']='A';
	mp['C']='G';
	mp['G']='C';
	
	cout << mp[b]<<endl;
	return 0;
}

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

int main(void){
  string s;
  cin >> s;
  bool ans = 1;
  int size = s.size()/2;
  for(int i = 0; i < size; i++){
    if(s.at(i) != s.at(s.size()-1-i)) ans = 0;
  }
  int size2 = (s.size() - 1)/2/2;
  for(int i = 0; i < size2; i++){
    if(s.at(i) != s.at((s.size()-1)/2-i-1)) ans = 0;
  }
  int j = 0;
  for(int i = (s.size()+3)/2 - 1; i < (s.size()+3)/2+(s.size() - 1)/2/2; i++){
    if(s.at(i) != s.at(s.size()-j-1)) ans = 0;
    j++;
  }
  if(ans) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}


Measured memory_bytes: 256
from bisect import bisect_left, bisect_right

n = int(input())
a = sorted(list(map(int, input().split())))
b = list(map(int, input().split()))
c = sorted(list(map(int, input().split())))

ans = 0
for i in b:
    ans += bisect_left(a, i) * (n - bisect_right(c, i))

print(ans)

Measured memory_bytes: 23564
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <list>
#include <map>

using namespace std;

#define int long long

int32_t main() {
  int n, k;
  cin >> n >> k;
  int arr[n];
  long long ans = 0;
  for (int i = 0; i < n; i++) cin >> arr[i], ans += arr[i];
  if (!(n <= k)) ans = 0ll;
  for (int i = 0; i < n; i++) {
    for (int j = i; j < n; j++) {
      if (n - (j - i + 1) > k) continue;
      int here = 0;
      multiset <int> ms;
      for (int k = 0; k < n; k++) {
        if (k >= i && k <= j) {

        } else {
          ms.insert(arr[k]);
          here += arr[k];
        }
      }
      int tmp = k - (n - (j - i + 1));
      while (tmp-- && !ms.empty()) {
        if (*ms.begin() < 0) {
          here -= *ms.begin();
          ms.erase(ms.begin());
        } else break;
      }
      ans = max(ans, here);
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
/*
    if you can't see the repay
    Why not just work step by step
    rubbish is relaxed
    to ljq
*/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#include <vector>
#include <stdlib.h>
#include <algorithm>
using namespace std;

#define dbg(x) cout<<#x<<" = "<< (x)<< endl
#define dbg2(x1,x2) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<endl
#define dbg3(x1,x2,x3) cout<<#x1<<" = "<<x1<<" "<<#x2<<" = "<<x2<<" "<<#x3<<" = "<<x3<<endl
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
#define lc (rt<<1)
#define rc (rt<<11)
#define mid ((l+r)>>1)

typedef pair<int,int> pll;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll _INF = 0xc0c0c0c0c0c0c0c0;
const ll mod =  (int)1e9+7;

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}
void exgcd(ll a,ll b,ll &x,ll &y,ll &d){if(!b) {d = a;x = 1;y=0;}else{exgcd(b,a%b,y,x,d);y-=x*(a/b);}}//printf("%lld*a + %lld*b = %lld\n", x, y, d);
const int MAX_N = 3005;
ll dp[MAX_N][MAX_N],arr[MAX_N];
int main()
{
    //ios::sync_with_stdio(false);
    //freopen("a.txt","r",stdin);
    //freopen("b.txt","w",stdout);
    int n;
    scanf("%d",&n);
    for(int i = 1;i<=n;++i) scanf("%d",&arr[i]);
    for(int L = n;L>=1;--L)
    {
        for(int R = L;R<=n;++R)
        {
            dp[L][R] = max(arr[L]-dp[L+1][R],arr[R]-dp[L][R-1]);
        }
    }
    printf("%lld\n",dp[1][n]);
    //fclose(stdin);
    //fclose(stdout);
    //cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}


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

N, M = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(M):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    #graph[b-1].append(a-1)
S, T = map(int, input().split())
S -= 1; T -= 1

D = [[-1]*3 for _ in range(N)]
D[S][0] = 0
q = [(S, 0)]
while q:
    qq = []
    for p, d in q:
        for np in graph[p]:
            nd = (d+1)%3
            if D[np][nd] == -1:
                D[np][nd] = D[p][d] + 1
                qq.append((np, nd))
    q = qq

if D[T][0] == -1:
    print(-1)
else:
    print(D[T][0]//3)

Measured memory_bytes: 42184
#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <map>
#include <math.h>
#include <iomanip>
#include <vector>
#include <queue>
#include <functional>
#include <random>
#include <time.h>
#include <cmath>
#include <cstdio>
#include <complex>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;

int main() {
	ll n, m, x;
	vector<ll> a;
	map<ll, ll> mp;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> x;
		a.push_back(x);
		if (i)a[i] = x + a[i - 1];
		a[i] %= m;
		mp[a[i]]++;
	}
	mp[0]++;
	ll ans = 0;
	sort(a.begin(), a.end());
	a.erase(unique(a.begin(), a.end()), a.end());
	for (int i = 0; i < a.size(); i++)if (mp[a[i]])ans += (mp[a[i]] - 1)*mp[a[i]] / 2;
	cout << ans << endl;
	return 0;
}

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

#define REP(i, n) for(int (i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for(int (i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for(int (i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9+7;

int dp1[3005][3005], dp2[3005][3005];

int main(){
  int N, T;
  cin >> N >> T;
  vector<pair<int, int>> pr(N);
  REP(i, N){
    int A, B;
    cin >> A >> B;
    pr[i] = {A, B};
  }
  REP(i, N){
    auto [A, B] = pr[i];
    REP(j, T){
      dp1[i+1][j] = max(dp1[i+1][j], dp1[i][j]);
      if(j+A > T) continue;
      dp1[i+1][j+A] = max(dp1[i+1][j+A], dp1[i][j]+B);
    }
  }
  for(int i = N; i > 0; i--){
    auto [A, B] = pr[i-1];
    REP(j,T){
      dp2[i-1][j] = max(dp2[i-1][j], dp2[i][j]);
      if(j+A > T) continue;
      dp2[i-1][j+A] = max(dp2[i-1][j+A], dp2[i][j]+B);
    }
  }
  int ans = 0;
  REP(i, N){
    REP(j, T){
      int score = pr[i].second + dp1[i][j] + dp2[i+1][T-1-j];
      ans = max(ans, score);
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 74032
#include <iostream>
#include <sstream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <map>

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
typedef long long ll;

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

using namespace std;

const ll BIG = 1000000007LL;

int inputValue(){
    int a;
    cin >> a;
    return a;
};

template<typename T>
void inputArray(T* p, int a){
    rep(i, a){
        cin >> p[i];
    }
};

template<typename T>
void inputVector(vector<T> * p, int a){
    rep(i, a){
        T input;
        cin >> input;
        p -> push_back(input);
    }
}

template <typename T>
void output(T a, int precision = 0) {
    if(precision > 0){
        cout << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

const int Max = 110;
int M, D;

int main(int argc, const char * argv[]) {
    cin >> M >> D;
    int ans = 0;
    repd(m, 1, M+1) {
        repd(d, 1, D+1) {
            int d10 = d / 10;
            int d1 = d - d10*10;
            if (d10 == 1 || d1 == 1) continue;
            if (m == d1*d10) {
                ans++;
                // printf("%d,%d,%d,%d\n", m, d, d10, d1);
            }
        }
    }
    output(ans);

    return 0;
}

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

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

typedef long long ll;

const int mod = 1e9 + 7;

using namespace std;

ll comb(int n, int r) {
	ll res=1;
	for (int i = 1; i <= r; i++) {
		res = res * (n - i + 1) / i;
	}
	return res;
}

ll pow2(int n) {
	if (n == 1)return 2;
	if (n == 0)return 1;

	auto temp = pow2(n / 2);
	if (n % 2 == 0) return temp * temp;
	else return temp * temp * 2;
}

ll solver(int N, int P, const vector<int> & A) {
	int even = 0, odd = 0;
	for (auto x : A) {
		if (x % 2 == 0)even++;
		else odd++;
	}
	ll even2 = pow2(even);

	ll ans = 0;
	for (int i = P; i <= N; i += 2) {
		ans += comb(odd, i)*even2;
	}

	return ans;
}

int main(void) {
	int N; cin >> N;
	int P; cin >> P;
	vector<int> A(N);
	rep(i, N)cin >> A[i];


	cout << solver(N, P, A) << endl;

	return 0;
}

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

const ll MOD=(ll)1e9+7;
int n;
string s[2];

int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout<<fixed;
#ifdef LOCAL_DEFINE
    freopen("in", "r", stdin); 
    freopen("out","w",stdout);
#endif
  cin>>n;
  cin>>s[0];
  cin>>s[1];
  int i=0;      
  int pre=0;
  ll ans=1;
  while(i<n){
    if(s[0][i]==s[1][i]){
      if(pre==0){
        ans=3;
        pre=1;
      }else if(pre==1){
        ans=((ans%MOD)*2LL)%MOD;
        pre=1;
      }else {
        pre=1;
      }
      i++;
    }else {
      if(pre==0){
        ans=6;
        pre=2;
      }else if(pre==1){
        ans=((ans%MOD)*2LL)%MOD;
        pre=2;
      }else {
        ans=((ans%MOD)*3LL)%MOD;
        pre=2;
      }
      i+=2;
    }
  }
  cout<<ans%MOD<<endl;
#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
  return 0;
}

Measured memory_bytes: 256
from fractions import gcd
N=int(input())
A=list(map(int,input().split()))
mod=10**9+7
M=1
for a in A:
    M=((M * a) // gcd(M, a))
M%=mod

count=0

for a in A:
    count=(count+M*pow(a,mod-2,mod))%mod
print(count)


Measured memory_bytes: 67416
#include<stdio.h>
#include<string.h>
 
#define M 1046527
#define NTL (-1)
#define L 14
 
char H[M][L];
 
int getChar(char ch) {
  if ( ch == 'A' ) return 1;
  else if ( ch == 'C' ) return 2;
  else if ( ch == 'G' ) return 3;
  else if ( ch == 'T' ) return 4;
  else return 0;
}
 
long long getKey(char str[]) {
  long long sum = 0, p = 1, i;
  for ( i=0; i<strlen(str); i++ ) {
   sum += p*(getChar(str[i]));
   p *= 5;
 }
 return sum;
}
 
int h1(int key){ return key % M; }
int h2(int key){ return 1 + (key % (M - 1));}
 
int find(char str[]) {
long long key, i, h;
key = getKey(str);
for ( i = 0;; i++ ) {
 h = (h1(key) + i * h2(key)) % M;
 if ( strcmp(H[h],str) == 0 ) return 1;
 else if ( strlen(H[h]) == 0 ) return 0;
  }
  return 0;
}
 
int insert(char str[]) {
 long long key, i, h;
 key = getKey(str);
 for ( i = 0; ; i++ ) {
  h = (h1(key) + i * h2(key)) % M;
  if ( strcmp(H[h], str) == 0 ) return 1;
  else if ( strlen(H[h]) == 0 ) {
   strcpy(H[h], str);
   return 0;
   }
  }
return 0;
}
int main() {
int i, n, h;
char str[L], com[9];
for ( i = 0; i < M; i++ ) H[i][0] = '\0';
scanf("%d", &n);
for ( i = 0; i < n; i++ ){
  scanf("%s %s", com, str);
 
  if ( com[0] == 'i' ){
    insert(str);
  } else {
   if ( find(str) ) {
    printf("yes\n");
   } else {
    printf("no\n");
   }
  }
 }
 return 0;
}


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

using namespace std;

int Prime[100000]={};

void pr(){
  Prime[0]=2;
  int tmp=1,num=1;
  while(num<100000){
    num+=2;
    int flag=0;
    for(int i=3;i<=sqrt(num);i+=2)
      if(num%i==0) flag++;
    if(flag==0) Prime[tmp++]=num;
  }
}

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

int main(){
  pr();
  int p,q; cin>>p>>q;
  int a=q/gcd(p,q);
  //while(sqrt(a)*sqrt(a)==a) a=sqrt(a);
  for(int i=0;a/Prime[i]>=Prime[i];++i){
    int tmp=a;
    while(a%Prime[i]==0) a=a/Prime[i];
    if(tmp!=a) a*=Prime[i];
  }
  cout << a << endl;

  
  return 0;
}



Measured memory_bytes: 3144
#include<iostream>
using namespace std;
#define MAX 100000
typedef long long llong;

int n, k;
llong T[MAX];

//最大積載量Pのk台のトラックで何個の荷物を積めるか？
int check(llong P) {
    int i=0;
    for(int j=0;j<k;j++){
        llong s = 0; 
        while(s+T[i]<=P){
            s += T[i];
            i++;
            if ( i == n ) return n;    
        } 
    }
    return i;
}

int solve() {
    llong left=0;
    llong right=100000*10000;  //荷物の個数×１個あたりの最大重量
    llong mid;
    while(right-left>1){
        mid = (left + right) / 2;
        int v = check(mid);  //mid == Pを決めて何個積めるかチェック
        if(v>=n)right=mid;
        else left = mid;
    } 

    return right;
}

int main() {
    cin>>n>>k;
    for(int i=0;i<n;i++)cin>>T[i];
    llong ans = solve();
    cout << ans << endl;
}



Measured memory_bytes: 3864
#include <stdio.h>
#define N_MAX 100

int main(){
  int i, j, minj, count = 0, n, tmp, a[N_MAX];

  scanf("%d", &n);
  for(i = 0; i < n; i++){
    scanf("%d", &a[i]);
  }

  for(i = 0; i < n; i++){
    minj = i;
    for(j = i; j < n; j++){
      if(a[j] < a[minj]){
	minj = j;
      }
    }
    if(i != minj){
      tmp = a[i];
      a[i] = a[minj];
      a[minj] = tmp;
      count++;
    }
  }

  for(i = 0; i < n; i++){
    printf("%d", a[i]);
    if(i != n-1) printf(" ");
  }
  printf("\n%d\n", count);

  return 0;
}



Measured memory_bytes: 2104
import java.util.*;
public class Main {
	static int a[];
	static int max;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		a = new int[n];
		for(int i=0;i<n;i++)a[i]=sc.nextInt();
		Arrays.sort(a);
		int len = a.length;
		max = a[len-1];
		
		int l=-1;
		int r=len;
		while(r>l+1) {
			int c = (l+r)/2;
			if(f(c)) r = c;
			else l = c;
		}
		int y = 0;
		int z = 0;
		if(Math.abs(2*a[l]-max)<Math.abs(2*a[r]-max)) {
			y = a[l];
			z = a[r];
		}
		else {
			y = a[r];
			z = a[l];
		}
		
		if(y==max) {
			y=z;
		}
		
		System.out.println(max+" "+y);
		
	}
	
	static boolean f(int c) {
		if (a[c]>max/2) return true;
		else return false;
	}
	
}


Measured memory_bytes: 50456
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
typedef long long int ll;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define in insert
#define For(i,n) for(ll i = 0 ; i < n ; i++)

int main(){
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);cout.tie(NULL);
  string s;
  cin>>s;
  ll a = 0 , b = 0;
  For(i,3){
    if(s[i] == 'A')a++;
    else b++;
  }
  if(a && b )cout<<"Yes";
  else cout<<"No";
}

Measured memory_bytes: 256
#include<iostream>
#include<cmath>
#include<vector>
#include<climits>
#include<algorithm>
#include<map>
using namespace std;
#define int long long
const int Mod = (int)1e9;
int CaseN=1;
int fast_power(int a,int b,int mod){
	int ans=1;
	while(b>0){
	if(b&1){
	ans=(a*ans)%mod;
	}
	b=b>>1;
	a=(a*a)%mod;
	}
	return ans;
	}


int nCr(int n ,int r)
{
int p= n;
r = min(r,n-r);
int a =1;
int b=1;	
while(r)
{
	a = a*p;
	b = b*r;
	
	int g = __gcd(a,b);
	
	a = a/g;
	b = b/g;
	
	p--;
	r--;	
}	
return a;
	
}


void solve()
{
int h1,m1,h2,m2,k;
cin>>h1>>m1>>h2>>m2>>k;
int sum = (h2-h1)*60;
sum += (m2-m1) ;
int ans ;

ans = (sum-k);
if(ans<0)
{
ans =0;	
}
cout<<ans<<endl;
return ;
}


signed main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t=1;
	//cin>>t;
	while(t--)
	{
	solve();	
	}
}


Measured memory_bytes: 3640
#include <bits/stdc++.h> 
using namespace std; 
typedef long long ll;
const int mxN=1e9;
const ll mod = 1e9+7;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	
    string s,t;
    cin>>s>>t;

    int dp[s.size()+1][t.size()+1];
    memset(dp,0,sizeof(dp));

    for(int i=1;i<=s.size();i++){
        for(int j=1;j<=t.size();j++){
            dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
            if(s[i-1]==t[j-1]) dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
        }
    }

    string ans = "";

    int i = s.size();
    int j = t.size();

    while(j>0){
        if(dp[i][j-1]==dp[i][j])j--;
        else if(dp[i][j]==dp[i-1][j])i--;
        else if(dp[i][j]==dp[i-1][j-1]+1){
            ans=t[j-1]+ans;
            j--;
            i--;
        }
    }

    cout<<ans<<endl;
}

Measured memory_bytes: 38704
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
#include <memory>
#include <time.h>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
const double EPS = 1e-9;
const double PI  = acos(-1.0);

int main(){
	int init_hp,max_hp;
	while(cin>>init_hp>>max_hp,init_hp|max_hp){
		int r,c;
		cin>>r>>c;
		vs a(r,string(c,' '));
		REP(i,r){
			cin>>a[i];
		}
		int t;
		cin>>t;
		vi trap_damages('Z'+1);
		REP(i,t){
			char c;
			cin>>c;
			cin>>trap_damages[c];
		}
		int s;
		cin>>s;
		int total_steps=0;
		int cnt=0;
		vector<pii> steps(1000);
		int y=0,x=0;
		REP(i,s){
			char direction;
			int step;
			cin>>direction>>step;
			int dy=0,dx=0;
			switch(direction){
			case 'U':
				dy=-1;break;
			case 'D':
				dy=1;break;
			case 'L':
				dx=-1;break;
			case 'R':
				dx=1;break;
			}
			REP(j,step){
				y+=dy;
				x+=dx;
				steps[cnt].first=y;
				steps[cnt].second=x;
				cnt++;
			}
			total_steps+=step;
		}
		int p;
		cin>>p;
		vi potions(p);
		REP(i,p){
			cin>>potions[i];
		}

		vi cured_hp_for_potion_mask(1<<p);
		REP(i,1<<p){
			REP(j,p){
				if(i&(1<<j)){
					cured_hp_for_potion_mask[i]+=potions[j];
				}
			}
		}

		vvi hp(total_steps+1,vi(1<<p));
		hp[0][0]=init_hp;
		REP(step,total_steps){
			char trap=a[steps[step].first][steps[step].second];
			int damage=trap_damages[trap];
			REP(potion_mask,1<<p){
				if(hp[step][potion_mask]<=0){
					continue;
				}
				if(hp[step][potion_mask]<=damage){
					REP(used_potion_mask,1<<p){
						if((used_potion_mask&potion_mask)==0){
							int new_hp=min(hp[step][potion_mask]+cured_hp_for_potion_mask[used_potion_mask],max_hp)-damage;
							hp[step+1][potion_mask|used_potion_mask]=max(hp[step+1][potion_mask|used_potion_mask],new_hp);
						}
					}
				}else{
					hp[step+1][potion_mask]=max(hp[step+1][potion_mask],hp[step][potion_mask]-damage);
				}
			}
		}
		int ans=0;
		REP(potion_mask,1<<p){
			ans=max(ans,hp[total_steps][potion_mask]);
		}
		cout<<(ans>0?"YES":"NO")<<endl;
	}

}

Measured memory_bytes: 17188
def main():

    n = int(input())
    A = list(map(int, input().split()))
    INF = 19**18

    X = []
    Y = []
    A = [INF] + A
    for i in range(1, n):
        if len(X) == len(Y):
            if A[i-1] >= A[i] and A[i] < A[i+1]:
                X.append(i)
        if len(X) > len(Y):
            if  A[i-1] <= A[i] and A[i] > A[i+1]:
                Y.append(i)
    else:
        if len(X) > len(Y):
            Y.append(i+1)
    #print(X)
    #print(Y)

    cur = 1000
    for i, j in zip(X, Y):
        q, cur = divmod(cur, A[i])
        cur += q*A[j]
    print(cur)



if __name__ == '__main__':
    main()


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

using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m;
	cin >> n >> m;

	vector<int> in;
	vector<vector<int> > G;
	in.resize(n);
	G.resize(n);

	set<pair<int, int> > s;

	for(int i = 0; i < m; ++i) {
		int a, b;
		cin >> a >> b;
		--a;
		--b;
	
		if(a > b) {
			swap(a, b);
		}

		++in[a];
	
		G[a].emplace_back(b);
		G[b].emplace_back(a);

		s.emplace(a, b);
	}

	vector<int> dep;
	vector<vector<int> > fa;
	dep.resize(n);
	fa.resize(n);
	for(int i = 0; i < n; ++i) {
		fa[i].assign(21, -1);
	}

	vector<bool> vis;
	vis.resize(n);
	vector<vector<int> > g;
	g.resize(n);

	function<void(int)> dfs = [&] (int u) {
		vis[u] = true;
		for(auto v : G[u]) {
			if(!vis[v]) {
				g[u].emplace_back(v);
				s.erase(minmax(u, v));
				dep[v] = dep[u] + 1;
				fa[v][0] = u;
				dfs(v);
			}
		}
	};

	dfs(0);

	for(int j = 1; j < 21; ++j) {
		for(int i = 0; i < n; ++i) {
			if(fa[i][j - 1] != -1) {
				fa[i][j] = fa[fa[i][j - 1]][j - 1];
			}
		}
	}

	auto lca = [&] (int u, int v) {
		if(dep[u] < dep[v]) {
			swap(u, v);
		}

		int d = dep[u] - dep[v];

		for(int i = 20; i >= 0; --i) {
			if(d >> i & 1) {
				u = fa[u][i];
			}
		}

		if(u == v) {
			return u;
		}

		for(int i = 20; i >= 0; --i) {
			if(fa[u][i] != fa[v][i]) {
				u = fa[u][i];
				v = fa[v][i];
			}
		}

		return fa[u][0];
	};

	vector<int> st;

	for(int i = 0; i < n; ++i) {
		if(in[i] & 1) {
			st.emplace_back(i);
		}
	}

	if(st.size() & 1) {
		cout << -1 << '\n';
		return 0;
	}

	vector<int> sum;
	sum.resize(n);

	while(!st.empty()) {
		int u = st.back();
		st.pop_back();
		
		int v = st.back();
		st.pop_back();

		int w = lca(u, v);
		
		sum[w] -= 2;
		sum[u] += 1;
		sum[v] += 1;
	}

	function<void(int, int)> Dfs = [&] (int u, int f) {
		for(auto v : g[u]) {
			if(v != f) {
				Dfs(v, u);
				sum[u] += sum[v];
				
				int a = u;
				int b = v;
				if(a > b) {
					swap(a, b);
				}

				if(sum[v] & 1) {
					s.emplace(b, a);
				} else {
					s.emplace(a, b);
				}
			}
		}
	};

	Dfs(0, -1);

	for(auto& p : s) {
		cout << p.first + 1 << ' ' << p.second + 1 << '\n'; 
	}

	return 0;
}

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

int main() {
    int N, K;
    cin >> N >> K;
    vector<int> d(K);
    vector<vector<int>> A;
    for (int i=0;i<K;i++) {
        cin >> d.at(i);
        A.push_back(vector<int>(d.at(i)));
        for (int j=0;j<d.at(i);j++) {
            cin >> A.at(i).at(j);
        }
    }
    vector<int> A_count(N,0);
    for(int i=0;i<K;i++){
        for (int j=0;j<d.at(i);j++){
            A_count.at(A.at(i).at(j)-1)++;
        }
    }
    int count=0;
    for (int i=0;i<N;i++) {
        if (A_count.at(i)==0) count++;
    }
    cout << count << endl;
}

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

using namespace std;
 
typedef long long ll;
typedef long double ld;
typedef pair<int, int> Pii;
typedef pair<int, ll> Pil;
typedef pair<ll, ll> Pll;
typedef pair<ll, int> Pli;

#define fi first
#define se second

const ll MOD = 1e9 + 7;
const ll MOD2 = 998244353;
const ll MOD3 = 1812447359;
const ll INF = 1ll << 62;
const double PI = 2 * asin(1);

void yes() {printf("yes\n");}
void no() {printf("no\n");}
void Yes() {printf("Yes\n");}
void No() {printf("No\n");}
void YES() {printf("YES\n");}
void NO() {printf("NO\n");}

int N, M, G[int(1e5+5)];
ll A[int(1e5+5)];
Pii P[int(1e5+5)];
vector <ll> now[int(1e5+5)];

void Unite(){
  int X, Y; cin >> X >> Y;
  int GX = G[X], GY = G[Y];
  while (GX != G[GX]) GX = G[GX];
  while (GY != G[GY]) GY = G[GY];

  G[max(GX, GY)] = min(GX, GY);
}

int main(){
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> A[i]; G[i] = i;
  }
  for (int i = 0; i < M; i++) Unite();
  for (int i = 0; i < N; i++){
    int Gi = G[i];
    while (Gi != G[Gi]) Gi = G[Gi];
    G[i] = Gi;
    now[G[i]].push_back(A[i]);
    P[G[i]].fi++; P[i].se = i;
  }
  sort(P, P + N, greater<Pii>());

  priority_queue <ll, vector<ll>, greater<ll> > que;
  for (int i = 0; i < (int)now[P[0].se].size(); i++) que.push(now[P[0].se][i]);
  
  ll ans = 0;
  for (int i = 1; i < N; i++){
    if (now[P[i].se].size() == 0) continue;

    if (que.size() == 0){
      cout << "Impossible" << endl;
      return 0;
    }
    ans += que.top(); que.pop();
    sort(now[P[i].se].begin(), now[P[i].se].end());
    ans += now[P[i].se][0];

    for (int j = 1; j < now[P[i].se].size(); j++){
      que.push(now[P[i].se][j]);
    }
  }
  cout << ans << endl;

  return 0;
}


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

void win(int id){
    cout << id+1 << endl;
    exit(0);
}

int main(){
    int n, t, h, l;
    cin >> n >> t >> h >> l;
    vector<int> a(n), b(n);
    for(int i = 0; i < n; i++)  cin >> a[i] >> b[i];
    int turn = 0, acc = 0;
    while(true){
        if(a[turn]+b[turn] == 0)    win(turn);
        if(a[turn]){
            a[turn]--;
            acc += 1;
            t++;
            if(t > l)   win(turn);
        }else{
            b[turn]--;
            acc += 10;
            h++;
        }
        if(acc >= 9){
            t -= acc-9;
            if(t < 0)   win(turn);
            a[turn] += acc-9;
            acc = 0;
        }
        turn++;
        if(turn == n)   turn = 0;
    }
}


Measured memory_bytes: 3100
#include <iostream>
#include <algorithm>

using namespace std;

int binary_search(long long n, long long *a, long long k) {
    int l = 0, r = n, mid;  //l以上r未満にkがあるとする

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

        if (a[mid] == k)
            return mid;
        else if (a[mid] < k)
            l = mid+1;
        else
            r = mid;
    }

    return l;
}

int main() {
    long long n, q, *a, *t;
    cin >> n;
    a = new long long[n];
    for (int i = 0; i < n; i++)
        cin >> a[i];
    cin >> q;
    t = new long long[q];
    for (int i = 0; i < q; i++)
        cin >> t[i];

    sort(a, a+n);
    sort(t, t+q);

    int ans = 0;
    for (int i = 0; i < q; i++) {
        if (a[binary_search(n, a, t[i])] == t[i])
            ans++;
    }

    cout << ans << endl;

    return 0;
}



Measured memory_bytes: 4332
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define MP make_pair
#define PII pair<int, int>
#define PLL pair<ll, ll>
#define PLI pair<ll, int>
#define PIL pair<int, ll>
#define STR string
#define Fst first
#define Snd second
#define SHN 1e9
#define LSHN 1e18
using namespace std;
int n,m;
int main()
{
	cin >>n>>m;
	cout <<(m>=n ? "unsafe" : "safe");
	return 0;
} 

Measured memory_bytes: 3636
#include<bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=(a),ed=(b);i<=ed;++i)
typedef long long ll;
inline int read(){
    register int x=0,f=1;register char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=0;ch=getchar();}
    while(isdigit(ch)){x=x*10+(ch^'0');ch=getchar();}
    return f?x:-x; 
}

const int N=2e5+10;
int n,m,cnt[N][2],del[N];
vector<int> E[N];
char s[N];

inline void Dfs(int u){
    REP(i,0,E[u].size()-1){
	int v=E[u][i];
	--cnt[v][s[u]-'A'];
	if(!del[v]&&!cnt[v][s[u]-'A'])del[v]=1,Dfs(v);
    }
}

int main(){
    //freopen("in.in","r",stdin);
    n=read(),m=read();scanf("%s",s+1);
    REP(i,1,m){int u=read(),v=read();E[u].push_back(v),E[v].push_back(u);++cnt[u][s[v]-'A'],++cnt[v][s[u]-'A'];}
    REP(i,1,n)if(!del[i]&&!(cnt[i][0]&&cnt[i][1]))del[i]=1,Dfs(i);
    REP(i,1,n)if(!del[i]){puts("Yes");return 0;}
    puts("No");
    return 0;
}


Measured memory_bytes: 14592
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <list>
#include <ctime>
#include <sstream>
#include <queue>
#include <stack>
#include <random>
#include <bitset>
#include <numeric>
#include <cassert>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long ll;
#define rep(x, b, e) for(int x=(b); x<(e); ++x)
#define ford(x, b, e) for(int x=((int)(b))-1; x>=(e); --x)
#define all(c) c.begin(),c.end()
#define sz(x) ((int)((x).size()))
#define pb push_back
#define st first
#define nd second
#define mp(x,y) make_pair(x,y)
typedef short int sint;

#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it) {
	*this << ((it != d.b) ? ", " : "") << *it;
  }
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

const int nax= 2e5+10;
char tab[nax];
int d[nax];

int main() {
	ios_base::sync_with_stdio(false);
	scanf("%s", tab + 1);
	int n = strlen(tab + 1);
	d[0] = 0;
	rep(i,1,n+1) {
		if (tab[i] != '?') {
			continue;
		}
		tab[i] = 'D';
	}
	printf("%s\n", tab + 1);
}

Measured memory_bytes: 3988