akhauriyash/Code-Regression huggingface dataset:
#include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(V),v.end()));
#define DUMP(v) REP(i, (v).size()) { cout << v[i]; if (i != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

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

typedef long long ll;
using namespace std;


int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string a, b, c;
	cin >> a >> b >> c;
	int turn = 0;
	while (1) {
		if (turn == 0) {
			if (a.empty()) {
				cout << 'A' << endl;
				return 0;
			}
			turn = a[0] - 'a';
			a.erase(a.begin());
		}
		else if (turn == 1) {
			if (b.empty()) {
				cout << 'B' << endl;
				return 0;
			}
			turn = b[0] - 'a';
			b.erase(b.begin());
		}
		else {
			if (c.empty()) {
				cout << 'C' << endl;
				return 0;
			}
			turn = c[0] - 'a';
			c.erase(c.begin());
		}
	}
	return 0;
}


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

int main() 
{
    string S;
    cin >> S;

    int num = atoi(S.c_str());

    int s = num / 1000;
    int h = (num / 100) % 10;
    int j = (num / 10) % 10;
    int i = num % 10;

   
    if( (s * 10 + h) <= 12 && 0 != (s * 10 + h) )
    {
        if( (j * 10 + i) <= 12 && 0 != (j * 10 + i)) cout << "AMBIGUOUS" << endl;
        else                                         cout << "MMYY" << endl;
    }
    else if( (j * 10 + i) <= 12 && 0 != (j * 10 + i) )
    {
        cout << "YYMM" << endl;
    }
    else
    {
        cout << "NA" << endl;
    }
}


Measured memory_bytes: 256
//BISMILLAHIR RAHMANIR RAHIM
//RABBI ZIDNI ILMA
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ff first
#define ss second
#define endl "\n"
#define inf (1LL<<61)
#define pi acos(-1)
#define mod 1000000007
#define all(x) x.begin(), x.end()
#define fastio ios_base::sync_with_stdio(0);cout<< fixed<< setprecision(10);
#define DEBUG(x) cout<<"["<<#x<<": "<<x<<"]\n"
#define DEBUG2(x, y) cout<<"["<<#x<<": "<<x<<"]"<<"  ["<<#y<<": "<<y<<"]\n"
#define DEBUG3(x, y, z) cout<<"["<<#x<<": "<<x<<"]"<<"  ["<<#y<<": "<<y<<"]"<<"  ["<<#z<<": "<<z<<"]\n"
#define DEBUG4(x, y, z, k) cout<<"["<<#x<<": "<<x<<"]"<<"  ["<<#y<<": "<<y<<"]"<<"  ["<<#z<<": "<<z<<"]"<<"  ["<<#k<<": "<<k<<"]\n"

const ll N = 1e5 + 9;

int main()
{
    fastio
    string s;
    cin>> s;
    ll val = 0;
    for(int i=0; i<s.size(); i++){
        val += s[i] - '0';
    }
    cout<< (val % 9 ? "No" : "Yes");
}



Measured memory_bytes: 3748
#define _USE_MATH_DEFINES
#include  <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include<math.h>
#include<iomanip>
#include<stdio.h>
#include <stdlib.h>
#include<stdio.h>
#include <queue>
#include<map>
#include <sstream>
#include<set>
#include<stack>

//#include<bits/stdc++.h>


using namespace std;



int main() {
	
	string s, t;
	cin >> s >> t;
	int n = s.size();
	map<char, char>x;
	map<char, bool>y;
	map<char, char>a;
	map<char, bool>b;
	for (int i = 0; i < n; i++) {
		if (y[t[i]]) {
			if (x[t[i]] == s[i])continue;
			else {
				cout << "No" << endl;
				return 0;
			}
		}
		if (b[s[i]]) {
			if (a[s[i]] == t[i])continue;
			else {
				cout << "No" << endl;
				return 0;
			}
		}

		b[s[i]] = 1;
		a[s[i]] = t[i];
		
		y[t[i]] = 1;
		x[t[i]] = s[i];
	}
	cout << "Yes" << endl;

	return 0;

}

Measured memory_bytes: 776
#include<iostream>
using namespace std;
int main(void)
{
    int N,i,cnt=0,a[11451];
    cin>>N;
    for(i=0;i<N;i++){
        cin>>a[i];
    }
    for(i=0;i<N;i++){
        if(a[i]==a[i+1]){
            cnt++;
            a[i+1]+=10000;
        }
    }
    cout<<cnt<<endl;

    return 0;

}

Measured memory_bytes: 256
n = list(map(int, (input()).split(' ')))
#n = [2500,7500]
#print(n)
x = [ i   for i in range(n[0]+1)]
#y = [ i   for i in range(n[1]+1)]
#z = [ i   for i in range(n[1]+1)]
#print(x)
counter = 0
if n[0]*3 >= n[1]:
  for x1 in x:
    for y1 in x:
      if 0<= n[1]-x1-y1 <=n[0]  :
          counter +=1

        
print(counter)        


Measured memory_bytes: 3060
#include<cstdio>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;

struct edge{
    int to,cost;
    edge(int to,int cost):
        to(to),cost(cost){}
    edge(){}
};
struct visit{
    int pos,total,b;
    visit(int pos,int total,int b):
        pos(pos),total(total),b(b){}
    visit(){}
    bool operator>(const visit &v)const{
        return total>v.total;
    }
};
int N,M,L,K,A,H;
vector<edge>G[100];
bool ice[100];

void solve(){
    bool used[100][101]={{0}};
    priority_queue<visit,vector<visit>,greater<visit> >Q;
    Q.push(visit(A,0,M));

    while(!Q.empty()){
        visit v=Q.top();Q.pop();
        if(used[v.pos][v.b])continue;
        used[v.pos][v.b]=true;
        if(v.pos==H){
            printf("%d\n",v.total);
            return;
        }
        for(int i=0;i<G[v.pos].size();i++){
            edge &e=G[v.pos][i];
            int tb=v.b-e.cost;
            if(tb<0)continue;
            if(used[e.to][tb])continue;
            Q.push(visit(e.to,v.total+e.cost,tb));
        }
        if(!ice[v.pos])continue;
        for(int i=v.b+1;i<=M;i++){
            if(used[v.pos][i])continue;
            Q.push(visit(v.pos,v.total+i-v.b,i));
        }
    }
    puts("Help!");
}
int main(){
    while(scanf("%d%d%d%d%d%d",&N,&M,&L,&K,&A,&H),N||M||L||K||A||H){
        fill_n(G,N,vector<edge>(0));
        fill_n(ice,N,false);
        for(int i=0;i<L;i++){
            int pos;scanf("%d",&pos);
            ice[pos]=true;
        }
        for(int i=0;i<K;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            G[a].push_back(edge(b,c));
            G[b].push_back(edge(a,c));
        }

        solve();
    }
    return 0;
}

