akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <fstream>
#include <string> 
#include <cmath>  
#include <cstdlib>
#include <ctime>
#include <vector>
#include <algorithm>
#include <numeric>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <functional>

using namespace std;
using ll = long long;

#define FOR(i, m, n) for(int i = (m);i < (n);i++)
#define REP(i,n) for(int i = 0; i < int(n); i++)
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define PAIR pair<int,int>
#define MP make_pair
#define VP vector<pair<int,int>>
#define VS vector<string>
#define QUE queue<int>
#define DEQ deque<int>
#define PQUE priority_queue<int> //5,5,4,3,3,2,...
#define REPQUE priority_queue<int, vector<int>, greater<int>> //1,1,2,3,4,4,5,...
#define SUM(obj) accumulate((obj).begin(), (obj).end(), 0)
#define SORT(obj) sort((obj).begin(), (obj).end()) // 1,2,3,4,5...
#define RESORT(obj) sort((obj).begin(), (obj).end(), greater<int>()) // 5,4,3,2,1...
#define UB(obj,n) upper_bound((obj).begin(), (obj).end(), n) //itr > n
#define LB(obj,n) lower_bound((obj).begin(), (obj).end(), n) //itr>= n

const ll MOD = (ll)1e9 + 7;

int main() {
	int N;
	cin >> N;
	VI L(2 * N);
	REP(i, 2 * N)cin >> L[i];
	SORT(L);
	int sum = 0;
	for (int i = 0; i < 2 * N; i += 2) sum += L[i];
	cout << sum << endl;
	return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	
	public static void main (String[] args)  {
	
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[] length = new long[n];
		
		for (int i=0;i<n;i++) {
			length[i]=sc.nextLong();
		}
		
		long total = 0;
		for (int i=0;i<n;i++) {
			total+=length[i];
		}
		long ans = total;
		long subTotal = 0;
		for (int i=0;i<n;i++) {
			subTotal+=length[i];
			ans = Math.min(ans, Math.abs(total-subTotal-subTotal));
		}
		System.out.println(ans);
	}
}


Measured memory_bytes: 89912
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Built using CHelper plug-in Actual solution is at the top
 */
public class Main {
	public final static double MAX_T = 1000000;
	public final static double MIN_T = 0;
	public static int s = 0;

	public static void main(String[] args) {
		InputStream inputStream = System.in;
		OutputStream outputStream = System.out;
		InputReader in = new InputReader(inputStream);
		PrintWriter out = new PrintWriter(outputStream);
		TaskC solver = new TaskC();
		solver.solve(1, in, out);
		out.close();
	}

	static class TaskC {

		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int h = in.nextInt();
			int w = in.nextInt();
			int n = in.nextInt();
			int[] a = new int[n];
			for (int i = 0; i < n; i++) {
				a[i] = in.nextInt();
			}
			int[][] m = new int[h][w];
			int r = 0, c = 0, s = 1, co = 0;
			for (; r < h; r++) {
				for (; c < w && c >= 0; c += s) {
					while (a[co] == 0) {
						co++;
					}
					a[co]--;
					m[r][c] = co + 1;
				}
				s *= -1;
				c += s;
			}
			for (int i = 0; i < h; i++) {
				for (int j = 0; j < w; j++) {
					out.print(m[i][j] + " ");
				}
				out.println();
			}
		}

	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader(InputStream stream) {
			reader = new BufferedReader(new InputStreamReader(stream), 32768);
			tokenizer = null;
		}

		public String next() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(reader.readLine());
				} catch (IOException e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

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

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

		public double nextFloat() {
			return Double.parseDouble(next());
		}

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

	}
}

Measured memory_bytes: 23636
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>

#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define REP(i, n) FOR(i, 0, n)
#define REP1(i, n) FOR(i, 1, int(n) + 1)
#define RREP(i, n) RFOR(i, 0, n)
#define RREP1(i, n) RFOR(i, 1, int(n) + 1)
#define ALL(c) begin(c), end(c)

int _ = (
#ifndef LOCAL
    std::cin.tie(nullptr), std::ios::sync_with_stdio(false),
#endif
    std::cout.precision(10), std::cout.setf(std::ios::fixed));

using ll = long long;
using ull = unsigned long long;
using ld = long double;
template <typename T> using vec = std::vector<T>;
using namespace std;

int main() {
  int A, B;
  cin >> A >> B;
  if (B == 1) {
    cout << 0 << endl;
    return 0;
  }
  REP1(i, 20) {
    if (A * i - i + 1 >= B) {
      cout << i << endl;
      break;
    }
  }
  return 0;
}


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


int main()
{
    int n,k;
    cin >>n>> k;
    int bef;
    long long ans=0;
    vector<int>h;
    for(int i=0;i<n;i++){
        cin>>bef;
        h.push_back(bef);
    }
    sort(h.begin(),h.end());

    for(int i=0;i<n-k;i++){
        ans+=h[i];
    }
    cout<<ans;
}


Measured memory_bytes: 1400
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define range(a) a.begin(), a.end()
 
int N;
 
void dfs(string s, char now) {
  if (s.size() == N) {
    cout << s << endl;
    return;
  }
  for (char i = 'a'; i < now; i++) {
    dfs(s + i, now);
  }
  dfs(s + now, now + 1);
}
 
int main() {
  cin >> N;
  dfs("", 'a');
}

Measured memory_bytes: 1536
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<string>
#include<stack>
#include<cstdio>
#include<cmath>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<int,P> P1;

#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)
#define sor(v) sort(v.begin(),v.end())
#define rev(s) reverse(s.begin(),s.end())
#define lb(vec,a) lower_bound(vec.begin(),vec.end(),a)
#define ub(vec,a) upper_bound(vec.begin(),vec.end(),a)
#define uniq(vec) vec.erase(unique(vec.begin(),vec.end()),vec.end())
#define mp1(a,b,c) P1(a,P(b,c))

const int INF=1000000000;
const int dir_4[4][2]={{1,0},{0,1},{-1,0},{0,-1}};
const int dir_8[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};

