akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;

using ll = int64_t;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) x.begin(), x.end()

using pi = pair<int, int>;
using vi = vector<int>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  REP(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

ll read() {
  ll i;
  scanf("%" SCNd64, &i);
  return i;
}

void printSpace() { printf(" "); }

void printEoln() { printf("\n"); }

void print(ll x, int suc = 1) {
  printf("%" PRId64, x);
  if (suc == 1)
    printEoln();
  if (suc == 2)
    printSpace();
}

string readString() {
  static char buf[3341000];
  scanf("%s", buf);
  return string(buf);
}

char *readCharArray() {
  static char buf[3341000];
  static int bufUsed = 0;
  char *ret = buf + bufUsed;
  scanf("%s", ret);
  bufUsed += strlen(ret) + 1;
  return ret;
}

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}

template <class T, class U> void chmin(T &a, U b) {
  if (b < a)
    a = b;
}

const int mod = 1e9 + 7;
const int inf = 1e9 + 5;

const int maxN = 1e5 + 5;
int cnt[maxN];

signed main() {
  int n = read();
  for (int i = 2; i <= n; i++) {
    int tmp = i, div = 2;
    while (div <= i) {
      if (tmp % div == 0)
        cnt[div]++, tmp /= div;
      else
        div++;
    }
  }
  int ans = 1;
  for (int i = 1; i <= n; ++i) {
    if (cnt[i] <= 0)
      continue;
    ans *= cnt[i] + 1;
    ans %= mod;
  }
  print(ans);
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(x, y) for(int x = 0; x < (y); x++)
#define REP(x, z, y) for(int x = (z); x < (y); x++)
#define all(x) x.begin(), x.end()
#define len(x) (sizeof(x) / sizeof(x[0]))
#define split_str(str, sp_word) istringstream stream(str); string res; for(int cnt = 0; getline(stream,res,sp_word); cnt++)
#define scanln(x) fgets(x, sizeof x, stdin)
#define down_queue(x) priority_queue<x>
#define up_queue(x) priority_queue<x, vector<x>, greater<x>>
#define i_str(x) atoi(x.c_str())
#define digit(x) ((int)log10((double)(x)) + 1)
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define pf(x) push_front(x)
using namespace std;
const int MOD = 1000000007; 
typedef long long ll;
typedef string str;
 
int main(){
  str line,ans = "";
  getline(cin,line);
  split_str(line,' '){
    ans += res[0];
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define endl '\n'
#define mod 1000000007
typedef long long int ll;
using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t=1; 
    while(t--){
	    char ch;
	    cin>>ch;
	    if(ch==toupper(ch))
		    cout<<"A"<<endl;
	    else
		    cout<<"a"<<endl;
    
    }
    return 0;
} 


Measured memory_bytes: 3648
# used DP

n = int(input())
A = list(map(int, input().split() ))
q = int(input())
M = list(map(int, input().split()))


dp = [[None]*(max(M)+1) for i in range(n)]

# we refer to the list dp only when i = 0, 1, ... , n-1 and m >= 0.
def solve(i, m):    
    if i >= n:
        return m == 0
    
    elif m < 0:
        return False
    
    elif dp[i][m] is not None:
        return dp[i][m]
    
    else:
        if m == 0:
            dp[i][m] = True
        elif solve(i+1, m):
            dp[i][m] = True
        elif solve(i+1, m - A[i]):
            dp[i][m] = True
        else:
            dp[i][m] =  False        
        return dp[i][m]

    
for num in M:
    print('yes' if solve(0, num) else 'no')



Measured memory_bytes: 5944
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import Counter
def main():
    n=int(input())
    A=list(map(int,input().split()))
    X,Y=[0]*n,[0]*n
    for i in range(n):
        X[i]=i+A[i]
        Y[i]=i-A[i]
    set_x=set(X)
    yc=Counter(Y)
    xc=Counter(X)
    ans=0
    for x in set_x:
        ans+=xc[x]*yc[x]
    print(ans)

if __name__=='__main__':
    main()

Measured memory_bytes: 72024
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = 0, k = 0 ,ans = 0, a = 0;
		n = sc.nextLong();
		k = sc.nextLong();
		a = n % k;
		System.out.print(ans = Math.min(a, k - a));
	}
}


Measured memory_bytes: 23508
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<random>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rrep(i, st, n) for (int i = st; i < n; ++i)
using pii = pair<int, int>;
//const int inf = 1e9 + 7;
//int dy[] = {0, 0, -1, 1};
//int dx[] = {1, -1, 0, 0};

int main() {
    int n, k; cin >> n >> k;
    int w[n]; rep(i, n) cin >> w[i];
    int l = 1, r = 100000 * 10000 + 1;
    while (l < r) { //break when l + 1 == r
        int flg = 0;
        int m = (l + r) / 2;
        if (m == l || m == r) {
            flg = 1;
        }
        int c = 1;
        int sum = 0;
        rep(i, n) {
            if (sum + w[i] <= m) {
                sum += w[i];
            } else if (w[i] <= m) {
                sum = w[i];
                c++;
            } else { //そもそも積めない
                c = k + 1;
                break;
            }
        }

        if (c <= k) { //余裕あり
            r = m; //1台あたりの限界重量小さく
        } else {
            l = m;
        }
        if (flg == 1) break;
        
        
    }
    cout << r << endl;
}



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

int main()
{
  vector<int> nums(3);
  for (int i = 0; i < nums.size(); i++)
  {
    cin >> nums.at(i);
  }
  sort(nums.begin(), nums.end());
  cout << nums.at(0) << " " << nums.at(1) << " " << nums.at(2) << endl;
}


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

using int32 = int_fast32_t;
using int64 = int_fast64_t;

const int32 INF = 1e9;
const int32 MOD = 1e9+7;
const int64 LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES\n" : "NO\n"  )
#define Yes(n) cout << ((n) ? "Yes\n" : "No\n"  )
#define ANS(n) cout << (n) << "\n"
#define REP(i,n) for(int64 i=0;i<(n);++i)
#define FOR(i,a,b) for(int64 i=(a);i<(b);i++)
#define FORR(i,a,b) for(int64 i=(a);i>=(b);i--)
#define ALL(obj) (obj).begin(),(obj).end()
#define pii pair<int32,int32>
#define pll pair<int64,int64>
#define pb(a) push_back(a)
#define mp make_pair

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int32 n;
    cin >> n;
    int32 a[n+1];
    REP(i,n)cin >> a[i+1];
    bool ans[n+1];
    int32 m = 0;
    FORR(i,n,1){
        int32 sum = 0;
        FOR(j,2,n+1){
            if(i * j > n)break;
            sum += ((ans[i * j])?1:0);
        }
        sum %= 2;
        if(sum == a[i]){
            ans[i] = false;
        }else{
            ans[i] = true;
            m++;
        }
    }
    cout << m << endl;
    FOR(i,1,n+1){
        if(ans[i])cout << i << " ";
    }
    return 0;
}

