akhauriyash/Code-Regression huggingface dataset:
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastScanner in = new FastScanner(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskB solver = new TaskB();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskB {
        public void solve(int testNumber, FastScanner in, PrintWriter out) {
            int n = in.nextInt();
            int[] a = new int[n + 1];
            ;
            boolean[] pushed = new boolean[n + 1];
            for (int i = 1; i <= n; i++)
                a[i] = in.nextInt();

            int cn = 1;
            boolean possible = false;
            int count = 0;
            while (!pushed[cn]) {
                pushed[cn] = true;
                if (cn == 2) {
                    possible = true;
                    break;
                }
                cn = a[cn];
                count++;
            }

            out.println(possible ? count : -1);
        }

    }

    static class FastScanner {
        private InputStream in;
        private byte[] buffer = new byte[1024];
        private int bufPointer;
        private int bufLength;

        public FastScanner(InputStream in) {
            this.in = in;
        }

        private int readByte() {
            if (bufPointer >= bufLength) {
                if (bufLength == -1)
                    throw new InputMismatchException();

                bufPointer = 0;
                try {
                    bufLength = in.read(buffer);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }

                if (bufLength <= 0)
                    return -1;
            }
            return buffer[bufPointer++];
        }

        private static boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public long nextLong() {
            long n = 0;

            int b = readByte();
            while (isSpaceChar(b))
                b = readByte();

            boolean minus = (b == '-');
            if (minus)
                b = readByte();

            while (b >= '0' && b <= '9') {
                n *= 10;
                n += b - '0';
                b = readByte();
            }

            if (!isSpaceChar(b))
                throw new NumberFormatException();

            return minus ? -n : n;
        }

        public int nextInt() {
            long n = nextLong();

            if (n < Integer.MIN_VALUE || n > Integer.MAX_VALUE)
                throw new NumberFormatException();

            return (int) n;
        }

    }
}



Measured memory_bytes: 22356
#include <iostream>

using namespace std;
typedef long long ll;

int main(){
    ll x;
    cin >> x;
    ll ans = 2 * (x / 11);
    x -= 11 * (x / 11);
    if(x > 6){
        ans += 2;
    }else if(x > 0){
        ans += 1;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#include<algorithm>
#include<set>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stack>
using namespace std;
typedef long long int ll;
const int MOD=1e9+7;
// power function with a^b 
ll power(ll a, ll b){
    ll ans=1;
    while(b>0){
        if(b%2!=0)
        ans=ans*a;

        b/=2;
        a=a*a;
    }
    return ans;
}
// power function with modulo i.e (a^b)%mod
ll powermod(ll a,ll b, ll mod){
    ll ans=1;
    a=a%mod;
    if(a==0) return 0;
    while(b>0){
        if(b%2!=0)
        ans=(ans*a)%mod;

        b/=2;
        a=(a*a)%mod;
    }
    return ans;
}
ll fact(ll n){
    ll ans=1;
    for(int i=n; i>=1; i--){
        ans*=i;
    }
    return ans;
}
int solve(){
    ll n;
    cin>>n;
    ll height[n];
    for(int i=0; i<n; i++){
        cin>>height[i];
    }
    ll dp[n];
    dp[0]=0;
    dp[1]=abs(height[0]-height[1]);
    for(int i=2; i<n; i++){
        dp[i]=min(dp[i-1]+abs(height[i]-height[i-1]),dp[i-2]+abs(height[i]-height[i-2]));
    }
    return dp[n-1];
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

   // ll tc;
   // cin >> tc;
    //for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t  << ": ";
        ll ans=solve();
        cout<<ans<<"\n";
    //}
}


Measured memory_bytes: 5176
package main

import (
	"fmt"
)

func main() {
	var n, k int
	fmt.Scan(&n, &k)

	if n == k {
		fmt.Print(1)
		return
	}

	n -= k
	k--
	cal := 1 + (n+k-1)/k
	fmt.Print(cal)
}


Measured memory_bytes: 512
#include<iostream>
#include<cctype>
using namespace std;
int main()
{
	char ma;
	int sum[123] = {}, i, j=0;
	while (cin.get(ma))
	{
		if (ma == '\n')j++;
		if (j >= 5)break;
		ma = tolower(ma);
		if (isalpha(ma))sum[ma]++;
	}
	for (i = 97; i <= 122; i++)
		cout << static_cast<char>(i) << " : " << sum[i] << endl;
	return 0;
}


Measured memory_bytes: 3100
#include<bits/stdc++.h>
using namespace std ;
#define int long long
#define ld long double
#define fast ios_base::sync_with_stdio(false) ;cin.tie(NULL);cout.tie(NULL)
#define F(i,a,b) for(int i= (int)(a); i<(int)(b);i++)
#define FI(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
#define RF(i,a,b) for(int i = (int)(a); i >= (int)(b); i--)
#define what_is(x) cerr << #x << " is " << x << endl;
#define ms(x,a) memset(x,(int)(a),sizeof(x))
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int)(x).size())
#define pii pair<int,int>
#define vi vector<int >
#define pb push_back
#define endl '\n'
#define mp make_pair
#define ff first
#define ss second
#define MOD 1000000007
const int N = 1e6+7 ;
int n ;
int cnt ;
bool chk(int x){
	set<int> s ;
	while(x){
		int rem = x % 10 ;
		s.insert(rem) ;
		x/= 10 ;
	}
	if(s.count(3) && s.count(5) && s.count(7))
		return true ;
	else
		return false ;
}
void dfs(int x){
	if(x > n)
		return ;
	if(chk(x))
		cnt++ ;
	dfs(x*10 + 3) ;
	dfs(x*10 + 5) ;
	dfs(x*10 + 7) ;
}

int32_t main(){fast ;
	cin >> n ;
	cnt = 0 ;
	dfs(0) ;
	cout << cnt << endl ;
}




Measured memory_bytes: 256
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
 
 
int main(){
int n;
int i;
unsigned long long a[88];
scanf("%d",&n);
for(i=0;i<=n;i++){
	if(i==0){
		a[i]=2;
		
	}else if(i==1){
		a[i]=1;
	}else{
		
		a[i]=a[i-1]+a[i-2];
	}
	//printf("%d\n",a[i]);
	
	
	
	
}
printf("%llu\n",a[n]);
return 0;
}

Measured memory_bytes: 128
n, dim = list(map(int, input().split()))

coodi = []
count = 0
for i in range(n):
	coodi.append( list(map(int, input().split())) )

for i in range(n):
	for j in range(i+1, n):
		dist = 0
		for d in range(dim):
			dist += ( coodi[i][d] - coodi[j][d] ) ** 2
		for k in range(0, 200):
			if k ** 2 == dist:
				count += 1
				break

