akhauriyash/Code-Regression huggingface dataset:
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		loop: while (true) {
			Point2D.Double[] points = new Point2D.Double[4];
			int maxx = Integer.MIN_VALUE;
			int maxy = Integer.MIN_VALUE;
			int minx = Integer.MAX_VALUE;
			int miny = Integer.MAX_VALUE;
			for (int i = 0; i < 4; i++) {
				int x = scanner.nextInt();
				int y = scanner.nextInt();
				if (i == 0 && x == 0 && y == 0) {
					break loop;
				}
				points[i] = new Point2D.Double(x, y);
				if (i != 3) {
					maxx = Math.max(maxx, x);
					maxy = Math.max(maxy, y);
					minx = Math.min(minx, x);
					miny = Math.min(miny, y);
				}
			}
			double r = scanner.nextDouble();
			Line2D.Double a = new Line2D.Double(points[1], points[2]);
			Line2D.Double b = new Line2D.Double(points[2], points[0]);
			Line2D.Double c = new Line2D.Double(points[0], points[1]);
			Ellipse2D.Double circle = new Ellipse2D.Double(points[3].x,
					points[3].y, r * 2, r * 2);
			double d0 = points[3].distance(points[0]);
			double d1 = points[3].distance(points[1]);
			double d2 = points[3].distance(points[2]);
			double ap = a.ptSegDist(points[3]);
			double bp = b.ptSegDist(points[3]);
			double cp = c.ptSegDist(points[3]);
			Path2D.Double path = new Path2D.Double();
			path.moveTo(points[0].x, points[0].y);
			path.lineTo(points[1].x, points[1].y);
			path.lineTo(points[2].x, points[2].y);
			if (d0 <= r && d1 <= r && d2 <= r) {
				System.out.println("b");
			} else if (ap >= r && bp >= r && cp >= r) {
				if (path.contains(points[3])) {
					System.out.println("a");
				} else {
					if (ap == r || bp == r || cp == r) {
						System.out.println("c");
					} else {
						System.out.println("d");
					}
				}
			} else {
				System.out.println("c");
			}

		}
	}
}

Measured memory_bytes: 23200
#include<stdio.h>
#include<algorithm>
using namespace std;
int v[]={1,5,10,50,100,500};
int b[10];
int dp[7][30][1010];
int main(){
	int a;
	while(scanf("%d",&a),a){
		for(int i=0;i<6;i++)scanf("%d",b+i);
		int ret=0;
		for(int i=5;i>=0;i--){
			while(b[i]&&a>=v[i]){
				a-=v[i];b[i]--;ret++;
			}
			if(b[i])break;
		}
		int val=999999999;
		for(int i=0;i<7;i++)for(int j=0;j<30;j++)for(int k=0;k<1010;k++)
			dp[i][j][k]=99999999;
		dp[0][0][0]=0;
		for(int i=0;i<6;i++){
			for(int j=0;j<29;j++){
				for(int k=0;k<1010;k++){
					if(dp[i][j][k]>99999)continue;
					dp[i+1][j][k]=0;
					if(k+v[i]<1010)dp[i][j+1][k+v[i]]=min(dp[i][j+1][k+v[i]],dp[i][j][k]+1);
				}
			}
		}
		for(int i=a;i<1010;i++){
			int tmp=9999999;
			for(int j=0;j<30;j++)if(dp[6][j][i]<9999){tmp=j;break;}
			int re=i-a;
			for(int j=5;j>=0;j--){
				tmp+=re/v[j];
				re%=v[j];
			}
			val=min(val,tmp);
		}
		printf("%d\n",ret+val);
	}
}

Measured memory_bytes: 1856
import numpy as np
from functools import reduce

n = int(input())

X = 35
i = np.arange(X, dtype=np.int64)[::-1]
# 別法：i = np.flip(np.arange(X))

i = (-2)**i

ans = np.zeros(X, dtype=np.int64)

for digit in range(X):

	i[digit]
	sum_of_opposite = i[digit+1::2].sum() # 反対符号の和

	if i[digit] > 0 and i[digit] + sum_of_opposite > n:
		ans[digit] = 0
	elif i[digit] < 0 and i[digit] + sum_of_opposite < n:
		ans[digit] = 0
	else:
		ans[digit] = 1
		n -= i[digit]

# ansの先頭の0を削除して、桁をつなげる
print(int(reduce(lambda x, y: str(x) + str(y), ans)))


Measured memory_bytes: 12512
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD = 1e9+7,MAX = 1e5;

struct food
{
	ll a,b,id,div;
};

bool cmp(food a,food b)
{
	if(a.div!=b.div)return a.div>b.div;
	if(a.a!=b.a)return a.a>b.a;
	return a.b > b.b;
}

int main()
{
	int n;
	cin >> n;
	vector<food> tkhs(n),aok(n);
	vector<ll> ttoi(n),atoi(n);

	ll ans = 0;
	for(int i =0;i<n;i++)
	{
		ll a,b;
		cin >> a >> b;
		tkhs[i].a = a;
		tkhs[i].b = b;
		tkhs[i].id = i;
		tkhs[i].div = a+b;
		ans -= b;
	}
	sort(tkhs.begin(),tkhs.end(),cmp);
	//for(int i = 0;i<n;i++){cout<<tkhs[i].a<<';'<<tkhs[i].b<<endl;}
	//cout<<ans<<endl;
	for(int i = 0;i<n;i++)
	{
		if(i%2==0)ans += tkhs[i].div;
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 8064
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr int Inf = 1000000010;
constexpr ll INF= 1000000000000000000;
constexpr ll MOD = 1000000007;
const double PI = 3.1415926535897;
typedef pair<int,int> P;

template<typename T>
T Pow(T a,T b) {
    T ret = 1;
    for(int i = 0;i < b;i++) {
        ret *= a;
    }
    return ret;
}

ll mod(ll val) {
    ll res = val % MOD;
    if(res < 0) {
        res += MOD;
    }
    return res;
}

ll RS(ll N, ll P, ll M){
    if(P==0) return 1;
    if(P < 0) {
        return 0;
    }
    if(P%2==0){
        ll t = RS(N, P/2, M);
        return t*t % M;
    }
    return N * RS(N, P-1, M) % M;
}

string S,T;
int dp[3030][3030]; //dp[i][j] = Sのi文字目、Tのj文字目までのLCS
string ret = "";

void restore(int a,int b,int cnt) {
    for(int i = a;i >= 1;i--) {
        for(int j = b;j >= 1;j--) {
            if(dp[i][j] == cnt && S.at(i - 1) == T.at(j - 1)) {
                ret.push_back(S.at(i - 1));
                restore(i - 1,j - 1,cnt - 1);
                return;
            }
        }
    }
    return;
}

int main() {
    cin >> S >> T;
    dp[0][0] = 0;
    for(int i = 0;i < S.size();i++) {
        for(int j = 0;j < T.size();j++) {
            if(S.at(i) == T.at(j)) {
                dp[i + 1][j + 1] = max(dp[i][j] + 1,max(dp[i + 1][j],dp[i][j + 1]));
            }
            else {
                dp[i + 1][j + 1] = max(dp[i + 1][j],dp[i][j + 1]);
            }
        }
    }
    restore(S.size(),T.size(),dp[S.size()][T.size()]);
    reverse(ret.begin(),ret.end());
    cout << ret << endl;
}

Measured memory_bytes: 39148
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    int ans = c - (a - b);
    if(ans < 0)
        ans = 0;
    printf("%d\n", ans);
    return 0;
}

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

const double PI = 3.14159265358979323846;
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
#define rep(i, n) for (int i = 0; i < (n); ++i)
//#define rep(i, n) for (ll i = 0; i < (n); ++i)
//typedef pair<ll, ll> P;
typedef pair<ll, ll> P;
const ll INF = 10e17;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)
#define ret() return 0;

