akhauriyash/Code-Regression huggingface dataset:
H, W = map(int, input().split())
A = []  #交差点毎の人数
for _ in range(H):
	A.append(list(map(int, input().split())))
#print(A)

mins, temp = 0, 0
for h in range(H):  #幹線道路:縦
	for w in range(W):  #幹線道路:横
		temp = 0
		for hs in range(len(A)):
			for ws in range(len(A[hs])):
				temp += A[hs][ws]*min(abs(hs-h), abs(ws-w))
		if mins==0 or mins>temp:
			mins = temp

print(mins)



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

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmin (T &a, T b) {return a > b ? a = b, 1 : 0; }
template <typename T> inline int Chkmax (T &a, T b) {return a < b ? a = b, 1 : 0; }
inline int read ()
{
	int sum = 0, fl = 1; char ch = getchar();
	for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
	for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
	return sum * fl;
}

const int Maxn = 1e5 + 100;

int N, f[2][Maxn];

struct graph
{
	int e, Begin[Maxn], To[Maxn << 1], Next[Maxn << 1];
	int size[Maxn], vis[Maxn];

	inline void add_edge (int x, int y) { To[++e] = y; Next[e] = Begin[x]; Begin[x] = e; }

}G[2], A;

inline void dfs (int x, int op)
{
	for (int i = G[op].Begin[x]; i; i = G[op].Next[i])
	{
		int y = G[op].To[i];
		dfs (y, op);
		G[op].size[x] ^= 1;
	}
}

int root[2];

inline void Check ()
{
	for (int op = 0; op <= 1; ++op)
	{
		for (int i = 1; i <= N; ++i) if (f[op][i] != -1) G[op].add_edge (f[op][i], i); else root[op] = i;
		dfs(root[op], op);
	}
	for (int i = 1; i <= N; ++i) if (G[0].size[i] != G[1].size[i]) {puts("IMPOSSIBLE"); exit(0);}
	puts("POSSIBLE");
}

inline void dfs_connect (int x, int op)
{
	if (!G[op].size[x]) G[op].vis[x] = x;
	for (int i = G[op].Begin[x]; i; i = G[op].Next[i])
	{
		int y = G[op].To[i];
		dfs_connect (y, op);
		if (G[op].vis[x])
		{
			A.add_edge (G[op].vis[x], G[op].vis[y]);
			A.add_edge (G[op].vis[y], G[op].vis[x]);
			G[op].vis[x] = 0;
		}
		else G[op].vis[x] = G[op].vis[y];
	}
}

int Ans[Maxn];

inline void dfs_calc (int x, int w)
{
	Ans[x] = w, A.vis[x] = 1;
	for (int i = A.Begin[x]; i; i = A.Next[i])
	{
		int y = A.To[i];
		if (!A.vis[y]) dfs_calc (A.To[i], w ^ 1);
	}
}

inline void Solve ()
{
	Check();
	for (int i = 0; i <= 1; ++i) dfs_connect (root[i], i);
	for (int i = 1; i <= N; ++i)
		if (!G[0].size[i] && !A.vis[i])
			dfs_calc (i, 1);
	for (int i = 1; i <= N; ++i)
		if (G[0].size[i]) printf("0 ");
		else printf("%d ", Ans[i] ? 1 : -1);
}

inline void Input ()
{
	N = read();
	for (int j = 0; j <= 1; ++j) for (int i = 1; i <= N; ++i) f[j][i] = read();
}

int main()
{
	Input();
	Solve();
	return 0;
}


Measured memory_bytes: 10752
import java.util.*

fun main(args: Array<String>) {
    val cin = Scanner(System.`in`)
    val A = cin.nextInt()
    val B = cin.nextInt()
    val C = cin.nextInt()
    val X = cin.nextInt()

    var ans = 0
    for(a in 0..A) {
        for(b in 0..B) {
            for(c in 0..C) {
               val x =  500*a + 100*b + 50*c
                if(x == X) ans++
            }
        }
    }
    println(ans)
}

Measured memory_bytes: 31264
if isfile("mystdin.txt");mystdin=open("mystdin.txt","r");redirect_stdin(mystdin);end
pI(x)=parse(Int,x)
pM(x::Array{SubString{String},1})=map(pI,x)
function main()
  h,w=pM(split(readline()))
  c=[chomp(readline()) for i=1:h]
  for i in c
    println(i)
    println(i)
  end
end
main()
if isdefined(Base, :mystdin);close(mystdin);end

Measured memory_bytes: 112516
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>

using namespace std;
typedef pair<int, int> Pi;
typedef long long ll;
#define pii Pi
#define pll PL
#define Fi first
#define Se second
#define pb(x) push_back(x)
//#define sz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
typedef tuple<int, int, int> t3;
typedef pair<ll, ll> pll;
typedef long double ldouble;
typedef pair<double, double> pdd;

const double PI = acos(-1);
const double EPS = 1e-9;

const int MX = 100005;
const int MM = 1000000007;

int main()
{
	int N, A, B;
	scanf("%d%d%d", &N, &A, &B);
	if( A > B ) printf("0\n");
	else if( N == 1 && A != B ) printf("0\n");
	else if( N <= 2 ) printf("1\n");
	else printf("%lld\n", (ll)(B-A) * (N-2) + 1);
}


Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
 
int main() {
    int n,a,b;
    cin>>n;
    a=n/4;
    b=n/7;
    string ans="No";
    for(int i=0;i<=a;i++){
      for(int j=0;j<=b;j++){
        if(i*4+j*7==n){
          ans="Yes";
        }
      }
    }
  cout<<ans<<endl;     
}


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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)   FOR(i,0,n)
#define ll long long
const int INF = 1e9 + 7;

using namespace std;