print(count)

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
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; }
const int INF = (1<<30) - 1;
const ll LINF = (1LL<<60) - 1;

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector<int> r(n, 0), l(n, 0);
    for (int i = 0; i < n; ++i) {
        int left = i - a[i];
        int right = i + a[i];
        if (left >= 0) l[left]++;
        if (right < n) r[right]++;
    }

    ll res = 0;
    for (int i = 0; i < n; ++i) {
        res += (ll)r[i] * (ll)l[i];
    }
    cout << res << endl;
}

Measured memory_bytes: 5636
#include<stdio.h>
int main()
{
  int N,i,num;
  char S[50];
  scanf("%d",&N);
  scanf("%s",&S);
  num=0;
  for(i=0;i<=N-3;i++)
  {
    if(S[i]=='A'&&S[i+1]=='B'&&S[i+2]=='C')
    {
      num++;
    }
  }
  printf("%d",num);
}
  

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

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

	ll n, k;
	cin >> n >> k;
	ll a[n];
	for (ll i = 0; i < n; i++) {
		cin >> a[i];
	}
	ll dp[n + 1][k + 1];
	memset(dp, 0, sizeof(dp));
	ll curr = 0;
	for (ll i = 0; i <= k; i++) {
		if (i <= a[0])
			curr += 1;
		dp[1][i] = curr;
	}
	for (ll i = 0; i <= n; i++)
		dp[i][0] = 1;
	for (ll i = 2; i <= n; i++) {
		curr = 1;
		for (ll j = 1; j <= k; j++) {
			ll sub = 0;
			if (j - a[i - 1] <= 0) {
				sub = (dp[i - 1][j]) % mod;
			} else {
				sub = (dp[i - 1][j] - dp[i - 1][j - a[i - 1] - 1] + mod) % mod;
			}
			curr = (curr + sub) % mod;
			dp[i][j] = curr;
		}
	}
	cout << (dp[n][k] - ((k - 1 < 0) ? 0 : dp[n][k - 1]) + mod) % mod;
	// for (ll i = 1; i <= n; i++) {
	// 	for (ll j = 0; j <= k; j++) {
	// 		cout << dp[i][j] << " ";
	// 	}
	// 	cout << "\n";
	// }


	return 0;
}

Measured memory_bytes: 82504
#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
char a[1001][1001];
int dp[1001][1001];
int solve(int h,int w)
{
    int c=0;
    int d=0;
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            if(i==0)
            {
                if(a[i][j]=='.' && c==0)
                dp[i][j]=1;
                else
                c++;
            }
            else if(j==0)
            {
                if(a[i][j]=='.' && d==0)
                dp[i][j]=1;
                else
                d++;
            }
            else
            {
                if(a[i][j]=='.')
                {
                    if(a[i-1][j]=='.' && a[i][j-1]=='.')
                    {
                        dp[i][j]=(dp[i-1][j]+dp[i][j-1])%mod;
                    }
                    else
                    {
                        dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                    }
                }
            }
        }
    }
    return dp[h-1][w-1]%mod;
}
int main()
{
    int h,w;
    cin >> h >> w;
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        cin >> a[i][j] ;
    }
    cout << solve(h,w) << endl;
}

Measured memory_bytes: 8412
#include<bits/stdc++.h>
using namespace std;
 
int main(){
  int N;
  cin >> N;
  int A[N];
  for(int i=0; i<N; i++){
    cin >> A[i];
  }
  long long ans=0;
  int M=0;
  for(int i=0; i<N; i++){
    M=max(M,A[i]);
    ans += M-A[i];
  }
  cout << ans << endl;
  return 0;
}  

Measured memory_bytes: 4380
from collections import deque
N=int(input())

C=[]
D=0
for _ in range(N):
    a,b=map(int,input().split())
    C.append((a,b))
    D+=b
    
C.sort(key=lambda x:x[0]+x[1])
C=deque(C)

i=0
A=0
while C:
    (a,b)=C.pop()
    if i==0:
        A+=a+b
    i^=1
print(A-D)


Measured memory_bytes: 87500
var GET=(function(){function f(s){return new g(s);}function g(s){this._s=s.trim().split("\n");this._y=0;}g.prototype.a=function(f){var s=this._s, y=this._y, r;if(typeof s[y]==="string")s[y]=s[y].split(" ").reverse();r=s[y].pop();if(!s[y].length)this._y++;return f?r:+r;};g.prototype.l=function(f){var s=this._s[this._y++].split(" ");return f?s:s.map(a=>+a);};g.prototype.m=function(n,f){var r=this._s.slice(this._y,this._y+=n).map(a=>a.split(" "));return f?r:r.map(a=>a.map(a=>+a));};g.prototype.r=function(n,f){var r=this._s.slice(this._y,this._y+=n);return f?r:r.map(a=>+a);};return f;})();
var o=GET(require("fs").readFileSync("/dev/stdin","utf8"));

console.log(main());
function main(){
  var n = o.a();
  var s = o.r(n,1);
  for(var i = 0; i < n; i++)s[i+n] = s[i];
  var ans = 0;
  for(i = 0; i < n; i++){
    var f = 1;
    j:
    for(var j = 0; j < n; j++)for(var k = j+1; k < n; k++){
      if(s[j+i][k] === s[k+i][j])continue;
      f = 0;
      break j;
    }
    if(f)ans += n;
  }
  return ans;
}

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

void solve(short dep,short n,int& ans,short node,bool *f,vector<short>* edge){
    if(dep==n){
        ans++;
        return;
    }
    f[node]=true;
    short l=edge[node].size();
    for(short i=0;i<l;i++){
        short j=edge[node][i];
        if(!f[j]){
            solve(dep+1,n,ans,j,f,edge);
        }
    }
    f[node]=false;
}

int main(){
    short n,m;
    while(cin>>n>>m){
        vector<short>edge[9];
        bool f[9]={0};
        int ans=0;
        while(m--){
            short x,y;
            cin>>x>>y;
            edge[x].push_back(y);
            edge[y].push_back(x);
        }
        solve(1,n,ans,1,f,edge);
        cout<<ans<<endl;
    }
    return 0;
}