std::istream &operator>>(std::istream &in, set<int> &o) {
    ll a;
    in >> a;
    o.insert(a);
    return in;
}

std::istream &operator>>(std::istream &in, queue<int> &o) {
    ll a;
    in >> a;
    o.push(a);
    return in;
}

bool contain(set<int> &s, int a) { return s.find(a) != s.end(); }

//ifstream myfile("C:\\Users\\riku\\Downloads\\0_00.txt");
//ofstream outfile("log.txt");
//outfile << setw(6) << setfill('0') << prefecture << setw(6) << setfill('0') << rank << endl;
// std::cout << std::bitset<8>(9);

//const ll mod = 1e10;
typedef priority_queue<P, vector<P>, greater<P> > PQ_ASK;

void dfs(int now, int prev, int hop, int color, vector<int> &colors, vector<vector<int>> &edges,
         vector<vector<bool>> &checked) {

    if (checked[now][hop]) return;
    checked[now][hop] = true;

    if (colors[now] == -1) colors[now] = color;
    if (hop == 0) return;


    for (int next : edges[now]) {
        if (next == prev) continue;
        dfs(next, now, hop - 1, color, colors, edges, checked);
    }

}

struct Splat {
    int u, size, color;
};

int main() {
    int n, m;
    cin >> n >> m;

    vector<vector<bool>> checked(n, vector<bool>(10, false));

    vector<int> colors(n, -1);
    vector<vector<int>> edges(n);

    rep(i, m) {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        edges[a].push_back(b);
        edges[b].push_back(a);
    }

    int q;
    cin >> q;

    vector<Splat> splats(q);
    for (Splat &s:splats) cin >> s.u >> s.size >> s.color, s.u--;
    reverse(splats.begin(), splats.end());
    for (Splat &s: splats) dfs(s.u, -1, s.size, s.color, colors, edges, checked);

    for (int &c : colors) if (c == -1) c = 0;
    for (int c : colors) cout << c << endl;

}


Measured memory_bytes: 15096
#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;
typedef vector<vector<int> > Graph;
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; }

struct UnionFind {
    vector<int> par;
    
    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};

ll comb(ll n){
    return n*(n-1)/2;
}

int main(){
    ll n,m;
    cin >> n >> m;
    vector<int> a(m),b(m);
    rep(i,m){
        cin >> a[i] >> b[i];
        a[i]--;b[i]--;
    }
    UnionFind U(n);
    ll cur=comb(n);
    vector<ll>res;
    rep(i,m){
        res.push_back(cur);
        int c=a[m-1-i];
        int d=b[m-1-i];
        if(U.issame(c,d))continue;  
        ll sa=U.size(c);
        ll sb=U.size(d);          
        cur-=U.size(c)*U.size(d);
        U.merge(c,d);    
    }
    reverse(res.begin(),res.end());
    rep(i,res.size()){
        cout<<res[i]<<endl;
    }
    return 0;
}

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

const int N = 1e4+5, MOD = 1e9+7, D = 105;
char str[N];
int n, d, dp[N][2][D];

void add_self(int& a, int b, int mod) {
  a += b;
  if (a >= mod)
    a -= mod;
}

void sub_self(int& a, int b, int mod) {
  a -= b;
  if (a < 0)
    a += mod;
}

int add(int a, int b, int mod) {
  return (a + b) % mod;
}

int f(int pos, bool limited, int cur_sum) {
  assert(cur_sum < d);

  if (pos >= n)
    return (cur_sum == 0);

  if (dp[pos][limited][cur_sum] != -1)
    return dp[pos][limited][cur_sum];

  int ans = 0;
  int max_digit = limited ? str[pos] : 9;

  for (int i = 0; i <= max_digit; i++) {
    if (i == max_digit && limited) {
      int tmp = f(pos + 1, 1, add(cur_sum, i, d));
      add_self(ans, tmp, MOD);
    } else {
      int tmp = f(pos + 1, 0, add(cur_sum, i, d));
      add_self(ans, tmp, MOD);
    }
  }

  return dp[pos][limited][cur_sum] = ans;
}

int32_t main() {
  scanf("%s%d", str, &d);

  n = strlen(str);
  for (int i = 0; i < n; i++)
    str[i] -= '0';

  memset(dp, -1, sizeof dp);
  int ans = f(0, 1, 0);
  sub_self(ans, 1, MOD);
  printf("%d\n", ans);
  return 0;
}


Measured memory_bytes: 9344
#include <bits/stdc++.h>
#define rep(i, n) for(ll i = 0; i < n; i++)
#define repr(i, n) for(ll i = n; i >= 0; i--)
#define reprr(i, m, n) for (ll i = m; i < n; i++)
#define inf 2e9
#define all(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
using Graph = vector<vector<pair<ll, ll>>>;


int main()
{
    ll a, b; cin >> a >> b;
    cout << a * b << endl;
    return 0;
}

Measured memory_bytes: 3620
#include<stdio.h>

