akhauriyash/Code-Regression huggingface dataset:
import java.util.*;

class Main{

	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int W= sc.nextInt(), H=sc.nextInt();
		int x=sc.nextInt(), y=sc.nextInt(), r=sc.nextInt();
		
		System.out.println
			( (H>=y+r && y-r>=0 && W>=x+r && x-r>=0)? "Yes" : "No" );

	}
}

Measured memory_bytes: 26356
w, h, n = map(int, input().split())

x1, x2, y3, y4 = [0], [w], [0], [h]

for _ in range(n):
    x, y, a = map(int, input().split())
    if a == 1:
        x1.append(x)
    elif a == 2:
        x2.append(x)
    elif a == 3:
        y3.append(y)
    else:
        y4.append(y)

print(max(min(x2) - max(x1), 0) * max(min(y4) - max(y3), 0))


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

using namespace std;

int main()
{
int array[100],n,i,x,p,m;
long long int sum;
scanf("%d",&n);
for(i=0;i<n;++i){
    scanf("%d",&array[i]);
}
scanf("%d",&m);
while(m--){
    sum=0;
    scanf("%d %d",&x,&p);
    for(i=0;i<n;++i){
        if(i==x-1){
            sum+=p;
        }
        else{
        sum+=array[i];}
    }
    printf("%lld\n",sum);
}


    return 0;
}


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

#define llong long long 
#define xx first
#define yy second
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define ii pair<int, int>

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  llong n, a, b; cin >> n >> a >> b;
  llong ans = min(a * n, b);
  rep(i, n + 1) {
    ans = min(ans, i * a + b);
  }
  cout << ans;

  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <stdio.h>
using namespace std;
#define ll long long
int n;
string str[10005];
ll answer = 0;
ll catone = 0, cattwo = 0, catthree = 0;
int main(){
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		cin >> str[i];
		for (int j = 0; j < str[i].length() - 1; j++){
			if (str[i][j] == 'A' && str[i][j + 1] == 'B'){
				answer++;
			}
		}
		if (str[i][0] == 'B' && str[i][str[i].length() - 1] == 'A'){
			catthree++;
			continue;
		}
		if (str[i][0] == 'B'){
			catone++;
		}
		if (str[i][str[i].length() - 1] == 'A'){
			cattwo++;
		}
	}
	if (catthree == 0){
		printf("%lld\n", answer + min(catone, cattwo));
		return 0;
	}
	if (catone == 0 && cattwo == 0){
		printf("%lld\n", answer + max((ll)0, catthree - 1));
	} else if (catone == 0){
		printf("%lld\n", answer + max((ll)0, catthree - 1) + 1);
	} else if (cattwo == 0){
		printf("%lld\n", answer + max((ll)0, catthree - 1) + 1);
	} else {
		printf("%lld\n", answer + max((ll)0, catthree - 1) + 2 + min(catone - 1, cattwo - 1));
	}
	//printf("%lld\n", answer + min(catone, cattwo));
}

Measured memory_bytes: 768
n = int(input())
a = list(map(int, input().split()))

ans = 0
xor = 0
tempSum = 0
l, r = 0, 0
while True:
    if xor ^ a[r] == tempSum + a[r]:
        xor ^= a[r]
        tempSum += a[r]
        r += 1
        ans += (r - l)
        if r == n:
            break
    else:
        xor ^= a[l]
        tempSum -= a[l]
        l += 1

print(ans)


Measured memory_bytes: 25684
#define George_Plover
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
#define MAXN 200001
#define MAXM 200001
#define MOD 100000007
#define LL long long
using namespace std;
LL n;
int main()
{
    cin>>n;
    
    for(LL i=-200;i<=200;i++)
        for(LL j=-200;j<=200;j++)
            if(i*i*i*i*i-j*j*j*j*j==n)
            {
                cout<<i<<" "<<j<<endl;
                return 0;
            }
    
    return 0;
}


Measured memory_bytes: 3592
#include <stdio.h>

int main(void) {
	int i,j,h,w;
	while(1){
		scanf("%d %d",&h,&w);
		if(h==0&&w==0)break;
		for(i=0;i<h;i++){
			for(j=0;j<w;j++){
				if(i%2==0&&j%2==0){
					printf("#");
				}else if(i%2==0&&j%2==1){
					printf(".");
				}else if(i%2==1&&j%2==0){
					printf(".");
				}else if(i%2==1&&j%2==1){
					printf("#");
				}
			}
			printf("\n");
		}
		printf("\n");
		
	}
	return 0;
}

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

using namespace std;
const int N = 200022;
const int INF = 1000000000;
struct node {
    int mn, mnInd;
    node() {mn = INF,mnInd = N;}

    node (int mnn , int idx) {mn = mnn,mnInd = idx;}
};
int n,arr[N];

struct interval {
    int l,r;
    pair<int,int> p;

    bool operator < (const interval & x) const {
        return arr[p.first] > arr[x.p.first];
    }
};

map<pair<int,int>, vector<interval> > children;
node odd[4*N], even[4*N];
vector <vector<pair<int,int> > > vec;

priority_queue < interval > pq;

void buildOdd(int pos,int l,int r) {
    if(l == r) {
        if(l % 2)
            odd[pos] = node (arr[l],l);
        return;
    }
    int mid = (l+r)/2;
    buildOdd(pos*2,l,mid), buildOdd(pos*2+1,mid+1,r);
    if(odd[pos*2].mn < odd[pos*2+1].mn)
        odd[pos] = odd[pos*2];
    else
        odd[pos] = odd[pos*2+1];
}

void buildEven(int pos,int l,int r) {
    if(l == r) {
        if(l % 2 == 0)
            even[pos] = node (arr[l],l);
        return;
    }
    int mid = (l+r)/2;
    buildEven(pos*2,l,mid), buildEven(pos*2+1,mid+1,r);
    if(even[pos*2].mn < even[pos*2+1].mn)
        even[pos] = even[pos*2];
    else
        even[pos] = even[pos*2+1];
}

node getOdd(int pos,int l,int r,int qLow,int qHigh) {
    if(l > qHigh || r < qLow || qLow>qHigh)
        return node();
    if(l >= qLow && r <= qHigh)
        return odd[pos];
    int mid = (l+r)/2;
    node lf = getOdd(pos*2,l,mid,qLow,qHigh), ri = getOdd(pos*2+1,mid+1,r,qLow,qHigh);
    if(lf.mn < ri.mn)
        return lf;
    return ri;
}

node getEven(int pos,int l,int r,int qLow,int qHigh) {
    if(l > qHigh || r < qLow || qLow > qHigh)
        return node();
    if(l >= qLow && r <= qHigh)
        return even[pos];
    int mid = (l+r)/2;
    node lf = getEven(pos*2,l,mid,qLow,qHigh), ri = getEven(pos*2+1,mid+1,r,qLow,qHigh);
    if(lf.mn < ri.mn)
        return lf;
    return ri;
}