Measured memory_bytes: 3152
// +build ignore
package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []byte {
	return []byte(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

func gcd(x, y int) int {
	if x%y != 0 {
		return gcd(y, x%y)
	}
	return y
}

func run(input io.Reader, output io.Writer) int {
	sc := NewScanner()
	r := sc.nextInt()
	g := sc.nextInt()
	b := sc.nextInt()

	if (r*100+g*10+b)%4 == 0 {
		fmt.Println("YES")
	} else {
		fmt.Println("NO")
	}

	return 0
}

func main() {
	os.Exit(run(os.Stdin, os.Stdout))
}


Measured memory_bytes: 640
#include<iostream>
#include<string>
using namespace std;
#define int long long
#define rep(i,n) for(int i = 0; i < n; i++)
#define INF (long long)(1e18)
#define MOD (int)(1e9+7)
#define MAX_N 103
#define MAX_V 10
#define MAX_M 101
#define yn(f) (f?"Yes":"No")
#define YN(f) (f?"YES":"NO")
#define pro "はいプロ　世界一○○が上手　○○界のtourist　○○時代の終焉を告げる者　実質○○　○○するために生まれてきた男"

signed main(){
	int n, m, a, b ,c;
	string str;
	cin>>n;
	for(int i = 1; i*i <= n; i++){
		c = i;
	}
	cout<<c*c<<endl;
	return 0;
}

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

const int N = 205;
int a[N],n,l;

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

    cin >> n >> l;
    int sum = 0;
    for(int i=1;i<=n;i++){
        a[i] = l+i-1;
        sum += a[i];
    }
    int dif = 1e9, ans;
    for(int i=1;i<=n;i++){
        if(abs(a[i]) < dif){
            dif = abs(a[i]);
            ans = sum - a[i];
        }
    }
    cout << ans << '\n';

    return 0;
}


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

int main(){
  int N,M; cin >> N >> M;
  int connect[10][10]={};
  for(int i=0; i<M; i++){
    int k; cin >> k;
    for(int j=0; j<k; j++){
      int tmp; cin >> tmp;
      connect[i][tmp-1]=1;
    }
  }
  int residue[10];
  for(int i=0; i<M; i++) cin >> residue[i];
  
  //offを0, onを1としてbit全探索する
  
  int cnt=0;
  for(int i=0; i<(1<<N); i++){
    bool f=true;
    for(int j=0; j<M; j++){
      int sum=0;
      for(int k=0; k<N; k++){
        if(connect[j][k]==1){
          if(i&(1<<k)) sum++;
        }
      }
      if(sum%2!=residue[j]) f=false;
    }
    if(f) cnt++;
  }
  cout << cnt << endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <set>
#include <cmath>
#include <algorithm>
using namespace std;

int main(void) {
  int tco=0,atc=0;
  string str;
  cin >> str;

  for (size_t i = 0; i < str.size(); i++) {
    if (str[i]=='p') {
      tco++;
    }else{
      tco--;
    }
  }

  if(str.size()%2 == 0){
    atc = 0;
  }else{
    atc = -1;
  }
  printf("%d\n",(atc-tco)/2);
}


Measured memory_bytes: 512
N, M = [int(_) for _ in input().split()]
A = set([int(input()) for _ in range(M)])

dp = [0 for _ in range(N+1)]
P = 10**9 + 7

dp[0] = 1
for i in range(1, N+1):
    if i in A:
        dp[i] = 0
    elif i == 1:
        dp[i] = dp[i-1] % P
    else:
        dp[i] = (dp[i-1] + dp[i-2]) % P
print(dp[-1])


Measured memory_bytes: 13252
// In the name of God

#include <iostream>
#include <algorithm>
#include <fstream>
#include <vector>
#include <deque>
#include <assert.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <stdio.h>
#include <string.h>
#include <utility>
#include <math.h>
#include <bitset>
#include <iomanip>
#include <complex>

using namespace std;

#define rep(i, a, b) for (int i = (a), i##_end_ = (b); i < i##_end_; ++i)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mp make_pair
#define x first
#define y second
#define pb push_back
#define SZ(x) (int((x).size()))
#define ALL(x) (x).begin(), (x).end()

template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool smin(T &a, const T &b)   { return a > b ? a = b : a;    }
template<typename T> inline bool smax(T &a, const T &b)   { return a < b ? a = b : a;    }

typedef long long LL;
#define int long long
const int N = (int) 1e6 + 6, mod = (int) 0;
int add[N], res[N], sz[N], par[N];
vector<int> all[N];
pair<int, pair<int, int>> em[N];
void unite(int u, int v, int w) {
	u = par[u];
	v = par[v];
	if (u == v) return;
	if (sz[u] > sz[v]) swap(u, v);
	add[v] += w * sz[u];
	for (int x : all[u]) {
		par[x] = v;
		res[x] += add[u];
		res[x] -= add[v];
		res[x] += w * sz[v];
		all[v].pb(x);
	}
	sz[v] += sz[u];
}
int32_t main() {
	int n;
	cin >> n;
	for (int j = 0; j < n - 1; ++j) {
		int u, v, w;
		cin >> u >> v >> w;
		--u, --v;
		em[j] = mp(w, mp(u, v));
	}
	for (int j = 0; j < n; ++j) par[j] = j, sz[j] = 1, all[j].pb(j);
	sort(em, em + n - 1);
	reverse(em, em + n - 1);
	for (int j = 0; j < n - 1; ++j) {
		int w = em[j].x, u = em[j].y.x, v = em[j].y.y;
		unite(u, v, w);
	}
	for (int j = 0; j < n; ++j)
		cout << res[j] + add[par[j]] << '\n';
}









Measured memory_bytes: 50548
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
#include <unordered_map>
using namespace std;


int main() {
	int a,b,c;
	cin >> a >> b >> c;
	if (a < b && b < c)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
}

Measured memory_bytes: 1156
#include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = numeric_limits<ll>::max();
const int INT_INF = 1 << 30;
#define MOD 1000000007LL
#define endl "\n"

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll N, K;
  cin >> N >> K;
  vll x(N), y(N);
  priority_queue<ll, vector<ll>, greater<ll>> xyque;
  map<ll, ll> xy, rev;
  for(int i = 0; i < N; i++){
    cin >> x.at(i) >> y.at(i);
    xyque.push(x.at(i)); xyque.push(y.at(i));
  }
  while(!xyque.empty()){
    ll n = xyque.top(); xyque.pop();
    if(!xy.count(n)){
      ll tmp = xy.size();
      xy[n] = tmp;
      rev[tmp] = n;
    }
  }
  ll nn = xy.size();
  vvll p(nn, vll(nn, 0));
  for(int i = 0; i < N; i++){
    ll xx = xy.at(x.at(i));
    ll yy = xy.at(y.at(i));
    p.at(xx).at(yy) = 1;
  }
  vvll sum(nn+1, vll(nn+1, 0));
  for(int i = 0; i < nn; i++){
    for(int j = 0; j < nn; j++){
      sum.at(i+1).at(j+1) = sum.at(i+1).at(j) + sum.at(i).at(j+1) - sum.at(i).at(j) + p.at(i).at(j);
    }
  }

  auto func = [&](int i, int j, int k, int l){
    ll S = sum.at(i).at(j) - sum.at(i).at(l) - sum.at(k).at(j) + sum.at(k).at(l);
    return S;
  };
  auto func2 = [&](int i, int j, int k, int l){
    ll S = (rev.at(i-1) - rev.at(k)) * (rev.at(j-1) - rev.at(l));
    return S;
  };
  ll ans = INF;
  for(int i = 1; i <= nn; i++){
    for(int j = 1; j <= nn; j++){
      for(int k = 0; k < i; k++){
        for(int l = 0; l < j; l++){
          if(func(i, j, k, l) == K) chmin(ans, func2(i, j, k, l));
        }
      }
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
const int sz = 1 << 16;
typedef pair< int, int > Pi;
vector< Pi > seg[2 * sz + 1];
int x[500000], y[500000];
int sx[20000], sy[20000], tx[20000], ty[20000];
vector< pair< int, Pi > > buff;
vector< int > nums;
vector< int > ret;
void InitRange(int k, int l, int r) {
  if(l + 1 >= r) {
    return;
  } else {
    InitRange(2 * k + 1, l, (l + r) >> 1);
    InitRange(2 * k + 2, (l + r) >> 1, r);
    if(k >= 114) {
      seg[k].assign(seg[2 * k + 1].size() + seg[2 * k + 2].size(), Pi());
      merge(seg[2 * k + 1].begin(), seg[2 * k + 1].end(), seg[2 * k + 2].begin(), seg[2 * k + 2].end(), seg[k].begin());
    }
  }
}
  
void PrintRange(int a, int b, int low, int high, int k, int l, int r) {
  if(a >= r || b <= l) {
    return;
  } else if(a <= l && r <= b && k >= 114) {
    for(vector< Pi >::iterator it = lower_bound(seg[k].begin(), seg[k].end(), Pi(low, -1)); it != seg[k].end() && it -> first <= high; it++) { 
      ret.push_back(it -> second);
    }
  } else {
    PrintRange(a, b, low, high, 2 * k + 1, l, (l + r) >> 1);
    PrintRange(a, b, low, high, 2 * k + 2, (l + r) >> 1, r);
  }
}
  
int main() {
  int n, q;
  scanf("%d", &n);
  for(int i = 0; i < n; i++) {
    scanf("%d %d", &x[i], &y[i]);
  }
  scanf("%d", &q);
  for(int i = 0; i < q; i++) {
    scanf("%d %d %d %d", &sx[i], &tx[i], &sy[i], &ty[i]);
    nums.push_back(sx[i]);
    nums.push_back(sx[i] - 1);
    nums.push_back(tx[i]);
  }
  sort(nums.begin(), nums.end());
  nums.erase(unique(nums.begin(), nums.end()), nums.end());
  for(int i = 0; i < n; i++) {
    x[i] = lower_bound(nums.begin(), nums.end(), x[i]) - nums.begin();
    buff.push_back(make_pair(x[i], make_pair(y[i], i)));
  }
  sort(buff.begin(), buff.end());
  for(int i = 0; i < n; i++) {
    seg[buff[i].first + sz - 1].push_back(buff[i].second);
  }
  InitRange(0, 0, sz);
 
  for(int i = 0; i < q; i++) {
    ret.clear();
    sx[i] = lower_bound(nums.begin(), nums.end(), sx[i]) - nums.begin();
    tx[i] = upper_bound(nums.begin(), nums.end(), tx[i]) - nums.begin();
  
    PrintRange(sx[i], tx[i], sy[i], ty[i], 0, 0, sz);
    sort(ret.begin(), ret.end());
    for(vector< int >::const_iterator it = ret.begin(); it != ret.end(); it++) {
      printf("%d\n", *it);
    }
    printf("\n");
  }
  
}

Measured memory_bytes: 54900
n=gets.to_i
a=gets.split.map(&:to_i)
b=gets.split.map(&:to_i)
c=gets.split.map(&:to_i)
sum=0
before=-10
n.times do |i|
    sum+=b[a[i]-1]
    sum+=c[a[i]-2] if a[i]==before+1
    before=a[i]
end
p sum

Measured memory_bytes: 1788
s = input()

for i in range(len(s)):
    if (i + 1) % 2 == 1 and s[i] == "L":
        print("No")
        break
    elif (i + 1) % 2 == 0 and s[i] == "R":
        print("No")
        break
else:
    print("Yes")

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

using namespace std;

int count(int a) {
    int r = 0,b = a;
    while(b != 0) {
        r = r + b % 10;
        b = (b - (b % 10)) / 10;
    }
    return r;
}

int main(void) {
    int n;
    cin >> n;
    int r = 99999;
    for(int i = 1;i < n;i++){
        r = min(r,count(i) + count(n-i));
    }
    cout << r << endl;
    return 0;
}


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

ll knapsack(ll N, ll W, ll v[], ll wt[])
{
	ll dp[N + 1][W + 1];

	for (ll i = 0; i <= W; i++)
		dp[1][i] = 0;
	dp[1][wt[1]] = v[1];

	for (ll i = 2; i <= N; i++)
	{
		for (ll w = 0; w <= W; w++)
		{
			dp[i][w] = dp[i - 1][w];	//Reject ith object

			if (wt[i] > w)
				continue;	//wt of ith obj is greater than available limit
			dp[i][w] = max(dp[i][w], v[i] + dp[i - 1][w - wt[i]]); //Select ith obj
		}
	}


	ll res = *max_element(dp[N], dp[N] + W + 1);
	return res;
}


int main()
{

	ll N, W;

	cin >> N >> W;

	ll v[N + 1], wt[N + 1];

	for (ll i = 1; i <= N; i++)
		cin >> wt[i] >> v[i];

	cout << knapsack(N, W, v, wt);
}

Measured memory_bytes: 81648
//author: dazhong_feliks

#include <bits/stdc++.h>
using namespace std;

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 << "Balanced" << endl;
}
  else if (( A + B ) < ( C + D ))
  { cout << "Right" << endl;
  }
}

Measured memory_bytes: 256
import java.util.*;
import java.math.*;

public class Main {

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int m = sc.nextInt();
    // long[] a = new long[n];
    // long[] b = new long[n];

    // for (long i = 0; i < n; i++) {
    //   a[i] = sc.nextLong();
    //   b[i] = sc.nextLong();
    // }

    Map<Long,Long> map = new TreeMap<>();

    for (long i = 0; i < n; i++) {
      long a = sc.nextLong();
      long b = sc.nextLong();
      if (map.containsKey(a)) {
        map.put(a,map.get(a)+b);
      } else {
        map.put(a,b);
      }
    }

    long count = 0;
    long sum = 0;

    for (long i : map.keySet()) {
      if (count > m) {
        break;
      }

      if (count+map.get(i) <= m) {
        sum += i*map.get(i);
      } else {
        sum += (m-count)*i;
      }

      count += map.get(i);

    }

    System.out.println(sum);

  }
}


Measured memory_bytes: 96716
#include <bits/stdc++.h>
#define sz(arr) (int)arr.size()
#define all(arr) arr.begin(), arr.end()

using namespace std;
typedef long long ll;

void _cin() {}
template <class Head, class... Tail>
void _cin(Head&& head, Tail&&... tail) {
  cin >> head;
  _cin(forward<Tail>(tail)...);
}

#define Cin(T, ...) \
  T __VA_ARGS__;    \
  _cin(__VA_ARGS__)

int main() {
  Cin(string, s, t);

  cout << t + s << endl;

  return 0;
}

Measured memory_bytes: 256
V, E = map(int, input().split())

PAR = list(range(V))
 
def root(x):
    if PAR[x] == x:
        return x
    else:
        PAR[x] = root(PAR[x])
        return PAR[x]
 
def same(x, y):
    return root(x) == root(y)
 
def unite(x, y):
    x = root(x)
    y = root(y)
    
    if x == y:
        return
    
    PAR[max(x, y)] = min(x, y)


class Edge:
    def __init__(self, u, v, cost):
        self.u = u
        self.v = v
        self.cost = cost
        
        

EDGES = []
for _ in range(E):
    s, t, w = map(int, input().split())
    EDGES.append(Edge(s, t, w))
    
def kruskal():
    edges = sorted(EDGES, key=lambda a: a.cost )
    res = 0
    for edge in edges:
        if (not same(edge.u, edge.v)):
            unite(edge.u, edge.v)
            res += edge.cost
    
    return res
    
print(kruskal())


Measured memory_bytes: 34928
import java.util.*;
import java.io.*;

public class Main {

	public static void main(String[] args) throws NumberFormatException, IOException {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	
		String s = sc.nextLine();
		String[] t = s.split("/");
		int[] n = new int[3];
		for (int i=0; i<3; i++) n[i] = Integer.parseInt(t[i]); 
		if (n[0]<2019) {
			System.out.println("Heisei");
		} else if (n[0]==2019) {
			if (n[1]<4) System.out.println("Heisei");
			else if (n[1]==4) {
				if (n[2]<=30) System.out.println("Heisei");
				else System.out.println("TBD");
			} else System.out.println("TBD");
		} else System.out.println("TBD");
		
		
		
		
	}
}


Measured memory_bytes: 21332
// AtCode.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <stdint.h>
#include <queue>
#include <bitset>

using namespace std;


long long gcd(long long a, long long b) {
    if (b == 0) {
        return a;
    }
    else {
        return gcd(b, a % b);
    }
}
// a,bの最小公倍数を求める
long long lcm(long long a, long long b) {
    return a * b / gcd(a, b);
}

vector<long long> A;

int main() {

    long long N, M;
    cin >> N >> M;
    for (long long i = 0; i < N; i++) {
        long long tmp;
        cin >> tmp;
        A.push_back(tmp);
    }

/*
半倍数＝２で割って奇数倍したもの
A配列をすべて、２で割ってその最小公倍数を求める
２を含む数が一致していない場合、半倍数の最小公倍数自体が作れないので答えは０

4:2,6,10,14,18...
6:3,9,15,21,27
8:4,12,20,28,
12:6,18,30,
16:8,24,40,

fx = x/2 * 奇数
2を含む個数を調べてチェック


*/
    long long tmp_dcount = 0;
    long long tmp_val = A[0];
    while (tmp_val % (long long)(2) == 0) {
        tmp_val = tmp_val / (long long)(2);
        tmp_dcount++;
    }

    for (long long i = 1; i < N; i++) {
        if ( (A[i]%((long long)(1)<<tmp_dcount) !=0) || (A[i] % ((long long)(1) << (tmp_dcount+1)) == 0) ) {
            cout << "0" << endl;
            return 0;
        }
    }

    for (long long i = 0; i < N; i++) {
        A[i] /= (long long)(2);
    }

    long long LCM = 1;
    for (long long i = 0; i < N; i++) {
        LCM = lcm(A[i], LCM);
    }

    //最小半公倍数が求まった
    long long ans = (M/LCM+ (long long)(1))/ (long long)(2);


    cout << ans << endl;

    return 0;
}



Measured memory_bytes: 1400
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,b) for(int i=a; i<=b; i++)
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define INF 1000000000000
#define MOD 10000007
#define PI 3.14159265359
#define dij priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>
typedef long long ll;
//