Measured memory_bytes: 2688
import java.util.*;
 
public class Main{
  public static void main(String[] args){
    
    Scanner sc = new Scanner(System.in);
    
    int N = sc.nextInt();
    int T = sc.nextInt();
    int A = sc.nextInt();
    
    double x = (T - A) *1000 / 6;
    double diff = -1;
    int no = -1;
    
    for(int i=0;i<N;i++)
    {
      int H = sc.nextInt();
      
      if (diff == -1 || diff > Math.abs(H-x) ){
        diff = Math.abs(H-x);
        no = i + 1;
      }
    }
    System.out.println(no);
  }
}

Measured memory_bytes: 24532
object Main extends App {
  val Array(sx, sy, tx, ty) = scala.io.StdIn.readLine.split(' ').map(_.toInt)
  val tate = ty - sy
  val yoko = tx - sx
  val path1 = "U" * tate + "R" * yoko
  val path2 = "D" * tate + "L" * yoko
  val path3 = "D" + "R" * (yoko + 1) + "U" * (tate + 1) + "L"
  val path4 = "U" + "L" * (yoko + 1) + "D" * (tate + 1) + "R"

  println(Array(path1, path2, path3, path4).mkString)
}


Measured memory_bytes: 25544
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
#define all(x) x.begin(),x.end()
#define rep(i,j,n) for (long long i = j; i < (long long)(n); i++)
#define _GLIBCXX_DEBUG
const ll MOD = 1000000007;
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; }
//(a+b-1)/b
//priority_queue<ll, vector<ll>, greater<ll>> q;

signed main(){
	string s; cin>>s;
	cout<<s[0]<<s[1]<<s[2]<<endl;

	return 0;
}

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

int main() {
  int a,b,c,d,e=0;
  cin>>a>>b>>c>>d;
  while(e<d){
    if(a>=b)b=b*2;
    else if(b>=c)c=c*2;
    e++;
  }
  if(a<b&&b<c)cout<<"Yes"<<endl;
  else cout<<"No"<<endl;
}

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

int main(void) {
	int n;
	cin >> n;
	int ans = 0;
	for (int i = 1; i <= n; i+=2) {
		int cnt = 0;
		for (int j = 1; j <= i; j++) {
			if (i % j == 0) cnt++;
		}
		if (cnt == 8) ans++;
	}
	cout << ans << endl;

	return 0;
}


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

int main() {
	int m,n;
	int taro[100],hanako[100],taro_sum,hanako_sum;

	while(cin >> n >> m && !(n == 0 && m == 0)) {
		taro_sum = hanako_sum = 0;
		for(int i = 0;i < n;i++) {
			cin >> taro[i];
			taro_sum += taro[i];
		}
		for(int i = 0;i < m;i++) {
			cin >> hanako[i];
			hanako_sum += hanako[i];
		}

		//????????????????????????????????§??????
		int min = 201,res_x,res_y;
		for(int i = 0;i < n;i++) {
			for(int j = 0;j < m;j++) {
				int taro_after = taro_sum - taro[i] + hanako[j],hanako_after = hanako_sum - hanako[j] + taro[i];
				if(taro_after == hanako_after && (taro[i] + hanako[j]) < min) {
					min = taro[i] + hanako[j];
					res_x = taro[i];
					res_y = hanako[j];
				}
			}
		}
		if(min == 201) cout << -1 << endl;
		else cout << res_x << " " << res_y << endl;
	}
}

Measured memory_bytes: 1156
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef pair<int, int> P;
typedef long long ll;
int main(){
    int n,m;
    cin >> n >> m;
    ll fact = 1;
    rep(i,m){
        fact*=2;
    }
    ll ans = (n-m)*100+m*1900;
    cout << ans*fact << endl;
    return 0;
}

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

using namespace std;
//typedef long long ll;
typedef unsigned long long ll;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
//#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) ll(x.size())
//typedef pair<int, int> P;
typedef pair<ll, ll> P;
//const double INF = 1e10;
//const ll INF = LONG_LONG_MAX;
const ll MINF = LONG_LONG_MIN;
const int INF = INT_MAX;
#define cmin(x, y) x = min(x, y)
#define cmax(x, y) x = max(x, y)

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


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

typedef priority_queue<ll, vector<ll>, greater<ll>> PQ_ASK;
const int mod = 1000000007;


int main() {
    string s;
    cin >> s;
    int ans = 0;
    for (int i = 0; i < s.size() - 1; i++) {
        if (s[i] != s[i + 1]) ans++;
    }
    cout << ans  << endl;

}


Measured memory_bytes: 512
package main

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

func main() {
	s := IOInitialize()
	N := Int(s)
	cnt := 0
	for i := 1; i <= N; i++ {
		_cnt := 0
		if i%2 == 0 {
			continue
		}
		for j := 1; j <= N; j++ {
			if i%j == 0 {
				_cnt++
			}
		}
		if _cnt == 8 {
			cnt++
		}
	}
	fmt.Println(cnt)
}

func IOInitialize() *bufio.Scanner {
	s := bufio.NewScanner(os.Stdin)
	s.Split(bufio.ScanWords)
	return s
}

func Int(s *bufio.Scanner) int {
	if !s.Scan() {
		panic("scan error")
	}

	value, err := strconv.Atoi(s.Text())
	if err != nil {
		panic("text to int error")
	}
	return value
}

func String(s *bufio.Scanner) string {
	if !s.Scan() {
		panic("scan error")
	}

	return s.Text()
}

func scanNums(s *bufio.Scanner, len int) (nums []int) {
	for i := 0; i < len; i++ {
		num := Int(s)
		nums = append(nums, num)
	}
	return
}

func scanstrings(s *bufio.Scanner, len int) (strs []string) {
	for i := 0; i < len; i++ {
		str := String(s)
		strs = append(strs, str)
	}
	return
}


Measured memory_bytes: 640
//=====================COMPILER OPTIMIZATION STARTS=======================
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
//======================COMPILER OPTIMIZATION ENDS=======================

#include <bits/stdc++.h>