int main(){
    int N,i=0,j=0,t=0,max,temp,l=0;

    scanf("%d",&N);

    int H[N];
    int k[100000]={0};

    for(;i<N;i++){
    scanf("%d",&H[i]);
    }
    for(i=0;i<N-1;i+=t){
        t=1;
        j=i;
        while(H[j]>=H[j+1] && j<N-1){
            k[l]++;
            j++;
            t++;
        }
        l++;
    }

    for(i=0;i<1;i++){
        max=i;
        for(j=i+1;j<l;j++){
            if(k[max]<k[j]){
                max=j;              
            }
        }
        if(max>i){
            temp=k[i];
            k[i]=k[max];
            k[max]=temp;
        }
    }
    printf("%d\n",k[0]);

    return 0;
}

Measured memory_bytes: 896
#include <iostream>
#include <vector>
#include <numeric>
#include <algorithm>
using namespace std;
 
int *a;
int n;
int calc(int sum, int i){
	if(i==n){
		if(sum%10==0){
			return 0;			
		}
		return sum;
	}
	return max(calc(sum+a[i], i+1),
	calc(sum,i+1));
}
 
int main() {
	// your code goes here
	cin >> n;
	a = new int[n];
	for(int i=0; i<n; i++){
		scanf("%d",&a[i]);
	}
		// cout << calc(0,0) << endl;

	int sum = accumulate(a,a+n,0);
	sort(a,a+n);
	if(sum%10==0){
		for(int i=0; i<n; i++){
			if((sum-a[i])%10!=0){
				cout << sum-a[i] << endl;
				return 0;
			}
		}
		cout << 0 << endl;
	}else{
		cout << sum << endl;
	}
	return 0;
}

Measured memory_bytes: 256
import sys
mod=10**9+7 ; inf=float("inf")
from math import sqrt, ceil
from collections import deque, Counter, defaultdict #すべてのkeyが用意されてる defaultdict(int)で初期化
# input=lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(11451419)
from decimal import ROUND_HALF_UP,Decimal  #変換後の末尾桁を0や0.01で指定
  #Decimal((str(0.5)).quantize(Decimal('0'), rounding=ROUND_HALF_UP))
from functools import lru_cache
#メモ化再帰defの冒頭に毎回 @lru_cache(maxsize=10**10)
#引数にlistはだめ
#######ここまでテンプレ#######
#ソート、"a"+"b"、再帰ならPython3の方がいい
#######ここから天ぷら########

h,w,k=map(int,input().split())
# R=[]
A=[[0]*w for i in range(h)]
dp=[[[0 for j in range(w)] for i in range(h)] for i in range(4)]

for s in sys.stdin.readlines():
    x,y,v=map(int,s.split())
    x-=1;y-=1
    A[x][y]=v
    # dp[x][y][1]=v
    # dp[x][y][2]=v
    # dp[x][y][3]=v;
dp[1][0][0]=A[0][0]
for i in range(h):
    for j in range(w):
        for k in range(4):
            here=dp[k][i][j]
            if i<h-1:
                dp[0][i+1][j] = max(here,dp[0][i+1][j])
                dp[1][i+1][j] = max(here+A[i+1][j], dp[1][i+1][j])
            if j<w-1:
                dp[k][i][j+1] = max(dp[k][i][j+1], here)
                if k<=2:
                    dp[k+1][i][j+1]= max(dp[k+1][i][j+1],here+ A[i][j+1])
ans=0
for i in range(4):
    ans=max(ans, dp[i][-1][-1])
print(ans)

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

int main(){
    long double a, b;
    cin >> a >> b;
    ll ans = a*b;
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3784
char buf[10000000],out[11];
sz;
cnt[60];
main()
{
	read(0,buf,10000000);
	long N=0;
	for(;;)
	{
		char k=buf[sz++];
		if(k<'0')break;
		N=N*10+k-'0';
	}
	for(;buf[sz]>='0';)
	{
		long A=0;
		for(;;)
		{
			char k=buf[sz++];
			if(k<'0')break;
			A=A*10+k-'0';
		}
		for(int i=0;i<60;i++)
		{
			A&1&&++cnt[i];
			A>>=1;
		}
	}
	long ans=0;
	for(int i=59;i>=0;i--)
	{
		ans=ans*2+(N-cnt[i])*cnt[i];
		if(ans>3000000000000000000)ans%=1000000007;
	}
	ans%=1000000007;
	int outsz=10;
	do{
		out[--outsz]=ans%10+'0';
	}while(ans/=10);
	write(1,out+outsz,10-outsz);
}


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

using namespace std;

#define ll long long
#define ar array

ll a,n;

ll tresh = 1000000000000000000;

void solve(){

    cin >> n;

    ll ans = 1;
    bool flag = false; 
    for (int i = 0; i < n; i++)
    {
        cin >> a;

        if(a <= tresh/ans){
            ans = ans * a;
        }
        else{
            flag = 1;
        }
        if(a == 0){
            cout << "0" << endl;
            return;
        }
        

    }
    if(flag ){
        cout << -1 << endl;
    }
    else  cout << ans << endl;
    
}





int main(){

    ios::sync_with_stdio(false);
    cin.tie(0);

    solve();

}

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

int main (){
  int fre,ca,num,N=0,x,y;
  cin>> fre >> ca;
  
  for (int i=0;i<ca;i++){
    cin>>num;
    N+=num;
  }
  y=N/(fre+1);
  x=N%(fre+1);
  
  if(x>0){
     cout<<y+1<<endl;
  }else{
    
  cout<<y<<endl;
  }
  
}



Measured memory_bytes: 3112
import sys
s = sys.stdin.readlines
A, V = [int(x) for x in input().split()]
B, W= [int(x) for x in input().split()]
T = int(input())

d = abs(A - B)
d2 = (V - W) * T
s = 'YES' if d <= d2 else 'NO'
print(s)



Measured memory_bytes: 9148
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <queue>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <stdio.h>
#include <complex>
#include <cstdint>
#include <tuple>
#include <regex>

#define M_PI       3.14159265358979323846

using namespace std;

//conversion
//------------------------------------------
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
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, PII> TIII;
typedef long long LL;
typedef unsigned long long ULL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;

//container util

//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define MOD 1000000007

PII ab[100005];

int main() {
	LL n, k;
	scanf("%lld%lld", &n, &k);
	REP(i, n) scanf("%d%d", &ab[i].first, &ab[i].second);
	sort(ab, ab + n);
	LL sum = 0;
	REP(i, n) {
		sum += ab[i].second;
		if (sum >= k) {
			printf("%d\n", ab[i].first);
			break;
		}
	}
	return 0;
}

Measured memory_bytes: 1024
/* ex 5_4
   nknm */

#include <stdio.h>
#include <string.h>

int main(void){
    

    char str[11], *ptr;      //必要な配列と整数を定義
    int i, count, max, *ptr_num;
    int num[11]={0};


    scanf("%s", str);     //文字列の入力

    ptr = str;      //ポインタを配列に先頭のアドレスへ
    ptr_num = num;

    count = 0;

    while(*ptr){
        if(*ptr == 'A' || *ptr == 'T' || *ptr == 'G' || *ptr == 'C'){
            count++;        //ATGCならカウントに1を足す
        }else{
            *ptr_num = count;       //ATGC以外のときにそれまでの連続するATGCの個数を他の配列に記録
            ptr_num++;      //記録用の配列のポインタを1つずらす
            count = 0;      //カウントを初期化
        }
        ptr++;      //次の文字を読む
    }
    *ptr_num = count;       //ATGCのいずれかで終わるときに最後に続くATGCの個数を記録
    
    max = num[0];       //記録した数の最大値を求める

    for(i = 0; i < 10; i++){
        if(num[i] < num[i+1]){
            max = num[i+1];
        }
    }

    printf("%d\n", max);

    return 0;
}

Measured memory_bytes: 1728
#include <cassert>

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
//#include <boost/foreach.hpp>
//#include <boost/range/algorithm.hpp>
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define ll long long
#define Sort(v) sort(all(v))
#define INF 1000000000
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define MP make_pair
#define MOD 1000000007LL
#define int long long
using namespace std;
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
struct edge{int to,from,cost;};


signed main (){
    int n;cin>>n;
     string s;cin>>s;
     int l=0,r=0;
     stack<char> st;
     rep(i,0,s.size()){
         if(st.empty()){
             st.push(s[i]);
             continue;
         }
         if(s[i]=='('){
             st.push(s[i]);
         }else {
             if(st.top()=='('){
                 st.pop();
             }else {
                 st.push(s[i]);
             }
         }
     }

     while(!st.empty()){
         if(st.top()=='(')r++;
         else l++;
         st.pop();
     }

     rep(i,0,l)cout<<'(';
     cout<<s;
     rep(i,0,r)cout<<')';
     cout<<endl;
    
}
/*
1-21-2
*/


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
/**
******************* Author:Bisnu sarkar ****************************
**/
/*
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
*/
#define fRead(x)       freopen(x,"r",stdin)
#define fWrite(x)      freopen (x,"w",stdout)
#define ull            unsigned long long
#define ll             long long
#define pii            pair<int,int>
#define sit            set<int> :: iterator
#define vrit           vector<int> :: reverse iterator
#define ff             first
#define ss             second
#define endl           '\n';
#define sz(s)          (int)s.size()
#define all(s)         s.begin(),s.end()
#define IO             ios_base::sync_with_stdio(false),cin.tie(NULL);  
#define what_is(x)     cerr << #x << " is " << x << endl;
///*....Debugger....*///
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {cout << endl;}
template<typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
}
///*....Input....*///
template <typename T> inline void Int(T &n) {
  n = 0; int f = 1; register int ch = getchar();
  for (; !isdigit(ch); ch = getchar()) if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) n = (n << 3) + (n << 1) + ch - '0';
  n = n * f;
}
 