Measured memory_bytes: 2548
#include <iostream>
#include <vector>
#include <iomanip>
#define _USE_MATH_DEFINES
#include <cmath>
using namespace  std;

const double cos60 = cos(M_PI / 3);
const double sin60 = sin(M_PI / 3);

struct StPnt
{
  double m_nx;
  double m_ny;

  StPnt() :
    m_nx(0), m_ny(0)
  { }
  StPnt(double nx, double ny) :
    m_nx(nx), m_ny(ny)
  { }

  ~StPnt() { }
};

void fnInput(int &rnRcSiz)
{
  cin >> rnRcSiz;
}

void fnPntGet(const StPnt &cnroPntL, const StPnt &cnroPntR, vector<StPnt> &rvoRtnPnt)
{
  rvoRtnPnt[0].m_nx = (2 * cnroPntL.m_nx + cnroPntR.m_nx) / 3;
  rvoRtnPnt[0].m_ny = (2 * cnroPntL.m_ny + cnroPntR.m_ny) / 3;
  rvoRtnPnt[2].m_nx = (cnroPntL.m_nx + 2 * cnroPntR.m_nx) / 3;
  rvoRtnPnt[2].m_ny = (cnroPntL.m_ny + 2 * cnroPntR.m_ny) / 3;

  rvoRtnPnt[1].m_nx =  (rvoRtnPnt[2].m_nx - rvoRtnPnt[0].m_nx) * cos60 
                     - (rvoRtnPnt[2].m_ny - rvoRtnPnt[0].m_ny) * sin60 + rvoRtnPnt[0].m_nx;
  rvoRtnPnt[1].m_ny =  (rvoRtnPnt[2].m_nx - rvoRtnPnt[0].m_nx) * sin60 
                     + (rvoRtnPnt[2].m_ny - rvoRtnPnt[0].m_ny) * cos60 + rvoRtnPnt[0].m_ny;
}

void fnResult(const StPnt &cnroPnt)
{
  cout << cnroPnt.m_nx << " " << cnroPnt.m_ny << endl;
}

void fnKoch(int nRcSiz, const StPnt &cnroPntL, const StPnt &cnroPntR)
{
  if (!nRcSiz) return;

  vector<StPnt> voRtnPnt(3);
  fnPntGet(cnroPntL, cnroPntR, voRtnPnt); 
  
  nRcSiz--;
  fnKoch(nRcSiz, cnroPntL, voRtnPnt[0]);
  fnResult(voRtnPnt[0]);

  fnKoch(nRcSiz, voRtnPnt[0], voRtnPnt[1]);
  fnResult(voRtnPnt[1]);

  fnKoch(nRcSiz, voRtnPnt[1], voRtnPnt[2]);
  fnResult(voRtnPnt[2]);

  fnKoch(nRcSiz, voRtnPnt[2], cnroPntR);
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(8);

  int nRcSiz;
  vector<StPnt> voPnt;

  fnInput(nRcSiz);
  StPnt oPntL(0, 0);
  StPnt oPntR(100, 0);

  fnResult(oPntL);
  fnKoch(nRcSiz, oPntL, oPntR);
  fnResult(oPntR);
  return 0;
}