void solve(int l,int r) {
    if(l > r)
        return;
    int ret = 1;
    if(l & 1) {
        node o = getOdd(1,0,n-1,l,r);
        node temp = getEven(1,0,n-1,o.mnInd+1,r);
//        printf("%d %d ",o.mn,temp.mn);
        pq.push(interval{l,r,make_pair(o.mnInd,temp.mnInd)});
//        ret+= solve(l,o.mnInd-1,curr+1);
//        ret+= solve(o.mnInd+1,temp.mnInd-1,curr+1);
//        ret+= solve(temp.mnInd+1,r,curr+1);
    } else {
        node e = getEven(1,0,n-1,l,r);
        node temp2 = getOdd(1,0,n-1,e.mnInd+1,r);
        pq.push(interval{l,r,make_pair(e.mnInd,temp2.mnInd)});
//        ret+= solve(l,e.mnInd-1,curr+1);
//        ret+= solve(e.mnInd+1,temp2.mnInd-1,curr+1);
//        ret+= solve(temp2.mnInd+1,r,curr+1);
    }
}

int main()
{
    scanf("%d",&n);
    for(int i = 0;i<n;i++)
        scanf("%d",&arr[i]);
    buildOdd(1,0,n-1);
    buildEven(1,0,n-1);
    solve(0,n-1);
    while(!pq.empty()) {
        interval curr = pq.top();
        pq.pop();
        printf("%d %d ",arr[curr.p.first],arr[curr.p.second]);
        solve(curr.l,curr.p.first-1);
        solve(curr.p.first+1,curr.p.second-1);
        solve(curr.p.second+1,curr.r);
    }

    return 0;
}



Measured memory_bytes: 15604
import bisect

n, m=(int(i) for i in input().split())
s=input()
alist=[]
for i in range(n+1):
    if s[i]=='0':
        alist.append(i)

now=n
swit=0
dame=0
root=[]
while swit==0:
    koho=max(0, now-m)
    #print(alist, koho)
    bango=bisect.bisect_left(alist, koho)
    tugi=alist[bango]
    if now==tugi:
        swit=1
        dame=1
    elif tugi==0:
        swit=1
    root.append(now-tugi)
    now=tugi

if dame==1:
    print(-1)
else:
    newt=[]
    for i in range(len(root)):
        #newt.append(root[len(root)-i-1])
        if i!=len(root)-1:
            print(root[len(root)-i-1], end=" ")
        else:
            print(root[len(root)-i-1])
    #print(newt)

Measured memory_bytes: 55140
#include<iostream>
using namespace std;
int main(){
    int n, ans = 0;
    cin >> n;
    int b[n - 1];
    for (int i = 0; i < n - 1; i++) cin >> b[i];
    for (int i =0; i < n - 2; i++) ans += min(b[i], b[i + 1]);
    cout << ans + b[0] + b[n-2] << endl;
    return 0;
}


Measured memory_bytes: 256
from fractions import gcd

n = int(input())
a = list(map(int, input().split()))
mod = 10 ** 9 + 7
l = 1
for i in a:
    l = i * l // (gcd(i, l))

cnt = 0
for i in a:
    cnt += l // i
print(cnt % mod)

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

#define COUNTOF(array) (sizeof(array) / sizeof(array[0]))    
#define OUT(P) cout << (P) << endl    
#define OUT2(P1, P2) cout << (P1) << " " << (P2) << endl    
#define PRINT_ARRAY(P) for(int i=0; i<P.size(); i++) cout << P[i] << endl;    
#define PRINT_ARRAY_INLINE(P) do { for(int i=0; i<P.size(); i++) cout << P[i] << " "; cout << endl; } while(0)    
#define PRINT_ARRAY_WITH_LABEL(L, P) do { cout << L << ": "; PRINT_ARRAY_INLINE(P); cout << endl; } while(0)    
typedef long long ll;    
const long DIVIDE = 1e9+7;

int sum_digits(int num);    
template <class T>    
vector<T> copy_vector(vector<T> vec);    
long gcd(long a, long b);    
long lcm(long a, long b);
vector<vector<long>> comb(long n, long r);
// ****** print functions ****** //
template <class T, class U>
void print_pairs(const vector<pair<T, U>> pairs);
template <class K, class V>
void print_map(const map<K, V> m);

int main() {    
    int n, m;
    cin >> n >> m;
    vector<long> a(n);
    for(int i=0;i<n; i++) {
        cin >> a[i];
    }

    priority_queue<long, vector<long>, less<long>> que;
    for(int i=0; i<n; i++) {
        que.push(a[i]);
    }
    for(int i=0; i<m; i++) {
        long ca = que.top();
        que.pop();
        que.push(ca/2);
    }
    long sum = 0;
    while(!que.empty()) {
        long ca = que.top();
        que.pop();
        sum += ca;
    }
    cout << sum << endl;
    return 0;    
}    

/******************************/    
/***** DECRARED FUNCTIONS *****/    
/******************************/    

int sum_digits(int num) {    
    int rtn = 0;    
    while(num != 0) {    
        int tmp = num % 10;    
        rtn += tmp;    
        num /= 10;    
    }    
    return rtn;    
}    

// vectorのコピー    
template <class T>    
vector<T> copy_vector(vector<T> vec) {    
    vector<T> rtn;    
    rtn.reserve(vec.size());    
    copy(vec.begin(), vec.end(), back_inserter(rtn));    
    return rtn;    
}    

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

long lcm(long a, long b) {
    long gcd_ = gcd(a, b);
    return (a * b) / gcd_;
}

vector<vector<long>> comb(long n, long r) {                                                                             
    vector<vector<long>> v(n+1, vector<long>(n+1, 0));    
    for(int i=0; i<v.size(); i++) {    
        v[i][0] = 1;    
        v[i][i] = 1;    
    }    
    for(int i=1; i<v.size(); i++) {    
        for(int j=1; j < i; j++) {    
            v[i][j] = (v[i-1][j-1] + v[i-1][j]) % DIVIDE;    
        }    
    }    
    return v;    
} 

template <class T, class U>
void print_pairs(const vector<pair<T, U>> &pairs) {
    for(int i=0; i<pairs.size(); i++) { 
        cout << "(" << pairs[i].first << ", " << pairs[i].second << ")" << " "; 
    } 
    cout << endl;
}

template <class K, class V>
void print_map(const map<K, V> &m) {
    for(auto it=m.cbegin(); it!=m.cend(); it++) {
        cout << it->first << ": " << it->second << endl;
    }
}