template <typename T, typename TT> inline void Int(T &n, TT &m) { Int(n); Int(m); }
template <typename T, typename TT, typename TTT> inline void Int(T &n, TT &m, TTT &l) { Int(n, m); Int(l);}
double ind(){double x; scanf("%lf",&x);return x;}

///*....Bitmask....*///
int set_1(int n,int pos){return n = (n | (1<<pos));}
int reset_0(int n,int pos){return n= n & ~(1<<pos);}
bool check_bit(int n,int pos){return n = n & (1<<pos);}

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

///*....Constraints....*///
const int N = (int) 1e6 + 5;
const int M = (int) 1e9 + 7;
const double pi=2* acos(0.0);
const double eps=1e-9;


int main()
{
    int t=1,te=0;
    //Int(t);
    while(t--){
        int n;
        Int(n);

        int tot=0;
        bool res=false;
        while(n--){
          int x,y;
          Int(x,y);
          if(x==y)++tot;
          else tot=0;
          if(tot>=3)res=true;
        }
        if(res){
          printf("Yes\n");
        }
        else{
          printf("No\n");
        }

        //printf("Case %d: ",++te);
    }

    //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
    return 0;
}






Measured memory_bytes: 3592
#include <iostream>

int main(){
    int x;
    std::cin >> x;
    if(400 <= x && x <= 599){
        std::cout << "8" << std::endl;
    }
    else if(600 <= x && x <= 799){
        std::cout << "7" << std::endl;
    }
    else if(800 <= x && x <= 999){
        std::cout << "6" << std::endl;
    }
    else if(1000 <= x && x <= 1199){
        std::cout << "5" << std::endl;
    }
    else if(1200 <= x && x <= 1399){
        std::cout << "4" << std::endl;
    }
    else if(1400 <= x && x <= 1599){
        std::cout << "3" << std::endl;
    }
    else if(1600 <= x && x <= 1799){
        std::cout << "2" << std::endl;
    }
    else if(1800 <= x && x <= 1999){
        std::cout << "1" << std::endl;
    }
}

Measured memory_bytes: 3616
#include<bits/stdc++.h>
#define ll 	 long long
#define pb push_back
#define mp make_pair
using namespace std;

int main()
{

int n ; cin>>n;

int cnt=0;

int a[n+9];

for(int i=0; i<n; i++) cin>>a[i];

sort(a, a+n);

for(int i=0; i<n-2; i++)
{
	for(int k=i+1; k<n-1; k++ )
	{
	
	for(int j=k+1; j<n; j++)
	{
		
	if(a[i]!=a[k] && a[k] != a[j] && a[i]!=a[j])
	{
		if(a[i]+a[k] > a[j])
	{
	//	cout<<a[i]<<" "<<a[k]<<" "<<a[j]<<endl;
		cnt++;
	}
	}
}
}
}

cout<<cnt<<endl;



    return 0;
}

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

using namespace std;

int main(void){
  
  int r,g,b;
  cin>>r>>g>>b;
  if( (100*r+10*g+b)%4==0 ){
    cout<<"YES"<<endl;
  }else{
    cout<<"NO"<<endl;
  }
  
  return 0;
}


Measured memory_bytes: 256
import java.util.*;

class Main{
    public static void main(String argv[]) {
        final int day =15;
        Scanner sc = new Scanner(System.in);
        String[] str = sc.nextLine().split("");
        Integer count=0;
        for(String i : str){
            if(i.equals("o"))count++;
        }
        if(8-count<=day-str.length){
            System.out.println("YES");
        }
        else{
            System.out.println("NO");
        }
    }
}