using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long int
#define mod 1000000007
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define prec(n) fixed << setprecision(n)
#define maxpq priority_queue<int>
#define minpq priority_queue<int, vector<int>, greater<int>>
#define PI 3.1415926535897932384626
#define INF 1000000000
#define bits(n) __builtin_popcount(n)

int a[100001];
int dp[100001];
int n;

int fun(int ind)
{
    if(ind==(n-1))
    return 0;
    else if(dp[ind]!=-1)
    return dp[ind];
    int left=abs(a[ind]-a[ind+1])+fun(ind+1);
    int right=INT_MAX;
    if(ind+2<n)
    {
        right=abs(a[ind]-a[ind+2])+fun(ind+2);
    }
    return dp[ind]=min(left,right);
}
void solve()
{
    //int n;
    cin>>n;
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    int res=fun(0);
    cout<<res;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    // for getting input from input.txt
    freopen("input1.txt", "r", stdin);
    // for writing output to output.txt
    freopen("output1.txt", "w", stdout);
#endif

    int t = 1;
    //cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Measured memory_bytes: 9092
N=int(input())
S=list(input().rstrip())
for i in range(N):
  if S[i]=='o': S[i]=True
  else: S[i]=False
S.append(S[0])
A=[True]*(N+2)
for a in [[True,True],[True,False],[False,True],[False,False]]:
  A[0],A[1]=a[0],a[1]
  for j in range(1,N+1):
    if A[j] ^ S[j]:
      A[j+1]=not A[j-1]
    else:
      A[j+1]=A[j-1]
  if A[0]==A[-2] and A[1]==A[-1]:
    for k in range(N):
      if A[k]: A[k]='S'
      else: A[k]='W'
    print("".join(A[:N]))
    exit()
print(-1)

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

double dist(double xi, double yi, double xj, double yj){
    return sqrt((xi-xj)*(xi-xj) + (yi-yj)*(yi-yj));
}

int main(){
    int n; cin >> n;
    int fact = 1;
    for(int i=1; i<=n; i++) fact *= i;
    vector<double> x(n), y(n);
    for(int i=0;i<n;i++) cin >> x[i] >> y[i];
    vector<int> v(n);
    for(int i=0;i<n;i++) v[i] = i;
    double sum = 0;
    do{
        for(int i=0;i<n-1;i++) sum += dist(x[v[i]], y[v[i]], x[v[i+1]], y[v[i+1]]);
    }while(next_permutation(v.begin(), v.end()));
    cout << fixed << setprecision(10) << sum/fact << endl;
}


Measured memory_bytes: 256
#include<iostream>
using namespace std;
int main(){
int a,b,c,k,x[1000][4],p[500];
while(true){
for(int i=0;i<500;i++){p[i]=2;}
cin>>a>>b>>c;
if(a==0){break;}
cin>>k;
for(int i=0;i<k;i++){
cin>>x[i][0]>>x[i][1]>>x[i][2]>>x[i][3];
if(x[i][3]==1){p[x[i][0]]=1;p[x[i][1]]=1;p[x[i][2]]=1;}
}
for(int i=0;i<k;i++){
if(p[x[i][0]]==1&&p[x[i][1]]==1&&x[i][3]==0){p[x[i][2]]=0;}
if(p[x[i][0]]==1&&p[x[i][2]]==1&&x[i][3]==0){p[x[i][1]]=0;}
if(p[x[i][1]]==1&&p[x[i][2]]==1&&x[i][3]==0){p[x[i][0]]=0;}
}
for(int i=1;i<=a+b+c;i++){cout<<p[i]<<endl;}
}
return 0;}

Measured memory_bytes: 1176
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int N, M, C; cin >> N >> M >> C;
    vector<int> B(M); for (auto& b : B) cin >> b;
    int ans = 0;
    for (int i = 0; i < N; i++) {
        int sum = C;
        for (int i = 0; i < M; i++) {
            int a; cin >> a;
            sum += a * B[i];
        }
        ans += sum > 0;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
n = int(input())
c = list(map(int, input().split()))
c.sort()

MOD = 10 ** 9 + 7
ans = 0

for i in range(n):
    ans += c[i] *(2 + (n - (i+1))) *  pow(4, n-1, MOD)
    ans %= MOD

print(ans)

Measured memory_bytes: 87500
#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long x,a,b5,b;
	cin>>x;
    for(a=-120;a<=120;a++)
    {
    	b5=a*a*a*a*a-x;
    	for(b=-120;b<=120;b++)
    	{
    		if(b5==b*b*b*b*b)
    		{
    			cout<<a<<" "<<b;
    			return 0;
			}
		}
	}
}

Measured memory_bytes: 3592
function Main(input){
	inputs = input.split(' ');
    X = inputs.map(inputs=>parseInt(inputs,10));
    A = X[0];
    B = X[1];
    T = X[2];
    multiple = Math.floor(T / A);
    ans = multiple * B;
    console.log(ans);
}
Main(require("fs").readFileSync("/dev/stdin","utf8"));

Measured memory_bytes: 7496
#define _GLIBCXX_DEBUG//TLEの原因になるので注意！！！！！！！！！！！
#include<cmath>
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<int> arr;
stack<int> st;
queue<int> qu;
queue<pair<int,int> > qu2;
priority_queue<int> pq;
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define rep2(i,n) for(int i=1; i<=(int)(n); i++)
#define mins(x,y) x=min(x,y)
#define maxs(x,y) x=max(x,y)
typedef set<int> set_t;
typedef set<string> set_g;
typedef complex<double> xy_t;
static const int NIL = -1;
static const int INF = 1000000007;
#define mp make_pair
#define sz(x) int(x.xise())
#define mod 1000000007
#define reps(i,s,n) for(int i = s; i < n; i++)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
deque<int> deq;
#define fi first
#define se second
//#define int long long

//cerrはコメントアウトしなくても通る//ただしTLEを引き起こすことがある

ll N,M,H,W,K,Q,A,B;
string S;
//const ll MOD = 998244353;
const ll MOD = (1e+9) + 7;
//const ll INF = 1LL << 60;
//typedef pair<ll,ll> P;
typedef pair<int, int> P;
bool chmx(int &a, int b) {
	if (b > a) {
		a = b;
		return true;
	}
	return false;
}

bool chmn(int &a, int b) {
	if (b < a) {
		a = b;
		return true;
	}
	return false;
}

int main(){
	ll n;
	cin >> n;
	multiset<ll> S;
	ll b[n];
	rep(i,n){
		ll a;
		cin >> a;
		b[i]=a;
		S.insert(a);
	}
	ll tot=0;
	ll d[200005];
	rep(i,200001){
		ll m=S.count(i);
		d[i]=m;
		tot+=m*(m-1)/2;
	}
	rep(i,n){
		ll ans=tot;
		ll e=d[b[i]];
		ans-=(e-1)*e/2;
		ans+=(e-1)*(e-2)/2;
		cout<<ans<<endl;
	}
    return 0;
}



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

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vint = vector<int>;
using vvint = vector<vint>;
using vvvint = vector<vvint>;
using vll = vector<ll>;
using uint = unsigned int;
using ull = unsigned long long;

template<typename T> using uset = unordered_set<T>;
template<typename T1, typename T2> using umap = unordered_map<T1, T2>;

constexpr int INF = (1 << 30) - 1;
constexpr ll LLINF = 1LL << 60;
constexpr int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
constexpr int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
constexpr char el = '\n';
constexpr int mod = 1000000007;

template<typename T> T gcd(T a, T b) { return (b ? gcd(b, a % b) : a); }
template<typename T> T lcm(T a, T b) { return (a / gcd(a, b) * b); }
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

template<typename T>
ostream& operator <<(ostream &os, vector<T> &v) {
	for (auto &u : v) os << u << el;
	return (os);
}

template<typename T>
istream& operator >>(istream &is, vector<T> &v) {
	for (auto &u : v) is >> u;
	return (is);
}

template<typename T1, typename T2>
istream& operator >>(istream &is, pair<T1, T2> &p) {
	return (is >> p.first >> p.second);
}

int main() {
	int N; cin >> N;
	if (N == 1) { cout << 0 << endl; return (0); }
	set<Pi> ans;
	for (int i = 0; i < N; i += 2) {
		for (int j = 1; j <= N; j += 3) {
			if (j == N) j -= 2;
			int p = i+1-(j&1);
			if (p < N) ans.emplace(p, j);
		}
	}
	for (int j = 2; j < N; j += 2) {
		ans.emplace(1, j);
	}
	
	set<Pi> validator;
	cout << ans.size() << endl;
	for (auto &v : ans) {
		cout << v.first << " " << v.second << el;
	}
	/*DEBUG
	bool flag[1005][1005] = {};
	function<int(int, int)> dfs = [&](int y, int x) -> int {
		int ret = 1 - ((y+x) & 1);
		if (flag[y][x]) return (0);
		cout << " " << y << " " << x << " " << ret << endl;
		flag[y][x] = true;
		for (int i = 0; i < 4; i++) {
			int ny = y + dy[i], nx = x + dx[i];
			if (nx < 0 || ny < 0 || ny >= N || nx >= N) continue;
			if ((ny + nx) % 2 == 0 || ans.count(Pi(ny, nx))) {
				ret += dfs(ny, nx);
			}
		}
		return (ret);
	};

	int dat = dfs(0, 0);
	if (ans.size() <= 170000 && dat == N*N/2 + (N&1)) {
		cout << "OK" << endl;
	} else {
		cout << "NG" << endl;
	}
	cout << "validator.size() = " << dat << endl;
	*/
	return (0);
}

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

using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using VB = vector<bool>;
using VVB = vector<VB>;
using VD = vector<double>;
using VVD = vector<VD>;
using VS = vector<string>;
using PII = pair<int, int>;
using VPII = vector<PII>;
using VL = vector<long long>;
using VVL = vector<VL>;

#define sz(var) (int)var.size()
#define REP(var, start, end) for(int var = start; var < (int)end; ++var)
#define CLEAR(var, with) memset(var, with, sizeof(var))
#define ALL(x) x.begin(), x.end()
#define LL long long
#define ULL unsigned long long


int g(int n)
{
    for(auto i : {1, 3, 5, 7, 8, 10, 12})
        if(i == n)
            return 1;
    for(auto i : {4, 6, 9, 11})
        if(i == n)
            return 2;
    return 3;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
#ifndef ONLINE_JUDGE
	//freopen("tmp.in", "r", stdin);
//	freopen("tmp.out", "w", stderr);
#endif
    int x, y;
    cin >> x >> y;

    if(g(x) == g(y))
        cout << "Yes" << endl;
    else
        cout << "No" << endl;

	return 0;
}

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

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

using namespace std;

//v.push_back(123);
//sort(v.begin(), v.end(),greater<int>());
//for(int x : v) {cout << x << endl;}

//for(int x: v) cout << x << endl;
//vector<vector<char>> v(H, vector<char>(W));
//vector<int> v(1000);

signed main(){
  int H, W;
  cin>>H>>W;
  string S[H];
  rep(i, H) cin >> S[i];
  int tate[H][W];
  int yoko[H][W];
  rep(i, H){
    rep(j, W){
      tate[i][j]=0;
      yoko[i][j]=0;
    }
  }
  int result = 0;
  int kosu = 1;
  int t=0;
  int y=0;
  rep(i, H){
    rep(j, W){
      t = 0;
      y = 0;
      if(S[i][j]=='.'){
        kosu = -1;

        // tate
        if(i-1>=0){
          if(S[i-1][j]=='.'){
            t = tate[i-1][j];
          }else{
            for(int k=i;k<H;k++){
              if(S[k][j]=='.'){
                t++;
              }else{
                break;
              }
            }
          }
        }else{
          for(int k=i;k<H;k++){
            if(S[k][j]=='.'){
              t++;
            }else{
              break;
            }
          }
        } 

        // yoko
        if(j-1>=0){
          if(S[i][j-1]=='.'){
            y = yoko[i][j-1];
          }else{
            for(int k=j;k<W;k++){
              if(S[i][k]=='.'){
                y++;
              }else{
                break;
              }
            }
          }
        }else{
            for(int k=j;k<W;k++){
              if(S[i][k]=='.'){
                y++;
              }else{
                break;
              }
            }
        }
        tate[i][j]=t;
        yoko[i][j]=y;
        kosu = t+y-1;
        if(result<kosu){
          //cout << i << ":" << j << endl;
          //cout << t << ":" << y << endl;
          result=kosu;
        }
      }
    }
  }
  /* 
  rep(i, H){
    rep(j, W){
      cout << tate[i][j];
    }
    cout << endl;
  }
  cout << endl;
  rep(i, H){
    rep(j, W){
      cout << yoko[i][j];
    }
    cout << endl;
  }
  */
  cout << result << endl;


  return 0;
}


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

struct edge{
	int color, to, id;
};
vector<int> ans;
int max_k = 1;

void dfs(int v, int p, int par_col, vector<vector<edge>> &G){
	int col = 1;
	for(auto nv : G[v]){
		if(nv.to == p) continue;
		if(col == par_col) ++col;
		nv.color = col++;
		ans[nv.id] = nv.color;
		chmax(max_k, nv.color);
		dfs(nv.to, v, nv.color, G);
	}
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	ans.resize(n);
	vector<vector<edge>> G(n);
	rep(i,n-1){
		int a, b;
		cin >> a >> b;
		--a; --b;
		edge e;
		e.color = -1, e.to = b, e.id = i;
		G[a].push_back(e);
		e.to = a;
		G[b].push_back(e);
	}
	dfs(0, -1, -1, G);
	cout << max_k << endl;
	rep(i,n-1) cout << ans[i] << endl;
	return 0;
}

Measured memory_bytes: 8320
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ff first
#define ss second
#define quick                    \
    ios::sync_with_stdio(false); \
    cin.tie(0);
#define time cerr << (0.1 * clock()) / CLOCKS_PER_SEC << endl;
#define mod 1000000007
typedef long long ll;
typedef pair<ll, ll> pl;
#define forn(n) for (ll i = 0; i < ll(n); i++)

int query(int i) {
    cout << i << endl;
    fflush(stdout);
    string s;
    cin >> s;
    if (s == "Vacant") {
        return -1;
    }
    if (s == "Female") {
        return 1;
    }
    return 0;
}

void solve() {
    int n;
    cin >> n;
    int ini = query(0);
    if (ini == -1) {
        return;
    }
    int l = 0, r = n;
    while (l < r) {
        int mid = l + ((r - l) / 2);
        auto res = query(mid);
        if (res == -1) {
            return;
        }
        int exp = ((mid) % 2 + ini) % 2;
        if (exp == res) {
            l = mid + 1;
        } else {
            r = mid;
        }
    }
    return;
}

int main() {
    quick;
    solve();
    // ll t;
    // cin >> t;
    // while (t--) {
    //     solve();
    // }
}

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

int cost(int x[], int l, int r) {
  int w = x[r] - x[l];
  if (abs(x[r]) <= abs(x[l])) {
    return(abs(x[r]) + w);
  }
  else {
    return(abs(x[l]) + w);
  }
}

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

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

  int c_min = 2*( abs(x[0]) + abs(x[N-1]) );
  for (int l=0; l<N-K+1; ++l) {
    int r = K + l - 1;
    int c = cost(x,l,r);
    if (c < c_min) {
      c_min = c;
    }
  }

  cout << c_min <<endl;
}