Measured memory_bytes: 2168
import java.util.*;
import java.lang.*;
import java.math.*;

public class Main {
	Scanner sc = new Scanner(System.in);
	boolean dp[];

	Main() {
		dp = new boolean[10001];
		Arrays.fill(dp, true);

		dp[0] = false;
		dp[1] = false;
		for (int i = 2; i < 100; i++) {
			if (dp[i]) {
				for (int j = i + i; j < 10001; j = j + i) {
					dp[j] = false;
				}
			}
		}
	}

	void run() {
		for(;sc.hasNextInt();){
			int n = sc.nextInt();
			
			int ans =0;
			for(int i = 1 ; i<n;i++){
				if(dp[i]){
					if(dp[n-i+1]){
						ans++;
					}
				}
			}
			System.out.println(ans);
		}
	}

	public static void main(String[] args) {
		Main m = new Main();
		m.run();
	}
}

Measured memory_bytes: 12000
#include <bits/stdc++.h>
#define ft first
#define sc second
#define pt(sth) cout << sth << "\n"
#define chmax(a, b) {if(a<b) a=b;}
#define chmin(a, b) {if(a>b) a=b;}
#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
static const ll INF=1e18;
static const ll MAX=1e5+7;
static const ll MOD=1e9+7;
ll max(ll a, ll b) {return a>b?a:b;}
ll min(ll a, ll b) {return a<b?a:b;}

ll dp[1111][1<<12]={};

int main(void) {
  ll N, M;
  cin >> N >> M;
  ll w[1111], v[1111];
  ll i, j;
  
  for(i=0; i<M; i++) {
    ll L;
    cin >> w[i] >> L;
    ll t=0;
    for(j=0; j<L; j++) {
      ll c;
      cin >> c;
      c--;
      t+=1<<c;
    }
    v[i]=t;
  }
  
  for(i=0; i<=M; i++) {
    for(ll S=0; S<1<<N; S++) {
      dp[i][S]=INF;
    }
  }
  dp[0][0]=0;
  
  
  
  for(ll S=0; S<1<<N; S++) {
    for(i=0; i<M; i++) {
      chmin(dp[i+1][S|v[i]], dp[i][S]+w[i]);
      chmin(dp[i+1][S], dp[i][S]);
    }
  }
  
  if(dp[M][(1<<N)-1]==INF)
    pt(-1);
  else
    pt(dp[M][(1<<N)-1]);
  
}


Measured memory_bytes: 33024
import java.util.*;

public class Main {
    public static void main(String[] args) {
        int H, W;
        Scanner num = new Scanner(System.in);
        while((H = num.nextInt()) != 0 && (W = num.nextInt()) != 0) {
            
            for(int i = 0; i < H; i++) {
                for(int j = 0; j < W; j++) {
                    if(i % 2 == 0) {
                        if(j % 2 == 0) {
                            System.out.print("#");
                        } else {
                            System.out.print(".");
                        }
                    } else {
                        if(j % 2 == 0) {
                            System.out.print(".");
                        } else {
                            System.out.print("#");
                        }
                    }
                }
                System.out.print("\n");
            }
            System.out.print("\n");
        }
    }
}


Measured memory_bytes: 37308
using System;
using System.Text;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using static System.Console;
using static System.Math;

namespace AtCoder
{
    public class Program
    {
        public static void Main(string[] args)
        {
            new Program().Solve(new ConsoleInput(Console.In, ' '));
        }

        public void Solve(ConsoleInput cin)
        {
            int n = cin.ReadInt;
            if(n == 1) Console.WriteLine("0");
            else Console.WriteLine("1");
            
        }
    }

    public class ConsoleInput
    {
        private readonly System.IO.TextReader _stream;
        private char _separator = ' ';
        private Queue<string> inputStream;
        public ConsoleInput(System.IO.TextReader stream, char separator = ' ')
        {
            this._separator = separator;
            this._stream = stream;
            inputStream = new Queue<string>();
        }
        public string Read
        {
            get
            {
                if (inputStream.Count != 0) return inputStream.Dequeue();
                string[] tmp = _stream.ReadLine().Split(_separator);
                for (int i = 0; i < tmp.Length; ++i)
                    inputStream.Enqueue(tmp[i]);
                return inputStream.Dequeue();
            }
        }
        public string ReadLine { get { return _stream.ReadLine(); }}
        public int ReadInt { get { return int.Parse(Read); }}
        public long ReadLong { get { return long.Parse(Read); }}
        public double ReadDouble { get { return double.Parse(Read); }}
        public string[] ReadStrArray(long N) { var ret = new string[N]; for (long i = 0; i < N; ++i) ret[i] = Read; return ret;}
        public int[] ReadIntArray(long N) { var ret = new int[N]; for (long i = 0; i < N; ++i) ret[i] = ReadInt; return ret;}
        public long[] ReadLongArray(long N) { var ret = new long[N]; for (long i = 0; i < N; ++i) ret[i] = ReadLong; return ret;}
    }
}


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

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

	if (n <= 3) {
		cout << 0 << endl;
		return 0;
	}
	
	vector<int> r(n);
	vector<int> g(n);
	vector<int> b(n);
	for (int i = 0; i < n; i++) {
		if (i != 0) {
			r.at(i) = r.at(i - 1);
			g.at(i) = g.at(i - 1);
			b.at(i) = b.at(i - 1);
		}
		if (s[i] == 'R') {
			r.at(i)++;
		}
		if (s[i] == 'G') {
			g.at(i)++;
		}
		if (s[i] == 'B') {
			b.at(i)++;
		}
	}

	int64_t ans = 0;
	for (int i = 0; i < n - 2; i++) {
		for (int j = i + 1; j < n - 1; j++) {
			if ((s[i] == 'G' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'G')) {
				ans += r.at(n - 1) - r.at(j);
				if (j + (j - i) <= n - 1 && s[j + (j - i)] == 'R') {
					ans--;
				}
			}
			if ((s[i] == 'R' && s[j] == 'B') || (s[i] == 'B' && s[j] == 'R')) {
				ans += g.at(n - 1) - g.at(j);
				if (j + (j - i) <= n - 1 && s[j + (j - i)] == 'G') {
					ans--;
				}
			}
			if ((s[i] == 'R' && s[j] == 'G') || (s[i] == 'G' && s[j] == 'R')) {
				ans += b.at(n - 1) - b.at(j);
				if (j + (j - i) <= n - 1 && s[j + (j - i)] == 'B') {
					ans--;
				}
			}
		}
	}

	cout << ans << endl;

}