Measured memory_bytes: 22096
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <functional>
#include <queue>
#include <string>
#include <cstring>
#include <numeric>
#include <cstdlib>
#include <cmath>
using namespace std;

typedef long long ll;

#define INF 10e17 // 4倍しても(4回足しても)long longを溢れない
#define rep(i,n) for(int i=0; i<n; i++)
#define rep_r(i,n,m) for(int i=m; i<n; i++)
#define END cout << endl
#define MOD 1000000007
#define pb push_back
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<int>())

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

  if (a + b > c + d) {
    cout << "Left" << endl;
  } else if (a + b < c + d) {
    cout << "Right" << endl;
  } else {
    cout << "Balanced" << endl;
  }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long
#define loop(n) for(ll i = 0; i<n; i++)
#define sort(a) sort(a.begin(), a.end())
using namespace std;

const int m = 1e6, M = 1e9+7;
ll inverse[3*m], fact[3*m], invfact[3*m];

ll nCr(ll n, ll r){
    ll ans = fact[n]*invfact[r]%M;
    ans = ans*invfact[n-r]%M;
  	return ans;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    inverse[1] = 1;
    for(ll i = 2; i<3*m; i++) inverse[i] = M - M/i * inverse[M%i]%M;
    fact[0] = invfact[0] = 1;
    for(ll i = 1; i<3*m; i++){
        fact[i] = fact[i-1]*i%M;
        invfact[i] = invfact[i-1]*inverse[i]%M;
    }

    ll r1, c1, r2, c2;
    cin>>r1>>c1>>r2>>c2;
    ll ans = nCr(r2+c2+2, c2+1)%M;
    ans = (ans - nCr(r1+c2+1, c2+1))%M;
    ans = (ans - nCr(r2+c1+1, c1))%M;
    ans = (ans + nCr(r1+c1, c1))%M;
    ans = (ans + M)%M;
    cout<<ans<<"\n";
    return 0;
}

Measured memory_bytes: 70528
#include <bits/stdc++.h>
#include<algorithm>
using namespace std;
#define ll long long
//int l[n+1];
int main() {
  int x,n;
  cin >> x >> n;
  vector<int> vec(n);
  for(int i = 0; i < n;i++){
    cin >> vec[i];
  }
  int tr;
  for(int i = 0;i<100;i++){
    tr = x-i;
    auto itr = std::find(vec.begin(), vec.end(), tr);
    size_t index = std::distance( vec.begin(), itr );
    if (index == vec.size()) { 
      
      break;
    }
    tr = x+i;
    itr = std::find(vec.begin(), vec.end(), tr);
    index = std::distance( vec.begin(), itr );
    if (index == vec.size()) { 
      break;
    }
  }
  
  cout << tr;
  
}

Measured memory_bytes: 3628
#include <bits/stdc++.h>
using namespace std;
int main() {
	int N, L;
	cin >> N >> L;
	vector<int> a(N);
	for (int& x : a) cin >> x;
	for (int i = 1; i < N; ++ i) if (a[i-1] + a[i] >= L) {
		cout << "Possible" << endl;
		for (int j = 1; j < i; ++ j) cout << j << endl;
		for (int j = N-1; j >= i; -- j) cout << j << endl;
		return 0;
	}
	cout << "Impossible" << endl;
}


Measured memory_bytes: 1152
function Main(input: string) {
    let splited = input.split(' ');
    let A = parseInt(splited[0]);
    let B = parseInt(splited[1]);

    let m = A > B ? A : B;
    let n = A > B ? B : A;

    while (n !== 0) {
        let tmp = n;
        n = m % n;
        m = tmp;
    }

    console.log(A * B / m);

}

Main(require('fs').readFileSync('/dev/stdin', 'utf8'));

Measured memory_bytes: 7372
#include <cstdio>

int cnt[101];

int main()
{
	int a, b, c, d;
	int ans = 0;
	scanf("%d %d %d %d", &a, &b, &c, &d);
	for (int i = a; i < b; i++)
		cnt[i]++;
	for (int i = c; i < d; i++)
		cnt[i]++;
	for (int i = 0; i < 101; i++) {
		if (cnt[i] == 2)
			ans++;
	}
	printf("%d\n", ans);
}

Measured memory_bytes: 128
#include <iostream>

using namespace std;

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

  if (a % 3 == 0 || b % 3 == 0 || (a + b) % 3 == 0)
    cout << "Possible" << endl;
  else
    cout << "Impossible" << endl;
  return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define Rep(i,s,n) for(int i=s;i<n;i++)
#define fi first
#define se second
#define all(c) c.begin(),c.end()
typedef long long int ll;
typedef pair<int, int> P;
typedef long double ld;
void Ans(bool x){if(x) cout<<"Yes"<<endl;else cout<<"No"<<endl;}

const ll INF=1001001001;
const ll mod=10000000007;

int main(){
  int a,b,c;cin>>a>>b>>c;
  int i=0;
  if(a==b) i++;
  if(a==c) i++;
  if(b==c) i++;
  
  Ans(i==1);
}

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

enum Dir {
    UP, RIGHT, DOWN, LEFT, DIR_NUM
};
const int64_t dx[] = { 0, 1, 0, -1 };
const int64_t dy[] = { -1, 0, 1, 0 };