int main(){
    int n,x,t;cin>>n>>x>>t;
    int ans;
    if(n%x!=0)ans=t*(n/x+1);
    else ans=t*(n/x);
    cout<<ans<<endl;
}


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

int main (){
  
  int N,A;
  
  cin>>N>>A;
  
  if (N%500<=A){
    
    cout<<"Yes"<<endl;
  }
  
  else{
    
    cout<<"No"<<endl;
  }
  return 0;
  
}



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

using namespace std;

#define show(x) cerr << #x << " = " << x << endl;


bool compare( const pair<long long, long long>& x, const pair<long long, long long>& y )
{
    return (x.first < y.first);
}
int main()
{
    vector<pair<pair<long long, long long>, long long>> m;

    long long n, k, a, b;
    cin>>n>>k;


    vector<pair<long, long>> v;
    while(n--)
    {
      cin>>a>>b;
      v.push_back(make_pair(a,b));
    }

    sort(v.begin(), v.end(), compare);

    int ans = 0;

    long long limInf = 0;
    for(int i=0; i<v.size(); i++)
    {

      a = v[i].first;
      b = v[i].second;
      pair<long long, long long> val = make_pair(limInf+1, limInf + b) ;

      limInf = limInf + b;

      pair<pair<long long, long long>, long long> mapping = make_pair(val, a);
      m.push_back(mapping);
    }

    for(int i=0; i<m.size(); i++)
   {

     ///cout<<m[i].first.first<<" "<<m[i].first.second<<" "<<m[i].second<<endl;

     if(k>=m[i].first.first && k<=m[i].first.second)
     {
      ans = m[i].second;
      break;
     }
   }


   cout<<ans<<endl;

  return 0;
}