Measured memory_bytes: 3640
// #include {{{
#include <iostream>
#include <cassert>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <bitset>
#include <vector>
#include <complex>
#include <algorithm>
using namespace std;
// }}}
// #define {{{
typedef long long ll;
typedef double db;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define de(x) cout << #x << "=" << x << endl
#define rep(i,a,b) for(int i=a;i<(b);++i)
#define per(i,a,b) for(int i=(b)-1;i>=(a);--i)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
// }}}

const int N = 50;
ll n , a[N] , s;

int main(){
    cin >> n;
    rep(i,0,n) cin >> a[i] , s += a[i];
    ll step = max(0ll , s - n * (n - 1));
    rep(i,0,n) a[i] += step;
    ll l = -1 , r = *max_element(a,a+n);
    while(l + 1 < r){
        ll m = (l + r) >> 1;
        ll round = 0;
        rep(i,0,n) round += max(0ll , (a[i] - m) / (n + 1));
        if(round <= step) r = m;
        else l = m;
    }
    ll round = 0;
    rep(i,0,n) {
        ll t = max(0ll , (a[i] - r) / (n + 1));
        round += t;
        a[i] -= t * (n + 1);
    }
    rep(i,0,n) a[i] -= step - round;
    step = round;
    while(*max_element(a , a + n) >= n) {
        int p=max_element(a , a + n) - a;
        a[p] -= n;
        rep(i,0,n) if(i != p) a[i]++;
        step ++;
    }
    cout << step << endl;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define endl "/n";

int main(){
    fast;
    int n;
    cin >> n;
    cout << pow(n,3);

}


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

using namespace std;

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

    vector<int> v(n);
    int count = 0;

    for(int i = 0; i < n; i++)
    {
        cin >> v[i];
    }

    int i = 0;
    for(; i < n; i++)
    {
        if(v[i] != i + 1)
        {
            count++;
            if(count > 2)
            {
                break;
            }
        }
    }

    if(i == n)
    {
        cout << "YES";
    }
    else
    {
        cout << "NO";
    }
    

    return 0;
}

Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int v1 = a + b;
        int v2 = a + c;
        int v3 = c + b;
        System.out.println(Math.min(Math.min(v1, v2), v3));
    }
}


Measured memory_bytes: 23764
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] a = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt() - 1;
		}
		int[] b = new int[n];
		for (int i = 0; i < n; i++) {
			b[i] = sc.nextInt();
		}
		int[] c = new int[n - 1];
		for (int i = 0; i < n - 1; i++) {
			c[i] = sc.nextInt();
		}
		sc.close();

		int ans = 0;
		ans += b[a[0]];
		for (int i = 1; i < n; i++) {
			ans += b[a[i]];
			if (a[i - 1] + 1 == a[i]) {
				ans += c[a[i - 1]];
			}
		}
		System.out.println(ans);
	}
}


Measured memory_bytes: 25172
#include <bits/stdc++.h>
using namespace std;
 
typedef pair<int,int> pii;
typedef long long ll;

void fastStream(){cin.tie(0);std::ios_base::sync_with_stdio(0);}

const int mod = 1000000000 + 7;
ll N;
ll C[200010];
ll tmpC[200010];
ll dp[200010];
map<ll, ll> sums;

int main(){


    cin >> N;
    for(int i = 0; i < N; i++) cin >> C[i];
    int tmp_n = 1;
    tmpC[0] = C[0];
    for(int i = 1; i < N; i++){
        if(C[i] != C[i-1]){
            tmpC[tmp_n++] = C[i];
        }
    }
    N = tmp_n;
    for(int i = 0; i < N; i++) C[i] = tmpC[i];

    for(int i = N; i >= 0; i--){
        ll res = 0;
        if(i == N){
            res = 1;
        }
        else{
            (res += sums[C[i]]) %= mod;
            if((i < N - 1 && C[i + 1] != C[i]) || i == N - 1){
                (res += dp[i + 1]) %= mod;
            }
            // C[i] と同じ色の総和を計算しておく
            (sums[C[i]] += dp[i+1]) %= mod;
        }
        dp[i] = res;
    }

    cout << dp[0] << endl;

    return 0;
}


Measured memory_bytes: 17408
import scala.io.StdIn._

object Main {
    def main(args: Array[String]){
        val n = readLine.toInt
        
        var x1, x2, y = 0L
        readLine.split(" ").map(_.toLong).sorted.foreach { a =>
            if (x1 != a) {
                x1 = a
            } else {
                y = x2
                x2 = a
                x1 = 0
            }
        }
        
        println(x2 * y)
    }
}

Measured memory_bytes: 42676
//you**can**do**anything 
#include <bits/stdc++.h> 
#define ll long long
#define read(i) scanf("%lli" , &i)
const ll maxn=1e6;
using namespace std; 
 
int main() 
{ 


string s, t ;
cin>>s>>t;
ll count=0 ;
 
 for(int i=0 ;i<s.size() ;i++)
 {
 	if(s[i] !=t[i])
 	count++;
 }

 cout<<count<<"\n";
 
}

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

using namespace std;


int main()
{
  int N;
  cin >> N;
  set< int > st;
  for(int i = 0; i < N; i++) {
    int x;
    cin >> x;
    if(st.count(x)) st.erase(x);
    else st.emplace(x);
  }
  cout << st.size() << endl;

}

Measured memory_bytes: 4864
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <list>
#include <string>
#include <map>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip> // setprecisionを使用するのに必要 cout << fixed << setprecision(15) << p でpを小数点以下15桁表示
using namespace std;
#define REPD(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) REPD(i, 0, n)
#define DIV0 1000000007
typedef long long ll;
#define fin(ans) cout << (ans) << '\n'
/*
    //方針
    配列を二つ用意
    一つは入力がいくつあるかをカウントするもの。
    一つはi番目に小さい数よりも小さい数で割り切れるものをfalseとしたもの。
    かぶってる場合はi番目の数もfalseにする。
*/
int main()
{
    int A[1000010];
    bool B[1000010];
    memset(A,0,sizeof A);
    memset(B,true,sizeof B);
    int N,temp;
    cin >> N;
    REP(i,N){
        cin >> temp;
        A[temp]++;
    }
    REP(i,1000010){
        if(A[i]){
            for(int j = 2*i; j <= 1000010; j+=i){
                B[j] = false;
            }
            if(A[i] > 1){
                B[i] = false;
            }
        }
    }
    int ans= 0;
    REP(i,1000010){
        if(B[i])
            ans += A[i];
    }
    fin(ans);
    return 0;
}

Measured memory_bytes: 8076
#include<bits/stdc++.h>
using namespace std;
int main(){
   int n,t,b,w=1,s=9999999;
   int h,a;
   cin>>n>>t>>b;
   for(int i=1;i<=n;i++)
   {
   	  cin>>h;
   	  a=abs(1000*t-6*h-b*1000);
     if(a<s)s=a,w=i;
   }
   cout<<w;
	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i,a,n) for(int i=(a); i<(n); i++)