Measured memory_bytes: 1268
/******************************************************************************

Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby, 
C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
Code, Compile, Run and Debug online from anywhere in world.

*******************************************************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int MOD = 1000000007;

LL fact[200000];
LL inv_fact[200000];

LL modpow(int a, int b){
    if (b == 0){
        return 1;
    }
    else if (b & 1){
        return (a * modpow(a, b - 1)) % MOD;
    }
    else{
        LL d = modpow(a, b / 2);
        return (d * d) % MOD;
    }
}

LL fermat_little(int n){
    int a = modpow(n, MOD - 2);
    return a;
}

void gen(int n){
    fact[0] = 1;
    inv_fact[0] = -1;
    for (LL i = 1; i <= n; i++){
        fact[i] = (fact[i - 1] * i) % MOD;
        inv_fact[i] = fermat_little(fact[i]);
    }
}

LL choose(int n, int k){
    if (n < k) return 0;
    if (k == 0) return 1;
    if (n == k) return 1;
    LL ans = fact[n];
    ans *= inv_fact[k]; ans %= MOD;
    ans *= inv_fact[n-k]; ans %= MOD;
    return ans;
}


int main()
{
    int h, w, a, b;
    cin >> h >> w >> a >> b;
    gen(h + w - 1);
    LL ans = 0;
    for (int i = b + 1; i <= w; i++){
        ans += (choose(h-a+i-2, h-a-1) * choose(w-i+a-1,w-i)) % MOD;
        ans %= MOD;
    }
    cout << ans<< endl;
    return 0;
}










Measured memory_bytes: 3328
//   ┏━━┓┏━━┓┏┓┏┓┏┳━┳┓┏━━┓┏━┳┓┏━━┓     ┏┳┓┏┳┓┏━┳━┓┏━━┓┏━┓
//   ┃┏┓┃┃━━┫┃┗┛┃┃┃┃┃┃┃┏┓┃┃┃┃┃┗┃┃┛     ┃┏┛┃┃┃┃┃┃┃┃┃┏┓┃┃╋┃
//   ┃┣┫┃┣━━┃┃┏┓┃┃┃┃┃┃┃┣┫┃┃┃┃┃┏┃┃┓     ┃┗┓┃┃┃┃┃┃┃┃┃┣┫┃┃┓┫
//   ┗┛┗┛┗━━┛┗┛┗┛┗━┻━┛┗┛┗┛┗┻━┛┗━━┛     ┗┻┛┗━┛┗┻━┻┛┗┛┗┛┗┻┛
#include<bits/stdc++.h>
using namespace std;
#define FOR(a, c) for (int a  = 0; a<c ; a++)
#define FORL(a, b, c) for (int a  = b;  a< c ; a++)
typedef long long int ll;
int main()
{
  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  int n;
  cin>>n;
  if(n==0){
    cout<< 1;
  }
  else{
    cout<< 0;
  }
  return 0;
}
// By Ashwani Kumar, Indian Institute Of Technology(IIT), Guwahati.
//   ╔══╗╔══╗╔══╗     ╔══╗╔╦╗╔╦═╦╗╔══╗╔╗╔╗╔══╗╔══╗╔══╗
//   ╚║║╝╚║║╝╚╗╔╝     ║╔═╣║║║║║║║║║╔╗║║╚╝║║╔╗║╚╗╔╝╚║║╝
//   ╔║║╗╔║║╗─║║─     ║╚╗║║║║║║║║║║╠╣║║╔╗║║╠╣║─║║─╔║║╗
//   ╚══╝╚══╝─╚╝─     ╚══╝╚═╝╚═╩═╝╚╝╚╝╚╝╚╝╚╝╚╝─╚╝─╚══╝


Measured memory_bytes: 3624
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
const long double PI = (acos(-1));
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int ctoi(char c)
{
    switch (c)
    {
    case '0':
        return 0;
    case '1':
        return 1;
    case '2':
        return 2;
    case '3':
        return 3;
    case '4':
        return 4;
    case '5':
        return 5;
    case '6':
        return 6;
    case '7':
        return 7;
    case '8':
        return 8;
    case '9':
        return 9;
    default:
        return 0;
    }
}
int main()
{
    vector<char> vec;
    string s;
    cin >> s;
    for (auto i : s)
    {
        if (i == '0')
        {
            vec.emplace_back(i);
        }
        else if (i == '1')
        {
            vec.emplace_back(i);
        }
        else
        {
            if (vec.size() == 0)
            {
                continue;
            }
            else
            {
                vec.pop_back();
            }
        }
    }
    for (auto j : vec)
    {
        cout << j;
    }
    cout << endl;
}

Measured memory_bytes: 256
#include<stdio.h>

int main(){
  int n, x, i, j, k, count;
  while (1) {
    count = 0;
    scanf("%d %d", &n, &x);
    if (n == 0 && x == 0) {
      break;
    }

    for (i=1; i<n-1; i++) {
      for (j=i+1; j<n; j++) {
        for (k=j+1; k<n+1; k++) {
          if (i+j+k == x) {
            count++;
            break;
          } else if (i+j+k > x) {
            break;
          }
        }
      }
    }
    printf("%d\n", count);
  }
  return 0;
}

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

int main(){
  int a,b;
  cin >> a >> b;
  int n;
  n = 100 * a;
  int ans = -1;
  rep(i,n+1){
    int A ,B ;
    if( i == 0 ) continue;
    A = 0.08 * i;
    B = 0.1 * i;
    if( A == a && B == b ) {
      ans = i;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    int n,k;
    cin >> n >> k;
    vector<int> v(n);
    for(int i=0;i<n;i++) cin >> v[i];

    int ans = 0;
    for(int i=1;i<=min(n, k);i++){
        for(int j=0;j<=i;j++){
            vector<int> hand;
            for(int l=0;l<j;l++) hand.push_back(v[l]);
            for(int l=n-1;l>max(n-1-(i-j), 0);l--) hand.push_back(v[l]);

            sort(hand.begin(), hand.end());
            for(int l=0;l<k-i;l++){
                if(hand.size()==0) break;
                if(hand[0]<0) hand.erase(hand.begin());
            }
            int tmp = 0;
            for(int x : hand) tmp += x;
            ans = max(ans, tmp);
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
s = input()
if s[0]!="A":
    print("WA")
else:
    if s[2:-1].count("C")!=1 or s[1]=="C" or s[-1]=="C":
        print("WA")
    else:
        s = s.replace("A","")
        s = s.replace("C","")
        if s.islower():
            print("AC")
        else:
            print("WA")

Measured memory_bytes: 3060
n = int(input())
s = input()
a = [set(), set(), set()]
for i in range(n):
    for j in a[1]:
        a[2].add(j + s[i])
    for j in a[0]:
        a[1].add(j + s[i])
    a[0].add(s[i])
print(len(a[2]))


Measured memory_bytes: 3188
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;
int main() {
  int r;
  cin >> r;
  if (r<1200) printf("ABC\n");
  else if (r<2800) printf("ARC\n");
  else printf("AGC\n");
}

Measured memory_bytes: 256
use std::collections::BTreeMap;
fn solve() {
    let s = std::io::stdin();
    let mut sc = Scanner { stdin: s.lock() };
    let n: usize = sc.read();
    let k: usize = sc.read();
    let a: Vec<usize> = sc.vec(n);
    let mut aa = vec![];
    for i in 0..n {
        let mut sum = 0;
        for j in i..n {
            sum += a[j];
            aa.push(sum);
        }
    }
    aa.sort();
    let mut candidates = BTreeMap::new();
    for x in aa.iter() {
        *candidates.entry(x).or_insert(0) += 1;
    }
    let mut result = 0;
    for bit in (0..60).rev() {
        let mut cnt_k = 0;
        let mut next_candidates = BTreeMap::new();
        let bit_v: usize = 1usize << bit;
        for (v, cnt) in candidates.iter() {
            if (*v & bit_v) != 0 {
                cnt_k += *cnt;
                next_candidates.insert(*v, *cnt);
            }
        }
        if cnt_k >= k {
            result |= bit_v;
            candidates = next_candidates;
        }
    }

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

fn main() {
    std::thread::Builder::new()
        .stack_size(64 * 1024 * 1024) // 64MB
        .spawn(|| solve())
        .unwrap()
        .join()
        .unwrap();
}

//snippet from kenkoooo
pub struct Scanner<R> {
    stdin: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn read_line(&mut self) -> String {
        use std::io::Read;
        let buf = self
            .stdin
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b'\n' || b == b'\r')
            .take_while(|&b| b != b'\n' && b != b'\r')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }

    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}


Measured memory_bytes: 33144
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        long a = sc.nextInt();
        long b = sc.nextInt();
        long c = sc.nextInt();
        int k = sc.nextInt();
        long f = Math.max(Math.max(a,b),c);
        if(a == f){
            System.out.println((int)(b + c + a * Math.pow(2,k)));
        }
        else if(b == f){
            System.out.println((int)(a + c + b * Math.pow(2,k)));
        }
        else System.out.println((int)(b + a + c * Math.pow(2,k)));

    }


}

Measured memory_bytes: 21844
#include <iostream>
#include <string>

int check_length(std::string S)
{
  int length = 0;
  
  while(S[length] != NULL)
  {
    length++;
  }
  
  return length;
}

bool check_even_string(std::string S, int length)
{
  for(int i = 0; i < length / 2; i++)
  {
    if(S[i] != S[(length / 2) + i])
    {
      return false;
    }
  }

  return true;
}

int main()
{
  std::string S;
  int length;
  std::cin >> S;
  
  length = check_length(S);
  
  for(int i = length - 1; i > 0; i--)
  {
    if(i % 2 != 0)
    {
      continue;
    }

    if(check_even_string(S, i) == true)
    {
      length = i;
      break;
    }
  }
  
  std::cout << length;
  return 0;
}

Measured memory_bytes: 3196
#include <iostream>
#include <cstring>
#include <algorithm>


using namespace std;


using ll = long long;


int main()
{
    ll N, W;
    cin >> N >> W;

    ll dp[N + 1][W + 1];
    ll weight[N + 1], value[N + 1];
    memset(dp, 0, sizeof(dp));

    for (ll i = 1; i <= N; i++) {
        cin >> value[i] >> weight[i];
    }

    dp[1][weight[1]] = value[1];
    for (ll i = 1; i <= N; i++) {
        for (ll w = 1; w <= W; w++) {
            dp[i][w] = max({
                            dp[i - 1][w],
                            w - weight[i] >= 0 ? dp[i - 1][w - weight[i]] + value[i] : 0,
                            w - weight[i] >= 0 ? dp[i][w - weight[i]] + value[i] : 0
                            });
        }
    }

    cout << dp[N][W] << endl;
}



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

using namespace std;

int a[100005], cnt[100005];

int main() {
	int n;
	scanf("%d", &n);
	
	for (int i = 0; i < n; i++) scanf("%d", &a[i]);
	
	long long res = 0;
	
	for (int i = 0; i < n; i++) {
		res += a[i];
		cnt[a[i]]++;
	}
	
	int q;
	scanf("%d", &q);
	
	for (int i = 0; i < q; i++) {
		int x, y;
		scanf("%d%d", &x, &y);
		res -= 1LL * cnt[x] * x;
		res += 1LL * cnt[x] * y;
		cnt[y] += cnt[x];
		cnt[x] = 0;
		
		printf("%lld\n", res);
	}
}


Measured memory_bytes: 4536
import math
n = int(input())
T = [0]*n
A = [0]*n
Tvote = [0]*n
Avote = [0]*n

for i in range(n):
    T[i], A[i] = map(int,input().split())

for k in range(n):
    if k == 0:
        Tvote[k] = T[k]
        Avote[k] = A[k]
    else:
        '''
        j = math.ceil(max(Tvote[k-1]/T[k],Avote[k-1]/A[k]))
        print(Tvote[k-1]/T[k])
        print(Avote[k-1]/A[k])
        print(max(Tvote[k-1]/T[k],Avote[k-1]/A[k]))
        print(j)
        print(Avote[k-1], A[k]*j)
        print(bool(Avote[k-1]<A[k]*j))
        print(bool(j>Avote[k-1]/A[k]))
        '''
        j = int(min(Tvote[k-1]//(-T[k]),Avote[k-1]//(-A[k])))*-1
        #普通に割り算やると誤差で死ぬ（floatがむずい。）
        # // は整数の除算で常に負の無限大方向に丸められる
        #一度も float になることがないはずなので、正しい計算結果が出る
        #https://paiza.hatenablog.com/entry/2017/08/01/Python3%E3%81%A7%E5%B7%A8%E5%A4%A7%E3%81%AA%E6%B5%AE%E5%8B%95%E5%B0%8F%E6%95%B0%E8%A8%88%E7%AE%97%E3%81%AE%E7%B5%90%E6%9E%9C%E3%81%8C%E5%A4%89%E3%81%A0%E3%81%A3%E3%81%9F%E3%81%AE%E3%81%A7%E7%90%86
        #正の無限大方向に丸めたいので-1かけてから計算して（丸めて）あとで-1かける。その際Av/AとTv/Tの比較も逆をとるのに注意
#        j = 1
#        while Tvote[k-1]>j*T[k] or Avote[k-1]>j*A[k]:
#            j += 1
#これは投票数が多すぎるのでTLE
        Tvote[k] = j*T[k]
        Avote[k] = j*A[k]
#    print("---input:{}:{}--vote{}:{}--diff{}".format(T[k],A[k],Tvote[k],Avote[k],bool(T[k]/A[k]==Tvote[k]/Avote[k])))   

#print("---Tvote:{}".format(Tvote))
#print("---Avote:{}".format(Avote))
#print('hoge')
#for i in range(n):
#    print(Tvote[i],Avote[i])
print(Tvote[n-1]+Avote[n-1])



Measured memory_bytes: 3188
#include <iostream>
#include <cstdlib>
using namespace std;

int main(){
    int x; cin >> x;
    string s = to_string(x);
    int sum = 0;
    for(auto&c : s){
        sum += c - '0';
    }
    if(x%sum==0) cout << "Yes\n";
    else cout << "No\n";

    return EXIT_SUCCESS;
}


Measured memory_bytes: 256
n = int(input())
a = list(map(int, input().split()))
mod = 10**9+7
k = 0
b = [0]*len(a)
ans = 0
for i in range(len(a)-1, 0, -1):
    k += a[i]
    k %= mod
    b[i] = k
for i in range(len(a)-1):
    ans += a[i] * b[i+1]
    ans %= mod

print(ans)

Measured memory_bytes: 31492
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std;
const int N = 100010;
int n, a[N], b[N], aq[N], bq[N], fa[N], ans;
map<int, int> pos;
 
int Find(int u)
{
    if (fa[u] == u) return u;
    return fa[u] = Find(fa[u]);
}
 
void Merge(int x, int y)
{
    if (Find(x) == Find(y)) return;
    fa[Find(x)] = Find(y);
    ans--;
}
 
int main()
{
    scanf("%d", &n);
    a[n+1] = 0;
    for (int i = 1; i <= n; i++){
        scanf("%d", &a[i]);
        aq[i] = a[i];
        a[n+1] ^= a[i];
    }
    aq[n+1] = a[n+1];
    sort(aq+1, aq+n+2);
    for (int i = 1; i <= n; i++){
        scanf("%d", &b[i]);
        bq[i] = b[i];
        b[n+1] ^= b[i];
    }
    bq[n+1] = b[n+1];
    sort(bq+1, bq+n+2);
    for (int i = 1; i <= n+1; i++)
        if (aq[i] != bq[i]){
            printf("-1");
            return 0;
        }
    ans = n*2;
    for (int i = 1; i <= n+1; i++) fa[i] = i;
    for (int i = 1; i <= n+1; i++){
        if (a[i] == b[i] && i != n+1)
            ans -= 2;
        else{
            if (pos.find(a[i]) != pos.end())
                Merge(pos[a[i]], i);
            else pos[a[i]] = i;
            if (pos.find(b[i]) != pos.end())
                Merge(pos[b[i]], i);
            else pos[b[i]] = i;
        }
    }
    printf("%d", ans);
    return 0;
}

Measured memory_bytes: 8448
import java.util.*;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;

import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
 
public class Main{
    static long mod=1000000007;
//    static int dx[]={-1,0,1,0};
//    static int dy[]={0,-1,0,1};

    public  static void main(String[] args)   throws Exception, IOException{        
        Reader sc = new Reader(System.in);
        PrintWriter out=new PrintWriter(System.out);

        int r = sc.nextInt();
        int g = sc.nextInt();
        int b = sc.nextInt();
        int n = sc.nextInt();
        int c=0;
        for (int i = 0; i*r <= n ; i++) {
			for (int t = 0; g*t+i*r <= n ; t++) {
				if( (n-i*r-g*t)%b!=0)continue;
				c++;
//				db(i,t,n-i-t);
			}
		}

        out.println(c);
        out.flush();
    }

    static boolean validpos(int x,int y,int r, int c){
        return x<r && 0<=x && y<c && 0<=y;
    }

    static void db(Object... os){
        System.err.println(Arrays.deepToString(os));
    }  
}

//class P {
//    int id, d;
//    P(int  id, int d) {
//        this.id=id;
//        this.d=d;
//    }
//}

//class P implements Comparable<P>{
//    int id,T;
//    P(int id, int T) {
//        this.id=id;
//        this.T=T;
//    }
//    public int compareTo(P p){
//        return id-p.id; //des
//    }
//}

class Reader
{ 
    private BufferedReader x;
    private StringTokenizer st;
    
    public Reader(InputStream in)
    {
        x = new BufferedReader(new InputStreamReader(in));
        st = null;
    }
    public String nextString() throws IOException
    {
        while( st==null || !st.hasMoreTokens() )
            st = new StringTokenizer(x.readLine());
        return st.nextToken();
    }
    public int nextInt() throws IOException
    {
        return Integer.parseInt(nextString());
    }
    public int[] nextIntArray(int size) throws IOException{
    	int r[] = new int[size];
    	for (int i = 0; i < size; i++) {
			r[i] = this.nextInt(); 
		}
    	return r;
    }
    public long[] nextLongArray(int size) throws IOException{
    	long r[] = new long[size];
    	for (int i = 0; i < size; i++) {
			r[i] = this.nextLong(); 
		}
    	return r;
    }
    public long nextLong() throws IOException
    {
        return Long.parseLong(nextString());
    }
    public double nextDouble() throws IOException
    {
        return Double.parseDouble(nextString());
    }
}


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

using namespace std;

#define S scanf
#define P printf
#define G getline
#define SZ size()
#define C clear()
#define B begin()
#define F front()
#define T top()
#define E end()
#define EM empty()
#define V vector
#define Q queue
#define DQ deque
#define PQ priority_queue
#define ST stack
#define FI first
#define SE second
#define PI acos(-1)
#define PS push
#define PP pop()
#define PSF push_front
#define PSB push_back
#define PPF pop_front()
#define PPB pop_back()
#define MP make_pair
#define LL long long int
#define ULL unsigned long long int
#define PII pair<int,int>
#define PSI pair<string,int>
#define PIS pair<int,string>
#define PLI pair<long long int,int>
#define PLL pair<long long int,long long int>
#define MII map<int,int>
#define MSI map<string,int>
#define MIS map<int,string>
#define MLI map<long long int,int>
#define FAST() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)





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






int main()
{
    FAST();

//    freopen("input.txt","r",stdin);
//    freopen("input.txt","w",stdout);

    string s;
    int i,l;
    cin >> s;
    l=s.SZ;
    for(i=0;i<l;i+=2)
    {
        if(s[i] == 'L')
        {
            cout << "No";
            return 0;
        }
    }
    for(i=1;i<l;i+=2)
    {
        if(s[i] == 'R')
        {
            cout << "No";
            return 0;
        }
    }
    cout << "Yes";
    return 0;
}


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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
int N, M;
vector<int> pathes[100500];
vector<int> inv[100050];
int parent[100050];
int Rank[100050];

void bfs(int from) {
    if(inv[from].empty()) {
        Rank[from] = 1;
        parent[from] = 0;
        return;
    }
    if(parent[from] != 0) return;
    int par = 0;
    int now = 0;
    for(int i = 0; i < inv[from].size(); i++) {
        int from_from = inv[from][i];
        bfs(from_from);
        if(now < Rank[from_from]) {
            now = Rank[from_from];
            par = from_from;
        }
    }
    parent[from] = par;
    Rank[from] = now + 1;
}

int main() {
    //cout.precision(10);
    cin >> N >> M;
    for(int i = 1; i <= N - 1 + M; i++) {
        int a, b;
        cin >> a >> b;
        pathes[a].push_back(b);
        inv[b].push_back(a);
    }
    for(int i = 1; i <= N; i++) {
        bfs(i);
    }
    for(int i = 1; i <= N; i++) {
        cout << parent[i] << endl;
    }
    return 0;
}


Measured memory_bytes: 17536
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define all(container) container.begin(),container.end()
#define fast ios::sync_with_stdio(0); cin.tie(0)
#define int long long
const int mod = 1e9+7;  
int power(int a,int b) { if(b==0) return 1; int res = power(a,b/2); if(b&1) return res * res * a; else return res * res; }


int32_t main(){
    
  fast;
  int n; cin >> n;
  vector<pair<string,int>> vp;

  for(int i=0;i<n;i++){
    string s; int d; cin >> s >> d;
    vp.push_back({s,d});
  }

  string x; cin >> x;
  int ans = 0;
  bool flag = false;

  for(int i=0;i<n;i++){
    if(flag) ans+=vp[i].second;
    if(vp[i].first==x){
      flag = true;
    }
  }

  cout << ans;


  return 0;
}

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

bool check(const vector<int> &v){
	int diff = v[1] - v[0];
	for(int i = 2 ; i < v.size() ; i++ ){
		if( v[i] - v[i-1] != diff ) return false;
	}
	return true;
}

int main(){
	int n;
	while( cin >> n , n ){
		vector<int> v(n+1);
		for(int i = 0 ; i < n + 1 ; i++ ){
			cin >> v[i];
		}
		int ans;
		for(int i = 0 ; i < n + 1 ; i++ ){
			vector<int> vv;
			for(int j=0 ; j < n + 1 ; j++ ){
				if( i == j ) continue;
				vv.push_back( v[j] );
			}
			if( check(vv) ){
				ans = v[i];
				break;
			}
		}
		cout << ans << endl;
	}
}

Measured memory_bytes: 1220
#include<iostream>

int main(){
	int *a;
	int *delta;

	a= new int[3]();
	delta= new int[3]();

	for(int i=0; i<3; i++)
		std::cin>> a[i];

	delta[0]= std::abs(a[0]- a[1]);
	delta[1]= std::abs(a[1]- a[2]);
	delta[2]= std::abs(a[2]- a[0]);

	std::cout<< delta[0]+delta[1]+delta[2]- std::max(delta[0], std::max(delta[1], delta[2]));

	return 0;
}

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

int main(){ 
  int n, k;
  cin >> n >> k;
  vector<P> v(3); /* 0 : r  1 : s   2 : p    */
  rep(i,3) cin >> v[i].first, v[i].second = i;
  string s;
  cin >> s;
  sort(v.begin(), v.end(), greater<P>());
  ll res = 0;
  vector<int> ch(n,-1);
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < n; j++) {
      if (ch[j] == -1) {
        if (v[i].second == 0 && s[j] == 's') {
          if (j - k >= 0 && s[j - k] == 's' && ch[j-k] != -1) continue;
          res += v[i].first;
          ch[j] = v[i].second;
        }
        if (v[i].second == 1 && s[j] == 'p') {
          if (j - k >= 0 && s[j - k] == 'p' && ch[j-k] != -1) continue;
          res += v[i].first;
          ch[j] = v[i].second;
        }
        if (v[i].second == 2 && s[j] == 'r') {
          if (j - k >= 0 && s[j - k] == 'r' && ch[j-k] != -1) continue;
          res += v[i].first;
          ch[j] = v[i].second;
        }
      }
    }
  }
  
  cout << res << endl;
  return 0;
} 