Measured memory_bytes: 640
#define D(x) cout << #x " = " << x << endl
#define DD(x, y) cout << "(" #x ", " #y ") = (" << x << ", " << y << ")" << endl
#define DDD(x, y, z) cout << "(" #x ", " #y ", " #z ") = (" << x << ", " << y << ", " << z << ")" << endl
#define DV(v) for (auto __macro_vi : v){ cout << __macro_vi << " ";} cout << endl;
#define DA(a, n) for (int __macro_i = 0; __macro_i < n; ++__macro_i) { cout << a[__macro_i] << " "; } cout << endl;
#define REP(i, n) for (int i = 0; i < n; ++i)
#define ALL(v) (v).begin(), (v).end()
#include<iostream>
#include<vector>
#include<cassert>
#include<cmath>

using namespace std;

int n, k;
int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> k;
    if (k > (n - 1) * (n - 2) / 2) {
        cout << -1 << endl;
        return 0;
    }

    cout << n * (n - 1) / 2 - k << endl;
    for (int i = 1; i < n; ++i) {
        for(int j = i + 1; j <= n; ++j) {
            if (i == 1 || k == 0) {
                cout << i << " " << j << endl;
            } else {
                k--;
            }
        }
    }
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>

using namespace std;
vector <vector<int> > randb (vector <vector<int> >island,int si,int sj,int w,int h,int num);
int main(){
  int w = 12;
  int h = 12;
  int cnt;
  char tmp;
  for(int c = 0 ; c < 20 ; c++){
    cnt = 2;
    vector <vector<int> > island(12,vector<int>(12,0));
    for(int i = 0 ; i < h ; i++){
      for(int j = 0 ; j < w ; j++){
        cin >> tmp;
        island[i][j] = (int)(tmp- '0');
      }
    }

    if(cin.good() != true){
      break;
    }

    for(int i = 0 ; i < h ; i++){
      for(int j = 0 ; j < w ; j++){
        if(island[i][j] == 1){
          island = randb(island,i,j,w,h,cnt);
          cnt++;
        }
      }
    }
    cout << cnt-2 << endl;
  }
  return 0;
}

 vector <vector<int> > randb (vector <vector<int> >island,int si,int sj,int w,int h,int num){
   if( si+1 < h  && island[si+1][sj] == 1){
     island[si+1][sj] = num;
     island = randb(island,si+1,sj,w,h,num);
   }
   if( si-1 >= 0 && island[si-1][sj] == 1){
     island[si-1][sj] = num;
     island= randb(island,si-1,sj,w,h,num);
   }
   if( sj+1 < w  && island[si][sj+1] == 1){
     island[si][sj+1] = num;
     island = randb(island,si,sj+1,w,h,num);
   }
   if( sj-1 >= 0 && island[si][sj-1] == 1){
     island[si][sj-1] = num;
     island = randb(island,si,sj-1,w,h,num);
   }
   return island;
}


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