#define rep(i,n) for(int i=0; i<(n); i++)
#define per(i,n) for(int i=(n)-1; i>=0; i--)
#define all(v) v.begin(), v.end()
#define debug(var) do{cout << #var << " : "; print(var); } while(0)
using namespace std;
typedef long long ll;
void print() { cout << endl; }
template <class Head, class... Tail> void print(Head&& head, Tail&&... tail) { cout << head; if (sizeof...(tail) != 0) cout << " "; print(forward<Tail>(tail)...); }
template <class T> void print(vector<T> &vec) { for (auto& a : vec) { cout << a; if (&a != &vec.back()) cout << " "; } cout << endl; }
template <class T> void print(vector<vector<T>> &df) { for (auto& vec : df) { print(vec); } }
const int INF = 1e9;
const int MOD = 1e9+7;

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

	while (true) {
		int n, m, p; cin >> n >> m >> p;
		if (n==0 && m==0 && p==0) break;

		float total = 0.0;
		int bet = 0;
		FOR(i, 1, n+1) {
			int x; cin >> x;
			total += x;
			if (i==m) bet = x;
		}
		// print(total, p, bet);
		total *= 100;
		total = total * (100-p) / 100.0;
		if (bet) cout << floor(total / bet) << endl;
		else cout << 0 << endl;
	}
}


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

int main(){
	double r;
	while(cin >> r , r != 0.0){
		pair<int,int> a(9999999,999999); 

		for(int i = 1 ; i < 100000 ; i++){
			for(int j = 0 ; j < 2 ; j++){
				int nu = (int)( (M_PI-r) * i) + j;
				if(abs(nu - M_PI*i) <= r*i){
					a = min(a,make_pair(i,nu));
				}
			}
		}
		cout << a.second << "/" << a.first << endl;
	}
}

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

int main(){
	double n;
	while(cin >> n){
		double i = 1, x = 0;
		while(i <= n){
			i += 2;
			x++;
		}
		printf("%.6lf\n",1.0 * x / n);
	}
	return 0;
}

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