Measured memory_bytes: 896
// Shift-JIS（シフトジス）

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

int gcd(long long a, long long b)
{
    if (a < b)
    {
        a ^= b;
        b ^= a;
        a ^= b;
    }

    return b ? gcd(b, a % b) : a;
}

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

int main()
{
    long long A, B;
    cin >> A >> B;
    if (A == B)
    {
        cout << 0 << endl;
    }
    else if ((A * A - B * B) % (2 * A - 2 * B) != 0)
    {
        cout << "IMPOSSIBLE" << endl;
    }
    else
    {
        cout << (A * A - B * B) / (2 * A - 2 * B) << endl;
    }
}

Measured memory_bytes: 256
n,k,*v=map(int,open(0).read().split())
m=0
for i in range(n):
 for j in range(min(k,n)-i+1):
  t=sorted(v[:i]+v[:~j:-1])
  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m=max(m,sum(t))
print(m)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
int main() {
    long long int x , d , k ;
    cin >> x >> k >> d ;
    if(x<0)x=-x;

    long long int ret , x0 , y ;
    y = min(x/d,k);
    k-=y;
    x-=1LL*d*y;
    if(k&1)x=min(abs(x-d),abs(x+d));
    else x=abs(x);
    cout << x << endl ;
}

Measured memory_bytes: 3672
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fr front()
#define tp top()
typedef long long int ll;
typedef long double ld;
typedef pair <int, int> pii;
typedef pair <pii, int> ppi;
typedef pair <int, pii> pip;
#define iofast ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define iofile freopen("input.txt","r",stdin),freopen("output.txt","w",stdout)
const ll LLINF = 1e18;
const int INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos(-1);
const int SIZE = 100005;