int main() {
    int64_t N;
    cin >> N;
    vector<int64_t> P(N * N);
    for (int64_t& p : P) {
        cin >> p;
        p--;
    }

    vector<int64_t> value(N * N, 0);
    vector<vector<int64_t>> value_each_dir(N * N, vector<int64_t>(DIR_NUM, 0));
    for (int64_t i = 0; i < N; i++) {
        for (int64_t j = 0; j < N; j++) {
            value[i * N + j] = min({ i, N - 1 - i, j, N - 1 - j });
        }
    }

    for (int64_t i = 0; i < N; i++) {
        for (int64_t j = 0; j < N; j++) {
            for (int64_t d = 0; d < DIR_NUM; d++) {
                int64_t ni = i + dy[d];
                int64_t nj = j + dx[d];
                if (ni < 0 || N <= ni || nj < 0 || N <= nj) {
                    continue;
                }
                int64_t next_node = ni * N + nj;
                value_each_dir[i * N + j][d] = value[next_node] + 1;
            }
        }
    }

    vector<bool> there(N * N, true);

    auto print = [&]() {
        for (int64_t i = 0; i < N; i++) {
            for (int64_t j = 0; j < N; j++) {
                cout << setw(2) << value[i * N + j] << " ";
            }

            cout << "      ";
            for (int64_t j = 0; j < N; j++) {
                cout << setw(2) << i * N + j << " ";
            }

            cout << endl;
        }
        cout << endl;
    };

    int64_t ans = 0;
    for (int64_t p : P) {
//        cout << "p = " << p << "を抜きたい" << endl;
//        print();
        ans += value[p];

        //pを抜くとなにが起きるか
        there[p] = false;
        stack<int64_t> st;
        st.push(p);
        while (!st.empty()) {
            int64_t top = st.top();
            st.pop();

            int64_t i = top / N;
            int64_t j = top % N;

            //この位置に繋がっている4方のコストを下げる
            for (int64_t d = 0; d < DIR_NUM; d++) {
                int64_t ni = i + dy[d];
                int64_t nj = j + dx[d];
                if (ni < 0 || N <= ni || nj < 0 || N <= nj) {
                    continue;
                }

                int64_t next_node = ni * N + nj;

                int64_t before_min = *min_element(value_each_dir[next_node].begin(), value_each_dir[next_node].end());

                value_each_dir[next_node][(d + 2) % DIR_NUM] = value[top] + there[top];

                int64_t after_min = *min_element(value_each_dir[next_node].begin(), value_each_dir[next_node].end());
                if (after_min < before_min) {
                    value[next_node] = after_min;
                    st.push(next_node);
                }
            }
        }
    }

    cout << ans << endl;
}

Measured memory_bytes: 25588
#include <iostream>
#include <algorithm>
#include <cctype>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define fs first
#define sc second
typedef pair<int,int> P;
int w,h,dw[4]={1,0,-1,0},dh[4]={0,1,0,-1};
double eps=1e-8;
bool visited[61][10];
vector<string> vc;
struct st{
	bool ok;
	double wei,g;
};
vector<P> dfs(int hh,int ww,int id){
//	cout << "   " << hh << ww << id << endl;
	visited[hh][ww]=true;
	vector<P> ret;
	ret.push_back(P(hh,ww));
	rep(i,4){
		int nh=hh+dh[i],nw=ww+dw[i];
		if(0<=nw && nw<w && 0<=nh && nh<h && !visited[nh][nw] && vc[nh][nw]==id+'0'){
			vector<P> pl=dfs(nh,nw,id);
			ret.insert(ret.end(),pl.begin(),pl.end());
		}
	}
	return ret;
}
st stable(vector<P> b,double l,double r){
//	cout << "stable" << b[0].fs << " " << b[0].sc << endl;
	double al=0,w=4;
	rep(i,4) al+=b[i].sc;
	rep(i,4){
//		cout << "instable " << b[0].fs << " " << b[0].sc << "  i=" << i << endl;
		if(isdigit(vc[b[i].fs+1][b[i].sc]) && vc[b[i].fs+1][b[i].sc]!=vc[b[i].fs][b[i].sc] && !visited[b[i].fs+1][b[i].sc]){
			int id=vc[b[i].fs+1][b[i].sc]-'0';
//			cout << "id " << id << endl;
			vector<P> nb=dfs(b[i].fs+1,b[i].sc,id);
			double l=100,r=-100;
/*			rep(j,h+1){
				rep(k,w) cout << vc[h-j][k];
				cout << endl;
			}
			cout << nb.size() << endl;*/
			rep(j,4){
//				cout << "j " << j << " " << nb[j].fs << " " << nb[j].sc << endl;
				char c=vc[nb[j].fs-1][nb[j].sc];
				if(isdigit(c) && c!=id+'0') r=max(r,0.5+nb[j].sc),l=min(l,-0.5+nb[j].sc);
			}
//			cout << "end" << endl;
			st s=stable(nb,l,r);
			if(!s.ok) return {false,0,0};
			al+=s.wei*s.g;
			w+=s.wei;
		}
	}
	if(l+eps<al/w && al/w+eps<r){
//		cout << "l=" << l << " , r=" << r << " g=" << al/w << endl;
		return {true,w,al/w};
	}
	else return {false,0,0};
}
int main(){
	while(true){
		cin >> w >> h;
		if(!w) break;
		vc.clear();
		vc.push_back(string(w,'.'));
		rep(i,h){
			string s;
			cin >> s;
			vc.push_back(s);
		}
		reverse(vc.begin(),vc.end());
		int l=100,r=-100;
		rep(j,w) if(isdigit(vc[0][j])) l=min(l,j),r=max(r,j);
//		cout << l << r << endl;
		rep(i,h) rep(j,w) visited[i][j]=0;
		vector<P> f=dfs(0,l,vc[0][l]-'0');
//		cout << f.size() << endl;
		rep(i,h) rep(j,w) visited[i][j]=0;
		cout << (stable(f,-0.5+l,0.5+r).ok ? "" : "UN") << "STABLE\n";
	}
}

Measured memory_bytes: 1204
#include <iostream>
using namespace std;
int main(void){
    // Your code here!
    int a, b;
    cin >> a >> b;
    
    int x = (a + b);
    
    if(x % 2 == 0){
        cout << x / 2 << endl;
    }
    
    else{
        cout << (x + 1) / 2 << endl;
    }
    
    
}


Measured memory_bytes: 3560
import java.util.Scanner;
public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String a = sc.next();
        boolean not = true;
        if (a.charAt(1) == a.charAt(0)) {
            if (a.charAt(2) == a.charAt(1)) {
                System.out.println("No");
            }
            else {
                System.out.println("Yes");
            }
        }
        else {
            System.out.println("Yes");
        }
    }
}


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


#define off ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define lmax1 9223372036854775807
#define max1 2147483647
#define lmin1 -9223372036854775808
#define pi 3.141592653589793116
#define mod 1e9+7

int gcd(int a,int b){
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}

int main() {
    off;
    int a,b,k;
    set <int > s;
    cin>>a>>b>>k;
    int x=gcd(a,b);
    for(int i=1;i<=sqrt(x);i++)
    {
        if(x%i==0){
            s.insert(i);
            s.insert(x/i);
        }
    }
    k=s.size()-k+1;
    //cout<<k<<'\n';
    for(auto it:s)
    {   --k;
        if(k==0)
        {
            cout<<it<<'\n';
            return 0;
        }
    }
      return 0;
    }

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

const int MaxN = 500;
const int MaxM = MaxN * MaxN;

int n;
int pos[MaxN + 1];

int mark[MaxM + 1];
int used[MaxN + 1];