int search(int A[], int n, int key)
{
  int i = 0;
  A[n] = key;
  while(A[i] != key)
  {
    i++;
  }
  return i!=n;
}
int main()
{
  int i, n, A[10001], q, key, sum = 0;

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

  scanf("%d", &q);
  for(i=0; i<q; i++)
  {
    scanf("%d", &key);
    if(search(A, n, key))
      sum++;
  }

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

  return 0;
}



Measured memory_bytes: 2712
#include <bits/stdc++.h>
#define D(x) cout << #x << "=" << x << endl
#define De(...)
using namespace std; 

int _; 

int n,a[200050];
long long ans=0;

int main()
{
	//for(scanf("%d",&_);_;_--)
	scanf("%d",&n);
	for(int i=0;i<n;i++)
		scanf("%d",a+i);
	sort(a,a+n); 
	if(n>1)
		ans += a[n-1];
	for(int i=2;i<n;i++)
		ans += a[n-1-i/2]; 
	printf("%lld\n",ans);
	return 0;
}





Measured memory_bytes: 4552
#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<map>
#include<set>
#include<complex>
#include<stack>
#include<cmath>
 
using namespace std;
 
#define reps(i,f,n) for(int i=f;i<int(n);i++)
#define rep(i,n) reps(i,0,n)
 