ld dist(ld x1,ld y1,ld x2,ld y2){
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

int main(){
	int n;
	int x[1005],y[1005],r[1005];
	scanf("%d%d%d%d%d",&x[0],&y[0],&x[1],&y[1],&n);
	r[0] = 0;
	r[1] = 0;
	rep1(i,n){
		scanf("%d%d%d",&x[i+1],&y[i+1],&r[i+1]);
	}
	
	static ld d[1005][1005];
	rep(i,n+2)rep(j,n+2){
		d[i][j] = max ( (ld)0.0 , dist(x[i],y[i],x[j],y[j])-r[i]-r[j] );
	}
	
	ld cost[1005];
	bool used[1005];
	priority_queue<pair<ld,int>,vector<pair<ld,int>>,greater<pair<ld,int>>> que;
	rep(i,1005){
		cost[i] = 10000000000.0;
		used[i] = false;
	}
	cost[0] = 0.0;
	que.push(pair<ld,int>(0.0,0));
	while(!que.empty()){
		pair<ld,int> p = que.top(); que.pop();
		if(!used[p.sc]){
			used[p.sc] = true;
			rep(i,n+2){
				if(cost[i] > cost[p.sc] + d[p.sc][i]){
					cost[i] = cost[p.sc]+d[p.sc][i];
					que.push(pair<ld,int>(cost[i],i));
				}
			}
		}
	}
	cout.precision(20);
	cout << cost[1] << endl;
}



Measured memory_bytes: 24300
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
  int L, R;
  cin >> L >> R;

  int GS1, GE1, GS2, GE2;
  if ((R-L) >= 2019) {
    GE1 = GS1 = GE2 = GS2 = -1;
  } else if (L%2019 < R%2019) {
    GS1 = 0;
    GE1 = L % 2019 - 1;
    GS2 = R % 2019 + 1;
    GE2 = 2019;
  } else {
    GS1 = R % 2019 + 1;
    GE1 = L % 2019 - 1;
    GS2 = GE2 = -1;
  }
  
  int ans = 2019*2019;
  for (int i = 0; i < 2019; i++) {
    if ((GS1<=i && i<=GE1) || (GS2<=i && i<=GE2)) continue;
    for (int j = i+1; j < 2019; j++) {
      if ((GS1<=j && j<=GE1) || (GS2<=j && j<=GE2)) continue;

      ans = min(ans, i*j%2019);
    }
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
// #include C/C++ {
#include <bits/stdc++.h>
// }
using namespace std;
// #typedef {
typedef long long int64;
typedef pair <int, int> PII;
typedef pair <int64, int64> PLL;
// }

// #parameter{
#ifdef DEBUG_MODE

#define TYPE decltype
#define RF(filename) {freopen((filename), "r", stdin);}
#define WF(filename) {freopen((filename), "w", stdout);}
#define DF(filename) {freopen((filename), "w", stderr);}
#define eprintf printf

#else

#define TYPE __typeof
#define RF(filename) {;}
#define WF(filename) {;}
#define DF(filename) {;}
#define eprintf(...)
#define fprintf(...)

#endif

// #define {
#define SZ(a) ((int)(a).size())
#define X first
#define Y second
#define MP make_pair
#define L(x) ((x)<<1)
#define R(x) ((x)<<1 | 1)
#define max3(x, y, z) (max(max((x), (y)), (z)))
#define min3(x, y, z) (min(min((x), (y)), (z)))
#define BIT(x, i) (((x) >> (i)) & 1)
#define ALL(it) (it).begin(), (it).end()
#define FILL(__space, __val) memset(__space, __val, sizeof(__space))
#define MOVE(__spaceTo, __spaceFrom) memmove(__spaceTo, __spaceFrom, sizeof(__spaceTo))
#define UNIQUE(__vector) sort(ALL(__vector)), __vector.resize(unique(ALL(__vector)) - __vector.begin())
#define FOR(it, c) for( TYPE((c).begin()) it = (c).begin(); it != (c).end(); it++)
/////////////////////////////////////////////////////////////
const double PI = acos(-1.0);
const double EPS = 1e-6;

#define MAX_N 100005
#define MAX_M 5005
#define MOD (int)(1e9+7)
#define INF ((1 << 30) - 1)
#define BINF ((1LL << 62) - 1LL)
#define NONE -1
#define NIL 0
// }

/////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////
int main() {
	RF("input.txt");
	//  WF("output.txt");
	DF("err.txt");

	int N, M;
	scanf("%d %d", &N, &M);
	PII a[MAX_N];
	for(int i = 0; i < N; i++) scanf("%d %d", &a[i].X, &a[i].Y);
	sort(a, a + N);
	int64 res = 0LL;
	for(int i = 0; i < N; i++){
		res += 1LL * min(M, a[i].Y) * a[i].X;
		M -= min(M, a[i].Y);
		if(M == 0) break;
	}
	printf("%lld\n", res);
	return 0;
}

Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
int n;
long long int a[200000];
long long int answer;
int main(){
  cin >> n;
  vector<long long int> sum(n+1,0);
  answer = 0;
  for(int i = 0;i < n;i++) cin >> a[i];
  for(int i = 1;i <= n;i++){
    sum[i] = sum[i-1] + a[i-1];
  }
  sort(sum.begin(), sum.end());
  long long int num = 0;
  for(int i = 0;i < n;i++){
    if(sum[i] == sum[i+1]) ++num;
    else{
      answer += num*(num+1)/2;
      num = 0;
    }
  }
  answer += num*(num+1)/2;
  cout << answer << endl;
}

Measured memory_bytes: 3328
#include <bits/stdc++.h>
 
using namespace std;
typedef long long ll;
#define vi vector<int>
#define vl vector<ll>
#define vii vector< vector<int> >
#define vll vector< vector<ll> >
#define vs vector<string>
#define pii pair<int,int>
#define pis pair<int,string>
#define psi pair<string,int>
#define pll pair<ll,ll>
#define tll tuple<ll, ll, ll>
#define all(c) c.begin(),c.end()

#define DEBUG(x) cerr<<#x<<": "<<x<<endl;
#define DEBUG_VEC(v) cerr<<#v<<":";for(int i=0;i<v.size();i++) cerr<<" "<<v[i]; cerr<<endl
 
const ll inf = 1000000001;
const ll INF = 1e18;
const ll MOD = 1000000007;

char albt[26] = {'a','b','c','d','e','f','g','h','i','j','k','l',
                 'm','n','o','p','q','r','s','t','u','v','w','x',
                 'y','z'};

//vector<ll> g[202020];
//fill(score, score+202020, 0);
//vector<ll> v;

int main(){
  ll n, c;
  cin >> n >> c;
  ll V[n];
  ll D[n];
  ll x, v;
  for (int i=0;i<n;i++){
    cin >> x >> v;
    D[i] = x;
    V[i] = v;
  }
  
  ll f[n+1];
  ll f2[n+1];
  f[0] = 0;
  f2[0] = 0;
  for (int i=1;i<=n;i++) f[i] = f[i-1] + V[i-1];
  for (int i=1;i<=n;i++) f[i] -= D[i-1];
  for (int i=1;i<=n;i++) f2[i] = f[i] - D[i-1];
  //for (int i=0;i<=n;i++) cout << f[i] << " ";
  //cout << endl;
  
  ll rf[n+1];
  ll rf2[n+1];
  rf[n] = 0;
  rf2[n] = 0;
  for (int i=n-1;i>=0;i--) rf[i] = rf[i+1] + V[i];
  for (int i=n-1;i>=0;i--) rf[i] -= (c-D[i]);
  for (int i=n-1;i>=0;i--) rf2[i] = rf[i] - (c-D[i]);
  //for (int i=0;i<=n;i++) cout << rf[i] << " ";
  //cout << endl;
  
  ll g[n+1];
  ll g2[n+1];
  g[0] = 0;
  g2[0] = 0;
  for (int i=1;i<=n;i++) g[i] = max(g[i-1],f[i]);
  for (int i=1;i<=n;i++) g2[i] = max(g2[i-1],f2[i]);
  //for (int i=0;i<=n;i++) cout << g[i] << " ";
  //cout << endl;
  
  ll ans = 0;
  ll cand1, cand2;
  for (int i=0;i<=n;i++){
    cand1 = rf[i] + g2[i];
    cand2 = rf2[i] + g[i];
    ans = max(ans, cand1);
    ans = max(ans, cand2);
  }
    
  cout << ans << endl;
}

Measured memory_bytes: 6528
#include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using namespace std;

using P = pair<int, int>;

int main() {
  int n;
  map<int, int> vas;
  map<int, int> vbs;

  cin >> n;
  int v;
  for(int i = 0; i < n; ++i) {
    cin >> v;
    if(i % 2 == 0) {
      vas[v]++;
    } else {
      vbs[v]++;
    }
  }
  vector<P> vas2;
  vector<P> vbs2;
  for(auto v : vas) {
    vas2.push_back(make_pair(v.second, v.first));
  }
  sort(vas2.begin(), vas2.end(), greater<P>());
  for(auto v : vbs) {
    vbs2.push_back(make_pair(v.second, v.first));
  }
  sort(vbs2.begin(), vbs2.end(), greater<P>());

  // それぞれ一番多い数字が異なる
  if(vas2[0].second != vbs2[0].second) {
    cout << (n - vas2[0].first - vbs2[0].first) << endl;
    return 0;
  }

  // 全部同じ数字
  if(vas2.size() == 1 && vbs2.size() == 1) {
    cout << (n / 2) << endl;
    return 0;
  }

  // どちらかが１種類で構成
  if(vas2.size() == 1 && vbs2.size() != 1) {
    cout << (n / 2 - vbs2[1].first) << endl;
    return 0;
  }

  if(vas2.size() != 1 && vbs2.size() == 1) {
    cout << (n / 2 - vas2[1].first) << endl;
    return 0;
  }

  // 一番多い数字が同じで数字は２種類以上ある
  cout << min(n - vas2[0].first - vbs2[1].first, n - vbs2[0].first - vas2[1].first) << endl;
  return 0;
}

Measured memory_bytes: 6136
from collections import Counter
input = raw_input
n = int(input())
As = list(map(int, input().split(" ")))
nums = [0] * (10**5+1)

counter = Counter(As)
cur_sum = sum(As)

for key, val in counter.items():
    nums[key] = val

q = int(input())
for i in range(q):
    b, c = map(int, input().split(" "))

    num1 = counter[b]
    counter[b] = 0
    counter[c] += num1

    diff = (c - b) * num1

    cur_sum += diff
    print(cur_sum)

Measured memory_bytes: 94248
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const ll INF=mod*mod;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(x) (x).begin(),(x).end()
ll min(ll a,ll b){
  if(a>b){
    return b;
    }
      else{
        return a;
      }
  }
  
  ll max(ll a,ll b){
    if(a>b){
      return a;
    }
    else{
      return b;
    }
  }

ll power(ll a,ll b){
 ll ans=1;
 for(ll i=1;i<=b;i++){
ans*=a;
  }
  return ans;
}

 ll gcd(ll m,ll n){
 	if(n==0){
       return m;
     }
   return gcd(n,m%n);
 }
ll lcm(ll a,ll b){
  ll g=gcd(a,b);
  return a/gcd(a,b)*b;
		

}
int main(){
  
  ll X;
  cin>>X;
  cout<<1000*(X/500)+5*((X%500)/5)<<endl;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define INF ((long long)1e18)
#define MOD ((int)1e9+7)
#define endl "\n"

#define yn(f) ((f)?"Yes":"No")
#define YN(f) ((f)?"YES":"NO")

#define MAX 500

int n, m;
string b[MAX];

int minn(){
	int re = 0;
	
	for(int i = 0; i < (1<<n); i++){
		vector<int> temp(m+1,0);
		int con = 0;
		bool flag = true;
		
		// cout<<"--------------------"<<endl;
		// for(int j = 0; j < m; j++){
			// cout<<temp[j]<<" ";
		// }cout<<endl;
		
		for(int j = 0; j < n; j++){
			if((1&(i>>j)) == 0) continue;
			con++;
			for(int k = 0; k < m; k++){
				temp[k] ^= (b[j][k] - '0');
			}
		}
		
		for(int j = 0; j < m; j++){
			if(temp[j]){
				flag = false;
				break;
			}
		}
		
		// for(int j = 0; j < m; j++){
			// cout<<temp[j]<<" ";
		// }cout<<endl;
		
		// cout<<"n = "<<n<<" i = "<<i<<" "<<flag<<" "<<re<<" "<<con<<endl;
		if(flag) re = max(re,con);
	}
	
	return re;
}

int minm(){
	int B[MAX] = {};
	vector<vector<int>> vec(2,vector<int>((1<<m)+10,0));
	vector<int> used(1<<m,0);
	
	
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			B[i] = B[i]*2 + (int)(b[i][j] - '0');
		}
		// cout<<"m = "<<m<<" "<<B[i]<<endl;;
	}
	
	used[0] = true;
	for(int i = 0; i < n; i++){
		// cout<<i<<" "<<" "<<vec[i][0]<<endl;
		for(int j = 0; j < (1<<m); j++){
			if(!used[j]) continue;
			// cout<<B[i]<<endl;
			vec[(i+1)%2][j^B[i]] = max(vec[(i)%2][j^B[i]], vec[(i)%2][j]+1);
			vec[(i+1)%2][j] = max(vec[(i+1)%2][j], vec[(i)%2][j]);
		}
		for(int j = 0; j < (1<<m); j++){
			if(!used[j]) continue;
			used[j^B[i]] = true;
			vec[(i)%2][j] = 0;
		}
	}
	
	// cout<<"return "<<endl;
	
	return vec[n%2][0];
}
int solve(){
	
	if(n <= 20){
		return minn();
	} else {
		return minm();
	}
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	while(true){
		
		cin>>n>>m;
	
		if(!n && !m) break;
		
		for(int i = 0; i < n; i++){
			cin>>b[i];
		}
	
		cout<<solve()<<endl;
	
	}
	return 0;
}


Measured memory_bytes: 216128
n, m = (int(i) for i in input().split())
h = list(map(int,input().split()))
A = []
B = []
for i in range(m):
    a, b = (int(i) for i in input().split())
    A.append(a)
    B.append(b)

x = [True]*n

for i in range(m):
    if(h[A[i]-1] >= h[B[i]-1]):
        x[B[i]-1] = False
    if(h[A[i]-1] <= h[B[i]-1]):
        x[A[i]-1] = False
ans = 0
for i in x:
    if(i):
        ans += 1
print(ans)


Measured memory_bytes: 22256
// includes
#include <cstdio>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <utility>
#include <functional>
#include <cmath>
#include <climits>
#include <bitset>
#include <list>
#include <random>

// macros
#define ll long long int
#define pb emplace_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;
 
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1e9 + 7;

// solve
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}