int N, W;
pii A[111];
ll dp[SIZE];


int main (){
	
	for(int i=0;i<SIZE;i++){
		dp[i]=LLINF;
	}
	dp[0]=0;
	scanf("%d %d", &N, &W);
	for(int i=0;i<N;i++){
		scanf("%d %d", &A[i].fi, &A[i].se);
		for(int j=SIZE-1;j>=A[i].se;j--){
			dp[j]=min(dp[j],dp[j-A[i].se]+A[i].fi);
		}
	}
	int ans=0;
	for(int i=0;i<SIZE;i++){
		if(dp[i]<=W){
			ans=max(ans,i);
		}
	}
	printf("%d\n", ans);
	
	return 0;
}

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

#define lint long long
#define P pair<int, int>
#define LLP pair<long long, long long>
#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())

const int IINF = (1 << 30) - 1;
const long long LLINF = 1LL << 61;
const long long MOD = 1000000007LL;
const int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
const int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};
const double EPS = 1e-8;

int n, k;
vector< vector<int> > cnt;

template<typename T>
bool chmax(T &_a, T _b){
    if(_a < _b){
        _a = _b;
        return true;
    }else{
        return false;
    }
}

int calc(int i, int j){
    int res = cnt[i][j] - cnt[i][j - k] - cnt[i - k][j] + cnt[i - k][j - k];
    res += cnt[min(i + k, k * 2)][min(j + k, k * 2)] - cnt[min(i + k, k * 2)][j] - cnt[i][min(j + k, k * 2)] + cnt[i][j];
    res += cnt[min(i + k, k * 2)][max(j - k, 0)] - cnt[min(i + k, k * 2)][max(j - k * 2, 0)] - cnt[i][max(j - k, 0)] + cnt[i][max(j - k * 2, 0)];
    res += cnt[max(i - k, 0)][min(j + k, k * 2)] - cnt[max(i - k, 0)][j] - cnt[max(i - k * 2, 0)][min(j + k, k * 2)] + cnt[max(i - k * 2, 0)][j];
    res += cnt[max(i - k, 0)][max(j - k, 0)] - cnt[max(i - k, 0)][max(j - k * 2, 0)] - cnt[max(i - k * 2, 0)][max(j - k, 0)] + cnt[max(i - k * 2, 0)][max(j - k * 2, 0)];
    return max(res, n-res);
}

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

    cin >> n >> k;

    cnt.resize(k * 2 + 1, vector<int>(k * 2 + 1, 0));
    rep(i, n){
        int x, y;
        char c;
        cin >> x >> y >> c;
        if(c == 'B'){
            x += k;
        }
        x %= (k * 2);
        y %= (k * 2);
        ++x;
        ++y;
        ++cnt[y][x];
    }

    reps(i, k * 2){
        reps(j, k * 2){
            cnt[i][j] += cnt[i][j - 1];
        }
    }

    reps(j, k * 2){
        reps(i, k * 2){
            cnt[i][j] += cnt[i - 1][j];
        }
    }

    int ans = 0;
    REP(i, k, k * 2 + 1){
        REP(j, k, k * 2 + 1){
            chmax(ans, calc(i, j));
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 16000
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;

inline int read()
{
	int x=0,w=1; char c=getchar();
	while(c<'0'||c>'9') {if(c=='-') w=-1; c=getchar();}
	while(c<='9'&&c>='0') {x=(x<<1)+(x<<3)+c-'0'; c=getchar();}
	return w==1?x:-x;
}

int n,a[maxn],b[maxn],c[maxn],d[maxn],cnt,n1,n2,nw,fl,f[maxn],t1,t2,ans;

inline int F(int x)
{
	if(f[x]==x) return x;
	return f[x]=F(f[x]);
}

int main()
{
	n=read(); rep(i,1,n) a[i]=read(),n1^=a[i]; rep(i,1,n) b[i]=read(),n2^=b[i];
	a[n+1]=n1; b[n+1]=n2;
	rep(i,1,n) if(a[i]!=b[i]) c[++cnt]=a[i],d[cnt]=b[i],ans++; c[++cnt]=n1; d[cnt]=n2;
	sort(c+1,c+cnt+1); sort(d+1,d+cnt+1);
	rep(i,1,cnt) if(c[i]!=d[i]) {fl=1; break;}
	if(fl) return puts("-1"),0;
	t1=unique(c+1,c+cnt+1)-c-1; rep(i,1,t1) f[i]=i;
	//rep(i,1,t1) cout<<c[i]<<" "; cout<<endl;
	rep(i,1,(n+1))
	{
		if(a[i]!=b[i])
		{
			int x=lower_bound(c+1,c+t1+1,a[i])-c;
			int y=lower_bound(c+1,c+t1+1,b[i])-c;
			int fx=F(x),fy=F(y); if(fx!=fy) f[fx]=fy;
		}
	}
	rep(i,1,t1) if(f[i]==i) ans++; cout<<ans-1<<endl;
	return 0;
}

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

int main(){
  int p[14][14]={0};
  int x,y,s;
  while(scanf("%d,%d,%d",&x,&y,&s)>0){
    if(s==1){
      p[x+2][y+1]++;p[x+1][y+2]++;p[x+2][y+2]++;p[x+3][y+2]++;p[x+2][y+3]++;
    }
    else if(s==2){
      for(int i=1;i<4;i++){
	for(int j=1;j<4;j++) p[x+i][y+j]++;
      }
    }
    else{
      for(int i=1;i<4;i++){
	for(int j=1;j<4;j++) p[x+i][y+j]++;
      }
      p[x][y+2]++;p[x+2][y]++;p[x+4][y+2]++;p[x+2][y+4]++;
    }
  }
  int w=0;
  int t=0;
  for(int i=2;i<12;i++){
    for(int j=2;j<12;j++){
      if(p[i][j]==0) w++;
      if(p[i][j]>t) t=p[i][j];
    }
  }
  printf("%d\n%d\n",w,t);
  return 0;
}

Measured memory_bytes: 1028
n = int(input())
a, b = map(int, input().split())
p = [int(i) for i in input().split()]

q = [0, 0, 0]
for pi in p:
    if pi <= a:
        q[0] += 1
    elif a+1 <= pi <= b:
        q[1] += 1
    else:
        q[2] += 1

if q[0] != 0 and q[1] != 0 and q[2] != 0:
    print(min(q))
else:
    print(0)


Measured memory_bytes: 3060
#include<iostream>
#include<string>
#include<vector>
#include<string.h>
using namespace std;

int h,w;

bool is_next[2][50][50];
vector<string> M;

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

void dfs(int x,int y,int is_black){
	if(x<0 || y<0 || x>=w || y>=h) return;
	if(M[y][x]!='.') return;
	if(is_next[is_black][y][x]) return;
	is_next[is_black][y][x]=true;

	for(int r=0;r<4;r++)
		dfs(x+dx[r],y+dy[r],is_black);
}

int main()
{
	while(cin>>w>>h && w!=0){
		M.clear();
		memset(is_next,false,sizeof(is_next));
		for(int i=0;i<h;i++){
			string s; cin>>s; M.push_back(s);
		}

		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				if(M[y][x]=='W'){
					for(int r=0;r<4;r++){
						dfs(x+dx[r],y+dy[r],1);
					}
				}
				else if(M[y][x]=='B'){
					for(int r=0;r<4;r++){
						dfs(x+dx[r],y+dy[r],0);
					}
				}
			}
		}

		int ans_b=0,ans_w=0;
		for(int y=0;y<h;y++){
			for(int x=0;x<w;x++){
				if(is_next[0][y][x] && !is_next[1][y][x]) ans_b++;
				if(is_next[1][y][x] && !is_next[0][y][x]) ans_w++;
			}
		}
		cout<<ans_b<<" "<<ans_w<<endl;
	}
	return 0;
}

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