int main() {
    int n;
    cin >> n;
    vector<int> v(n);
    for (int i = 0; i < n; i++) {
        cin >> v[i];
    }
    sort(v.rbegin(), v.rend());
    
    long long ans = 0;
    for (int i = 1; i == 1; i++) {
        ans += v[0];
        //cout << v[0] << endl;
    }
    //cout << ans << endl;
    int w = 2;
    for (int i = 2; i < n; i++) {
        ans += v[w / 2];
        //cout << w / 2 << endl;
        w++;
        //cout << ans << endl;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 4056
n = int(input())
a = list(map(int, input().split()))
 
count = 0
while True:
    for i in range(n):
        if a[i] % 2 == 0:
            a[i] = a[i]//2
        else:
            print(count)
            exit()
    count += 1
print(count)

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

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

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

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

template <typename _Ty>
struct Edge {
    int src, to;
    _Ty cost;

    constexpr Edge(int _to, _Ty _cost) noexcept
        : src(-1), to(_to), cost(_cost) {}
    constexpr Edge(int _src, int _to, _Ty _cost) noexcept
        : src(_src), to(_to), cost(_cost) {}
};
template <typename _Ty>
using WeightedGraph = std::vector<std::vector<Edge<_Ty>>>;

WeightedGraph<ll> g;
int N;
Vl dist;

void dfs(int idx, int par, ll d) {
    dist[idx] = d;
    for (const auto& to : g[idx]) {
        if (to.to == par) continue;
        dfs(to.to, idx, d + to.cost);
    }
}


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

    cin >> N;
    g.resize(N);
    for (int i = 0; i < N - 1; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        --a, --b;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }

    int Q, K;
    cin >> Q >> K;
    --K;

    dist.resize(N);
    dfs(K, -1, 0);

    while (Q--) {
        int x, y;
        cin >> x >> y;
        --x, --y;

        cout << dist[x] + dist[y] << '\n';
    }

    return 0;
}

Measured memory_bytes: 14336
def judge(a, b, y, x, m):
	for i in range(0, m):
		for j in range(0, m):
			if a[y+i][x+j] != b[i][j]: return False
	return True

N, M = map(int, raw_input().split())
A = []
B = []
for i in range(0, N):
	A.append(raw_input())
for i in range(0, M):
	B.append(raw_input())

ans = 'No'
for i in range(0, N-M+1):
	for j in range(0, N-M+1):
		if judge(A, B, i, j, M):
			ans = 'Yes'
			break

print ans


Measured memory_bytes: 2568
#include <iostream>
using namespace std;
int main(){
  long long int a,b,c,d,e;
  cin >> a >> b >> c;
  d=c-a-b;
  if(d<=0) cout << "No" << endl;
  else{
     d=d*d;
     e=4*a*b;
     if(e<d) cout << "Yes" << endl;
     else cout << "No" << endl;
  }
  return 0;
}

Measured memory_bytes: 3536
S = str(input())

left = -2
right = -2

for i in range(max(1, len(S)-2)):
    if S[i] == S[i+1]:
        left = i
        right = i+1
        break
    if len(S) == 2:
        break
    if S[i] == S[i+2]:
        left = i
        right = i+2
        break
    if S[i+1] == S[i+2]:
        left = i+1
        right = i+2
        break

print(left+1, right+1)


Measured memory_bytes: 3188
n, m = map(int, input().split())

if n == 1 and m == 0:
    print("0")
    exit(0)
    
num = [-1]*n
s = [0]*m
c = [0]*m

flag = 1

for i in range(m):
    s[i], c[i] = map(int, input().split())
    if n != 1:
        if s[i] == 1 and c[i] == 0:
            flag = 0

if flag == 0:
    print("-1")
    exit(0)

if flag == 1:
    for i in range(m):
        if num[s[i]-1] == -1:
            num[s[i]-1] = c[i]
        elif num[s[i]-1] != c[i]:
            print("-1")
            exit(0)


for i in range(n):
    if num[0] != -1:
        if num[i] == -1:
            num[i] = 0
    else:
        num[0] = 1

if n != 1 and set(num) == {0}:
    print("-1")
else:
    print(''.join(map(str, num)))


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

typedef long long int lli;

int N,M;
lli comb(lli n,lli p){
	if(n<p)return 0;
	lli res=1;
	vector<bool> div(p,false);
	if(n-p<=p){
		p=n-p;
	}
	for(int i=n;i>n-p;i--){
		res*=i;
		for(int j=0;j<p;j++){
			if(res%(j+1)==0&&!div[j]){
				res/=(j+1);
				div[j]=true;
			}
		}

	}
	return res;
}
string solve(int n,int p,lli c){
	lli t=comb(n-1,p);
	if(n==0)return "";
	if(c>=t)return "1"+solve(n-1,p-1,c-t);
	return "0"+solve(n-1,p,c);
}
int main(){
	lli N,M;
	for(;(cin>>N>>M),N;){
		M-=1;
		for(int i=0;i<=N;i++){
			lli t=comb(N,i);
			if(t>M){
				cout<<solve(N,i,M)<<endl;
				break;
			}
			M-=t;
		}
	}
}

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

void fnInput(vector<vector<int>>& rvvnNum)
{
  for (int i = 0; i < rvvnNum.size(); i++)
    for (int j = 0; j < rvvnNum[i].size(); j++)
      cin >> rvvnNum[i][j];
}

int fnCheck(const vector<vector<int>>& cnrvvnNum)
{
  vector<int> vnDx(2);

  for (int j = 0; j < cnrvvnNum[0].size() - 1; j++)
    vnDx[j] = cnrvvnNum[0][j + 1] - cnrvvnNum[0][j];

  for (int i = 1; i < cnrvvnNum.size(); i++)
    for (int j = 0; j < cnrvvnNum[i].size() - 1; j++)
      if (cnrvvnNum[i][j + 1] == cnrvvnNum[i][j] + vnDx[j]);
      else
        return 0;

  return 1;
}

int main()
{
  vector<vector<int>> vvnNum(3, vector<int>(3));

  fnInput(vvnNum);
  if (fnCheck(vvnNum)) cout << "Yes" << endl;
  else                 cout << "No"  << endl;

  return 0;
}

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

int main() {
	// your code goes here
	int a,b;
	cin>>a>>b;
	if(a%3==0||b%3==0||(a+b)%3==0) cout<<"Possible\n";
	else cout<<"Impossible\n";
	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <deque>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <unordered_map>
#include <bitset>
#include <sstream>

typedef  long long ll;
#define mop 1000000007
using namespace std;


int main() {
    
    ll n;
    cin >> n;
    vector <ll> a;
    for (ll i=0;i<n;i++){
        ll p;
        cin >> p;
        a.push_back(p);
    }
    sort(a.begin(), a.end());
    ll tmp = 0;
    ll tri = 0;
    for (ll i=0;i<n-1;i++){
        tmp += a[i];
        if(i+1 < n and a[i+1] > tmp*2){
           tri = i+1;
        }
    }
    cout << n - tri << endl;

}




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

/*                    ॐ Shree ॐ                     */
/* ॐ ॐ ॐ
ॐ भूर् भुवः स्वः
तत् सवितुर्वरेण्यं
भर्गो देवस्य धीमहि
धियो यो नः प्रचोदयात्
*/
using namespace std;
typedef long long ll;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
// online submission
#endif
	long long res = 1, a;
	int n;
	cin >> n;
	ll max = 1000000000000000000;
	vector<ll> v(n);
	for (int i = 0; i < n; i++) {
		cin >> v[i];
		if (v[i] == 0) {
			cout << 0;
			return 0;
		}

	}
	
	for(int i=0;i<n;i++){
		a = v[i];
		if ((max) / res < a) {
			cout << -1;
			return 0;
		} else {
			res *= a;
		}
	}
	cout << res;
}

Measured memory_bytes: 3992
<?php
[$N, $M, $Q] = ints();
for($i = 0; $i < $Q; $i++){
    list($a[], $b[], $c[], $d[]) = ints();
}
dfs([], 1);
$max = 0;
foreach($ret as $array){
    $score = 0;
    for($i = 0; $i < $Q; $i++){
        if($array[$b[$i]-1]-$array[$a[$i]-1]==$c[$i])$score+=$d[$i];
    }
    chmax($max, $score);
}
echo $max;

function dfs($array, $min){
    global $N, $M, $ret;
    if(count($array) == $N){
        $ret[] = $array;
        return;
    }
    for($i = $min; $i <= $M; $i++){
        $array2 = $array;
        $array2[] = $i;
        dfs($array2, $i);
    }
}

function str(){return trim(fgets(STDIN));}
function ints(){return array_map('intval',explode(' ',trim(fgets(STDIN))));}
function int(){return intval(trim(fgets(STDIN)));}
function chmax(&$a,$b){if($a<$b){$a=$b;return 1;}return 0;}
function chmin(&$a,$b){if($a>$b){$a=$b;return 1;}return 0;}
function popcount($x){$c=0;while($x){$x&=$x-1;++$c;}return$c;}
function o(...$val){
    if(count($val)==1)$val=array_shift($val);
    echo debug_backtrace()[0]['line'].")";
    if(is_array($val)){
        if(count($val) == 0)echo "empty array";
        elseif(!is_array(current($val)))echo "array: ",implode(" ", addIndex($val)),"\n";
        else{
            echo "array:array\n";
            if(isCleanArray($val))foreach($val as $row)echo implode(" ", addIndex($row)),"\n";
            else foreach($val as $i => $row)echo "[".$i."] ".implode(" ", addIndex($row)),"\n";
        }
    }else echo $val."\n";
}
function addIndex($val){if(!isCleanArray($val)){$val = array_map(function($k, $v){return $k.":".$v;}, array_keys($val), $val);}return $val;}
function isCleanArray($array){$clean=true;$i = 0;foreach($array as $k=>$v){if($k != $i++)$clean = false;}return $clean;}
// 座圧対象の配列 -> [圧縮された配列,復元用のMap,圧縮用のMap]
function atsu($array){$a = array_flip($array);$fuku=array_flip($a);sort($fuku);$atsu = array_flip($fuku);foreach($array as $i=>$val)$array[$i]=$atsu[$val];return [$array, $fuku, $atsu];}
function gcdAll($array){$gcd=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$gcd=gcd($gcd,$array[$i]);}return $gcd;}
function gcd($m, $n){if(!$n)return $m;return gcd($n, $m % $n);}
function lcmAll($array){$lcm=$array[0];for($i=1,$I=count($array);$i<$I;++$i){$lcm=lcm($lcm,$array[$i]);}return $lcm;}
function lcm($a, $b) {return $a / gcd($a, $b) * $b;}



function loadTree($N = false){
  if($N === false)$N = $GLOBALS['N'];
  return loadGraph($N, $N-1);
}
function loadGraph($N = false, $M = false, $both = true){
  if($N === false)$N = $GLOBALS['N'];
  if($M === false)$M = $GLOBALS['M'];
  $G = array_fill(1, $N, []);
  for($i = 0; $i < $M; $i++){
    $values = ints();
    if(count($values) == 2){
      list($a, $b) = $values;
      $G[$a][] = $b;
      if($both)$G[$b][] = $a;
    }else{
      list($a, $b, $d) = $values;
      $G[$a][] = [$b, $d];
      if($both)$G[$b][] = [$a, $d];
    }
  }
  return $G;
}


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