ll R, C;

bool comp(const P &a, const P &b){
  return a.first > b.first;
}

int main(int argc, char const* argv[])
{
  cin >> R >> C;
  int n;
  cin >> n;
  set<P> st;
  vector<P> vec[4];
  rep(i, n){
    int a[4];
    rep(j, 4)cin >> a[j];
    st.insert(mk(a[0], a[1]));
    st.insert(mk(a[2], a[3]));
    if(!(a[0] == 0 || a[0] == R || a[1] == 0 || a[1] == C))continue;
    if(!(a[2] == 0 || a[2] == R || a[3] == 0 || a[3] == C))continue;
    if(a[0] == 0){
      vec[3].pb(mk(a[1], i));
    }else if(a[0] == R){
      vec[1].pb(mk(a[1], i));
    }else if(a[1] == 0){
      vec[0].pb(mk(a[0], i));
    }else{
      vec[2].pb(mk(a[0], i));
    }
    if(a[2] == 0){
      vec[3].pb(mk(a[3], i));
    }else if(a[2] == R){
      vec[1].pb(mk(a[3], i));
    }else if(a[3] == 0){
      vec[0].pb(mk(a[2], i));
    }else{
      vec[2].pb(mk(a[2], i));
    }
  }
  if(sz(st) != 2 * n){
    cout << "NO" << endl;
    return 0;
  }
  sort(all(vec[0]));
  sort(all(vec[1]));
  sort(all(vec[2]), comp);
  sort(all(vec[3]), comp);
  stack<int> s;
  rep(j, 4){
    rep(i, sz(vec[j])){
      if(sz(s) == 0 || s.top() != vec[j][i].second){
        s.push(vec[j][i].second);
      }else{
        s.pop();
      }
    }
  }
  if(sz(s) == 0)cout << "YES" << endl;
  else cout << "NO" << endl;
	return 0;
}