int main(){
    cin.tie(0), ios::sync_with_stdio(false);
    int x,y,ans=0;
    cin >> x >> y;
    FOR(i,x,y+1){
        if(i/10000 == i%10){
            int a=i/1000,b=i%100;
            if(a%10 == b/10){
                ++ans;
            }
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
n = int(input())

F = []
P = []
for i in range(n):
    F.append(list(map(int, input().split())))
for i in range(n):
    P.append(list(map(int, input().split())))
ans = -10**9
for i in range(1, 1 << (10)):
    C = [0]*n
    for j in range(10):
        if i & 2**j:
            for k in range(n):
                if F[k][j]:
                    C[k] += 1
    current = 0
    for k in range(n):
        current += P[k][C[k]]
    ans = max(current, ans)
print (ans)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll, int> plli;
typedef pair<int, pii> pipii;
typedef vector<vector<int> > mati;
typedef vector<vector<double> > matd;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;
typedef vector<bool> vb;
typedef vector<vector<bool>> vvb;
typedef vector<vector<vector<bool>>> vvvb;
typedef vector<pll> vpll;

#define FOR(i,x,y) for(ll i=(ll)x; i<(ll)y; ++i)
#define REP(i,y) FOR(i, 0, y)
#define RFOR(i,x,y) for(ll i=(ll)x; i>=(ll)y; --i)
#define RREP(i,x) RFOR(i, x, 0)
#define ALL(a) a.begin(), a.end()
#define pb push_back

inline void IN(void){
  return;
}

template <typename First, typename... Rest>
void IN(First& first, Rest&... rest){
  cin >> first;
  IN(rest...);
  return;
}

inline void OUT(void){
  cout << "\n";
  return;
}

template <typename First, typename... Rest>
void OUT(First first, Rest... rest){
  cout << first << " ";
  OUT(rest...);
  return;
}

constexpr int INF = (1<<30);
constexpr ll INFLL = 1LL<<62;
constexpr long double EPS = 1e-12;
constexpr ll MOD = (ll)((1E+9)+7);

int main(){
  ll H, W, A, B;
  IN(H,W,A,B);

  ll flag = 0;

  vvll s(H, vll(W, 0));
  REP(i,H){
    if(i==B) flag = 1;
    REP(j,A){
      s[i][j] = 1-flag;
    }
    FOR(j,A,W){
      s[i][j] = flag;
    }
  }

  REP(i,H){
    REP(j,W){
      printf("%lld", s[i][j]);
    }
    printf("\n");
  }

  return 0;
}

Measured memory_bytes: 9088
use std::io;

fn main() {
    let mut input_text = String::new();
    io::stdin()
        .read_line(&mut input_text)
        .expect("Failed to read");

    let v:Vec<i32> = input_text
        .trim()
        .split(' ')
        .map(|val| val.parse().unwrap())
        .collect();

    if v[0] == v[1] {
        println!("{}", v[2]);
    } else if v[1] == v[2] {
        println!("{}", v[0]);
    } else {
        println!("{}", v[1]);
    }
}


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

int main(void) {
    int a,b;
    cin >> a;
    b = a % 100;
    a /= 100;
    if(a> 12 || a == 0) {
        if(b > 12 || b == 0){
            cout << "NA\n";
        } else {
            cout << "YYMM\n";
        }
    } else {
        if(b > 12 || b == 0){
            cout << "MMYY\n";
        } else {
            cout << "AMBIGUOUS\n";
        }
    }
    return 0;
}


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


int h, w;
vector<string> ma; //二次元配列ではなく文字列のベクトル


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

int main() {
    cin >> h >> w;
    ma.resize(h); //ベクトルのサイズを変更
    rep(i,h) cin >> ma[i];

    // 多点をスタートとして扱う
    vector<vector<int> > dist(h, vector<int>(w, -1)); //一番近い黒からの距離,-1で初期化
    queue<P> que;
    rep(i,h){
        rep(j,w){
            if (ma[i][j] == '#') {
                dist[i][j] = 0; 
                que.push(P(i, j)); //まずすべての黒の位置をキューに入れておく。これで多点スタート
            }
        }
    }

    while (!que.empty()) {
        auto cur = que.front(); //別にintでもよい
        que.pop();
        for (int i = 0; i < 4; i++) {
            int ny = cur.first + dy[i];
            int nx = cur.second + dx[i];
            if (nx < 0 || nx >= w || ny < 0 || ny >= h) continue;
            if (dist[ny][nx] == -1) {
                dist[ny][nx] = dist[cur.first][cur.second] + 1;
                que.push(P(ny, nx));
            }
        }
    }
    int res = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            res = max(res, dist[i][j]);
        }
    }
    cout << res << endl;
}

Measured memory_bytes: 13456
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
    string s;
    cin >> s;
    for(int i = s.size() - 1; i >= 0; i--){
        if(s[i] == 'm' && i >= 4){
            if(s.substr(i-4, 5) == "dream"){
                i -= 4;
                continue;
            }else{
                cout << "NO" << endl;
                return 0;
            }
        }else if(s[i] == 'e' && i >= 4){
            if(s.substr(i-4, 5) == "erase"){
                i -= 4;
                continue;
            }else{
                cout << "NO" << endl;
                return 0;
            }
        }else if(s[i] == 'r'){
            if(i >= 6){
                if(s.substr(i-5, 6) == "eraser"){
                    i -= 5;
                    continue;
                }else if(s.substr(i-6, 7) == "dreamer"){
                    i -= 6;
                    continue;
                }else{
                    cout << "NO" << endl;
                    return 0;
                }
            }else if(i == 5){
                if(s.substr(i-5, 6) == "eraser"){
                    i -= 5;
                    continue;
                }else{
                    cout << "NO" << endl;
                    return 0;
                }
            }else{
                cout << "NO" << endl;
                return 0;
            }
        }else{
            cout << "NO" << endl;
        }
    }
    cout << "YES" << endl;
}

Measured memory_bytes: 512
n,m=map(int,input().split())
s = [list(map(int,input().split())) for i in range(n)]
p = [[0,10**18] for i in range(n)]
for j in range(m):
  c,d=map(int,input().split())
  for k in range(n):
    if p[k][1]>(abs(c-s[k][0])+abs(d-s[k][1])):
      p[k][0]=j+1
      p[k][1]=(abs(c-s[k][0])+abs(d-s[k][1]))
for i in range(n):
  print(p[i][0])

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

int main(){
    int n,m;
    cin >> n >> m;
    vector <int> p(m);
    vector <string> s(m);
    rep(i,m){
        cin >> p[i] >> s[i];
    }

    vector <int> ac(n+1,0);
    int pena,sei;
    pena=0;
    sei=0;
    rep(i,m){
        if (s[i]=="AC"){
            if (ac[p[i]]!=-1){
            pena+=ac[p[i]];
            ac[p[i]]=-1;
            ++sei;

            }
        }
        else{
            if(ac[p[i]]!=-1){
                ++ac[p[i]];
            }
            
        }
    }
    cout << sei << " " << pena << endl;

    return 0;
}