#define debug(x) cerr << #x << ": " << x << endl
#define debugArray(x, n)                                                       \
    for(long long hoge = 0; (hoge) < (n); ++(hoge))                            \
    cerr << #x << "[" << hoge << "]: " << x[hoge] << endl
using namespace std;

using ll = long long;
using ull = unsigned long long;
const ll INF = LLONG_MAX / 2;
const ll MOD = 1e9 + 7;

signed main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    int D, G;
    cin >> D >> G;
    G /= 100;
    int p[D], c[D];
    int N = 0;
    for(int i = 0; i < D; i++) {
        cin >> p[i] >> c[i];
        c[i] /= 100;
        N += p[i];
    }
    ll dp[N + 1];
    fill(dp, dp + N + 1, -INF);
    dp[0] = 0;
    for(int i = 1; i <= D; i++) {
        for(int n = N; n >= 1; n--) {
            for(int j = min(n, p[i - 1]); j >= 1; j--) {
                dp[n] = j < p[i - 1] ? max(dp[n], dp[n - j] + i * j) :
                                       max(dp[n], dp[n - j] + i * j + c[i - 1]);
            }
        }
    }
    for(int n = 0; n <= N; n++) {
        if(dp[n] >= G) {
            cout << n << endl;
            break;
        }
    }

    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++)
typedef long long ll;


string N;


void input() {
    cin >> N;
}


int main() {
    input();
    int n = N.size();
    vector<int> w(n+1);
    rep(i, n) w[n - i - 1] = N[i] - '0';
    w[n] = 0;
    ll ans = 0;
    rep(i, n) {
        if (w[i] < 5) ans += w[i];
        else if (w[i] == 5 && w[i+1] < 5) ans += 5;
        else {
            ans += 10 - w[i];
            w[i+1]++;
        }
    }
    ans += w[n];
    cout << ans << endl;
}


Measured memory_bytes: 5252
#include <iostream>
using namespace std;
int main()
{
	int d,n,j,sum=0;
  	cin >> d >> n;
  	for(int i=0;i<n;i++)
    {
        cin >> j;
        sum += j;
    }
  	if(sum <= d)
    {
        cout << d-sum << endl;
    }
  	else
    {
	    cout << -1 << endl;
    }
}

Measured memory_bytes: 3636
#include <iostream>
using namespace std;
int main(void){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
       if(i%3==0){
           cout<<" "<<i;
       } 
       else if(i/1000==3){
           cout<<" "<<i;
       }
       else if((i%1000)/100==3){
           cout<<" "<<i;
       }
       else if((i%100)/10==3){
           cout<<" "<<i;
       }
       else if(i%10==3){
           cout<<" "<<i;
       }
    }
    cout<<endl;
}

Measured memory_bytes: 1164
#define _CRT_SECURE_NO_WARNINGS

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;
#define INF 1e+18
#define MOD 1000000007
#define int long long

typedef pair<int, int> P;

int mod_inv(int X) {
	int a = X, b = MOD, u = 1, v = 0;
	while (b) {
		int t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	if (u < 0) u += MOD;
	return u;
}

vector<int> fact, factinv;

void nCr_init(int N) {
	N = min(N, (int)MOD);
	fact.resize(N + 1);
	factinv.resize(N + 1);
	fact[0] = 1;
	for (int i = 1; i <= N; i++) fact[i] = fact[i - 1] * i % MOD;
	factinv[N] = mod_inv(fact[N]);
	for (int i = N; i >= 1; i--) factinv[i - 1] = factinv[i] * i % MOD;
}

int nCr(int ncr_n, int ncr_r) {
	return ncr_r > ncr_n ? 0 : fact[ncr_n] * factinv[ncr_n - ncr_r] % MOD * factinv[ncr_r] % MOD;
}

vector<int> G[100010];
bool flag = true;
int cnt;
bool visited[100010];

void dfs(int v, int par) {
	cnt++;
	visited[v] = true;
	for (int to : G[v]) {
		if (to != par) {
			if (visited[to]) flag = false;
			else dfs(to, v);
		}
	}
}

signed main() {
	int n;
	nCr_init(100010);
	while (cin >> n, n) {
		int x = 0, y = 0, ans = 0;
		set<P> st;
		for (int i = 0; i < n; i++) G[i].clear();
		for (int i = 0; i < n; i++) {
			int a;
			cin >> a; a--;
			if (!st.count(P(min(i, a), max(i, a)))) {
				G[a].push_back(i);
				G[i].push_back(a);
				st.insert(P(min(i, a), max(i, a)));
			}
		}
		flag = true;
		for (int i = 0; i < n; i++) {
			if (G[i].size() >= 3) flag = false;
		}
		if (!flag) {
			cout << 0 << endl;
			continue;
		}
		for (int i = 0; i < n; i++) visited[i] = false;
		for (int i = 0; i < n; i++) {
			if (!visited[i]) {
				cnt = 0;
				dfs(i, -1);
				if (cnt == 2) y++;
				else x++;
			}
		}
		if (!flag) {
			cout << 0 << endl;
			continue;
		}
		if (x >= 1) {
			for (int i = 0; i <= y; i++) {
				if (x >= 2) ans = (ans + (x + y - 1 - i) * x % MOD * (x - 1) % MOD * fact[x + y - 2 - i] % MOD * fact[i + 1] % MOD * (int)((i + 2) / 2) % MOD * mod_inv(i + 1) % MOD * nCr(y, i)) % MOD;
				//cout << ans << endl;
				ans = (ans + 2 * x % MOD * fact[x + y - 1 - i] % MOD * fact[i + 1] % MOD * (int)((i + 2) / 2) % MOD * mod_inv(i + 1) % MOD * nCr(y,i)) % MOD;
				//cout << ans << endl;
			}
		}
		else ans = fact[y + 1] * (int)((y + 2) / 2) % MOD * mod_inv(y + 1) % MOD;
		for (int i = 0; i < x + y; i++) ans = ans * 2 % MOD;
		cout << ans << endl;
		//cout << endl;
	}
}


Measured memory_bytes: 16492
#include<stdio.h>
int main(){
    int a,b,d;
	char c;
	while(1){
		scanf("%d %c %d",&a,&c,&b);
		if(c=='?')break;
		if(c=='+'){
			d=a+b;
			printf("%d\n",d);
		}
		if(c=='-'){
			d=a-b;
			printf("%d\n",d);
		}
		if(c=='*'){
			d=a*b;
			printf("%d\n",d);
		}
		if(c=='/'){
			d=a/b;
			printf("%d\n",d);
		}
	}

	return 0;
}

Measured memory_bytes: 2700
#include<stdio.h>

int main(void)
{
	int l,r;
	int i,j;
	int min=2018;
	int mod;
	scanf("%d%d",&l,&r);
	i=l;
	if(r-l>=2019)
		printf("0");
	else if(l%2019>r%2019)
		printf("0");
	else{
		for(i=l;i<r;i++){
			for(j=1;i+j<=r;j++){
				mod=(i%2019*(i%2019+j))%2019;
				if(mod<min)
					min=mod;
				}
			}
		printf("%d",min);
		}
	return 0;
}
		

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

#define ll long long
#define llu unsigned long long
#define ld long double
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define vi vector < int >
#define vll vector < ll >
#define pb push_back
#define pf push_front
#define iter :: iterator
#define pii pair < int , int >
#define pll pair < ll, ll >
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'
#define all(x) x.begin(),x.end()
#define foreach(it,v) for( __typeof((v).begin())it = (v).begin() ; it != (v).end() ; it++ )
#define present(v,x) (v).find(x) != (v).end()
#define cpresent(v,x) (find(all(v),x) != (v).end())
#define mset(x,v) memset(x, v, sizeof(x))
#define sz(x) (int)x.size()
#define shout()  {cout << "I'm Here...!!!" << endl;}
#define dbg(x) { cout<< #x << ": " << (x) << endl; }
#define dbg2(x,y) { cout<< #x << ": " << (x) << " , " << #y << ": " << (y) << endl; }

inline ll fpow(ll n, ll k, int p = MOD) {ll r = 1; for (; k; k >>= 1) {if (k & 1) r = r * n % p; n = n * n % p;} return r;}
inline int inv(int a, int p = MOD) {return fpow(a, p - 2, p);}
inline int addmod(int a, int val, int p = MOD) {{if ((a = (a + val)) >= p) a -= p;} return a;}
inline int submod(int a, int val, int p = MOD) {{if ((a = (a - val)) < 0) a += p;}return a;}
inline int mult(int a, int b, int p = MOD) {return (ll) a * b % p;}

#define MAXN 303
vector<int> a(MAXN);
ld n;

ld dp[MAXN][MAXN][MAXN];

ld expSushi(int x1, int x2, int x3){

	if(x1==0 and x2==0 and x3==0)
		return 0;

	ld pEmpty = (n-x1-x2-x3)/n;
	ld pX1 = x1/n;
	ld pX2 = x2/n;
	ld pX3 = x3/n;

	if(dp[x1][x2][x3]>0)
		return dp[x1][x2][x3];

	return (
	dp[x1][x2][x3]=(1/(1-pEmpty))*(1 
						+ pX1*(x1==0?0:expSushi(x1-1, x2, x3))
						+ pX2*(x2==0?0:expSushi(x1+1, x2-1, x3))
						+ pX3*(x3==0?0:expSushi(x1, x2+1, x3-1)))
	);
}

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

	int N;
	cin >> N;
	vector<int> a(N+1);
	int x1=0, x2=0, x3=0;
	n = N;
	for(int i=1; i<=N; i++){
		cin >> a[i];
		if(a[i]==1)
			x1++;
		if(a[i]==2)
			x2++;
		if(a[i]==3)
			x3++;
	}
	mset(dp, 0);

	cout << setprecision(20) << expSushi(x1, x2, x3) << endl;

	return 0;
}

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