vector<long long> divisor(long long n){
    vector<long long> res;
    for(long long i = 1; i*i <= n; i++){
        if(n%i==0){
            res.push_back(i);
            if(n/i != i)res.push_back(n/i);
        }
    }
    return res;
}

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

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

    ll GCD = gcd(a,b);
    vector<ll> res;

    res = divisor(GCD);

    sort(res.rbegin(),res.rend());

    cout << res[k-1]<< endl;


    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, from, to) for (int i = from; i < (to); ++i)
#define mp(x,y) make_pair(x,y)
#define all(x) (x).begin(),(x).end()
#define pb push_back
using ll = long long;
using vin=vector<int>;
using vll=vector<ll>;
using vst=vector<string>;
using P = pair<int, int>;
const int inf=1e9+7;
const ll INF=1e18;
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };


int main(){cout<<fixed<<setprecision(10);
		   char a,b;
           cin>>a>>b;
           if(a=='H'&&b=='H'||a=='D'&&b=='D')cout<<'H'<<endl;
           else cout<<'D'<<endl;
}

Measured memory_bytes: 3620
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define MAX_V 100001
const int INF = 1001001001;
int dp[101][MAX_V];

int main(){
  int N,W,w[101],v[101],ans=0;cin>>N>>W;
  REP(i,N) cin>>w[i]>>v[i];
  REP(i,MAX_V) dp[0][i]=(i?INF:0);

  REP(i,N) REP(j,MAX_V){
    if((j-v[i]>=0)&&(dp[i][j-v[i]]!=INF)) dp[i+1][j]=min(dp[i][j-v[i]]+w[i],dp[i][j]);
    else dp[i+1][j]=dp[i][j];
  }

  REP(i,N+1) REP(j,MAX_V) if(dp[i][j]<=W) ans=max(ans,j);  
  cout<<ans<<endl;
}