Measured memory_bytes: 11628
#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < (long long)n; i++)
//#define _GLIBCXX_DEBUG
using namespace std;
using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using Vl = vector<ll>;
using vi = vector<int>;
using Graph = vector<vi>;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(15); //"for float-base
  // problem"
  ll A, B, C, K;
  cin >> A >> B >> C >> K;
  if (K < A)
    cout << K << endl;
  else if (K <= A + B)
    cout << A << endl;
  else
    cout << A - (K - A - B) << endl;
}

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

using namespace std;

#define pb push_back
#define mp make_pair
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
typedef long long LL;
typedef pair<int, int> PII;

int n, m;
int from[300000], to[300000];
int cnt[100001] = {};

vector<int> who[100001];

vector<int> q[100001];

int fen[100001] = {};

void fenInc(int pos) {
    for (; pos <= m; pos |= pos + 1) {
        ++fen[pos];
    }
}

int fenGet(int pos) {
    if (pos > m) {
        pos = m;
    }
    int res = 0;
    for (; pos >= 0; pos = (pos & (pos + 1)) - 1) {
        res += fen[pos];
    }
    return res;
}

int main() {
    scanf("%d%d", &n, &m);
    forn(i, n) {
        scanf("%d%d", from + i, to + i);
    }
    for (int i = 1; i <= m; ++i) {
        for (int j = 0; j <= m; j += i) {
            q[j].pb(i);
        }
    }
    forn(i, n) {
        who[from[i]].pb(to[i]);
    }
    forn(i, m + 1) {
        for (int x : who[i]) {
            fenInc(x);
        }
        for (int x : q[i]) {
            cnt[x] += fenGet(i + x - 1) - fenGet(i - 1);
        }
    }
    for (int i = 1; i <= m; ++i) {
        printf("%d\n", cnt[i]);
    }
    return 0;
}


Measured memory_bytes: 19192
#include<bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i,n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(),v.end()
#define puts(i) cout << i << endl
#define INF INT_MAX;
#define INFL LLONG_MAX;
typedef long long ll;
using namespace std;

int main(){
  int64_t n; cin >> n;
  int64_t ans =0;
  for(int i=1;i<=n;i++){
    ans += i-1;
  }

  cout << ans << endl;
}


Measured memory_bytes: 256
package main

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

var sc = bufio.NewScanner(os.Stdin)

func read() string {
	sc.Scan()
	return sc.Text()
}

var n int
var s string

func main() {
	fmt.Scan(&n)
	s = read()
	rs := []rune(s)
	max := 0
	for l1 := 0; l1 < n-1; l1++ {
		z := Zalgo(rs[l1:])
		for l2 := l1+1; l2 < n; l2++ {
			length := z[l2-l1]
			if l1 + length <= l2 && l2 <= n-length+1 && max < length {
				max = length
			}
		}
	}
	fmt.Println(max)
}


func Zalgo(rs []rune) []int {
	n := len(rs)
	Z := make([]int, n)
	Z[0] = n
	i := 1
	j := 0
	for i < n {
		for i+j < n && rs[j] == rs[i+j] {
			j++
		}
		Z[i] = j
		if j == 0 {
			i++
			continue
		}
		k := 1
		for k < j && k+Z[k] < j {
			Z[i+k] = Z[k]
			k++
		}
		i += k
		j -= k
	}
	return Z
}


Measured memory_bytes: 10752
#include<bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(0);
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define endl '\n'

using namespace std;