#define X real()
#define Y imag()
typedef complex<double> P;
 
namespace std{
    bool operator<(const P &a, const P &b){return a.X==b.X ? a.Y<b.Y : a.X<b.X;}
}
 
double cross(P a,P b){return a.X*b.Y-a.Y*b.X;}
bool ccw(P a,P b,P c){return cross(b-a,c-a)>=0;}
 
vector<P> convex_VP(vector<P> p){
     
    sort(p.begin(),p.end());
    vector<P> ret(p.size()*2);
     
    int k=0;
    for(int i=0;i<p.size();i++){
        while(k>1 && !ccw(ret[k-2],ret[k-1],p[i]))k--;
        ret[k]=p[i];k++;
    }
     
    int t=k;
    for(int i=p.size()-2;i>=0;i--){
        while(k>t && !ccw(ret[k-2],ret[k-1],p[i]))k--;
        ret[k]=p[i];k++;
    }
     
    return vector<P>(ret.begin(), ret.begin()+k-1);
}
 
int main(){
     
    while(1){
        int n;
        cin>>n;
        if(n==0)break;
         
        vector<P> p;
        rep(i,n){
            double a,b;
            scanf("%lf,%lf",&a,&b);
            p.push_back(P(a,b));
        }
         
        vector<P> convex = convex_VP(p);
	  /*
        rep(i,convex.size()){
            printf("%lf %lf\n",convex[i].X,convex[i].Y);
        }*/
        cout<<(int)p.size()-(int)convex.size()<<endl;
    }
}

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

int main()
{
    int n, q;
    cin >> n >> q;
    ll a[100002];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        a[i] *= 2;
    }
    reverse(a, a + n);
    if(n % 2){
        a[n] = 0;
        n++;
    }
    ll ans[100002];
    for(int i = 0; i < n; i += 2) ans[0] += a[i];
    for(int i = 1; i < n / 2; i++) ans[i] = ans[i - 1] + a[i] - a[i * 2];
    ll d[100002];
    for(int i = 1; i < n / 2; i++) d[i - 1] = -(a[i] + a[i * 2]) / 2;
    for(int i = 0; i < q; i++){
        ll x;
        cin >> x;
        x *= 2;
        cout << ans[upper_bound(d, d + n / 2, -x) - d] / 2 << endl;
    }
}

Measured memory_bytes: 3328
# 解説を参考に作成
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
from collections import deque
import heapq


# from decorator import stop_watch
#
#
# @stop_watch
def solve(N, K, Vi):
    ans = 0
    for ab in range(min(N, K) + 1):
        for a in range(ab + 1):
            b = ab - a
            have = Vi[:a] + Vi[N - b:]
            have.sort()
            s = sum(have)
            for i in range(K - ab):
                if len(have) <= i:
                    break
                elif have[i] < 0:
                    s += abs(have[i])
                else:
                    break
            ans = max(ans, s)
    print(ans)


if __name__ == '__main__':
    N, K = map(int, input().split())
    Vi = [int(i) for i in input().split()]
    solve(N, K, Vi)

    # # test
    # import random
    # from func import random_str
    # N, K = 100, 50
    # Vi = [random.randint(-(10 ** 7), 10 ** 7) for _ in range(N)]
    # solve(N, K, Vi)


Measured memory_bytes: 9444
from itertools import groupby

S=input()
K=int(input())