Measured memory_bytes: 6528
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define KK 500
#define N 100005
#define MXN 200005
vector<int> prime;
int isprime[100005];
void Find(){
    for(int i = 2;i<3005;i++){
        if(isprime[i]==0){
            prime.pb(i);
            for(int j =i*2;j<3005;j+=i)
                isprime[j]=1;
        }
    }
}
int main(){
    Find();
    //printf("%d\n",prime.size());
    int n;
    scanf("%d",&n);
    map<pll,int> m;
    for(int i = 0;i<n;i++){
        LL x;
        scanf("%lld",&x);
        LL a=1,b=1;
        for(auto &it:prime){
            if(it>x)break;
            int cnt=0;
            while(x%it==0){
                x/=it;
                cnt++;
            }
            cnt%=3;
            if(cnt==1){
                a=a*it;
                b=b*it;
                if(b>1e10)b=1e10+1;
                b=b*it;
                if(b>1e10)b=1e10+1;
            }
            else if(cnt==2){
                a=a*it*it;
                b=b*it;
            }
        }
        LL tmp=sqrt(x);
        if(tmp*tmp==x){
            b*=tmp;
            a*=x;
        }
        else{
            a*=x;
            if(x>1e5)
            b=1e10+1;
            else{
                b*=x;
                if(b>1e10)b=1e10+1;
                else b*=x;
            }
        
        }
        //if(x!=1){
        //a=x,b=1e10+1;
        //}
        m[mp(a,b)]++;
    }
    int ans=0,ans2=0,ans3=0;
    for(auto &it:m){
        pll p=it.x;
        swap(p.x,p.y);
        if(it.x==mp(1ll,1ll)){
            ans3=1;
        }
        else if(m.find(p)!=m.end()){
            ans+=max(it.y,m[p]);
            //printf("%d %d %d %d\n",p.x,p.y,it.y,m[p]);
        }
        else{
            ans2+=it.y;
        }
    }
   // printf("%d %d %d\n",ans,ans2,ans3);
    printf("%d\n",ans/2+ans2+ans3);
}


Measured memory_bytes: 6528
#include<bits/stdc++.h>
#include<string.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 yesno(flg) if(flg){cout<<"YES"<<endl;}else{cout<<"NO"<<endl;}
#define MAX_N 1002
#define i197 1000000007

using namespace std;

typedef long long ll;
typedef pair<ll,ll> P1;
typedef pair<int,int> Pi;
typedef pair<double,Pi> Pdi;
typedef pair<ll,int>Pli;
typedef pair<P1,ll> P2;
const ll INF=1<<29;
struct edge{int to,cost;};
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};
struct Road{double cost;int a,b;};

int main() {

	string s;
	cin>>s;
	if(s=="Sunny")cout<<"Cloudy"<<endl;
	if(s=="Cloudy")cout<<"Rainy"<<endl;
	if(s=="Rainy")cout<<"Sunny"<<endl;

	return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {
    double a, b, x;
    cin >> a >> b >> x;
    cout << fixed;
    cout << setprecision(6);
    if (x > a*a*b/2.0) {
        cout << atan(2.0*(a*a*b-x)/(a*a*a)) * 180 / M_PI << endl;
    } else {
        cout << atan(a*b*b/(2*x)) * 180 / M_PI << endl;
    }
}

Measured memory_bytes: 3956
//https://atcoder.jp/contests/abc086/tasks/abc086_a

#include <iostream>
using namespace std;

int main() {
    int a, b;
    cin >> a >> b;
    int c = a * b;
    if (c % 2 == 0) cout << "Even" << endl;
    else cout << "Odd" << endl;
}

Measured memory_bytes: 256
#pragma region _head

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<iomanip>
#include<queue>
#include<stack>
#include<numeric>
#include<utility>
#include<regex>

void Init() {
	std::cin.tie(0); std::ios::sync_with_stdio(false);
	struct Init_caller { Init_caller() { Init(); } }caller;
}

#pragma region _define

#define int LL
#define f(i,a,b) for(LL i=a;i<b;i++)
#define rep(i,n) for(LL i=0;i<n;i++)
#define f_vI(v,n) f(i,0,n)cin>>v[i]
#define f_v2I(v1,v2,n) f(i,0,n)cin>>v1[i]>>v2[i]
#define f_v3I(v1,v2,v3,n) f(i,0,n)cin>>v1[i]>>v2[i]>>v3[i]
#define f_vO(v,n) f(i,0,n)cout<<v[i]<<endl
#define ei else if
#define all(a) a.begin(),a.end()
#define size(s) ((LL)s.size())
#define F first
#define S second
#define check() cout<<"! ! !"
#define endl "\n"
#define _y() cout<<"Yes"<<endl
#define _Y() cout<<"YES"<<endl
#define _n() cout<<"No"<<endl
#define _N() cout<<"NO"<<endl
#define INT_INF 1<<29
#define LL_INF 1LL<<60
#define MOD 1000000007

#pragma endregion

#pragma region _using

using namespace std;

using LL = long long;
using st = string;
using vi = vector<LL>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vd = vector<double>;
using vvd = vector<vd>;
using vvvd = vector<vvd>;
using vc = vector<char>;
using vvc = vector<vc>;
using vs = vector<st>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
using qi = queue<LL>;
using qc = queue<char>;
using qs = queue<st>;
using si = stack<LL>;
using sc = stack<char>;
using ss = stack<st>;
using pi = pair<LL, LL>;
using ppi = pair<pi, LL>;
using mll = map<LL, LL>;
using mcl = map<char, LL>;
using vpi = vector<pi>;
using vppi = vector<ppi>;


#pragma endregion

struct edge {
	int from, to, cost;
};

void y_n(bool p) {
	p ? _y() : _n();
}

void Y_N(bool p) {
	p ? _Y() : _N();
}

LL vmax(vi v, LL n) {
	int MAX = 0;
	f(i, 0, n) {
		MAX = max(MAX, v[i]);
	}
	return MAX;
}

LL vmin(vi v, LL n) {
	int MIN = LL_INF;
	f(i, 0, n) {
		MIN = min(MIN, v[i]);
	}
	return MIN;
}

LL gcd(LL a, LL b) {
	LL r;
	while ((r = a % b) != 0) {
		a = b;
		b = r;
	}
	return b;
}
LL lcm(LL a, LL b) {
	return (a / gcd(a, b) * b);
}

//素数判定
bool is_prime(int n) {
	for (int i = 2; i*i <= n; i++) {
		if (n % i == 0) {
			return false;
		}
	}
	return true;
}

//エラトステネスの篩
void sieve(LL n) {
	vb s(n + 1, true);
	f(i, 2, n + 1) {
		for (int j = 2; i*j <= n; j++) {
			s[i*j] = false;
		}
	}
	cout << "2から" << n << "までの素数" << endl;
	f(i, 2, n + 1) {
		if (s[i]) {
			cout << i << " ";
		}
	}
}

//階乗計算
LL factorial(LL n) {
	LL a = 1, ret = 1;
	while (a < n) {
		a++;
		ret *= a;
		ret %= MOD;
	}
	return ret;
}


#pragma endregion 


/*****************************************************************************/

signed main() {
	int n;
	cin >> n;
	int i;
	while (n != 0) {
		for (i = 1; i * (i + 1) / 2 < n; i++);
		cout << i << endl;
		n -= i;
	}
	return 0;
}

Measured memory_bytes: 256
#include <map>
#include <list>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define fast ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
using namespace std;
int n,a,b;
int main()
{
	fast;
	cin>>n>>a>>b;
	cout<<min(n*a,b);
    return 0;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;

class Myon
{
    public Myon() { }
    public static int Main()
    {
        new Myon().calc();
        return 0;
    }

    Scanner cin;


    void calc()
    {
        cin = new Scanner();
        int N = cin.nextInt();
        Console.WriteLine(N*N*N);
    }


}
class Scanner
{
    string[] s;
    int i;
    char[] cs=new char[]{' '};
    public Scanner()
    {
        s=new string[0];
        i=0;
    }
    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }
    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }
    public long nextLong()
    {
        return long.Parse(next());
    }
    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }
    public double nextDouble()
    {
        return double.Parse(next());
    }
    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}