main()
{
    fast;
    int a,b,k,i;
    cin>>a>>b>>k;
    a=__gcd(a,b);
    priority_queue<int> pq;
    for (i=1;i<=sqrt(a);i++)
    {
        if (a%i==0)
        {
            if (i*i==a)
                pq.push(i);
            else
            {
                pq.push(i);
                pq.push(a/i);
            }
        }
    }
    k--;
    while (k)
    {
        pq.pop();
        k--;
    }
    cout<<pq.top()<<endl;
}

Measured memory_bytes: 256
#include <stdio.h>
#include <vector>
#include <queue>
#include <algorithm>
#include <iostream>
#include <string>
#include <bitset>
#include <map>
#include <set>
#include <tuple>
#include <string.h>
#include <math.h>
#include <random>
#include <functional>
#include <assert.h>
#include <math.h>
#include <iterator>
#include <chrono>
#include <stack>
#define MOD 1000000007
#define all(x) (x).begin(), (x).end()
#define xx first
#define yy second
#define PI 3.1415926535

using namespace std;

using i64 = long long int;
using ii = pair<int, int>;
using ii64 = pair<i64, i64>;

struct Study
{
	i64 hour;
	i64 reduce;
	i64 b, l, u;

	bool operator<(const Study& r) const
	{
		return reduce * r.hour < r.reduce* hour;
	}
};

int main()
{
	i64 n, x;
	scanf("%lld %lld", &n, &x);

	vector<Study> study(n + 1);
	i64 need = 0;

	for (int i = 1; i <= n; i++)
	{
		scanf("%lld %lld %lld", &study[i].b, &study[i].l, &study[i].u);
		study[i].hour = x;
		study[i].reduce = study[i].l * study[i].b + study[i].u * (x - study[i].b);
		need += study[i].l * study[i].b;
	}

	sort(study.begin() + 1, study.end());

	vector<i64> reduceSum(n + 2);

	for (int i = 1; i <= n; i++)
		reduceSum[i] += reduceSum[i - 1] + study[i].reduce;

	reduceSum[n + 1] = reduceSum[n];

	i64 hour = 0;
	i64 lo = 0, hi = x * n;

	while (lo <= hi)
	{
		i64 mid = (lo + hi) / 2;
		i64 remain = mid;
		i64 make = 0;
		i64 c = remain / x;
		i64 t = remain % x;

		if (t == 0)
		{
			make = reduceSum[n] - reduceSum[n - c];
		}
		else
		{
			for (int i = 1; i <= n; i++)
			{
				//i 번째를 cut으로 쓰고, 나머지에서 최대한 큰거 고르기
				i64 now = 0;

				if (t >= study[i].b)
					now += study[i].l * study[i].b + study[i].u * (t - study[i].b);
				else
					now += study[i].l * t;

				if (i >= n - c)
				{
					now += reduceSum[n] - reduceSum[i];
					now += reduceSum[i - 1] - reduceSum[n - c - 1];
				}
				else
				{
					now += reduceSum[n] - reduceSum[n - c];
				}

				make = max(make, now);
			}
		}

		if (make >= need)
		{
			hour = mid;
			hi = mid - 1;
		}
		else
		{
			lo = mid + 1;
		}
	}

	printf("%lld\n", hour);

	return 0;
}

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