Measured memory_bytes: 43080
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
typedef unsigned long long ull;
#define MP make_pair
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define VI vector<int>
#define PII pair<int, int>
#define FI first
#define SE second
#define ALL(x) (x).begin(), (x).end()
const int INF=1<<30;
const ll LINF=1LL<<60 ;
const ll MOD=1e9+7 ;
///////////////////
const int MX = 100000 + 7;
vector<pair<ll, int>> a;
int b[MX];

int main(){
	int n,m; cin >> n  >> m;
	FOR(i,1,n){
		ll a_i;
		cin >> a_i >> b[i];
		a.push_back(MP(a_i, i));
	}
	sort(ALL(a));
	ll ans=0;
	int cur_m=0;
	int k=0;
	FOR(i,0,n-1){
		int now = b[a[i].SE];
		cur_m += now;
		if(cur_m < m){
			ans += now*a[i].FI;
		}
		else{
			k = m - cur_m + now;
			ans += k * a[i].FI;
			break;
		}

	}
	cout << ans;
    return 0;
}

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

int main() {
    int n, h_, f = 0;
    cin >> n;
    int h[n];
    for(int i = 0; i < n; ++i) {
        cin >> h[i];
    }
    for(int i = 1; i < n; ++i) {
        h_ = h[i-1] - h[i];
        if(h_ > 1) {
            cout << "No" << '\n';
            return 0;
        } else if(h_ == 1) {
            if(f == 0) {
                f = 1;
            } else {
                cout << "No" << '\n';
                return 0;     
            }
        } else if(h_ <= -1) {
            f = 0;
        }
    }
    cout << "Yes" << '\n';
}