Measured memory_bytes: 18028
#include <array>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <ctime>
#include <iterator>
#include <bitset>
#include <numeric>
#include <list>
#include <iomanip>
using namespace std;


typedef long long LL;
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
	v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
	stringstream ss;
	ss << f;
	ss >> t;
}


#define REP(i,n) for(int i=0;i<int(n);++i)
#define ALL(v) (v).begin(),(v).end()
#define RALL(v) (v).rbegin(),(v).rend()
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define PB push


#define MOD 1000000007LL
#define EPS 1e-10

int main()
{
    int n;
    cin >> n;
    int numAlphabet[26];
    REP(i, 26) numAlphabet[i] = 100;
    REP(i, n)
    {
        string S;
        cin >> S;
        int tmp[26];
        REP(i, 26) tmp[i] = 0;
        for (char c : S)
        {
            ++tmp[c - 'a'];
        }
        REP(i, 26) numAlphabet[i] = min(numAlphabet[i], tmp[i]);
    }
    string ans = "";
    REP(i, 26)
    {
        REP(j, numAlphabet[i])
        {
            ans += 'a' + i;
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <list>

using namespace std;
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
typedef long long ll;

int main() {
    int n;

    ll ans = 0;
    cin >> n;
    vector<ll> a(n), b(n), c(n);
    rep(i, n) cin >> a[i];
    rep(i, n) cin >> b[i];
    rep(i, n) cin >> c[i];
    sort(a.begin(), a.end());
    sort(c.begin(), c.end());

/*
    rep(i, n) cout << a[i] << " ";
    cout << endl;
    rep(i, n) cout << b[i] << " ";
    cout << endl;
    rep(i, n) cout << c[i] << " ";
    cout << endl;
    */


    rep(i, n) {
        auto ai = lower_bound(a.begin(), a.end(), b[i]);
        auto ci = upper_bound(c.begin(), c.end(), b[i]);

        ll ac = ai - a.begin();
        ll cc = c.end() - ci;
        //cout << ac << " : " << cc << endl;
        ans += ac * cc;
    }

    cout << ans << endl;
}


Measured memory_bytes: 2560
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()

using namespace std;

typedef long long ll;

static const int INTINF = (2147483647);
static const ll LLINF = (9223372036854775807);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;

namespace Printer{
    template<class T>
    void print(T x){
        cout << x << endl;
    }

    template<class T>
    void print(T x, T y){
        cout << x << "," << y << endl;
    }

    template<class T>
    void print(vector<T> v){
        rep(i, v.size()){
            if(i) cout << " ";
            cout<< v[i];
        }
        cout << endl;
    }

    template<class T>
    void print(vector<vector<T> > vv){
        rep(i, vv.size()){
            rep(j, vv[i].size()){
                if(j) cout << " ";
                cout << vv[i][j];
            }
            cout << endl;
        }
    }

    void print(pair<int, int> p){
        cout << "(" << p.first << ", " << p.second << ")" << endl;
    }
};
using namespace Printer;

//--global--//



//----------//

ll search(vector<ll> t, ll val){
    if(t.empty()) return -1;
    ll l = -1;
    ll r = t.size();
    ll ans = -1;
    while(r - l >1){ 
        ll mid = (r + l) / 2;
        if(t[mid] >= val){
            r = mid;
            ans = mid;
        }
        else{
            l = mid;
        }
    }

    return t[ans];
}

int main(int argc, const char * argv[]) {
    //提出時、消す----//
    //--------------//

    string s, t;
    cin >> s >> t;
    int n = int(s.size());
    s = s + s;
    vector<vector<ll> > order(1000);
    rep(i, s.size()){
        order[s[i]].push_back(i+1);
    }
    ll bef = 0;
    ll cnt = 0;
    rep(i, t.size()){
        if(order[t[i]].empty()){
            cout << -1;
            return 0;
        }

        ll x = *upper_bound(ALL(order[t[i]]), bef);

        bef = x;
        if (bef >= n) {
            cnt += n;
            bef -= n;
        }
    }
    cnt += bef;
    //print(order['s']);
    print(cnt);
    
    
    return 0;
}


Measured memory_bytes: 2804
n = list(map(int, input().split()))
x = list(map(int, input().split()))
y = list(map(int, input().split()))
result = True

if max(x) < min(y):
     # print("max(x)=", max(x))
     # print('min(y)=', min(y))
     range(max(x), min(y))
     
     for i in range(max(x)+1, min(y)+1):
          if i > max(x) and i <= min(y):
               if i > n[2] and i <= n[3]:
                    print("No War")
                    result = False
                    break
     if result:
          print("War")
else:
     print("War")


Measured memory_bytes: 3064
import os

x,n = (int(x) for x in input().split())
p = [int(i) for i in input().split()]

if x not in p:
    print(x)
    exit()

mini = 0
min = 1000
for b in range(-200,200):
    if min>abs(x-b):
        if b not in p:
            min = abs(x-b)
            mini = b

print(mini)

Measured memory_bytes: 9196
S = raw_input()
if ((S.find("N")>=0 and S.find("S")>=0 or S.find("N")<0 and S.find("S")<0) and
    (S.find("E")>=0 and S.find("W")>=0 or S.find("E")<0 and S.find("W")<0)):
	print "Yes"
else:
	print "No"


Measured memory_bytes: 2572
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define pb push_back
#define fi first
#define se second
const int N=1e6+10;
const int mod7=1e9+7;
const int mod=998244353;
void read(int &a)
{
    a=0;int d=1;char ch;
    while(ch=getchar(),ch>'9'||ch<'0')
        if(ch=='-')
            d=-1;
    a=ch^48;
    while(ch=getchar(),ch>='0'&&ch<='9')
        a=(a<<3)+(a<<1)+(ch^48);
    a*=d;
}
void read(ll &a)
{
    a=0;int d=1;char ch;
    while(ch=getchar(),ch>'9'||ch<'0')
        if(ch=='-')
            d=-1;
    a=ch^48;
    while(ch=getchar(),ch>='0'&&ch<='9')
        a=(a<<3)+(a<<1)+(ch^48);
    a*=d;
}
ll solve(ll x) {return x*x*x*x*x;}
int main()
{
    ll x;read(x);
    for(re int i=-1000;i<=1000;i++)
    {
        for(re int j=-1000;j<=1000;j++)
        {
            if(solve(i)-solve(j)==x)
            {
                return cout<<i<<" "<<j,0;
            }
        }
    }
}

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

long digitCount(long x) {
  long ans = 0;
  do {
    x /= 10;
    ans++;
  } while (0 < x);
  return ans;
}

int main() {
  long N;
  cin >> N;
  long B, ans = digitCount(N), sN = ceil(sqrt(N));
  for (long A = 2; A <= sN; A++) {
    if (N % A != 0) {
      continue;
    }
    B = N / A;
    long tmp = max(digitCount(A), digitCount(B));
    if (tmp < ans) {
      ans = tmp;
    }
  }
  cout << ans << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int n,m,sum[30][30],max1,tail;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
char ch[30][30];
struct hzy{
	int x,y;
}q[1001];
void bfs(int x,int y){
	memset(sum,-1,sizeof(sum));
	sum[x][y]=0;
	tail=1;
	q[1].x=x;
	q[1].y=y;
	for(int i=1;i<=tail;i++){
		int xx=q[i].x;
		int yy=q[i].y;
		for(int j=0;j<4;j++){
			int xxx=xx+dx[j];
			int yyy=yy+dy[j];
//			cout<<xxx<<' '<<yyy<<endl;
			
			if(xxx>=1&&yyy>=1&&xxx<=n&&yyy<=m&&sum[xxx][yyy]<0&&ch[xxx][yyy]=='.'){
//				cout<<xxx<<' '<<yyy<<endl;
		
				sum[xxx][yyy]=sum[xx][yy]+1;
				max1=max(max1,sum[xxx][yyy]);
				q[++tail].x=xxx;
				q[tail].y=yyy;
			}
		}
	}
//	for(int i=0;i<=n;i++){
//		for(int j=0;j<=m;j++){
//			cout<<sum[i][j]<<' ';
//		}
//		cout<<endl;
//	}
//	cout<<tail<<endl;
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>ch[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			if(ch[i][j]=='.'){
				bfs(i,j);
			}
		}
	}
	cout<<max1;
	return 0;
}


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

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

Measured memory_bytes: 256
n,m=map(int,input().split())
abc=[list(map(int,input().split())) for i in range(m)]

inf=float("inf")
data=[-inf]*(n+1)
data[1]=0
for i in range(n):
    for a,b,c in abc:
        if data[a]!=inf:
            data[b]=max(data[b],data[a]+c)

lst=data[:]
for i in range(n):
    for a,b,c in abc:
        if data[a]!=inf:
            data[b]=max(data[b],data[a]+c)

que=[]
for i in range(1,n+1):
    if lst[i]!=data[i]:
        que.append(i)
        
flag=[0]*(n+1)
for u in que:
    flag[u]=1

li=[[] for i in range(n+1)]
for a,b,c in abc:
    li[a].append(b)

while que:
    h=que.pop()
    for u in li[h]:
        if flag[u]==0:
            flag[u]=1
            que.append(u)

if flag[n]==1:
    print("inf")
else:
    print(data[n])

Measured memory_bytes: 48344
#include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <queue>
using namespace std;

int main(){
    int N;
    cin >> N;
    pair<long long,int> *px,*py;
    vector<pair<long long,int>> *edge;
    px=new pair<long long,int>[N];
    py=new pair<long long,int>[N];
    edge=new vector<pair<long long,int>>[N];

    for(int i=0;i<N;i++){
        long long x,y;
        cin >> x >> y;
        px[i]=make_pair(x,i);
        py[i]=make_pair(y,i);
    }
    sort(px,px+N);
    sort(py,py+N);

    for(int i=1;i<N;i++){
        int a=px[i-1].second,b=px[i].second;
        long long c=px[i].first-px[i-1].first;
        edge[a].push_back(make_pair(c,b));
        edge[b].push_back(make_pair(c,a));

        a=py[i-1].second; b=py[i].second;
        c=py[i].first-py[i-1].first;
        edge[a].push_back(make_pair(c,b));
        edge[b].push_back(make_pair(c,a));
    }

    long long d=0;
    bool *chk;
    chk=new bool[N];
    for(int i=0;i<N;i++) chk[i]=false;

    priority_queue<pair<long long,int>> Q;
    chk[0]=true;
    for(auto e : edge[0]){
        Q.push(make_pair(-e.first,e.second));
    }

    while(!Q.empty()){
        auto t=Q.top();
        Q.pop();

        if(chk[t.second]) continue;
        d-=t.first;
        chk[t.second]=true;
        for(auto e : edge[t.second]){
            Q.push(make_pair(-e.first,e.second));
        }
    }

    cout << d << endl;
    
    delete[] px,py,edge,chk;
    return 0;
}

Measured memory_bytes: 22892
#include <iostream>
#include <string>
#include <cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<iomanip>
#define _USE_MATH_DEFINES
#include <math.h>
#include <functional>
#include<complex>
using namespace std;

#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)

typedef long long ll;
const ll INF = 1e17;
const ll MOD = 1000000007;
const ll MAX = 4000001;
const double eps = 1E-18;

ll max(ll a, ll b) {
	if (a > b) { return a; }
	return b;
}

ll min(ll a, ll b) {
	if (a > b) { return b; }
	return a;
}

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

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

struct edge {
	ll ind;
	ll fr;
	ll to;
	ll d;
};

class mint {
	long long x;
public:
	mint(long long x = 0) : x((x% MOD + MOD) % MOD) {}
	mint operator-() const {
		return mint(-x);
	}
	mint& operator+=(const mint& a) {
		if ((x += a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator-=(const mint& a) {
		if ((x += MOD - a.x) >= MOD) x -= MOD;
		return *this;
	}
	mint& operator*=(const  mint& a) {
		(x *= a.x) %= MOD;
		return *this;
	}
	mint operator+(const mint& a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint& a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint& a) const {
		mint res(*this);
		return res *= a;
	}

	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime MOD
	mint inv() const {
		return pow(MOD - 2);
	}
	mint& operator/=(const mint& a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint& a) const {
		mint res(*this);
		return res /= a;
	}

	friend ostream& operator<<(ostream& os, const mint& m) {
		os << m.x;
		return os;
	}
};

mint pw(mint a, ll b) {
	if (b == 0) { return 1; }
	mint ret = pw(a, b >> 1);
	ret *= ret;
	if (b & 1) { ret *= a; }
	return ret;
}

typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<vector<vector<ll>>> vvvll;

typedef vector<mint> vmint;
typedef vector<vector<mint>> vvmint;
typedef vector<vector<vector<mint>>> vvvmint;

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

void ad(vll& basis, ll x) {

	for (ll y : basis) {
		ll z = y & (-y);
		if ((x & z) == z) { x=x^y; }
	}
	if (x != 0) { basis.push_back(x); }
}

bool judge(vll& basis, ll x) {
	for (ll y : basis) {
		ll z = y & (-y);
		if ((x & z) == z) { x=x^y; }
	}
	if (x == 0) { return true; }
	else { return false; }
}

int main() {
	ll T;
	cin >> T;

	repn(t, T) {
		ll N;
		cin >> N;

		vll A(N);
		rep(i, N) cin >> A[i];
		string S;
		cin >> S;

		vll basis;

		ll ans = 0;

		for (ll i = N - 1; i >= 0; i--) {
			if (S[i] == '0') { ad(basis, A[i]); }
			else {
				bool b = judge(basis, A[i]);
				if (!b) { ans = 1; }
			}

			//for (ll x : basis) { cout << x << " "; }
			//cout << endl;

		}

		cout << ans << endl;
	}
}


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

int main(){
    string deck, ans = "";
    int m, h, deck_number;
    while(1){
        cin >> deck;
        if(deck == "-") break;
        cin >> m;
        deck_number = deck.size();

        for(int i = 0; i < m; i++){
            cin >> h;
            deck = deck.substr(h, deck_number - h) + deck.substr(0, h);
        }
        ans += deck + "\n"; 
    }
    ans.erase(ans.end()-1);
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 3220
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define all(v) v.begin(),v.end()

using namespace std;
typedef long long ll;




int main()
{
    
    int N;
    string S;
    const ll mod = 1000000007;
    
    cin >> N >> S;
    
    vector<ll> dp(26, 0);
    
    rep(i,N) dp[S[i]-'a']++;
    ll ans = 1;
    rep(i, 26) {
        ans *= ((dp[i]+1)%mod);
        ans %= mod;
        
    }
    cout << (ans + mod - 1)%mod << endl;

    //rep(i, 26) cout << char('a' + i) << " " << dp[i] << endl;
    return 0;
}



Measured memory_bytes: 512
n, m = map(int, input().split())
A = []
B = []
for i in range(n):
    x, y = map(int, input().split())
    A.append(x)
    B.append(y)

ans = 0
for price, amount in sorted(zip(A, B)):
    ans += price * min(m, amount)
    m -= amount
    if m <= 0:
        break
print(ans)

Measured memory_bytes: 64984
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>

#include <atcoder/string>

int main() {
  char buf[1048576];
  scanf("%s", buf);
  std::string s = buf;
  size_t n = s.length();

  auto sa = atcoder::suffix_array(s);
  auto lcp = atcoder::lcp_array(s, sa);

  intmax_t res = n-sa[0];
  for (size_t i = 1; i < n; ++i) {
    res += n-sa[i];
    res -= lcp[i-1];
  }

  printf("%jd\n", res);
}


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

using namespace std;

void f(long long n){
    n-=1;
    if(n<26){
        cout <<(char)('a' + n);
        return;
    }

    f(n/26);
    cout <<(char)('a' + n%26);
}

int main(){
    long long n;
    cin >>n;
    f(n);
}


Measured memory_bytes: 3588
#include <bits/stdc++.h>
#define rep(i,n) for (ll i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using ull = long long unsigned;
using P = pair<int,int>;
const int INF=1001001001;
const int NMAX=8;
const int mod = 1000000007;

int main()
{
  int n,m;
  cin>>n>>m;
  vector<vector<int>> J(100005);
  rep(i,n){
    int a,b;
    cin>>a>>b;
    if (a<=m) J[a].push_back(b);
  }
priority_queue <int> q;
int ans=0;
for(int i=1;i<=m;i++){
  for(auto j:J[i]){
    q.push(j);
  }
    if (!q.empty()){
      ans+=q.top();
      q.pop();
    }
  }
  cout<<ans;

}


Measured memory_bytes: 5632
#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

int main() {
	int L, R, d; cin >> L >> R >> d;
	int ans = 0;
	for (int i = L; i <= R; i++) {
		if (i % d == 0)ans++;
	}
	cout << ans << endl;
	return 0;
}



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

#define MOD 1000000007ll
#define all(v) v.begin() , v.end()
#define allr(v) v.rbegin(), v.rend()
#define for0(i,n) for(__typeof(n) i = 0; i < n ; i++)  
#define forc(c,it) for(__typeof(c.begin()) it = c.begin(); it != c.end(); it++)
#define pb     push_back
#define mp     make_pair
#define MAX 100005
template<typename T, template<typename ELEM, typename ALLOC=std::allocator<ELEM> > class Container>
std::ostream& operator<< (std::ostream& o, const Container<T>& container)
{ typename Container<T>::const_iterator beg = container.begin();
  /*o << "[";*/ while(beg != container.end()) o << " " << *beg++; /*o << " ]";*/ return o; }

string s1 , s2;

int main(int argc, char const *argv[])
{
  ios_base::sync_with_stdio(false);
 
  cin >> s1 >> s2;

  int siz1 = s1.size();
  int siz2 = s2.size();

  if(siz1 > siz2)
  {
  	cout << "GREATER";
  	return 0;
  }	
  else if(siz1 < siz2)
  {
  	cout << "LESS";
  	return 0;
  }	
  else
  {
  	for(int i = 0 ; i < siz1 ; i++)
  	{
  		if(s1[i] > s2[i])
  		{
  			cout << "GREATER";
  			return 0;
  		}
  		else if(s1[i] < s2[i])
  		{
  			cout << "LESS";
  			return 0;
  		}
  	}
  }

  cout << "EQUAL";

  return 0;
} 

Measured memory_bytes: 256
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
s = S()
ans = 0
for i in range(1000):
    num = str(i).zfill(3)
    if s.find(num[0]) == -1:
        continue
    s1 = s[s.find(num[0])+1:]
    if s1.find(num[1]) == -1:
        continue
    s2 = s1[s1.find(num[1])+1:]
    if s2.find(num[2]) == -1:
        continue
    ans+=1
print(ans)



Measured memory_bytes: 9304
#include <iostream>
#include <string.h>

using namespace std;
int main() {
	int x;
	int str[1001];
	cin >> x;
	for (int i = 0; i < x; i++) {
		cin >> str[i];
	}

	for (int i = x - 1; i >= 0; i--) {
		if(i!=0)	cout << str[i] << " ";
		else cout << str[i];

	}


	cout << endl;
	return 0;
}

Measured memory_bytes: 3064
from collections import deque
d = {
    'a': deque(input()),
    'b': deque(input()),
    'c': deque(input()),
}
q = deque(d['a'].popleft())
while q:
    x = q.popleft()
    if len(d[x]) != 0:
      y = d[x].popleft()
      q.append(y)
    else:
      print(x.upper())
      exit()

Measured memory_bytes: 3316
package main

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

const (
	initialBufSize = 10000
	maxBufSize     = 100000 + 1024
)

func main() {

	scanner := bufio.NewScanner(os.Stdin)
	buf := make([]byte, initialBufSize)
	scanner.Buffer(buf, maxBufSize)

	/* 1st line */
	var n, q int
	fmt.Scan(&n, &q)

	/* 2nd line */
	scanner.Scan()
	text := scanner.Text()

	/* Get ACs */
	ac := getAC(text)

	for i := 0; i < q; i++ {
		scanner.Scan()
		t := scanner.Text()
		l, _ := strconv.Atoi((strings.Split(t, " "))[0])
		r, _ := strconv.Atoi((strings.Split(t, " "))[1])
		fmt.Println(ac[l-1]-ac[r-1])
	}

}

func getAC(s string) []int {
	n := len(s)
	ac := make([]int, n)
	k := 0
	strRune := []rune(s)
	ac[n-1] = 0
	for i := n - 2; 0 <= i; i-- {
		if strRune[i] == 'A' {
			if strRune[i+1] == 'C' {
				k++
			}
		}
		ac[i] = k
	}
	return ac
}


Measured memory_bytes: 6144
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using ld = long double;
long double const pi = std::acos(-1.0L);
typedef pair<int,int> P;

int main() {
  int d, t, s;
  cin >> d >> t >> s;
  if(s*t>=d) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}

Measured memory_bytes: 3572
using System;
using System.Linq;

namespace ABC103C_cs
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            var a = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            Console.WriteLine(a.Sum() - N);
        }
    }
}


Measured memory_bytes: 13396
#include <stdio.h>

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

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

int main(void) {
	long a,b;

	while(scanf("%ld %ld",&a,&b)!=EOF) {
		printf("%ld %ld\n",gcd(a,b),lcm(a,b));
	}
	return 0;
}

Measured memory_bytes: 596
const calc = (a) => {
  let sum = 0;
  if (a < 10) {
    return a;
  } else {
    sum += 9;
  }

  if (a < 100) return sum;

  if (a < 1000) {
    sum += a - 100 + 1;
    return sum;
  } else {
    sum += 900;
  }

  if (a < 10000) return sum;

  if (a < 100000) {
    sum += a - 10000 + 1;
    return sum;
  } else {
    sum += 90000;
  }

  return sum;
}

// inputに入力データ全体が入る
function Main(input) {
	// 1行目がinput[0], 2行目がinput[1], …に入る
	input = input.split("\n");
	//文字列から10進数に変換するときはparseIntを使います
  const a = parseInt(input[0], 10);
  const result = calc(a);
  //出力
	console.log('%d',result);
}
//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）

Main(require("fs").readFileSync("/dev/stdin", "utf8"));



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

int main() {
	int a, b;
	cin >> a >> b;
	if (a <= 0 && 0 <= b) cout << "Zero";
	else if (b < 0 && (b - a & 1) == 0) cout << "Negative";
	else cout << "Positive";
	return 0;
}
/*
31536000のコメント解説欄
ここテンプレで用意してるから、A問題とかだとこの先空欄の危険あり
また、コンテスト後に https://31536000.hatenablog.com/ で解説していると思うので、良かったら読んでねー

まず、a ≦ 0 ≦ bならばZero, 0 < aならばPositive
最後にb < 0の時、答えはb-aが偶数ならNegative、それ以外はPositive
*/

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

int main(){
  int N;
  cin >> N;
  vector<long long> A(N+1), B(N);
  for(int i = 0; i < N + 1; i++) cin >> A.at(i);
  for(int i = 0; i < N; i++) cin >> B.at(i);
  long long count = 0;
  long long nokori = 0;
  for(int i = 0; i < N; i++){
    if(A.at(i) >= B.at(i)){
      count += B.at(i);
      B.at(i) = 0;
    }else{
      count += A.at(i);
      B.at(i) -= A.at(i);
    }
    if(A.at(i + 1) >= B.at(i)){
      count += B.at(i);
      A.at(i + 1) -= B.at(i);
    }else{
      count += A.at(i + 1);
      A.at(i + 1) = 0;
    }
  }
  cout << count << endl;
}

Measured memory_bytes: 1792
#include <bits/stdc++.h>
using namespace std;
int a,b,c,len;
int main()
{
    scanf("%d%d",&a,&b);
    c=b;
    while(c>0)
    {
        c/=10;
        len++;
    }
    a=a*pow(10,len)+b;
    if(sqrt(a)==floor(sqrt(a)))
    {
        printf("Yes");
        cout<<endl;
        return 0;
    }
    printf("No");
    cout<<endl;
    return 0;
}

Measured memory_bytes: 256
//#include "stdafx.h"
#include <iostream>
#include <set>
#include <queue>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <climits>
#include <sstream>
#include <iomanip>
#include <map>
#include <stack>
#include <tuple>
#include <numeric>
#include <assert.h>
#include <functional>
#include <unordered_map>

using namespace std;

/*-----------------------------------------------------------------------------
　定義
 -------------------------------------------------------------------------------*/
#define REP(i, n)				for (int (i) = 0 ; (i) < (int)(n) ; ++(i))
#define REPN(i, m, n)			for (int (i) = m ; (i) < (int)(n) ; ++(i))
#define INF						2e9
#define MOD						(1000 * 1000 * 1000 + 7)
#define Ceil(x, n)				(((((x))+((n)-1))/n))		/* Nの倍数に切り上げ割り算 */
#define CeilN(x, n)				(((((x))+((n)-1))/n)*n)		/* Nの倍数に切り上げ */
#define FloorN(x, n)			((x)-(x)%(n))				/* Nの倍数に切り下げ */
#define IsOdd(x)				(((x)&0x01UL) == 0x01UL)			
#define IsEven(x)				(!IsOdd((x)))						
#define	BitSetV(Val,Bit)		((Val) |= (Bit))			
#define	BitTstV(Val,Bit)		((Val) & (Bit))				
#define ArrayLength(x)			(sizeof( x ) / sizeof( x[ 0 ]))
#define	MAX_QWORD				((QWORD)0xFFFFFFFFFFFFFFFF)
#define M_PI					3.14159265358979323846
typedef long long				ll;
typedef unsigned long long int	QWORD;
typedef pair<ll, ll>			P;

/*-----------------------------------------------------------------------------
　処理
 -------------------------------------------------------------------------------*/
ll dfs(bool prevLineExist, int i, int num)
{
	if ((num <= 0)||(num < i)) {
		return 1;
	}

	if (i == num) {
		return 1;
	}

	ll cnt = 0;
	int next = i + 1;
	if (prevLineExist) {
		// 引けない
		cnt = dfs(false, next, num);
	} else {
		// 引く or 引かない
		cnt = dfs(true, next, num);
		cnt += dfs(false, next, num);
	}

	cnt %= MOD;
	return cnt;
}


int main()
{
	int H, W, K;
	cin >> H >> W >> K;

	vector<vector<ll>> dp(H + 1, vector<ll>(W, 0));
//	static ll dp[20][20];
	dp[0][0] = 1;
	REP(i, H) {
		REP(j, W) {
			if (0 < dp[i][j]) {
				ll sum;
				ll ptnCnt;
				ll ptnNoneL = dfs(false, 0, j - 1);
				ll ptnExistL = dfs(false, 0, j - 2);
				ll ptnNoneR = dfs(false, j + 1, W - 1);
				ll ptnExistR = dfs(false, j + 2, W - 1);

				// ひだり
				if (j != 0) {
					ptnCnt = (ptnExistL * ptnNoneR) % MOD;
					sum = (dp[i][j] * ptnCnt) % MOD;
					dp[i + 1][j - 1] += sum;
					dp[i + 1][j - 1] %= MOD;
				}
				
				// まんなか
				ptnCnt = (ptnNoneL * ptnNoneR) % MOD;
				sum = (dp[i][j] * ptnCnt) % MOD;
				dp[i + 1][j] += sum;
				dp[i + 1][j] %= MOD;
				
				// みぎ
				if (j != W - 1) {
					ptnCnt = (ptnNoneL * ptnExistR) % MOD;
					sum = (dp[i][j] * ptnCnt) % MOD;
					dp[i + 1][j + 1] += sum;
					dp[i + 1][j + 1] %= MOD;
				}
			}
		}
	}
	cout << dp[H][K-1] << endl;
	return 0;
}


Measured memory_bytes: 256
N = int(input())
P = [list(map(int, input().split())) for i in range(N)]
t = [0] * N
x = [0] * N
y = [0] * N
for i in range(N):
    t[i] = P[i][0]
    x[i] = P[i][1]
    y[i] = P[i][2]

can = True

t.insert(0, 0)
x.insert(0, 0)
y.insert(0, 0)

for i in range(N):
    dt = t[i+1] - t[i]
    dist = abs(x[i+1]-x[i]) + abs(y[i+1]-y[i])
    if dt < dist:
        can = False
        break
    if dt%2 == 0 and dist%2 == 0:
        continue
    elif dt%2 == 1 and dist%2 == 1:
        continue
    else:
        can = False
        break

if can == True:
    print('Yes')
elif can == False:
    print('No')

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

int main(){
  long long n,m;

  cin >> n >> m;

  if(n == 1 && m == 1){
    cout << 1 << endl;
  }
  else if(m == 1) {
    cout << n-2 << endl;
  }
  else if(n == 1){
    cout << m-2 << endl;
  }
  else {
    cout << (n*m)-(((n-1)*2)+(((m-1)*2))) << endl;
  }

  return 0;
}


Measured memory_bytes: 256