int seq[MaxM + 1];

inline bool construct()
{
	for (int i = 1; i <= n * n; ++i)
	{
		if (mark[i] > 0)
		{
			int k = mark[i];
			if (++used[k] != k)
				return false;
			seq[i] = k;
			continue;
		}

		int cur = 0;
		for (int k = 1; k <= n; ++k)
		{
			if (used[k] >= k - 1 || i > pos[k])
				continue;
			if (cur == 0 || pos[k] < pos[cur])
				cur = k;
		}

		for (int k = 1; !cur && k <= n; ++k)
			if (used[k] < n && i > pos[k])
				cur = k;

		if (!cur)
			return false;

		seq[i] = cur, ++used[cur];
	}

	return true;
}

int main()
{
	cin >> n;
	for (int i = 1; i <= n; ++i)
	{
		cin >> pos[i];
		mark[pos[i]] = i;
	}

	if (!construct())
		puts("No");
	else
	{
		puts("Yes");
		for (int i = 1; i <= n * n; ++i)
			printf("%d ", seq[i]);
	}

	return 0;
}

Measured memory_bytes: 2816
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;



int main() {
    string a, b;
    cin >> a >> b;
    int output = 0;
    for (int i = 0; i < a.length(); i++) {
        if (a[i] != b[i]) {
            output++;
        }
    }
    cout << output;
    return 0;
}

Measured memory_bytes: 3472
N = int(input())
B = list(map(int, input().split(" ")))

A = []
A.append(B[0])
for i in range(N-2):
    v  = B[i]
    vn = B[i+1]
    A.append(min([v,vn]))

A.append(B[-1])
#print(A)

Total = sum(A)
print(Total)