Measured memory_bytes: 640
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long ll;

int main()
{
    ll i,j,k,l,m,n,a,b,c,ans=0;

    cin >> n;

    for(a=1;a<=n;a++)
    {
        for(b=1;b<=n;b++)
        {
            for(c=1;c<=n;c++)
            {
                m = __gcd(a,b);
                m = __gcd(m,c);
                ans += m;
            }
        }
    }

    cout << ans << endl;
}




Measured memory_bytes: 3632
let [|n;k|] = stdin.ReadLine().Split() |> Array.map int
let h = stdin.ReadLine().Split() |> Array.map int64

Array.scan (fun x t -> 
    let cost = x |> Array.map (fun (h,c) -> c+(abs (t-h))) |> Array.min
    let l = Array.length x
    if l = k then Array.append x.[1..k-1] [|(t,cost)|]
    else Array.append x [|(t,cost)|]) [|(h.[0],0L)|] h.[1..n-1]
|> ((fun x -> 
    let arr = x.[n-1]
    arr.[(Array.length arr) - 1]) >> (fun (_,c) -> c))
|> stdout.WriteLine

Measured memory_bytes: 141952
// Submitted By Subhash Yadav
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.IntStream;

public class Main {

	public static void main(String[] args) throws IOException {

		FastScanner sc = new FastScanner();
		PrintWriter out=new PrintWriter(System.out);
		String s=sc.next();
		int n=s.length();
		out.println(s+(s.charAt(n-1)=='s'?"es":"s"));
		out.close();

	}

	static class FastScanner {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer("");

		String next() {
			while (!st.hasMoreTokens())
				try {
					st = new StringTokenizer(br.readLine());
				} catch (IOException e) {
					e.printStackTrace();
				}
			return st.nextToken();
		}

		int nextInt() {
			return Integer.parseInt(next());
		}

		int[] readArray(int n) {
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
				a[i] = nextInt();
			return a;
		}

		long nextLong() {
			return Long.parseLong(next());
		}

		double nextDouble() {
			return Double.parseDouble(next());
		}
	}

}

Measured memory_bytes: 37436
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
#include <numeric>

using ll = long long;

using namespace std;

long sig[8][3] = {{ 1,  1,  1},
                  { 1,  1, -1},
                  { 1, -1,  1},
                  { 1, -1, -1},
                  {-1,  1,  1},
                  {-1,  1, -1},
                  {-1, -1,  1},
                  {-1, -1, -1}};

int main() {
    int n, m;
    cin >> n >> m;
    long val[8][1010] = {{0}};
    for (int i=0; i<n; i++) {
        for (int j=0; j<3; j++) {
            long xyz; cin >> xyz;
            for (int k=0; k<8; k++) {
                val[k][i] += sig[k][j] * xyz;
            }
        }
    }
    long ans=0;
    for (int i=0; i<8; i++) {
        sort(val[i], val[i]+n, greater<long>());
        ans = max(ans, accumulate(val[i], val[i]+m, 0l));
    }
    cout << ans << '\n';
    return 0;
}

Measured memory_bytes: 256
// AOJ 2170: Marked Ancestor
// 2018.1.2 bal4u@uu

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

#define INF 0x01010101
typedef struct { int i, v; } T;
T tbl[100003]; int sz;
int p[100003];
int mk[100003];

//#define getchar_unlocked()  getchar()
int in()
{
	int n, c;

//	while ((c = getchar_unlocked()) < '0');
	c = getchar_unlocked();
	n = 0;
	do n = (n<<3)+(n<<1) + (c & 0xf), c = getchar_unlocked();
	while (c >= '0');
	return n;
}

int find(int v, int i)
{
	if (mk[v] < i) return v;
	return p[v] = find(p[v], i);
}

int main()
{
	int N, Q, i, cmd, v;
	long long ans;

	while (N = in()) {
		Q = in();
		p[1] = 1; for (i = 2; i <= N; i++) p[i] = in();

		memset(mk, INF, sizeof(mk));
		mk[1] = 0;

		sz = 0;
		for (i = 1; i <= Q; i++) {
			cmd = getchar_unlocked(); getchar_unlocked();
			v = in();
			if (cmd == 'M') { if (mk[v] == INF) mk[v] = i; }
			else tbl[sz].i = i, tbl[sz++].v = v;
		}

		ans = 0;
		while (sz--) ans += find(tbl[sz].v, tbl[sz].i);
		printf("%lld\n", ans);
	}
	return 0;
}


Measured memory_bytes: 3084
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>
#include<tuple>
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef unsigned long long int ull;
const int INF=1e9;
const ll MOD=1e9+7;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
const int MAXN=2e5;

int n,q;
vector<vector<int> > G(MAXN);
vector<int> par(MAXN,-1),cnt(MAXN,0);

void dfs(int u){
    for(auto v:G[u]){
        if(par[u]!=v){
            par[v]=u;
            cnt[v]+=cnt[u];
            dfs(v);
        }
    }
    return ;
}

int main(){
    cin>>n>>q;
    for(int i=0;i<n-1;i++){
        int a,b;cin>>a>>b;
        a--;b--;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }
    for(int i=0;i<q;i++){
        int p,x;cin>>p>>x;
        p--;
        cnt[p]+=x;
    }
    par[0]=-1;
    dfs(0);
    for(int i=0;i<n;i++){
        cout<<cnt[i];
        if(i!=n-1) cout<<' ';
    }
    cout<<endl;
    return 0;
}

Measured memory_bytes: 25072