using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
#define all(x) x.begin(), x.end()

template <typename T1, typename T2> inline void chkmin(T1 &x, const T2 & y) {if (x > y) x = y;}
template <typename T1, typename T2> inline void chkmax(T1 &x, const T2 & y) {if (x < y) x = y;}

int n;
vector<int> a;

void read() {
	cin >> n;
	a.resize(n);
	for (int i = 0; i < n - 1; i++) {
		int x;
		cin >> x;
		x--;
		a[x]++;
	}
	for (auto i : a)
		cout << i << "\n";
}

void run() {

}

void write() {

}

signed main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	read();
	run();
	write();
	return 0;
}

Measured memory_bytes: 3968
#include <cstdio>
#include <cmath>
#include <cstring>
#define fo(i,a,b) for (i=a; i<=b; i++)
using namespace std;

int n, x, i, ans;

int main()
{
	scanf("%d",&n);
	fo(i,1,n) scanf("%d",&x), ans+=x-1;
	printf("%d",ans);
}

Measured memory_bytes: 128
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
using u64 = unsigned long long;
#define REP(i, n) for (int i = 0; (i64)(i) < (i64)(n); ++i)

#ifdef ENABLE_DEBUG
template <typename T>
void debug(T value) {
  cerr << value;
}
template <typename T, typename... Ts>
void debug(T value, Ts... args) {
  cerr << value << ", ";
  debug(args...);
}
#define DEBUG(...)                              \
  do {                                          \
    cerr << " \033[33m (L" << __LINE__ << ") "; \
    cerr << #__VA_ARGS__ << ":\033[0m ";        \
    debug(__VA_ARGS__);                         \
    cerr << endl;                               \
  } while (0)
#else
#define debug(...)
#define DEBUG(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  string S;
  cin >> S;
  int x = 0;
  deque<char> ans;
  REP(i, N) {
    if (S[i] == '(') {
      ++x;
      ans.push_back(S[i]);
    } else {
      assert(S[i] == ')');
      --x;
      if (x < 0) {
        ans.push_front('(');
        ++x;
      }
      ans.push_back(')');
    }
  }
  while (x > 0) {
    --x;
    ans.push_back(')');
  }
  string r(ans.begin(), ans.end());
  cout << r << '\n';
}

Measured memory_bytes: 3608
#include<bits/stdc++.h>
#define rep(i, n) for(int (i) = 0; (i) < (n); (i)++)
#define printd(x) printf("%.12f\n", ans);
#define print(x) cout << (x) << endl;
#define Yes() cout << "Yes" << endl;
#define No() cout << "No" << endl;
#define YES() cout << "YES" << endl;
#define NO() cout << "NO" << endl;
#define all(x) (x).begin(),(x).end()
#define printa(x, n) for(ll i = 0; i < n; i++) {cout << (x[i]) << " ";} cout << endl;
template<class T> bool chmin(T& a, const T& b) {if (a > b) {a = b; return 1;} return 0;}
template<class T> bool chmax(T& a, const T& b) {if (a < b) {a = b; return 1;} return 0;}
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;

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

    s += 'u';
    int ans = 0;
    int num = 0;
    rep (i, s.size()) {
        if (s[i] == 'A' || s[i] == 'C' || s[i] == 'G' || s[i] == 'T') {
            num++;
        }
        else {
            chmax(ans, num);
            num = 0;
        }
    }

    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256