Measured memory_bytes: 7152
/*
ID: jerrywcy
TASK: AtCoder ABC 099 Problem D
LINK: https://abc099.contest.atcoder.jp/tasks/abc099_d
LANG: C++
*/
#include <bits/stdc++.h>

#define r(i,n) for(long long i=0;i<n;i++)

using namespace std;

long long a[510][510],d[40][40];
long long s[3][40];

int main(){
  	long long n,c,ans=1e9;
  	cin>>n>>c;
  	int i,j,k,l;

  	for (i=0;i<3;i++)
  		for (j=1;j<=c;j++)
	  		s[i][j]=0;

  	for (i=1;i<=c;i++)
  		for (j=1;j<=c;j++)
		  	cin>>d[i][j];
  	for (i=1;i<=n;i++)
	  	for (j=1;j<=n;j++)
		  	cin>>a[i][j];

  	for (i=1;i<=n;i++)
	  	for (j=1;j<=n;j++)
		  	s[(i+j)%3][a[i][j]]++;
  	for (i=1;i<=c;i++)
	  	for (j=1;j<=c;j++)
	  		if(i!=j)
			  	for (k=1;k<=c;k++)
				  	if(i!=k && j!=k){
					    long long sum=0;
					    for (l=1;l<=c;l++){
					      sum+=d[l][i]*s[0][l];
					      sum+=d[l][j]*s[1][l];
					      sum+=d[l][k]*s[2][l];
					    }
					    ans=min(ans,sum);
  					}
  	cout<<ans<<endl;
}