Measured memory_bytes: 3064
a,b = map(int,input().split())
num1 = (a//2)*(b//2)

if a == 1 or b==1:
    print(1)
else:
    
    # print(num1)
    p = b//2
    q = a//2
    num2 = (b - p )*(a - q)
    ans = num1+num2
    print(num1+num2)

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

i64 MOD = 1e9 + 7;
i64 inv_mod(i64 a, i64 m = MOD) {
  i64 b, x, u, q, abs_m, tmp;

  abs_m = (m < 0) ? -m : m;
  b = m;
  x = 1;
  u = 0;
  while (b > 0) {
    q = a / b;
    tmp = u;
    u = x - q * u;
    x = tmp;
    tmp = b;
    b = a - q * b;
    a = tmp;
  }

  return (x < 0) ? abs_m + x : x;
}
int main(){
  int N;
  cin >> N;

  if(N == 2){
    cout << 1 << endl;
    return 0;
  }
  else if(N == 3){
    cout << 4 << endl;
    return 0;
  }

  vector<i64> fact(1010101,0);
  fact[0] = 1;
  rep(i,1,1010100){
    fact[i] = (fact[i - 1] * i) % MOD;
  }
  vector<i64> inv_fact(1010101,0);
  inv_fact[1010100] = inv_mod(fact[1010100]);
  for(int i = 1010100 - 1;i >= 0;i--){
    inv_fact[i] = (inv_fact[i + 1] * (i + 1)) % MOD;
  }
  i64 ans = 0;

  i64 before = 0;
  rep(i,1,N - 1){
    i64 p = fact[i] * fact[N - i - 1];
    p %= MOD;
    int n = i - 1;
    int r = N - i - 1;
    if(n < r) continue;
    i64 q = (fact[n] * inv_fact[r]) % MOD ;
    q = (q * inv_fact[n - r]) % MOD;
    ans = (ans + ((p * q % MOD + MOD - before) % MOD) * i) % MOD;
    before = q * p % MOD;
  }

  cout << ans << endl;
}

Measured memory_bytes: 16000
from collections import Counter
import heapq
n, k = map(int,input().split())
a = list(map(int,input().split()))
c = Counter(a)
heapc = list(c.values())
heapq.heapify(heapc)
l = len(c)
cnt = 0
if k < l:
    for _ in range(l-k):
        cnt += heapq.heappop(heapc)
print(cnt)

Measured memory_bytes: 32568
#include<iostream>
#include<string>
using namespace std;
int main(){
  long long N,K,Dx,Dy;
  cin >> N >> K;
  for(int i=0;i<K;i++){
    cin >> Dx >> Dy;
    if(Dx>N/2)
      Dx=N-Dx+1;
    if(Dy>N/2)
      Dy=N-Dy+1;
    cout << (min(Dx,Dy)-1)%3+1 << endl;
  }
}

Measured memory_bytes: 1156
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>
using namespace std;

int main(){
	int N;
	cin >> N;
	string S[N];
	for(int i=0;i<N;i++){
		cin >> S[i];
	}
	int ans = 0, A = 0, B=0,AB = 0;
	for(int i=0;i<N;i++){
		for(int j=0;j+1<(int)S[i].size();j++){
			if(S[i][j] == 'A' && S[i][j+1] == 'B')ans++;
		}
		if(S[i][0] == 'B')B++;
		if(S[i][(int)S[i].size()-1] == 'A')A++;
		if(S[i][0] == 'B' && S[i][(int)S[i].size()-1] == 'A')AB++;
	}
	ans += min(A,B);
	if(A == AB && B == AB && AB)ans--;
	cout << ans << endl;
}

Measured memory_bytes: 768
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using llpair=pair<ll,ll>;
using llvec=vector<ll>;
using llmat=vector<vector<ll>>;
#define llmattp(name,a,b,num) name(a,vector<ll>(b,num))
#define LLINF 1LL<<60
#define ABS(x) ( (x)>0 ? (x) : -(x) )

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

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

ll gcd(ll a, ll b) {
	if(a==0||b==0) return 0;
	if(a<b) swap(a,b);
	ll tmp = a%b;
	while(tmp!=0) {
		a = b;
		b = tmp;
		tmp = a%b;
	}
	return b;
}

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

ll factorial(ll x) {
	ll f=1;
	for(ll i=2; i<x; i++) {
		f*=i;
	}
	return f;
}

ll nPr(ll n, ll r) {
	ll result=1;
	for(ll i=r+1; i<=n; i++) result*=i;
	return result;
}

ll nCr(ll n, ll r) {

	if (n == 0) { return 0; }
	if (r == 0) { return 1; }
	if (r == 1) { return n; }
	if (n == r) { return 1; }

	if (r > n / 2) { r = n / 2; }

	double result = 1;
	for (double i = 1; i <= r; i++) {
		result *= (n - i + 1) / i;
	}

	return (ll)result;
}

bool IsPrime(ll num) {
	if (num < 2) return false;
	else if (num == 2) return true;
	else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

	double sqrtNum = sqrt(num);
	for (ll i = 3; i <= sqrtNum; i += 2)
	{
		if (num % i == 0)
		{
			// 素数ではない
			return false;
		}
	}

	// 素数である
	return true;
}

ll GreaterBinarySearch(ll *array, ll key, ll max, ll min) {
	if(array[max]<array[min]) {
		return NULL;
	} else {
		ll mid = max + (min-max)/2;
		if(array[mid]<key) {
			return GreaterBinarySearch(array,key,max,mid-1);
		} if(array[mid]>key) {
			return GreaterBinarySearch(array,key,mid+1,min);
		} else {
			return mid;
		}
	}
}

int main() {
	string s; cin>>s;
	for(int i=0; i<s.length(); i++) {
		cout<<"x";
	}
	cout<<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
long long  a[100000+5];
long long b[100000+5];
int main(){
	int n;
	cin>>n;
	for(int i=0;i<=n;i++)
	cin>>b[i];
	long long sum=0;
	for(int i=0;i<n;i++){
		cin>>a[i];
		if(b[i]>=a[i]){
			sum=sum+a[i];
		}
		else if((b[i]+b[i+1])>a[i]){
				sum=sum+a[i];
				b[i+1]=(b[i+1]+b[i]-a[i]);
			}
		else{
			sum=sum+b[i]+b[i+1];
			b[i+1]=0;
		}
	}
	cout<<sum<<"\n";
	return 0;
}

Measured memory_bytes: 1792
h,w=map(int,input().split())
a=[0]*h
book={}
for _ in range(h):
    a=input()
    for i in a:
        if i in book:
            book[i]+=1
        else:
            book[i]=1


if h%2==0 and w%2==0:
    for i in book:
        if book[i]%4!=0:
            print("No")
            exit()
    print("Yes")

elif h%2==1 and w%2==1:
    cnt=0
    odd=0
    for i in book:
        if book[i]%2==1:
            odd+=1
            if odd==2:
                print("No")
                exit()
        cnt+=book[i]//4
    if odd==0:
        print("No")
    else:
        if cnt>=(h-1)*(w-1)//4:
            print("Yes")
        else:
            print("No")
else:
    cnt=0
    for i in book:
        if book[i]%2==1:
            print("No")
            exit()
        cnt+=book[i]//4
    if h%2==1:
        if cnt>=(h-1)*w//4:
            print("Yes")
        else:
            print("No")
    else:
        if cnt>=(w-1)*h//4:
            print("Yes")
        else:
            print("No")

Measured memory_bytes: 9216
#include<iostream>
#include<set>
using namespace std;
#define inRange(x,a,b) (a <= x && x < b)

int main(){
    int n;
    cin >> n;
    set<int> s[n*n+1];
    for(int i = 0; i < 2*n*n-2*n; i++){
        int a, b;
        cin >> a >> b;
        s[a].insert(b);
        s[b].insert(a);
    }
    int ans[n][n], next;
    for(int i = 1; i <= n*n; i++){
        if(s[i].size() != 2)    continue;
        ans[0][0] = i;
        for(int j : s[i]){
            s[j].erase(i);
            next = j;
        }
        ans[0][1] = next;
        int tmp = -1, mi = 4;
        for(int j : s[next]){
            s[j].erase(next);
            if(inRange(s[j].size(), 0, mi)){
                tmp = j;
                mi = s[tmp].size();
            }
        }
        next = tmp;
        break;
    }
    set<int> done; // ugly!
    done.insert(ans[0][0]);
    done.insert(ans[0][1]);
    for(int i = 0; i < n; i++){
        if(i){
            // ugly!
            for(int j : s[ans[i-1][0]])   if(done.count(j) == 0)  next = j;
        }
        for(int j = (i==0 ? 2 : 0); j < n; j++){
            ans[i][j] = next;
            done.insert(next);
            int tmp = -1, mi = 4;
            for(int k : s[next]){
                s[k].erase(next);
                if((i==0 || j==n-1 || s[ans[i-1][j+1]].count(k)) && inRange(s[k].size(), 0, mi)){
                    tmp = k;
                    mi = s[tmp].size();
                }
            }
            next = tmp;
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cout << ans[i][j] << " \n"[j==n-1];
        }
    }
    return 0;
}


Measured memory_bytes: 5464
import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[] point = new int[3];
        point[0] = sc.nextInt();
        point[1] = sc.nextInt();
        point[2] = sc.nextInt();
        String t = sc.next();
        int[] machine = new int[n];
        int[] takahashi = new int[n];
        for (int i=0;i<n;i++){
            int j = t.charAt(i);
            if (j=='r')machine[i]=0;
            else if (j=='s')machine[i]=1;
            else machine[i]=2;
        }
        long ans = 0;
        for (int i=0;i<k;i++){
            int j = i;
            int pre = 3;
            while (j<n){
                int win = (machine[j]+2)%3;
                if (pre!=win){
                    ans+=point[win];
                }else {
                    if (j+k<n){
                        while (win==pre||win==(machine[j+k]+2)%3)win=(win+1)%3;
                    }
                }
                pre=win;
                j+=k;
            }
        }
        System.out.println(ans);
    }
}


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

using namespace std;

int main()
{
    int n, m;   cin >> n >> m;

    int Sum = 0;

    vector <int> A(n);
    for(int& x : A) scanf("%d", &x), Sum += x;
    sort(A.begin(), A.end(), greater <int> ());

    puts(4 * m * A[m - 1] >= Sum ? "Yes" : "No");
}


Measured memory_bytes: 256
N = int(input())
A = list(map(int,input().split()))

pre = [0]
for i in range(1,N+1):
    pre.append(pre[i-1] + A[i-1])

L = 1
r = 3
ans = 10**18
        
for i in range(2,N-1):
    while L + 1 < i and abs(pre[L+1] - pre[i] + pre[L+1]) < abs(pre[L] - pre[i] + pre[L]):
        L += 1

    while r <= i or (r + 1 < N and abs(pre[r+1] - pre[i] - pre[N] + pre[r+1]) < abs(pre[r] - pre[i] - pre[N] + pre[r])):
        r += 1

    ans = min(ans,max(pre[L],pre[i]-pre[L],pre[r]-pre[i],pre[N]-pre[r]) - \
          min(pre[L],pre[i]-pre[L],pre[r]-pre[i],pre[N]-pre[r]))

print(ans)
        


Measured memory_bytes: 24684