def divisor(N):
    from math import sqrt
    D = []
    for i in range(1, int(sqrt(N)) + 1):
        if N % i == 0:
          D.append(i)
          D.append(N // i)

    return list(set(D))

s = sum(A)
divisors = divisor(s)
divisors.sort(reverse=True)

pride = 0
tention = 0
TKM = []
for d in divisors:
    for i, a in enumerate(A):
        l = -(a % d)
        h = d - (a % d)
        TKM.append((h, l))

    hekinan = sorted(TKM, key=lambda shisho: abs(shisho[0]))
    if abs(hekinan[0][0]) <= abs(hekinan[-1][1]):
        pride += abs(hekinan[0][0])
        tention += hekinan[0][0]
        hekinan = hekinan[1:]
    else:
        pride += abs(hekinan[-1][1])
        tention += hekinan[-1][1]
        hekinan = hekinan[:-1]
    while len(hekinan) != 0:
        if tention >= 0:
            pride += abs(hekinan[-1][1])
            tention += hekinan[-1][1]
            hekinan = hekinan[:-1]
        else:
            pride += abs(hekinan[0][0])
            tention += hekinan[0][0]
            hekinan = hekinan[1:]

    if pride // 2 <= K:
        print(d)
        break
    pride = 0
    tention = 0
    TKM = []



Measured memory_bytes: 3192
#include <bits/stdc++.h>
#define int long long
#define N (int) 3e5
using namespace std;
int n;
int col[N];
int num[N];	
struct Edge {
	int v, nxt;
} edge[N<<1];
int fst[N], ecnt;
void add_edge (int u, int v) {
	edge[ecnt].v = v, edge[ecnt].nxt = fst[u], fst[u] = ecnt ++;
}
void init_e () {
	memset (fst, -1, sizeof (fst));
}

int cnt[N], ans[N], sz[N];
void dfs (int u, int f) {
	sz[u] = 1;
	int org = cnt[col[u]];
	for (int e = fst[u]; e != -1; e = edge[e].nxt) {
		int v = edge[e].v;
		if (v == f) continue;
		int tmp = cnt[col[u]];
		dfs (v, u);
		int tmpp = sz[v] - (cnt[col[u]] - tmp);
		ans[col[u]] += (tmpp - 1) * tmpp >> 1;
		sz[u] += sz[v];
	}
	
	cnt[col[u]] = org + sz[u];
}
signed main () {
	init_e ();
	
	cin >> n;
	for (int i = 1; i <= n; i ++) cin >> col[i], num[col[i]] ++;
	for (int i = 1; i < n; i ++) {
		int u, v; cin >> u >> v;
		add_edge (u, v), add_edge (v, u);
	}
	
	dfs (1, 0);
	
	int tot = n * (n-1) >> 1;
	for (int i = 1; i <= n; i ++) {
		int tmp = n - cnt[i];
		if (! tmp) continue;
		ans[i] += tmp * (tmp - 1) >> 1;
	}
	for (int i = 1; i <= n; i ++) cout << (tot - ans[i] + num[i]) << endl;
}

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

int main() {
  int N, T, A;
  cin >> N >> T >> A;
  vector<int> h(N);
  for (int i=0; i<N; ++i) cin >> h[i];
  double idx = -1, best = 1e8;
  for (int i=0; i<N; ++i) {
    if (best > abs(A - (T - h[i]*0.006))) {
      best = abs(A - (T - h[i]*0.006));
      idx = i;
    }
  }
  cout << idx+1 << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
namespace IO
{
    const int __S=(1<<20)+5;char __buf[__S],*__H,*__T;
    inline char getc()
    {
        if(__H==__T) __T=(__H=__buf)+fread(__buf,1,__S,stdin);
        if(__H==__T) return -1;return *__H++;
    }
    template <class __I>inline void read(__I &__x)
    {
        __x=0;int __fg=1;char __c=getc();
        while(!isdigit(__c)&&__c!='-') __c=getc();
        if(__c=='-') __fg=-1,__c=getc();
        while(isdigit(__c)) __x=__x*10+__c-'0',__c=getc();
        __x*=__fg;
    }
    inline void readd(double &__x)
    {
        __x=0;double __fg=1.0;char __c=getc();
        while(!isdigit(__c)&&__c!='-') __c=getc();
        if(__c=='-') __fg=-1.0,__c=getc();
        while(isdigit(__c)) __x=__x*10.0+__c-'0',__c=getc();
        if(__c!='.'){__x=__x*__fg;return;}else while(!isdigit(__c)) __c=getc();
        double __t=1e-1;while(isdigit(__c)) __x=__x+1.0*(__c-'0')*__t,__t=__t*0.1,__c=getc();
        __x=__x*__fg;
    }
    inline void reads(char *__s,int __x)
    {
        char __c=getc();int __tot=__x-1;
        while(__c<'!'||__c>'~') __c=getc();
        while(__c>='!'&&__c<='~') __s[++__tot]=__c,__c=getc();
        __s[++__tot]='\0';
    }
    char __obuf[__S],*__oS=__obuf,*__oT=__oS+__S-1,__c,__qu[55];int __qr;
    inline void flush(){fwrite(__obuf,1,__oS-__obuf,stdout);__oS=__obuf;}
    inline void putc(char __x){*__oS++ =__x;if(__oS==__oT) flush();}
    template <class __I>inline void print(__I __x)
    {
        if(!__x) putc('0');
        if(__x<0) putc('-'),__x=-__x;
        while(__x) __qu[++__qr]=__x%10+'0',__x/=10;
        while(__qr) putc(__qu[__qr--]);
    }
    inline void prints(const char *__s,const int __x)
    {
        int __len=strlen(__s+__x);
        for(int __i=__x;__i<__len+__x;__i++) putc(__s[__i]);
    }
    inline void printd(double __x,int __d)
    {
        long long __t=(long long)floor(__x);print(__t);putc('.');__x-=(double)__t;
        while(__d--)
        {
            double __y=__x*10.0;__x*=10.0;
            int __c=(int)floor(__y);
            putc(__c+'0');__x-=floor(__y);
        }
    }
    inline void el(){putc('\n');}inline void sp(){putc(' ');}
}using namespace IO;
#define ll long long
map<ll,ll>iv,cnt;ll a,p[100005];int n,vis[100005],tot,ans,gg;
int main()
{
    for(int i=2;i<=100000;i++)
    {
        if(!vis[i]) p[++tot]=i;
        for(int j=1;j<=tot&&i*p[j]<=100000;j++){vis[i*p[j]]=1;if(i%p[j]==0) break;}
    }
    read(n);for(int i=1;i<=n;i++)
    {
        read(a);ll t=a,val=1,inv=1;
        for(int j=1;j<=tot&&1ll*p[j]*p[j]*p[j]<=a;j++) if(t%p[j]==0)
        {
            int mi=0;while(t%p[j]==0) t/=p[j],mi++;mi%=3;
            for(int i=1;i<=mi;i++) val*=p[j];for(int i=1;i<=(3-mi)%3;i++) inv*=p[j];
        }
        if(t>1){val*=t;ll sq=sqrt(t);if(sq*sq==t) inv*=sq;else inv*=t*t;}
        if(val==1) gg=1;else cnt[val]++,iv[inv]=val,iv[val]=inv;
    }
    for(map<ll,ll>::iterator it=cnt.begin();it!=cnt.end();++it)
    {
        if(cnt.find(iv[it->first])==cnt.end()) ans+=it->second;
        else ans+=max(it->second,cnt[iv[it->first]]),it->second=0,cnt[iv[it->first]]=0;
    }
    printf("%d\n",ans+gg);
}

Measured memory_bytes: 20480
#include <functional>
#include <iostream>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#include <string>
#include <cstring>
#include <math.h>
using namespace std;

int main() {
   int n;
   int m;
   int a[10010][2];
  
   int x = 0;
   int y = 0;
  
   while(cin >> n){
     
     cin >> m;
     
     
     a[x][0]=n;
     a[y][1]=m;
     
     x++;
     y++;
   }
  
     x=0;
     y=0;
   
   while(1){
     
    
     if(a[x][0]==0&&a[x][1]==0){
     
        break;
       
     }else{
       if(a[x][0]<a[x][1]){
         
         cout << a[x][0] << " "<<a[x][1]<< endl;
         
         
       }else{
         cout << a[x][1] << " "<<a[x][0]<< endl;
       }
       
     x++;
     y++;
      
     }
     
     
   }
  
 

}


Measured memory_bytes: 3068
from collections import deque
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
b = 0
c = deque()
c.append(a[0])
for i in range(1, n):
    c.append(a[i])
    c.append(a[i])
    b += c.popleft()
print(b)

Measured memory_bytes: 32424
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
int main() {
  int a,b; cin >> a >> b;
  char A[a][a],B[b][b];
  for(int i=0;i<a;i++){
    for(int j=0;j<a;j++){
      cin >> A[i][j];
    }
  }
  for(int i=0;i<b;i++){
    for(int j=0;j<b;j++){
      cin >> B[i][j];
    }
  }
  string ans="No";
  for(int i=0;i<(a-b+1);i++){
    for(int j=0;j<(a-b+1);j++){
      if(A[i][j]==B[0][0]){
        bool judge=true;
        for(int k=0;k<b;k++){
          for(int l=0;l<b;l++){
            if(A[i+k][j+l]!=B[k][l]) judge=false;
          }
        }
        if(judge) {ans="Yes"; break;}
      }
    }
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 256
#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>;

string reverse_copy_string(string s) {
    string t;
    int n = s.size();
    for (int i = n-1; i >= 0; i--) {
        t.push_back(s[i]);
    }
    return t;
}

int main() {
    int n, k;
    cin >> n >> k;
    string s, t, u;
    cin >> s;
    t = reverse_copy_string(s);
    u = s + t;
    rep(i,min(14, k)) {
        t = u.substr(0,n);
        rep(j,n-1) {
            if (t > u.substr(j+1,n)) { 
                t = u.substr(j+1,n);
            }
        }
        s = reverse_copy_string(t);
        u = s + t;
    }
    cout << t << endl;
}


Measured memory_bytes: 384
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define reps(i,a,b) for(int i=a;i<b;++i)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<typename T> inline bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T> inline bool chmin(T &a,T b){if(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U &u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U &u,const V... v){for(auto &e:u)fill_v<T>(e,v...);}

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

    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    rep(i, n) cin >> a[i], a[i]--;
    vector<int> sum(n+1);
    rep(i, n) sum[i+1] = (sum[i] + a[i]) % k;
    ll ans = 0;
    map<int, ll> mp;
    rep(i, n+1) {
        ans += mp[sum[i]];
        mp[sum[i]]++;
        if (i-k+1 >= 0) mp[sum[i-k+1]]--;
    }
    cout << ans << endl;
}


Measured memory_bytes: 17164
#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define dbg(x) cerr << #x << " = " << x << endl
#define _ << ' ' <<
using namespace std;
using ll = long long;
using vi = vector<int>;


int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int a, b;
    cin >> a >> b;
    if (b % a == 0)
        cout << a + b;
    else
        cout << b - a;
}


Measured memory_bytes: 256
#include <iostream>
#include <bitset>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <regex>
using namespace std;
using ll = long long int;
using dd = long double;
const ll MOD = 1e9 + 7;

vector<string> dfs(string s, ll depth, vector<char> symbol)
{
    vector<string> ret;
    if (depth == 0)
    {
        ret.push_back(s);
        return ret;
    }

    for (ll i = 0; i < symbol.size(); i++)
    {
        auto vec = dfs(s + symbol[i], depth - 1, symbol);
        for (auto &&e : vec)
        {
            ret.push_back(e);
        }
    }
    return ret;
}

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

int main()
{
    ll N, A, B;
    cin >> N >> A >> B;
    cout << min(N * A, B) << endl;
}

Measured memory_bytes: 256
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <sstream>
#include <algorithm>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <list>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <climits>
#include <bitset>
#include <functional>
#include <numeric>
#include <ctime>
#include <cassert>
#include <cstring>
#include <fstream>

#define FOR(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define IFOR(i, a, b) for(int (i)=(a);(i)<=(b);(i)++)
#define RFOR(i, a, b) for(int (i)=(a);(i)>=(b);(i)--)

using namespace std;

int main() {
	string s;
	cin >> s;
	int cnt = 0;
	bool white = s[0] == 'W';
	FOR(i, 0, s.size()) {
		bool x = s[i] == 'W';
		if (white != x)
			cnt++;
		white = x;
	}
	cout << cnt << endl;
	return 0;
}

Measured memory_bytes: 640
#include<iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main(){

  int N;
  cin >> N;
  string s[N];
  int last[N];

  int ans = 0;

  for(int i=0;i<N;i++){
    cin >> s[i];
    last[i] = s[i].size() - 1;
    for(int j=0;j<last[i]-1;j++){
      if(s[i][j]=='A' && s[i][j+1]=='B') ans++;
    }
  }

  int cnt_ba = 0;
  int cnt_b = 0;
  int cnt_a = 0;

  for(int i=0;i<N;i++){
    if(s[i][0]=='B'){
      if(s[i][last[i]]=='A') cnt_ba++;
      else cnt_b++;
    }else if(s[i][last[i]]=='A'){
      cnt_a++;
    }
  }
  //cout << cnt_a << " " << cnt_b << " " << cnt_ba <<endl;
  if(cnt_a==0 && cnt_b==0){
    if(cnt_ba>=1) ans += cnt_ba-1;
  }
  else ans += cnt_ba + min(cnt_a,cnt_b);

  cout << ans <<endl;

  
  return 0;
}

Measured memory_bytes: 896
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
const int inf = 1001001001;



//2910201
//size = 4,5,6,7,8,9,....s.size();
//serch_times = n-3, n-4, n-5, ...1= O(n^2)
//for(int k = 4; k<n; k++)
//for(i = 0; i < n; i++)
//for(j=i; j<=k; j++)


//200000/2019 ~ 100; 

//x -> x-1 + x-2 + x -3 + ......1 = x*(x-1)/2

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

  int n = s.size();
  vector<int> val(2019,0);
  int c = 0;
  int fac = 1;
  val[c]++;
  rep(i,n){
    c += (s[n-1-i] - '0')*fac;
    c %= 2019;
    val[c]++;
    fac *= 10;
    fac %= 2019;
  }

  int ans = 0;
  rep(i,2019) ans += val[i]*(val[i]-1)/2;
  cout << ans << endl;
}


Measured memory_bytes: 3720
/*
ID: varunra2
LANG: C++
TASK: buffet
*/
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <limits>
#include <list>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <utility>
#include <vector>

using namespace std;

#define EPS 1e-9
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert(B <= A && A <= C)
#define INF (int)1e9
#define MEM(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define MP make_pair
#define PB push_back
#define PI 3.1415926535897932384626433832795
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()

typedef pair<int, int> PII;
typedef vector<int> VI;

const double pi = acos(-1.0);
typedef long int int32;
typedef long long int int64;
typedef map<int, int> MPII;
typedef multiset<int> MSETI;
typedef set<int> SETI;
typedef set<string> SETS;
typedef unsigned long int uint32;
typedef unsigned long long int uint64;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<string> VS;

#define watch(x) cerr << (#x) << " is " << (x) << endl
#define printvii(v)                                    \
  cerr << (#v) << "=";                                 \
  for_each(v.begin(), v.end(), [](PII i) -> void {     \
    cerr << "(" << i.first << "," << i.second << "),"; \
  });                                                  \
  cerr << endl;
#define printvi(v)                                                       \
  cerr << (#v) << "=";                                                   \
  for_each(v.begin(), v.end(), [](int i) -> void { cerr << i << ","; }); \
  cerr << endl;

int main(int argc, char const *argv[]) {

  int n;
  cin >> n;

  VI vals(n);
  VI inp(n - 1);
  for(int i = 0; i < n - 1; i++) {
    cin >> inp[i];
  }

  vals[0] = inp[0];
  vals[n - 1] = inp[n - 2];
  for(int i = 1; i < n - 1; i++) {
    vals[i] = min(inp[i], inp[i - 1]);
  }

  cout << accumulate(vals.begin(), vals.end(), 0) << endl;

  return 0;
}


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

int main(void) {
    int64_t N;
    cin >> N;
    unordered_set<string> prizes;
    string prize;

    for(int i=0;i<N;i++) {
        cin >> prize;
        prizes.insert(prize);
    }

    cout << prizes.size() << "\n";
    return 0;
}


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

int main() {
  int x, y;
  cin >> x >> y;
  int ans = 0;
  for (int i = 1; i <= x; i++) {
    if (2 * i + 4 * (x - i) == y ||
      4 * i + 2 * (x - i) == y) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
}

Measured memory_bytes: 3596
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;
  cin >> N;
  int ans = 0;
  for(int i = 0; i < N; i++){
    int l;
    int r;
    cin >> l >> r;
    ans += r - l + 1;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 3584
import math

N, X = map(int, input().split())
x= list(map(int, input().split()))

absx = [abs(X - xx) for xx in x]
absx.sort()

divx0 = []

for i in range(math.ceil(math.sqrt(absx[0])), 0, -1):
    if absx[0] % i == 0:
        divx0.append(i)

t = []
for x in divx0:
    t.append(x)
    t.append(absx[0] // x)

divx0 = t
divx0.sort(reverse=True)

for dx0 in divx0:
    isok = True
    for xx in absx:
        if xx % dx0 != 0:
            isok = False
            break

    if isok:
        print(dx0)
        exit()

Measured memory_bytes: 20788
n = gets.to_i
x, l = n.times.map { gets.split.map(&:to_i) }.transpose
a = []
n.times do |i|
  a << [x[i] - l[i], x[i] + l[i]]
end
a.sort_by! { |ai| ai[1] }

ans = 0
t = -Float::INFINITY
n.times do |i|
  if t <= a[i][0]
    ans += 1
    t = a[i][1]
  end
end
p ans


Measured memory_bytes: 16432
while True:
    a, op, b = input().rstrip().split()
    if op == "?":
        break
    if op == "+":
        print(int(a) + int(b))
    elif op == "-":
        print(int(a) - int(b))
    elif op == "*":
        print(int(a) * int(b))
    elif op == "/":
        print(int(a) // int(b))
        


Measured memory_bytes: 5596
#include<bits/stdc++.h>
#define fi first
#define se second
#define Mp make_pair
#define pb push_back
#define rep(i, j, k) for (int i = (j); i <= (k); i++)
#define per(i, j, k) for (int i = (j); i >= (k); i--)
using namespace std;

typedef long long ll;
typedef double db;
typedef pair<int, int> PII;
typedef vector<int> VI;

const int N = 503;
int n,pr[1500],v[10003],tot,cnt;
ll ans[N][N];
map<PII,ll> ch;

void init(){
    for(int i = 2; i <= 10000; i++){
        if(!v[i]){
            pr[++tot]=i;
            v[i]=i;
        }
        for(int j = 1; j <= tot && i*pr[j] <= 10000; j++){
            v[pr[j]*i]=pr[j];
            if(v[i]==pr[j]) break;
        }
    }
}
ll getval(int x,int y){
    ll ret=1;
    int stx[4]={1,-1,0,0},sty[4]={0,0,1,-1};
    auto valid=[&](int x,int y){
        return 1<=x&&x<=n&&1<=y&&y<=n;
    };
    for(int i = 0; i < 4; i ++){
        int nx=x+stx[i],ny=y+sty[i];
        if(!valid(nx,ny)) continue;
        ret=ret/__gcd(ret,ans[nx][ny])*ans[nx][ny];
    }
    return ret+1;
}
int main(){
    //freopen("my.out","w",stdout);
    scanf("%d",&n);
    if(n==2){
        printf("4 7\n23 10\n");
        return 0;
    }
    init();
    for(int i = 2; i <= n+n; i+=2) ch[Mp(i,0)]=pr[++cnt];
    for(int i = 0,j = 0; i < n; i+=2,j-=2) ch[Mp(i,1)]=pr[++cnt],ch[Mp(j,1)]=pr[++cnt];
    //puts("1");
    //for(auto it:ch) printf("%d %d %d\n",it.fi.fi,it.fi.se,it.se);
    rep(i,1,n) rep(j,1,n) if((i+j)%2==0){
        ans[i][j]=ch[Mp(i+j,0)]*ch[Mp(i-j,1)];
    }
    //rep(i,1,n) rep(j,1,n) printf("%lld%c",ans[i][j],j==n?'\n':' ');
    rep(i,1,n) rep(j,1,n) if((i+j)%2==1){
        ans[i][j]=getval(i,j);
    }
    // printf("%d\n",n);
    rep(i,1,n) rep(j,1,n) printf("%lld%c",ans[i][j],j==n?'\n':' ');
    return 0;
}

Measured memory_bytes: 5248
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;
map<char,int> m;
int H,W;
int main(){
    cin>>H>>W;
    for(int i{};i<H;++i)
        for(int j{};j<W;++j){
            char c;cin>>c;
            ++m[c];
        }
    vector<int> v,w;
    for(auto& x:m){
        int l{x.second};
        while(l>4)w.push_back(4),l-=4;
        if(l)w.push_back(l);
    }
    sort(w.begin(),w.end());
    for(int i{};i<(H+H%2)/2;++i){
        for(int j{};j<(W+W%2)/2;++j)
            v.push_back((W%2&&(j==(W+1)/2-1)?1:2)*(H%2&&(i==(H+1)/2-1)?1:2));
    }
    sort(v.begin(),v.end());
    if(v.size()<w.size()){
        cout<<"No"<<endl;
        return 0;
    }
    while(w.size()<v.size()){
        int p{w.back()};
        if(p!=4)break;
        w.erase(next(w.begin(),w.size()-1));
        w.insert(w.begin(),2);
        w.insert(w.begin(),2);
    }
    sort(w.begin(),w.end());
    cout<<(v==w?"Yes":"No")<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define syosu(x) fixed<<setprecision(x)
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> P;
typedef pair<double,double> pdd;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<string> vs;
typedef vector<P> vp;
typedef vector<vp> vvp;
typedef vector<pll> vpll;
typedef pair<int,P> pip;
typedef vector<pip> vip;
const int inf=1<<29;
const ll INF=1ll<<60;
const double pi=acos(-1);
const double eps=1e-9;
const ll mod=1e9+7;
const int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int n,m;

int main(){
	cin>>n>>m;
	vi a(m),b(m);
	for(int i=0;i<m;i++){
		cin>>a[i]>>b[i];
	}
	for(int i=0;i<1000;i++){
		auto s=to_string(i);
		bool B=1;
		if(s.size()!=n) continue;
		for(int j=0;j<m;j++) if(s[a[j]-1]-'0'!=b[j]) B=0;
		if(B){
			cout<<i<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
}

Measured memory_bytes: 256