Measured memory_bytes: 2304
import java.util.Scanner;

public class Main {
	public static void main(String arg[]){
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int S[] = new int[n+1];
		int key = 0;
		int ans = 0;

		for(int i=0; i<n; i++){
			S[i] = sc.nextInt();
		}
		int q = sc.nextInt();
		int T[] = new int[q];
		for(int i=0; i<q; i++){
			T[i] = sc.nextInt();
		}
		for(int i=0; i<q; i++){
			key = T[i];
			if(linearSearch(S, key, n) == 1){
				ans++;
			}
		}
		System.out.println(ans);
	}
	
	public static int linearSearch(int A[], int key, int n){
		int i =0;
		A[n] = key;
		while (A[i] != key){
			i++;
			if(i==n) return -1;
		}
		return 1;
	}
}


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

int main(){
  long long int K,A,B;
  cin >>K>>A>>B;
  long long int c=1;
  long long int i=0;
  while(i<K){
    if(c<A){
      c++;
      i++;
      continue;
    }
    else if(B-A >2 && i<K-1){
      c =c-A+B;
      i+=2;
      continue;
    }
    c++;
    i++;
  }
  cout << c<<endl;
}

Measured memory_bytes: 256
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int x = sc.nextInt();

        int[] a = new int[n];

        for(int i = 0; i < n; i++){
            a[i] = sc.nextInt() - x;
        }

        int gcd = a[0];

        for(int i = 1; i < n; i++){
            gcd = gcd(gcd, a[i]);
        }

        System.out.println(Math.abs(gcd));

    }

    static int gcd (int a, int b) {
        int temp;
        while((temp = a%b)!=0) {
            a = b;
            b = temp;
        }
        return b;
    }
}


Measured memory_bytes: 48984
#include <bits/stdc++.h>
#define N 55
using namespace std;
const int INF = 1e9;
const int mod = (1e9)+7;
const double EPS = 1e-8;
const double PI = 6.0 * asin(0.5);
template<class T> T Max(T &a,T b){return a=max(a,b);}
template<class T> T Min(T &a,T b){return a=min(a,b);}
typedef pair<int,int> P;
typedef pair<P,P> PP;
typedef priority_queue<PP,vector<PP>,greater<PP> > PQ;
int n, fee[N], height[N],G[2][N][N];
int D[N][N][1<<10];

int Push(PQ &Q,int cost,int p1,int p2,int bit){
  if(D[p1][p2][bit] <= cost) return 0 ;
  D[p1][p2][bit] = cost;
  Q.push(PP(P(cost,p1),P(p2,bit)));
  return 1;
}

int dijkstra(){

  int idx[N];
  map<int,int> cnt;
  for(int i=0;i<n;i++) idx[i] = cnt[height[i]]++;

  for(int i=0;i<N;i++)
    for(int j=0;j<N;j++)
      for(int k=0;k<(1<<10);k++) D[i][j][k] = INF;
  PQ Q;
  Push(Q,0,0,0,1<<idx[0]);
  
  while(!Q.empty()){
    PP t = Q.top();Q.pop();
    int cost = t.first.first;
    int p1 = t.first.second;
    int p2 = t.second.first;
    int bit = t.second.second;
    if(D[p1][p2][bit] < cost) continue;
    if(p1 == n-1 && p2 == n-1) return cost;
    
    for(int to=0;to<n;to++){
      int ncost = cost + G[0][p1][to] + fee[to];
      int nbit = bit;
      if(height[p1] == height[to] && bit>>idx[to]&1) ncost -= fee[to] ;
      if(height[p1] == height[to]) nbit |= 1<<idx[to];
      else nbit = 1<<idx[to];
      Push(Q,ncost,to,p2,nbit);
    }
    
    for(int to=0;to<n;to++){
      if(height[p1] < height[to]) continue;
      int ncost = cost + G[1][p2][to] + fee[to];
      if(height[p1] == height[to] && bit>>idx[to]&1) ncost -= fee[to];
      Push(Q,ncost,p1,to,bit);
    }
  }
  return -1;
}

signed main(){
  while(1){
  int m;
  cin>>n>>m;
  if(n == 0 && m == 0) return 0;
  memset(fee,0,sizeof(fee));
  memset(height,0,sizeof(height));
  for(int i=0;i<N;i++)
    for(int j=0;j<N;j++) G[0][i][j] = G[1][i][j] = INF;
  
  height[0] = 0;
  height[n-1] = 1000;
  for(int i=1;i<n-1;i++) cin>>fee[i]>>height[i];
  
  for(int i=0;i<m;i++){
    int a,b,c;
    cin>>a>>b>>c; a--, b--;
    if(height[a] <= height[b]) Min(G[0][a][b],c);
    if(height[a] >= height[b]) Min(G[1][b][a],c);
  }
  cout<<dijkstra()<<endl;
  }
  return 0;
}

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

#define INF 1e9
using namespace std;
using ll = long long;

int main() {

    ll N; cin >> N;
    string S; cin >> S;

    ll ans = 0;

    for (int i = 0; i < S.size(); ++i) {
        ll cnt = 0;

        set<char> X = {};
        for (int j = 0; j <= i; ++j) {
            X.insert(S[j]);
        }

        set<char> Y = {};
        for (int k = i+1; k < S.size(); ++k) {
            Y.insert(S[k]);
        }

        for (auto x : X) {
            if (Y.find(x) != Y.end()) {
                ++cnt;
            }
        }

        ans = max(cnt, ans);
    }

    cout << ans << endl;
}

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