l=[(k, list(v)) for k, v in groupby(S)]
if len(l) == 1:
    len_S = len(S)
    if len_S%2:
        n = len_S * (K//2) + (len_S//2) * (K%2)
    else:
        n = (len_S//2) * K
else:
    n=sum([len(v)//2 for k, v in l if len(v) > 1])*K
    if l[0][0]==l[-1][0] and len(l[0][1])%2 !=0 and len(l[-1][1])%2 !=0:
        n = n + K - 1
print(n)


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

int main() {
  int n,m,min_id,max_id;
  cin >> n >> m;
  min_id=1;
  max_id=n;
  vector<int> l(m);
  vector<int> r(m);
  for(int i=0;i<m;i++) cin >> l.at(i) >> r.at(i);
  for(int i=0;i<m;i++) {
    min_id=max(min_id,l.at(i));
    max_id=min(max_id,r.at(i));
  };
  cout << max(0,(max_id-min_id+1)) << endl;
}

Measured memory_bytes: 1024
#include <iostream>

using namespace std;

int a,b,c,d,j,k;

int main()
{
    cin >> a >> b >> c >> d;
    j=a+b;
    k=c+d;
    if(j>k)
        cout << "Left";
    else if(j<k)
        cout << "Right";
    else
        cout << "Balanced";
    return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>

#define STACK_SIZE 100
struct StackStruct
{
	int top;
	int data[STACK_SIZE];
};
typedef struct StackStruct Stack;

void stack_clear(Stack *stack)
{
	stack->top = 0;
}

int stack_pop(Stack *stack)
{
	return stack->data[--stack->top];
}

void stack_push(Stack *stack, int value)
{
	stack->data[stack->top++] = value;
}

void stack_dump(Stack *stack)
{
	printf("[");
	for(int i = 0; i < stack->top; ++i)
	{
		printf(" %d", stack->data[i]);
	}
	printf(" ]\n");
}

int main()
{
	char token[8];
	Stack stack;
	stack_clear(&stack);

	while(scanf("%s", token) != EOF)
	{
		int valueL, valueR;
		//printf("token = '%s'\n", token);
		
		switch(token[0])
		{
			case '+':
				valueR = stack_pop(&stack);
				valueL = stack_pop(&stack);
				stack_push(&stack, valueL + valueR);
				break;
			
			case '-':
				valueR = stack_pop(&stack);
				valueL = stack_pop(&stack);
				stack_push(&stack, valueL - valueR);
				break;
			
			case '*':
				valueR = stack_pop(&stack);
				valueL = stack_pop(&stack);
				stack_push(&stack, valueL * valueR);
				break;
			
			case '/':
				valueR = stack_pop(&stack);
				valueL = stack_pop(&stack);
				stack_push(&stack, valueL / valueR);
				break;
			
			default:
				stack_push(&stack, atoi(token));
				break;
		}
		//stack_dump(&stack);
	}
	printf("%d\n", stack_pop(&stack));
}



Measured memory_bytes: 2148
let () = Scanf.scanf "%d" (fun n ->
  try
    for h = 1 to 3500 do
      for w = h to 3500 do
        if 4 * h * w > n * w + n * h && (n * h * w) mod (4 * h * w - n * w - n * h) = 0
        then (Printf.printf "%d %d %d\n" h ((n * h * w) / (4 * h * w - n * w - n * h)) w; raise Not_found)
      done
    done
  with Not_found -> ())

Measured memory_bytes: 2432
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using db = double;
using ld = long double;
template<typename T> using V = vector<T>;
template<typename T> using VV = vector<vector<T>>;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(v) (v).begin(),(v).end()
#define siz(v) (ll)(v).size()
#define rep(i,a,n) for(ll i=a;i<(ll)(n);++i)
#define repr(i,a,n) for(ll i=n-1;(ll)a<=i;--i)
#define ENDL '\n'
typedef pair<int,int> Pi;
typedef pair<ll,ll> PL;
constexpr ll mod = 1000000007;
constexpr ll INF = 1000000099;
constexpr ll LINF = (ll)(1e18 +99);
const vector<ll> dx={-1,1,0,0},dy={0,0,-1,1};
template<typename T,typename U> inline bool chmin(T& t, const U& u){if(t>u){t=u;return 1;}return 0;}
template<typename T,typename U> inline bool chmax(T& t, const U& u){if(t<u){t=u;return 1;}return 0;}
template<typename T> inline T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<typename T,typename Y> inline T mpow(T a, Y n) {
  T res = 1;
  for(;n;n>>=1) {
    if (n & 1) res = res * a;
    a = a * a;
  }
  return res;
}



signed main(){
  cin.tie(0);ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(20);
  ll n,m;cin>>n>>m;
  VV<int> e(n,V<int>(0));

  rep(i,0,m){
    int a,b;cin>>a>>b;
    a--;b--;
    e[a].pb(b);
    e[b].pb(a);
  }

  V<int> flag(n,-1);

  queue<int> q;
  q.push(0);
  flag[0]=0;
  q.push(0);
  q.push(-1);


  while(q.size()){
    int now=q.front();q.pop();
    int dis=q.front();q.pop();
    int pre=q.front();q.pop();

    for(auto&& i:e[now]){
      if(i==pre)continue;

      if(flag[i]==-1){
        q.push(i);
        flag[i]=dis^1;
        q.push(dis^1);
        q.push(now);
      }else if(flag[i]^(dis^1)){
        cout<<n*(n-1)/2 -m<<ENDL;
        return 0;
      }
    }
  }

  ll x,y;x=y=0;
  rep(i,0,n){
    if(flag[i]==0)x++;
    else y++;
  }

  cout<<x*y-m<<ENDL;


}
//! ( . _ . ) ! 
//CHECK overflow,vector_size,what to output?

Measured memory_bytes: 6272
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a) for(int i = 0; i < (a); i++)
#define ALL(a) (a).begin(),(a).end()
typedef long long ll;
typedef pair<int, int> P;
const int INF = 1e9;
const int MOD = 1e9 + 7;

signed main(){
    string o,e;
    cin >> o >> e;
    string ans;
    REP(i, o.size() + e.size()){
        if(i % 2 == 0) ans.push_back(o[i / 2]);
        else ans.push_back(e[i / 2]);
    }
    cout << ans << endl;
}

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

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

using pi = pair<int, int>;

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

  int w, h;
  while(cin >> w >> h, h != 0) {
    int tx, ty, kx, ky;
    cin >> tx >> ty >> kx >> ky;
    vector<vector<int>> d(h + 2, vector<int>(w + 2, 1));
    for(int i = 1; i <= h; ++i) {
      for(int j = 1; j <= w; ++j) {
        cin >> d[i][j];
      }
    }

    map<pair<pi, pi>, int> mp;
    queue<pair<pi, pi>> que;
    pair<pi, pi> now({{tx, ty}, {kx, ky}}), nxt;
    que.push(now);
    mp[now] = 0;

    int ans = 1e9;
    while(!que.empty()) {
      now = que.front();
      que.pop();

      if(mp[now] >= 100) break;
      if(now.first.first == now.second.first && now.first.second == now.second.second) {
        ans = mp[now];
        break;
      }

      for(int i = 0; i < 4; ++i) {
        int ntx = now.first.first + dx[i];
        int nty = now.first.second + dy[i];
        int nkx = now.second.first - dx[i];
        int nky = now.second.second - dy[i];
        if(d[nty][ntx] == 1) {
          ntx -= dx[i]; nty -= dy[i];
        }
        if(d[nky][nkx] == 1) {
          nkx += dx[i]; nky += dy[i];
        }

        nxt = {{ntx, nty}, {nkx, nky}};
        if(!mp.count(nxt)) {
          mp[nxt] = mp[now] + 1;
          que.push(nxt);
        }
      }
    }

    if(ans == 1e9) cout << "NA\n";
    else cout << ans << endl;
  }
}


Measured memory_bytes: 13996
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <tuple>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <numeric>
#include <utility>
#include <iterator>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <ctime>
#include <cassert>
#include <random>
#include <cstring>

#define rep(i,n) for(int i=0;i<int(n);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back

using namespace std;
using ll = long long;

const ll mod = 1000000007;

int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int ddx[8]={-1,-1,0,1,1,1,0,-1};
int ddy[8]={0,1,1,1,0,-1,-1,-1};
// bool debug=false;

/*---------------------------------------------------*/

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> field(H);
  vector<vector<int>> up = vector<vector<int>>(H, vector<int>(W, 0));
  vector<vector<int>> down = vector<vector<int>>(H, vector<int>(W, 0));
  vector<vector<int>> right = vector<vector<int>>(H, vector<int>(W, 0));
  vector<vector<int>> left = vector<vector<int>>(H, vector<int>(W, 0));
  for(int i = 0; i < H; i++) cin >> field[i];

  for(int y = 0; y < H; y++) {
    // 右
    for(int x = 0; x < W; x++) {
      if(x == 0 && field[y][x] == '.') {
	right[y][x] = 1;
      } else if(field[y][x] == '.'){
	right[y][x] = right[y][x - 1] + 1;
      }
    }
    // 左
    for(int x = W - 1; x >= 0; x--) {
      if(x == W - 1 && field[y][x] == '.') {
	left[y][x] = 1;
      } else if(field[y][x] == '.') {
	left[y][x] = left[y][x + 1] + 1;
      }
    }
  }

  for(int x = 0; x < W; x++) {
    // 下
    for(int y = 0; y < H; y++) {
      if(y == 0 && field[y][x] == '.') {
	down[y][x] = 1;
      } else if(field[y][x] == '.') {
	down[y][x] = down[y - 1][x] + 1;
      }
    }
    // 上
    for(int y = H - 1; y >= 0; y--) {
      if(y == H - 1 && field[y][x] == '.') {
	up[y][x] = 1;
      } else if(field[y][x] == '.') {
	up[y][x] = up[y + 1][x] + 1;
      }
    }
  }

  int ans = 0;
  for(int y = 0; y < H; y++) {
    for(int x = 0; x < W; x++) {
      if(field[y][x] == '.') {
	ans = max(ans, down[y][x] + up[y][x] + left[y][x] + right[y][x] - 3);
      }
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 67200
s = input()
n = len(s)+1
a = [0]*n

for i in range(n-1):
    if s[i] == '<':
        a[i+1] += max(a[i]+1,a[i+1])
for j in range(len(s)-1,-1,-1):
    if s[j] == '>':
        a[j] = max(a[j],a[j+1]+1)
print(sum(a))
    

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

int hw[100][100];
int hw2[50][50];

int main() {
    int w, h;
    while (cin >> w >> h and (w or h)) {

        rep(i, h*2-1) {
            if (i%2 == 0) {
                rep(j, w-1) {
                    int t;
                    cin >> t;
                    hw[i][ j * 2 + 1 ] = t;
                }
            }
            else {
                rep(j, w) {
                    int t;
                    cin >> t;
                    hw[i][ j * 2 ] = t;
                }
            }
        }

        using P = pair<int, int>;
        queue<P> que;
        que.push(P(0, 0));
        int dist[100][100];
        fill(dist[0], dist[100], 1e8);
        dist[0][0] = 1;

        int dx[] = { 1, 0, -1, 0 };
        int dy[] = { 0, 1, 0, -1 };
        while (que.size()) {
            P p = que.front(); que.pop();

            int x = p.second;
            int y = p.first;

            rep(d, 4) {
                int px = x + dx[d];
                int py = y + dy[d];
                if (0 <= px and px < w*2-1 and 0 <= py and py < h*2-1 and hw[py][px] == 0 and dist[py][px] == 1e8) {
                    dist[py][px] = dist[y][x] + 1;
                    dist[py+dy[d]][px+dx[d]] = dist[y][x] + 1;
                    que.push(P(py + dy[d], px + dx[d]));
                }
            }

        }

        cout << (dist[h*2-2][w*2-2] == 1e8 ? 0 : dist[h*2-2][w*2-2]) << endl;

        // rep(i, h*2-1) {
        //     rep(j, w*2-1) {
        //         cerr << dist[i][j] << " ";
        //     }
        //     cerr << endl;
        // }

    }
}

Measured memory_bytes: 3100
N = int(input())
li = list(map(int, input().split()))
li.sort()
a = li[N//2]
flag = True
for i in range (N//2):
    if a == li[i]:
        flag = False
if flag:
     print(li[N//2]-li[N//2-1])
else:
     print(0)

Measured memory_bytes: 20624
N, L = map(int, input().split())
total = 0
minimam = abs(L)
x = L
for i in range(1, N+1):
    if minimam > abs(L+i-1):
        minimam = abs(L+i-1)
        x = L+i-1
    total += (L+i-1)
print(total-x)

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

int main(){
    int input;
    cin >> input;
    int D = input%10;
    int C = (input%100 - D)/10;
    int B = (input%1000 - C*10 - D)/100;
    int A = (input - B*100 - C*10 -D)/1000;

    bool is_seven = false;
    int op1, op2, op3;
    for(int i=0; i<pow(2,3);i++){
        op1 = (i>>0 & 1) ? 1 : -1;
        op2 = (i>>1 & 1) ? 1 : -1;
        op3 = (i>>2 & 1) ? 1 : -1;

        if(A + op1*B + op2*C + op3*D == 7){
            is_seven = true;
            break;
        } 
    }

    if(is_seven){
        string st_op1 = (op1 == 1) ? "+" : "-";
        string st_op2 = (op2 == 1) ? "+" : "-";
        string st_op3 = (op3 == 1) ? "+" : "-";
        cout << to_string(A) << st_op1 << to_string(B) << st_op2 << to_string(C) << st_op3 << to_string(D) << "=7" << endl;
    }else{
        cout << "unexpected input" << endl;
    }
    return 0;

} 


Measured memory_bytes: 3696
// https://atcoder.jp/contests/abc052/tasks/abc052_b

#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); i++)
#define INF 1e9
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;

int main()
{
  /* Magic word */
  // cin.tie(0);
  // ios::sync_with_stdio(false);
  /* ---------- */

  int n, x = 0, mx = 0;
  string s;

  cin >> n >> s;

  REP(i, n)
  {
    if (s[i] == 'I')
      x++;
    else
      x--;

    mx = max(x, mx);
  }

  cout << mx << '\n';

  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int N, A;
int inp[55];
long long dp[55][2600][55];
int main()
{
	scanf("%d %d", &N, &A);
	for(int n=0; n<N; n++)
	{
		scanf("%d", &inp[n]);
	}
	long long ans=0;
	dp[0][inp[0]][1]=1;
	dp[0][0][0] = 1;
	for(int n=0; n<N; n++)
	{
		for(int a=0; a<2600; a++)
		{
			for(int i=0; i<=N; i++)
			{
				if(dp[n][a][i])
				{
					//printf("%d %d %d is %d\n", n, a, i, dp[n][a][i]);
					dp[n+1][a+inp[n+1]][i+1] += dp[n][a][i];
					dp[n+1][a][i] += dp[n][a][i];
				}
				if(n==N-1 && i!=0)
				{
					if(a/i==A && a%i==0)
					{
						ans+= dp[n][a][i];
					}
				}
			}
		}
	}
	printf("%lld\n", ans);
}

Measured memory_bytes: 14976
n=gets.to_i
ab_count=0
b_front=0
a_tail=0
bf_at=0
n.times do|i|
	str=gets.chomp
	ab_count+=str.scan("AB").length
	if(str[str.length-1]=="A")
		if(str[0]=="B")
			bf_at+=1	
		else
			a_tail+=1
		end
	elsif(str[0]=="B")
		b_front+=1
	end
end
flag=false
if(bf_at>0 and a_tail>0)
	bf_at-=1
	a_tail-=1
	ab_count+=1
	ab_count+=bf_at
	flag=true
else
	if(bf_at>0)
		ab_count+=(bf_at-1)
		flag=true
	end
end
if(flag and b_front>0)
	ab_count+=1
	b_front-=1
end
if a_tail>b_front
	ab_count+=b_front
else
	ab_count+=a_tail
end
puts ab_count


Measured memory_bytes: 1788