int main() {
	
	vector<int> c(9);
	for(auto&i:c) cin >> i;
	vector<int>a(3), b(3);
	
	a.at(0) = 0;
	b.at(0) = c.at(0);
	b.at(1) = c.at(1);
	b.at(2) = c.at(2);
	a.at(1) = c.at(3) - b.at(0);
	a.at(2) = c.at(6) - b.at(0);
	
	bool flag = 1;
	if(c.at(4)!=a.at(1)+b.at(1)) flag = 0;
	if(c.at(5)!=a.at(1)+b.at(2)) flag = 0;
	if(c.at(7)!=a.at(2)+b.at(1)) flag = 0;
	if(c.at(8)!=a.at(2)+b.at(2)) flag = 0;
	
	if(flag) cout << "Yes" << endl;
	else cout << "No" << endl;
	
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
	char s[3][3];
	for (int i = 0; i < 3; ++i)
	{
		for (int j = 0; j < 3; ++j)
		{
			cin >> s[i][j];
		}
	}
	cout << s[0][0] << s[1][1] << s[2][2];
}

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

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define rep(i,n) for(int i=0; i<(int)(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using P = pair<ll, ll>;

//constexpr ll mod = 998244353;
constexpr ll mod = 1e9+7;
const double PI = acos(-1.0);
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int h, w; cin >> h >> w;

  vector<vector<bool>> mat(h, vector<bool>(w));
  rep(i, h) {
    rep(j, w) {
      char c; cin >> c;
      mat[i][j] = (c == '#');
    }
  }

  vector<vector<bool>> mat2(h-1, vector<bool>(w-1));

  rep(i, h-1) {
    rep(j, w-1) {
      mat2[i][j] = (mat[i][j] ^ mat[i+1][j] ^ mat[i][j+1] ^ mat[i+1][j+1]);
    }
  }

  vector<vector<int>> mat3(h-1, vector<int>(w));
  rep(i, h-1) {
    rep(j, w-1) {
      if (!mat2[i][j]) {
        mat3[i][j] = 1;
        if (i>0) mat3[i][j] += mat3[i-1][j];
      }
    }
  }

  int ans = max(h, w);
  for(int i=h-2; i>=0; i--) {
    stack<P> st;
    rep(j, w) {
      if (st.empty() || mat3[i][j] > st.top().second) {
        st.emplace(j, mat3[i][j]);
      } else if (mat3[i][j] < st.top().second) {
        int target = j;
        while(!st.empty() && st.top().second > mat3[i][j]) {
          P p = st.top(); st.pop();
          int height = p.second;
          int pos = p.first;
          chmax(ans, (height+1)*(j-pos+1));
          target = pos;
        }
        st.emplace(target, mat3[i][j]);
      }
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 17152
#include<stdio.h>
#include<limits.h>
#define d_flag 0
#define MAX 100000
#define INF 1e12
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)<(b)?(b):(a))
#define abs(a) max((a),-(a))
typedef long long int li;

int cp(li a,li b){
  // cp(a,b) means (a<=b)
  if(b== -1)return 1;
  // -1 means INF
  else return(a<=b);
}

int main(){
  int n,W;
  scanf("%d%d",&n,&W);
  int i,j,v[n];
  li w[n],dp[MAX+1];   //dp[v]=w
  
  for(i=0;i<n;i++){
    scanf("%lld%d",&w[i],&v[i]);
  }

  if(d_flag) printf("%ld\n",LONG_MAX);

  int max_v=0;
  li temp;
 
  for(i=0;i<=MAX;i++)dp[i]=INF;
  //  if()
  dp[0]=0;
  for(i=0;i<n;i++){
  if(d_flag) printf("i:%d, j=%d downto %d\n",i,max_v,0);
    for(j=max_v+v[i];j>=v[i];j--){
      temp=dp[j-v[i]]+w[i];
      if(d_flag) printf("temp:%ld, W:%d, dp[%d]=%d\n",temp,W,j,dp[j]);
      if(temp<=W && temp<dp[j]){
	dp[j]=temp;
	if(max_v<j){
	  max_v=j;
	}
      }
      if(d_flag) printf("\n");
    }
  }
  
  if(d_flag){
    for(i=0;i<=max_v;i++)
      printf(" %ld",dp[i]);
    printf("\n");
  }
  printf("%lld",max_v);
}


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

int main(int argc, const char * argv[])
{
    int cu[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};
    int g, j, cu2[10];
    while (cin >> g) {
        for (int i = 0; i < 10; i++)
            cu2[i] = 0;
        j = 0;
        for (int i = 0; i < 11; i++) {
            if ((g - cu[i]) < 0) {
                g -= cu [i - 1];
                cu2[j] = cu[i - 1];
                i = 0; j++;
            }
            if (g == 0) break;
        }
        for (int i = 0; i < j / 2; i++) {
            int tmp;
            tmp = cu2[j - 1 - i];
            cu2[j - 1 - i] = cu2[i];
            cu2[i] = tmp;
        }
        for (int i = 0; i < j; i++) {
            printf("%d", cu2[i]);
            if (i + 1 < j)
                printf(" ");
        }
        puts("");
    }
    return 0;
}

Measured memory_bytes: 1152
#include<bits/stdc++.h>
#define     ll          long long
#define     MOD         10e9+7
#define     pb          push_back
#define     vll         vector<long long>
#define     vi          vector<int>
#define     vl          vector<long>
#define     pii         pair<int, int>
#define     all(a)      (a).begin(),(a).end()
#define     rep(i,a,b)  for(int i=a;i<b;i++)
#define     MAX         100002 
#define     ios	      ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

int main() {
    
      ios
      ll a,b,c,k;
      cin >> a>> b >> c >> k;
      ll ans=0;
      if(a>=k)cout << k;
      else{
            ans+=a; k-=a;
            if(b>=k)cout << ans;
            else{
                  k-=b;
                  cout << ans-k;
            }
      }
      return 0;
        
}


Measured memory_bytes: 3572
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define mit map<int,int>::iterator
#define sit set<int>::iterator
#define itrm(g,x) for(mit g=x.begin();g!=x.end();g++)
#define itrs(g,x) for(sit g=x.begin();g!=x.end();g++)
#define ltype int
#define rep(i,j,k) for(ltype(i)=(j);(i)<=(k);(i)++)
#define rap(i,j,k) for(ltype(i)=(j);(i)<(k);(i)++)
#define per(i,j,k) for(ltype(i)=(j);(i)>=(k);(i)--)
#define pii pair<int,int>
#define fi first
#define se second
#define mpr make_pair
#define pb push_back
#define fastio ios::sync_with_stdio(false)
const int inf=0x3f3f3f3f,mod=1000000007;
const double pi=3.1415926535897932,eps=1e-6;
string txt,s;int z[10005],l,r,n,ans;
void calc(){
    int l=0,r=0;
    for (int i=1;i<s.size();i++){
        z[i]=0;
        if (i>r){
            l=i,r=i;
            while(r<s.size()&&s[r-l]==s[r]) r++;
            z[i]=r-l,r--;
        }else{
            int k=i-l;
            if(z[k]<r-i+1) z[i]=z[k];
            else{
                l=i;
                while(r<s.size()&&s[r-l]==s[r]) r++;
                z[i]=r-l,r--;
        	}
        }
    }
}
int main()
{
    fastio;
    cin>>n>>txt;
    rap(i,0,n-1){
        s=txt.substr(i);
        calc();
        rap(j,0,s.size()) ans=max(ans,min(z[j],j));
    }
    cout<<ans;
    return 0;
}

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

// template {{{  0 
// using {{{ 1
using ll = long long int;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vii = vector<pii>;
using vll = vector<pll>;
// }}} 1
// definition {{{ 1
// scaning {{{ 2
#define Scd(x) scanf("%d", &x)
#define Scd2(x,y) scanf("%d%d", &x, &y)
#define Scd3(x,y,z) scanf("%d%d%d", &x, &y, &z)
#define Scll(x) scanf("%lld", &x)
#define Scll2(x,y) scanf("%lld%lld", &x, &y)
#define Scll3(x,y,z) scanf("%lld%lld%lld", &x, &y, &z)
#define Scc(c) scanf("%c", &c);
#define Scs(s) scanf("%s", s);
#define Scstr(s) scanf("%s", &s);
// }}} 2
// constants {{{ 2
#define EPS (1e-7)
#define INF (2e9)
#define PI (acos(-1))
// }}} 2
// systems {{{ 2
#define Repe(x,y,z) for(ll x = z; x < y; x++)
#define Rep(x,y) Repe(x,y,0)
#define RRepe(x,y,z) for(ll x = y-z-1; x >= 0; x--)
#define RRep(x,y) RRepe(x,y,0)
// }}} 2
// output {{{ 2
#define YesNo(a) (a)?printf("Yes\n"):printf("No\n")
#define YESNO(a) (a)?printf("YES\n"):printf("NO\n")
// }}} 2
// }}} 1
// input {{{ 1
// }}} 1
// }}} 0

int main() {

    char s[1020];
    Scs(s);
    int N = strlen(s);
    int cnt[256] = {};

    Rep(i,N){
        cnt[s[i]]++;
    }
    bool ans = true;
    if( (cnt['W']>0) xor (cnt['E']>0) ) ans = false;
    if( (cnt['N']>0) xor (cnt['S']>0) ) ans = false;

    YesNo(ans);

    return 0;
}



Measured memory_bytes: 256
# https://atcoder.jp/contests/abc131/tasks/abc131_c
# A~Bと言われたら、0~B と 0~Aの個数で成り立つのではないか？と考える典型問題

from fractions import gcd


def lcm(a, b):
    g = gcd(a, b)
    return a * b // g


def f(n, c, d):
    # nを含んでcでもdでも割り切れる数の個数を返す
    return n // c + n // d - n // lcm(c, d)


A, B, C, D = list(map(int, input().split()))

print((B - A + 1) - (f(B, C, D) - f(A - 1, C, D)))


Measured memory_bytes: 5176
#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=mod*mod*3LL;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
vector<ll> x;
vector<vector<pair<ll, ll> > > v;
bool used[200000];

vector<ll> ans;
ll dfs(ll now){
	used[now] = true;
	ll res = x[now];
	for(auto& to: v[now]){
		if(used[to.first]) continue;
		ll a = dfs(to.first);
		if(a) ans.pb(to.second);
		res ^= a;
	}
	return res;
} 
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    ll n, m;
    cin >> n >> m;
    vector<pair<ll, ll> > a(n);
    for(int i=0;i<n;i++) cin >> a[i].first >> a[i].second;
    sort(be(a));
    x.resize(n+1);
    x[0] = a[0].second;
    for(ll i=1;i<n;i++) x[i] = a[i].second ^ a[i-1].second;
    x[n] = a[n-1].second;
    v.resize(n+1);
    for(ll i=0;i<m;i++){
    	ll l, r;
    	cin >> l >> r;
    	l = lower_bound(be(a), make_pair(l, 0LL)) - a.begin();
    	r = upper_bound(be(a), make_pair(r, 1LL)) - a.begin();
    	v[l].push_back({r, i + 1});
    	v[r].push_back({l, i + 1});
    }
    for(ll i=0;i<=n;i++){
    	if(used[i]) continue;
    	if(dfs(i)){
    		cout << -1 << endl;
    		return 0;
    	}
    }
    cout << ans.size() << endl;
    sort(be(ans));
    for(auto& i: ans){
    	cout << i << " ";
    }
    cout << endl;
    return 0;
}


Measured memory_bytes: 19836
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <iomanip>
#define rep(i,n) Rep(i,0,n)
#define Rep(i,k,n) for(int i=k ; i<n ; i++)
#define rep1(i,n) for(int i=1 ; i<=n ; i++)
#define vi vector<int>
#define vii vector<int,int>
#define Sort(v) sort(v.begin(),v.end())
#define Reverse(v) reverse(v.begin(),v.end())

//const int MOD = 1000000007;
//const int INF = 1<<30;
using namespace std;

int main(){
    int n,m,l;
    cin >> n >> m >> l;
    
    int a[n][m],b[m][l];
    
    rep(i,n) rep(j,m) cin >> a[i][j];
    rep(i,m) rep(j,l) cin >> b[i][j];
    
    long long c[n][l];
    
    rep(i,n){
        rep(j,l) {
            c[i][j] = 0;
            rep(k,m) {
                c[i][j] += a[i][k]*b[k][j];
            }
        }
    }
    
    rep(i,n) {
        rep(j,l-1){
            cout << c[i][j] << " " ;
        }
        cout << c[i][l-1] << endl;
    }
}








Measured memory_bytes: 3244
s = gets.strip.split('')
k = gets.strip.to_i


if s.uniq.length==1
  p (s.length*k)/2; exit
end

f = (s.first==s.last)

rl = []
t=[s.shift]
s.each do |e|
  if e != t.last
    rl << t.length
    t=[e]
  else
    t<<e
  end
end
rl << t.length

result=0
if f
  first = rl.first
  last = rl.last

  result += first/2 + last/2

  rl[0] = rl[0]+rl.pop
  result += k*rl.map {|e|e/2}.inject(:+)

  result -= rl[0]/2

  p result
else
  p result = k*rl.map {|e|e/2}.inject(:+)
end


Measured memory_bytes: 1788