akhauriyash/Code-Regression huggingface dataset:
#include<stdio.h>
#include<stdlib.h>

struct gr{
	int fr;
	int to;
	long long co;
};

struct gr abc[200000];
int numb[100001];
long long dis[100001];
int n;

int compare_int(const void *a,const void *b){
	struct gr A=*(struct gr*)a,B=*(struct gr*)b;
	return A.fr-B.fr;
}

void dfs(int now,long long cost){
	dis[now]=cost;
	int i;
	for(i=0;abc[i+numb[now]].fr==now&&i<2*n-2;i++){
		if(dis[abc[i+numb[now]].to]<0)dfs(abc[i+numb[now]].to,cost+abc[i+numb[now]].co);
	}
	return;
}

int main(){
	int i,j;
	scanf("%d",&n);
	for(i=0;i<n-1;i++){
		int a,b;long long c;
		scanf("%d%d%lld",&a,&b,&c);
		abc[i*2].fr=a;
		abc[i*2].to=b;
		abc[i*2].co=c;
		abc[i*2+1].fr=b;
		abc[i*2+1].to=a;
		abc[i*2+1].co=c;
	}
	int q,k,xy[100000][2];
	scanf("%d%d",&q,&k);
	for(i=0;i<q;i++)scanf("%d%d",&xy[i][0],&xy[i][1]);
	qsort(abc,2*n-2,sizeof(struct gr),compare_int);
	for(i=1;i<2*n-2;i++){
		if(abc[i].fr!=abc[i-1].fr)numb[abc[i].fr]=i;
	}
	for(i=0;i<=n;i++)dis[i]=-1;
	dfs(k,0);
	for(i=0;i<q;i++)printf("%lld\n",dis[xy[i][0]]+dis[xy[i][1]]);
	return 0;
}

Measured memory_bytes: 11488
#include <bits/stdc++.h>
#define x first
#define y second
#define ll long long
#define pi pair<int,int>
#define pl pair<ll,ll>
#define pd pair<double,double>
#define ld long double
#define pld pair<ld,ld>
#define lg length()
#define sz size()
#define vi vector<int>
#define vl vector<ll>
#define vp vector<pi>
#define vpl vector<pl>
#define pb push_back
#define INF 1000000005
#define LINF 1000000000000000005
using namespace std;
vector <int> g[100001];
int dpmax;
int dp[100001];
int dpi(int nod){
    int r = 1;
    if(dp[nod])return dp[nod];
    for(int i = 0 ; i < g[nod].size() ; i++){
        dp[nod] = max(dp[nod],dpi(g[nod][i])+1);
    }
    dpmax = max(dpmax,dp[nod]);
    return dp[nod];
}
int a,b;
int n, m;
int main(){
    ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
    cin >> n >> m;
    for(int i = 1 ; i <= m; i++){
        cin >> a >> b;
        g[a].pb(b);
    }
    for(int i = 1;i <= n; i++){
        dpi(i);
    }
    cout << dpmax;
}

Measured memory_bytes: 8192
#include<vector>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <cmath>
#include<queue>
#include <sstream>
#include <set>
#include<stack>
#include <utility>

const long long MOD = 1000000007;
using namespace std;
typedef long long llong;

//int isalpha(char ch): ch がアルファベットなら true を返す
//int isdigit(char ch): ch が数字なら true を返す
//int islower(char ch): ch が小文字なら true を返す
//int isupper(char ch): ch が大文字なら true を返す
//int tolower(char ch): ch の小文字を返す
//int toupper(char ch): ch の大文字を返す

//string型
//size()	文字数を返す
//Insert()	（指定した場所に）文字・文字列を挿入する
//erase()	（指定した場所の）文字・文字列を削除する
//clear()	すべての文字を削除する
//substr()	文字列の（指定した）部分文字列を返す
//replace()	（指定した）部分文字列を新しい文字列に置換する
//c_str()変換
//文字列の比較は、<=や==などを使え
//replace関数を使い、簡単に文字列を置換
//リバース関数：reverse(str.begin(), str.end());
//map<type, type> dict;で宣言
//グラフ理論用変数
//vector<vector<llong> > graph(N);

//ソート
//降順sort(v.begin(), v.end(), std::greater<Type>());

//大文字から小文字へんかん
//w[i] = w[i]-'A'+'a';

//vector
//assignメソッド　引数：サイズ、値
//与えられたサイズと値でvectorを初期化する

//queueクラス
//find()次に取り出す値の表示をする。
//pop()値を取り出す。戻り値はなし
//push()キューに値をプッシュする

//priority_queueクラス

//切り上げ
//ceil
//floor

long long modpow(long long a, long long n, long long mod) {
    long long res = 1;
    while (n > 0) {
        if (n & 1) res = res * a % mod;
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

long long mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0) res += m;
  return res;
}

int main(){
    llong N;
    cin >> N;
    llong total=modpow(10, N, MOD);
    llong zero_num = mod(modpow(9, N, MOD)-modpow(8, N, MOD), MOD);
    total = mod(total-zero_num, MOD);
    total = mod(total-zero_num, MOD);
    total = mod(total-modpow(8, N, MOD), MOD);
    cout << total << endl;
    return 0;
}

Measured memory_bytes: 3160
#include<bits/stdc++.h>
using namespace std;
bool vis[1001][1001];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int h,w;
    cin >> h >> w;
    int mod=1e9+7;
    long long dp[h+1][w+1];
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            char c;
            cin>> c;
            if(c=='#'){
               vis[i][j]=1;
            }
        }
    }
    for(int i=1;i<=h;i++){
        if(vis[i][1]){
            break;
        }
        dp[i][1]=1;
    }
    for(int i=1;i<=w;i++){
        if(vis[1][i]) break;
        dp[1][i]=1;
    }
    for(int i=2;i<=h;i++){
        for(int j=2;j<=w;j++){
            if(!vis[i][j]){
                dp[i][j]=(dp[i-1][j]%mod+dp[i][j-1]%mod)%mod;
            }
        }
    }
    cout << dp[h][w] << endl;
}


Measured memory_bytes: 9088
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

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

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = (int)1e5 + 9;
const ll inf = (ll)1e18;
 
ll dp[N];
ll x[N];
 
int main(){
    fastIO;
    int n;
    ll w, e;
    cin >> n >> e >> w;
    int p=1;
    ll v1 = inf;
    priority_queue<pii, vector<pii>, greater<pii>> kk;
    for(int i = 1; i <= n; i ++ ){
        cin >> x[i];
        kk.push(mp(dp[i-1] - x[i - 1] + w, i));
        dp[i] = inf;
        while(2ll * (x[i] - x[p]) >= w){
            v1 = min(v1, dp[p-1] - 2ll * x[p] - x[p - 1]);
            p ++ ;
        }
        while(!kk.empty() && kk.top().se < p){
            kk.pop();
        }
        dp[i] = min(dp[i], 3ll * x[i] + v1);
        dp[i] = min(dp[i], x[i] + kk.top().fi);
    }
    cout << dp[n]+(e-x[n]);
    return 0;
}

Measured memory_bytes: 2548
import java.util.*;
class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String A = sc.next();
        String B = sc.next();
        String C = sc.next();
        char a = A.charAt(A.length() - 1);
        char b1 = B.charAt(0);
        char b2 = B.charAt(B.length() - 1);
        char c = C.charAt(0);
        if(a == b1 && b2 == c) {
            System.out.println("YES");
        }else {
            System.out.println("NO");
        }
    }
}

Measured memory_bytes: 21844
#include<iostream>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<algorithm>
#include<complex>
using namespace std ;
typedef vector<int> vi ;
typedef vector<vi> vvi ;
typedef vector<string> vs ;
typedef pair<int, int> pii; 
typedef long long ll ;

#define loop(i,a,b) for(int i = a ; i < b ; i ++)
#define rep(i,a) loop(i,0,a)
#define all(a) (a).begin(),(a).end()
int main(void){
  double r,l;
    vi re(4,0);
    vi le(4,0);
    while(cin>>r>>l){
    if(r>=1.1)re[0]++;
    else if(r>=0.6)re[1]++;
    else if(r>=0.2)re[2]++;
    else if(r>=0)re[3]++;

    if(l>=1.1)le[0]++;
    else if(l>=0.6)le[1]++;
    else if(l>=0.2)le[2]++;
    else if(l>=0)le[3]++;


  }
  
  rep(i,4)
    cout<<re[i]<<" "<<le[i]<<endl;

}

Measured memory_bytes: 1224
// This is the start of shubhams template//
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define int long long int
void input(){
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
}
#define deg(x) cout<<#x<<" = "<<x;
#define vll vector<int>
#define pb push_back
#define pll pair<int,int>
#define mp(a,b) make_pair(a,b)
#define ff first
#define ss second
#define INF LONG_LONG_MAX
#define mod 1000000007
#define all(x) x.begin(),x.end()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define cases int t; cin>>t; while(t--)
#define array(a,n) vll a(n); rep(i,0,n) cin>>a[i];
#define sortall(a) sort(a.begin(),a.end())
#define printall(a) rep(i,0,a.size()) cout<<a[i]<<" "; cout<<endl;
//this is the end of shubhams template //
int power(int a,int n);
void solve();
int32_t main()
{
	//input();
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    int n ,flag = 0;
    cin>>n;
    vector<pair<int,int>>a(n);
    for(int i = 0;i<n ;i++)
    {
    	cin>>a[i].first>>a[i].second;

    	if(i>=2 && a[i].first==a[i].second && a[i-1].first==a[i-1].second&&a[i-2].first==a[i-2].second)
    	flag = 1;
    }
    cout<<(flag?"Yes":"No");
}
int power(int a,int n)
{
    if(n==0)
        return 1;
    if(n%2!=0)
    {
        return (a*power((a*a),n/2));
    }
    return power((a*a),n/2);
}

//shubham gupta


Measured memory_bytes: 3640
#ifndef Local
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx")
#endif

#include <bits/stdc++.h>

#include <ext/numeric>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
using namespace std;

#define popCnt(x) (__builtin_popcountll(x))

typedef long long Long;

const int RANDOM =
    chrono::high_resolution_clock::now().time_since_epoch().count();
struct chash {  // To use most bits rather than just the lowest ones:
  const uint64_t C = Long(4e18 * acos(0)) | 71;  // large odd number
  Long operator()(Long x) const { return __builtin_bswap64((x ^ RANDOM) * C); }
};
__gnu_pbds::gp_hash_table<Long, Long, chash> ht({}, {}, {}, {}, {1 << 16});

Long a, b, c, d;

Long mul(Long x, Long y) {
  if (x > LLONG_MAX / y) return LLONG_MAX;
  return x * y;
}

Long solve(Long x) {
  if (x == 0) return 0;
  if (x == 1) return d;
  if (x == 2) return d + min(a, d);
  auto it = ht.find(x);
  if (it != ht.end()) return it->second;
  Long res = 1e15;
  // 2
  res = min(res, solve(x / 2) + min(a, mul(x / 2, d)) + (x % 2) * d);
  res = min(res, solve((x + 1) / 2) + a + d);
  // 3
  res = min(res, solve(x / 3) + min(b, mul(2 * (x / 3), d)) + (x % 3) * d);
  res = min(res, solve(x / 3 + 1) + b + (3 - (x % 3)) * d);
  // 5
  res = min(res, solve(x / 5) + min(c, mul(4 * (x / 5), d)) + (x % 5) * d);
  res = min(res, solve(x / 5 + 1) + c + (5 - (x % 5)) * d);

  return ht[x] = res;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#ifdef Local
  freopen("test.in", "r", stdin);
#else
#define endl '\n'
#endif

  int t;
  cin >> t;

  while (t--) {
    Long n;
    cin >> n >> a >> b >> c >> d;
    ht.clear();
    cout << solve(n) << endl;
  }
  return 0;
}

Measured memory_bytes: 4632
#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>

using namespace std;

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

int main(){
	int n;
	cin>>n;
	cout<<n*n*n<<endl;
}

Measured memory_bytes: 1160
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MN = 3012;
ll n,a[MN],dp[MN][MN];
int main (void) {
    ios::sync_with_stdio(false);cin.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];
    for (int i = n-1; i >= 0; i--) {
        for (int j = i; j < n; j++)
            dp[i][j] = (i == j) ? a[i] : max(a[i]-dp[i+1][j], a[j]-dp[i][j-1]);
    }
    cout << dp[0][n-1] << '\n';
    return 0;
}



Measured memory_bytes: 70144
S = input()
T = input()
SD = dict()
TD = dict()

valid = True
for s, t in zip(S, T):
    if s not in SD.keys():
        SD[s] = t
    else:
        if SD[s] != t:
            valid = False
            break
    if t not in TD.keys():
        TD[t] = s
    else:
        if TD[t] != s:
            valid = False
            break

print('Yes' if valid else 'No') 


Measured memory_bytes: 3632
sc :: [String] -> Int
sc (a:op:b:_)
  | op == "+" = ia + ib
  | op == "-" = ia - ib
  | op == "*" = ia * ib
  | op == "/" = div ia ib
  where
    ia = (read :: String -> Int) a
    ib = (read :: String -> Int) b

main = do
  aopb <- getLine
  if (words aopb) !! 1 == "?"
    then
      return []
    else do
      putStrLn $ show $ sc $ words aopb
      main

Measured memory_bytes: 4008
cnt = 0
input()
s = input()

for i in range(1000):
    x = str(i).zfill(3)
    a = s[:-1].find(x[0])
    if a != -1:
        b = s[a + 1 : -1].find(x[1])
        if b != -1:
            c = s[a + b + 2 :].find(x[2])
            if c != -1:
                cnt += 1

print(cnt)

Measured memory_bytes: 9204
N, K = map(int, input().split()) 

sunukes = []

for i in range(K):
    _ = int(input())
    p = list(map(int, input().split()))
    
    for j in range(len(p)):
        if p[j] not in sunukes:
            sunukes.append(p[j])

n = 0

for i in range(N):
    if i+1 not in sunukes:
        n += 1

print(n)

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

int main(){
  int64_t N;
  cin >> N;
  vector<int64_t> A(N);
  int64_t M=0;
  for(int64_t i=0; i<N; i++){
    cin >> A.at(i);
    if(A.at(i)<0){
      M++;
    }
    A.at(i)=abs(A.at(i));
  }
  sort(A.begin(), A.end());
  int64_t Ans=0;
  if(M%2==1){
    Ans-=A.at(0);
    for(int64_t i=1; i<N; i++){
      Ans+=A.at(i);
    }
  }
  else{
    for(int64_t i=0; i<N; i++){
      Ans+=A.at(i);
    }
  }
  cout << Ans << endl;
}
  

Measured memory_bytes: 1024
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <vector>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef string::const_iterator State;
int num(State &be){
//	cout << "num " << *be << endl;
	int ret=0;
	while(isdigit(*be)){
		ret*=10;
		ret+=(*be-'0');
		be++;
	}
//	cout << "numret " << ret << endl;
	return ret;
}
int ex(State& be){
//	cout << "ex " << *be << endl;
	be++;
	vector<int> vc;
	if(isdigit(*be)){
		int ret=num(be);
		be++;
		return ret/2+1;
	}
	vc.push_back(ex(be));
	while(*be=='['){
		vc.push_back(ex(be));
	}
	be++;
	int ans=0;
	sort(vc.begin(),vc.end());
	rep(i,vc.size()/2+1) ans+=vc[i];
//	cout << "exret " << ans << endl;
	return ans;
}
int main(){
	int n;
	scanf("%d",&n);
	rep(i,n){
		string s;
		cin >> s;
		State be=s.begin();
		printf("%d\n",ex(be));
	}
	return 0;
}

Measured memory_bytes: 1304
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define BUF_SIZE 20

int get_int2(int *a1, int *a2) {
#ifdef BUF_SIZE
  char line[BUF_SIZE];
  if(!fgets(line, BUF_SIZE, stdin)) return -1;
  sscanf(line, "%d %d", a1, a2);
#else
#error
#endif
  return 0;
}

struct pair {
    int v1;
    int v2;
};

struct vertex {
    int depth;
    int parent;
};

#define VERTEX_MAX 50
#define EDGE_MAX ((VERTEX_MAX*(VERTEX_MAX-1))/2)

void init_ufind(struct vertex ufind[], int size) {
    int i;
    for(i = 0; i < size; i++) {
        ufind[i].depth = 1;
        ufind[i].parent = i;
    }
    return;
}

int get_root(struct vertex ufind[], int i) {
    struct vertex *v = &ufind[i];
    int base = i;
    while(base != v->parent) {
        // update
        base = v->parent;
        v = &ufind[v->parent];
    }
    return base;
}

void connect(struct vertex ufind[], int i1, int i2) {
    int root1 = get_root(ufind, i1);
    int root2 = get_root(ufind, i2);
    struct vertex *v1 = &ufind[root1];
    struct vertex *v2 = &ufind[root2];
    if(root1 == root2) return;
    // ensure that i1 and i2 are separated;
    // update v2
    if(v1->depth > v2->depth) { // v1 <- v2
        if(v1->depth == v2->depth) {
            v1->depth++;
        }
        v2->depth = v1->depth;
        v2->parent = root1;
    } else { // update v1
        v1->depth = v2->depth;
        v1->parent = root2;
    }
}

int judge(struct vertex ufind[], int v1, int v2) {
    return get_root(ufind, v1) == get_root(ufind, v2);
}

int main(void) {
    int vs, es;
    get_int2(&vs, &es);
    struct pair ps[EDGE_MAX];
    int i, j;
    for(i = 0; i < es; i++) {
        int v1, v2;
        get_int2(&v1, &v2);
        // 1-indexed -> 0-indexed
        ps[i].v1 = v1-1;
        ps[i].v2 = v2-1;
    }

    int ans = 0;
    for(i = 0; i < es; i++) {
        struct vertex ufind[VERTEX_MAX];
        init_ufind(ufind, vs);
        int check1, check2;
        for(j = 0; j < es; j++) {
            int v1 = ps[j].v1; int v2 = ps[j].v2;
            if(i == j) { check1 = v1; check2 = v2; continue; }
            connect(ufind, v1, v2);
        }
        // separate?
        if(!judge(ufind, check1, check2)) ans++;
    }
    printf("%d\n", ans);
    return 0;
}

Measured memory_bytes: 128
import math
import numpy as np
 
 
def inputs(): return list(map(int, input().split()))
 
 
def main():
    _, A, B, C, D = inputs()
    S = input()
    maxDst = max(C, D)
    courseA = S[A-1:C]
    courseB = S[B-1:D]
    cnt = 0
    ans = []
    for i in courseB:
        if i == '#':
            cnt += 1
        else:
            cnt = 0
        if cnt == 2:
            ans.append(False)
            break
    else:
        ans.append(True)
    cnt = 0
    for i in courseA:
        if i == '#':
            cnt += 1
        else:
            cnt = 0
        if cnt == 2:
            ans.append(False)
            break
    else:
        ans.append(True)
    if D < C:
        if '...' in S[B-2: D+1]:
            ans.append(True)
        else:
            ans.append(False)

    if all(ans):
        ans = 'Yes'
    else:
        ans = 'No'
    print(ans)
 
 
if __name__ == '__main__':
    main()

Measured memory_bytes: 21500
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<cstdlib>
using namespace std;
int N,ans,a[100005],b[100005],c[100005],dp[4][100005];
int Mac(int x){
    dp[1][x+1] = max(dp[2][x] + a[x+1],dp[3][x]+a[x+1]);
    dp[2][x+1] = max(dp[3][x] + b[x+1],dp[1][x]+b[x+1]);
    dp[3][x+1] = max(dp[1][x] + c[x+1],dp[2][x]+c[x+1]);
    return max(dp[1][x+1],max(dp[2][x+1],dp[3][x+1]));
    }
int main(){
    scanf("%d",&N);
    for(int i = 1;i <= N;i++)
        scanf("%d%d%d",&a[i],&b[i],&c[i]);
    dp[1][1] = a[1];dp[2][1] = b[1];dp[3][1] = c[1];
    ans = max(a[1],max(b[1],c[1]));
    for(int i = 1;i < N;i++){
    	ans = Mac(i);
	}
	printf("%d\n",ans);
}


Measured memory_bytes: 2560
// 問題URL
// https://atcoder.jp/contests/abc145/tasks/abc145_a

import java.util.Scanner;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        //コード
        Scanner scanner = new Scanner(System.in);
        // 半径1の円の面積=1*1*3.14
        double area = 3.14;
        int r = Integer.parseInt(scanner.next());
        int result = (int)(Math.ceil(r*r*3.14/area));
        System.out.println(result);
    }
}

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

mt19937 rng(123);

typedef long long i64;
typedef unsigned long long u64;

struct chash {
  u64 operator()(const pair<int, int> &p) const {
    return p.first * 12323123 + p.second;
  }
};

int n, k, s, t, a[50];

int main() {
  cin >> n >> k >> s >> t;
  vector<gp_hash_table<pair<int, int>, i64, chash>> dp(k + 1);
  dp[0][{(1 << 18) - 1, 0}] = 1;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if ((a[i] & s) != s || (a[i] | t) != t) {
      --i;
      --n;
      continue;
    }
  }
  shuffle(a, a + n, rng);
  for (int i = 0; i < n; ++i)
    for (int j = k - 1; j >= 0; --j)
      for (const auto &p : dp[j]) {
        int x = p.first.first & a[i], y = p.first.second | a[i];
        if ((x & s) != s)
          continue;
        if ((y | t) != t)
          continue;
        dp[j + 1][{x, y}] += p.second;
      }
  i64 ans = 0;
  for (int i = 1; i <= k; ++i)
    ans += dp[i][{s, t}];
  cout << ans << endl;
}


Measured memory_bytes: 191548
n=int(input())
T,X,Y=0,0,0

a='Yes'
for i in range(n):
    t,x,y=map(int,input().split())
    if abs(x-X)+abs(y-Y)>t-T or t%2!=(x+y)%2:
        a='No'
        break
    T,X,Y=t,x,y
print(a)

Measured memory_bytes: 3060
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
int main(void){
	ll ans=0;
	ll m=0,a=0,r=0,c=0,h=0;
	int n,i;
	cin>>n;
	string s;
	for(i=0;i<n;++i){
		cin>>s;
		if(s[0]=='M')m++;
		else if(s[0]=='A')a++;
		else if(s[0]=='R')r++;
		else if(s[0]=='C')c++;
		else if(s[0]=='H')h++;
	}
	ans=m*a*r+m*a*h+m*a*c+m*r*c+m*r*h+m*c*h+a*r*c+a*r*h+a*c*h+r*c*h;
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 256
fun main(args: Array<String>) {
    val N = nextInt()
    val S = next()
    var ans = 0
    var last = '@'
    S.forEach {
        if (last != it) {
            ans += 1
            last = it
        }
    }
    println(ans)
}
fun next() = readLine()!!
fun nextInt(delta: Int = 0) = Integer.parseInt(next()) + delta
fun listOfString() = next().split(" ")

Measured memory_bytes: 34736
#include<iostream>
#include<algorithm>
#include<vector>
#include<numeric>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<deque>
#include<bitset>
#include<iomanip>
#include<cctype>
#include<map>
#include<cstring>
#include<bitset>
#include<cassert>

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

using namespace std;
using ll = long long int;

const int inf = 1001001000;
const long long int Inf = 1001001001001001000;



void print(vector<vector<int>> a){
	for (int i = 0; i < a.size(); i++)
	{
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<vector<long long int>> a){
	for (int i=0;i<a.size();i++){
		for (int j=0;j<a[i].size();j++){
			cout << a[i][j] << " ";
		}
		cout << endl;
	}
}

void print(vector<int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(vector<long long int> a){
    int n = a.size();
    for (int j=0;j<n;j++) {
        if (j != n-1) cout << a[j] << " ";
        else cout << a[j] << endl;
    }
}

void print(set<int> a){
	for (auto x:a)
		cout << x << " ";
	cout << endl;
}

ll dp[1000005][2][2][2];

int main()
{
	ll n;
	cin >> n;
	ll mod = 1e9 + 7;
	string s(n, '9');
	// cout << s << endl;
	dp[0][0][0][0] = 1;
	rep(i,n){
		int D = s[i] - '0';
		rep(j,2){
			rep(k,2){
				rep(l,2){
					for (int d = 0; d <= (j ? 9 : D);d++){
						dp[i][j][k][l] %= mod;
						dp[i + 1][j || (d < D)][k || d == 0][l || d == 9] += dp[i][j][k][l];
						dp[i + 1][j || (d < D)][k || d == 0][l || d == 9] %= mod;
					}
				}
			}
		}
	}
	cout << dp[n][0][1][1] + dp[n][1][1][1] << endl;
	return 0;
}




Measured memory_bytes: 66624
using System;

namespace AOJ.Volume3
{
    public class NumberOfParticipants
    {
        public static int Main()
        {
            var input = Console.ReadLine().Split(' ');
            int answer = 0;
            for(int i = 0; i < 3; i++)
            {
                answer += int.Parse(input[i]);
            }

            Console.WriteLine(answer);
            return 0;
        }
    }
}

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

using namespace std;

int main() {
  
  int N, M;
  while(cin >> N >> M && (N|M)) {
    vector<int> vec(N+M);
    for(int i=0; i<N+M; i++)
      cin >> vec[i];
    sort(vec.begin(), vec.end());
    int mx = 0, prev = 0;
    for(int i=0; i<N+M; i++) {
      mx = max(mx, vec[i]-prev);
      prev = vec[i];
    }
    cout << mx << endl;
  }
  
  return 0;
}

Measured memory_bytes: 1292
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pl;
#define all(a) (a).begin(),(a).end()
#define overload4(_1, _2, _3, _4, name, ...) name
#define rep1(n) for(ll i = 0;i < (n);i++)
#define rep2(i,n) for(ll i = 0;i < (n);i++)
#define rep3(i,a,b) for(ll i = (a);i < (b);i++)
#define rep4(i,a,b,c) for(ll i = (a);i < (b);i += (c))
#define rep(...) overload4(__VA_ARGS__, rep4, rep3, rep2, rep1)(__VA_ARGS__)
#define rrep1(n) for(ll i = (n)-1;i >= 0;i--)
#define rrep2(i,n) for(ll i = (n)-1;i >= 0;i--)
#define rrep3(i,a,b) for(ll i = (a)-1;i >= (b);i--)
#define rrep4(i,a,b,c) for(ll i = (a)-1;i >= (b);i -= (c))
#define rrep(...) overload4(__VA_ARGS__, rrep4, rrep3, rrep2, rrep1)(__VA_ARGS__)
#define SHOW(x) cout << #x << ": " << x << endl;
template<class T> ll sumArr(const T& a){ return accumulate(all(a), 0LL);}
template<class T> auto minArr(const T& a){ return *min_element(all(a));}
template<class T> auto maxArr(const T& a){ return *max_element(all(a));}
template<class T> void DEBUG_PRINT(T tmp){cout<<tmp<<endl;}
template<class T1,class... T2> void DEBUG_PRINT(T1 tmp,T2... tmp2){cout<<tmp<<" ";DEBUG_PRINT(tmp2...);}
ll gcd(ll a, ll b){ while(b){ ll c = b; b = a % b; a = c; } return a; }
const ll INF=1e18,MOD = 1e9+7;
const int iINF = 1e9;


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

    ll n,ans = 0;
    cin>>n;
    unordered_map<ll,ll>mp;
    vector<int>a(n);
    rep(i,n){
        cin>>a[i];
        ans += mp[i - a[i]];
        mp[i + a[i]]++;
    }

    cout<<ans<<endl;
    return 0;
}

// j-i == a+b
// j = i+a+b
// j - b = i + a


Measured memory_bytes: 19316
n = int(input().rstrip())

ans = [0 for x in range(10000)]
for x in range(1,101):
  for y in range(1,101):
    for z in range(1,101):
      f = ((x+y)**2 + (y+z)**2 + (z+x)**2)//2
      if f <= 10000:
        ans[f-1] += 1

for i in range(n):
  print(ans[i])

Measured memory_bytes: 9232
while True:
    m=int(input())
    p=1000-m
    a=p//500
    x=p%500
    b=x//100
    y=x%100
    c=y//50
    z=y%50
    d=z//10
    o=z%10
    e=o//5
    f=o%5
    if m==0:
        break
    print(a+b+c+d+e+f)



Measured memory_bytes: 5584
#include <bits/stdc++.h>
using namespace std;
void init(){
    cin.tie(0);
    cin.sync_with_stdio(0);
}

int main() {
    init();
    string s;
 cin>>s;
 if(s[0]=='7'||s[1]=='7'||s[2]=='7'){
    cout<<"Yes";
 }
 else cout<<"No";}

Measured memory_bytes: 3636
#include <set>
#include <stdio.h>
#include <iterator>
using namespace std;
int main(){
    int n,m,q,temp;
    set<int>s;
    set<int>::iterator now;
    scanf("%d%d%d",&n,&m,&q);
    for(int i=0;i<n;i++)s.insert(i);
    now=s.find(0);
    for(int i=0;i<m;i++){
        scanf("%d",&temp);
        if(temp%2==0){
            while(temp){
                while(now!=s.end()){
                    now++;
                    temp--;
                    if(temp==0)break;
                }
                if(now==s.end())now=s.begin();
            }
        }
        else{
            while(temp){
                while(now!=s.begin()){
                    now--;
                    temp--;
                    if(temp==0)break;
                }
                if(now==s.begin())now=s.end();
            }
            if(now==s.end())now=s.begin();
        }
        temp=*now;
        s.erase(now);
        now=s.lower_bound(temp);
    }
    while(q--){
        scanf("%d",&temp);
        if(s.find(temp)==s.end())printf("0\n");
        else printf("1\n");
    }
}


Measured memory_bytes: 12000
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp setprecision
#define pb(x) push_back(x)
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<ld, ld> pdd;
ll MOD = 1e9+7;
ll MOD2 = 998244353;
int inf = 1e8;
ll INF = 1e16;
string alpha = "abcdefghijklmnopqrstuvwxyz";

int main(){
    int N = 1e5;
    bool prime[N+1];
    fill(prime, prime+N+1, true);
    prime[1] = false;
    rep2(i, 2, N){
        if(!prime[i]) continue;
        rep2(j, 2, N/i){
            prime[i*j] = false;
        }
    }
    int sum[N+1];
    sum[0] = 0;
    rep2(i, 1, N){
        if(i%2 == 0) sum[i] = sum[i-1];
        else{
            if(prime[i] && prime[(i+1)/2]){
                sum[i] = sum[i-1]+1;
            }
            else sum[i] = sum[i-1];
        }
    }
    int Q;
    cin >> Q;
    rep(i, Q){
        int l, r;
        cin >> l >> r;
        cout << sum[r]-sum[l-1] << endl;
    }
}

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

int main( int argc, char *argv[] )
{
int i;
int H;
int W;
char S[ 110 ][ 110 ];

	ios::sync_with_stdio( false );
	cin.tie( 0 );

	cin >> H;
	cin >> W;
	for( i = 0; i < H; i++ ) cin >> S[ i ];

	for( i = 0; i < W + 2; i++ ) cout << "#";

	cout << endl;

	for( i = 0; i < H; i++ )
	{
		cout << "#" << S[ i ] << "#" << endl;
	}

	for( i = 0; i < W + 2; i++ ) cout << "#";

	cout << endl;

	return 0;
}




Measured memory_bytes: 256
using System;

namespace AOJ
{
    class Program
    {
        static void Main(string[] args)
        {

            string card_list="";
            int shuffle_num;


            while (true)
            {
                var read_text = Console.ReadLine();
                if (read_text == "-")
                {
                    break;
                }
                card_list = read_text;
                read_text = Console.ReadLine();
                shuffle_num = int.Parse(read_text);

                var shuffle_card = new int[shuffle_num];
                for(int i=0;i<shuffle_num;i++)
                {
                    read_text = Console.ReadLine();
                    shuffle_card[i] = int.Parse(read_text);
                }

                foreach(int i in shuffle_card)
                {
                    string temp = card_list.Substring(0, i);
                    card_list=card_list.Remove(0, i);
                    card_list += temp;
                }
                Console.WriteLine(card_list);
            }


            
        }
    }
}

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

int main(){
  string s;
  deque<char>st;
  cin >> s;
  rep(i,s.size()){
    if(s[i]!='B')
      st.push_back(s[i]);
    else if(!st.empty())
      st.pop_back();
  }
  int st_size = st.size();
  rep(i,st_size){
    cout << st.front();
    st.pop_front();
  }
  return 0;
}

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

bool find(vector<int>& vec, int x) {
  for (auto e : vec) {
    if (e == x)
      return true;
  }
  return false;
}

int main() {
  int x, y;
  vector<int> g1 = {1, 3, 5, 7, 8, 10, 12};
  vector<int> g2 = {4, 6, 9, 11};
  vector<int> g3 = {2};
  cin >> x >> y;

  string ans = "No";

  if ((find(g1, x) && find(g1, y)) || (find(g2, x) && find(g2, y)) ||
      (find(g3, x) && find(g3, y)))
    ans = "Yes";

  cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <math.h>

using namespace std;

int main() {
int a,b,c,d,e,k;
cin >> a >>b>>c>>d>>e>>k;
if(
abs(a-b)>k ||
abs(a-c)>k ||
abs(a-d)>k ||
abs(a-e)>k ||
abs(b-c)>k ||
abs(b-e)>k ||
abs(c-d)>k ||
abs(c-e)>k ||
abs(d-e)>k
){
cout << ":(" << endl;
}
else cout << "Yay!" << endl;

return 0;








}


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

int main(){
    char s[19];
    cin>>s;
    for(int i=0;i<5;i++){
        cout<<s[i];
    }
    cout<<" ";
    for(int i=6;i<13;i++){
        cout<<s[i];
    }
    cout<<" ";
    for(int i=14;i<19;i++){
        cout<<s[i];
    }
    cout<<endl;
    return 0;
    
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
int main(){
int n,m,x,y;
  cin>>n>>m;
  vector<int> g[n+1];
  int indegree[n+1];
  int dp[n+1];

  for(int i=0;i<=n;i++){
      indegree[i]=0;
      dp[i]=0;
  }
// cout<<dp[2]<<endl;
  for(int i=0;i<m;i++){
      cin>>x>>y;
    g[x].push_back(y);
    indegree[y]++;
  }
  queue<int> q;
  for(int i=1;i<=n;i++){
  if(indegree[i]==0){
      q.push(i);
  }
  }
  
  while(!q.empty()){
  int t=q.front();
    q.pop();
    for(int i=0;i<g[t].size();i++){
        int v=g[t][i];
      dp[v]=max(dp[v],dp[t]+1);
      indegree[v]--;
      if(indegree[v]==0)
          q.push(v);
    }
  }
  int h = dp[1];
  for(int i=2;i<=n;i++){
    h=max(h,dp[i]);
  }
  cout<<h<<endl;
  
  return 0;
}


Measured memory_bytes: 6528
#include <iostream>
#include <string>
#include <map> //map<string, int> cnt; key とvalue
#include <set>//  set<int> s;
#include <cstdlib>  // abs() for integer
#include <vector>
#define rep(i,n) for(int i = 0 ; i < (n); ++i)
using namespace std;
using ll = long long;
// g++ -std=c++11
int main(){
  int x,y;
  cin >> x >> y;
  rep(i,x+1) {
    if(2*i+4*(x-i) == y) {
      cout << "Yes" << endl;
      return 0;
    }
  }
  cout << "No" << endl;
  return 0;
}

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

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

	for (int j = 0; j < s.size(); j++)
	{
		if(s[j] + i > 90){
			s[j] -= 26;
		}
		s[j] += i;
		/* code */
	}
	
	cout << s << endl;
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define FALSE 0
#define TRUE 1
typedef int Status;
int main()
{
    int sx,sy,tx,ty;
    scanf("%d%d%d%d",&sx,&sy,&tx,&ty);
    for(int i=sx;i<tx;i++)
        cout<<"R";
    for(int i=sy;i<ty;i++)
        cout<<"U";
     for(int i=sx;i<tx;i++)
        cout<<"L";
    for(int i=sy;i<ty;i++)
        cout<<"D";
        cout<<"LU";
    for(int i=sy;i<ty;i++)
        cout<<"U";
         for(int i=sx;i<tx;i++)
        cout<<"R";
        cout<<"RD";
        cout<<"RD";
          for(int i=sy;i<ty;i++)
        cout<<"D";
            for(int i=sx;i<tx;i++)
        cout<<"L";
        cout<<"LU"<<endl;


}


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

//const ll mod =  1000000007;
//const ll maxn = 1000000000;

//---------------------------------------------------------------------------------------------------
using vi = vector<int>; // intの1次元の型に vi という別名をつける
using vll = vector<ll>; // intの1次元の型に vi という別名をつける
using vvll = vector<vll>; // intの2次元の型に vvi という別名をつける
using vs = vector<string>; // stringの1次元の型に vs という別名をつける
using pll = pair<ll, ll>; // これ以降 pii という型名はpair<ll, ll> と同じ意味で使える
//---------------------------------------------------------------------------------------------------


int main() {
    int n,k,ans=0;
    cin>>n>>k;
    vll a(n);
    rep(i,n)cin>>a.at(i);
    sort(ALL(a));
    rep(i,k)ans+=a.at(i);
    cout<<ans<<endl;
}


Measured memory_bytes: 3624
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double lld;
ll MOD = (1e9)+7;
const int dr[] = {+1, -1, +0, +0, +1, -1, +1, -1};
const int dc[] = {+0, +0, +1, -1, +1, -1, -1, +1};
const int kx[] = {+1, +2, -1, -2, +1, +2, -1, -2};
const int ky[] = {+2, +1, +2, +1, -2, -1, -2, -1};
//#pragma GCC target ("avx2")
//#pragma GCC optimization ("O3")
//#pragma GCC optimization ("unroll-loops")
inline void fastIO(){
  ios_base::sync_with_stdio(0);cin.tie(0);
}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }

#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif

int main(){
  fastIO();
  int n,ans=0;
  cin>>n;
  for(int a=1;a<=n;a++){
    for(int b=1;b<=n;b++){
      if(a*b>n)break;
      if(a*b<n)ans++;
    }
  }
  cout<<ans<<endl;
  return 0;
}


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

int main(){
    string s,t,u;
    int a,b;
    cin >> s >> t;
    cin >> a >> b;
    cin >> u;
    if(s == u) cout << a-1;
    else cout << a;
    if(t == u) cout << " " << b-1 << endl;
    else  cout << " " << b << endl;
    return 0;

}

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

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

template <typename T>
function<T(T, T)> op_max = [](T a, T b) -> T { return max(a, b); };
template <typename T>
function<T(T, T)> op_min = [](T a, T b) -> T { return min(a, b); };

template <typename T>
function<T(T, T)> op_sum = [](T a, T b) -> T { return a + b; };

namespace detail {
template <typename T, std::size_t NDIMS>
struct vector_builder {
  using type = std::vector<typename vector_builder<T, NDIMS - 1>::type>;

  static type make(std::vector<std::size_t> dims, const T& v = {}) {
    const auto vec = std::vector<T>(dims.empty() ? 0 : dims.back(), v);
    if (!dims.empty()) dims.pop_back();
    return vector_builder<std::vector<T>, NDIMS - 1>::make(dims, vec);
  }
};

template <typename T>
struct vector_builder<T, 1> {
  using type = std::vector<T>;

  static type make(std::vector<std::size_t> dims, const T& v = {}) {
    return type(dims.empty() ? 0 : dims.back(), v);
  }
};
}  // namespace detail

template <typename T, typename... SIZE_T>
auto exvector(const T& v, SIZE_T... dims) {
  static_assert(sizeof...(dims) != 0, "invalid dimension");
  std::vector<std::size_t> vec_dims{dims...};
  return detail::vector_builder<T, sizeof...(dims)>::make(vec_dims, v);
}

using namespace std;
//********************  dumps ************************//

template <typename T>
void dump(const T& data, vector<int>& iter) {
  cout << data << " ";
}
template <typename T>
void dump(const vector<T>& data, vector<int>& iter) {
  for (auto elem : data) dump(elem, iter);
  cout << endl;
}

template <typename T>
void dump(const vector<vector<T>>& data, vector<int>& iter) {
  for (auto elem : iter) {
    cout << "[" << elem << "]";
  }
  cout << endl;
  for (int i = 0; i < data.size(); i++) {
    iter.push_back(i);
    dump(data[i], iter);
    iter.pop_back();
  }
  cout << endl;
}

template <typename T>
void dump(const vector<T>& data, int dummy) {
  for (int i = 0; i < data.size(); i++) {
    cout << "[" << i << "] " << data[i] << endl;
  }
}
template <typename T>
void dump(const T& data) {  // T : data, U = base type val.
  vector<int> iter;
  dump(data, iter);
}
///////////////////////////////////////////////

const string YES = "Yes";
const string NO = "No";

void solve(long long N, std::vector<long long> L) {
  LL sum = 0;
  LL maxim = 0;
  for (int i = 0; i < L.size(); i++) {
    auto& l = L[i];
    sum += l;
    chmax(maxim, l);
  }
  cout << ((maxim < sum - maxim) ? YES : NO) << endl;
}

int main() {
  cout << setprecision(10);
  long long N;
  scanf("%lld", &N);
  std::vector<long long> L(N);
  for (int i = 0; i < N; i++) {
    scanf("%lld", &L[i]);
  }
  solve(N, std::move(L));
  return 0;
}


Measured memory_bytes: 256
INF = 1<<29
loop do
  n, m = gets.split.map(&:to_i)
  break if n == 0
  s = [[[0, 0]], *n.times.map { gets.split.map(&:to_i)[1..-1].each_slice(2).to_a } ]
  ss = s.flatten(1).transpose[0]
  ss.sort!.uniq!
  s_index = (n+1).times.map{ [] }
  (n+1).times do |i|
    s[i].each do |x, _|
      s_index[i] << ss.index(x)
    end
  end

  dp = (n+1).times.map { (m+1).times.map { [INF] * ss.size } }

  (m+1).times{ |i| dp[0][i].fill(0) }
  n.times do |i|
    (m+1).times do |j|
      s[i].each_with_index do |(x0, d0), k|
        s[i+1].each_with_index do |(x1, d1), l|
          v = i == 0 ? 0 : dp[i][j][s_index[i][k]] + (x0 - x1).abs * (d0 + d1)
          dp[i+1][j][s_index[i+1][l]] = v if dp[i+1][j][s_index[i+1][l]] > v
        end
      end
    end
    next if i == n - 1
    m.times do |j|
      s[i].each_with_index do |(x0, d0), k|
        s[i+2].each_with_index do |(x1, d1), l|
          v = i == 0 ? 0 : dp[i][j][s_index[i][k]] + (x0 - x1).abs * (d0 + d1)
          dp[i+2][j+1][s_index[i+2][l]] = v if dp[i+2][j+1][s_index[i+2][l]] > v
        end
      end
    end
  end
  p [*dp[n-1][0,m].flatten , *dp[n][m].flatten].min
end

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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
#define MOD (1000000007)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1}; // 移動方向

ll n;
ll a[200001];
ll s[200001];

void input(){
    cin >> n;
    for(int i=0;i<n;i++) cin >> a[i];
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    input();
    ll snuke;
    ll araiguma;
    ll ans = 2 * INF + 1;
    s[0] = a[0];
    for(int i=1;i<n;i++){
        s[i] = a[i] + s[i-1] ; // 累積和
    }
    for(int i=0;i<n-1;i++){
        snuke = s[i];
        araiguma = s[n-1] - s[i];
        ans = min(ans, abs(snuke - araiguma));
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3328
#include <bits/stdc++.h>
 
using namespace std;
using ll = long long;
// #define int ll
using PII = pair<int, int>;
 
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
 
template<typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
 
template<typename T> vector<T> make_v(size_t a) { return vector<T>(a); }
template<typename T,typename... Ts>
auto make_v(size_t a,Ts... ts) { 
  return vector<decltype(make_v<T>(ts...))>(a,make_v<T>(ts...));
}
template<typename T,typename V> typename enable_if<is_class<T>::value==0>::type
fill_v(T &t, const V &v) { t=v; }
template<typename T,typename V> typename enable_if<is_class<T>::value!=0>::type
fill_v(T &t, const V &v ) { for(auto &e:t) fill_v(e,v); }
 
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')'; return out;
}
template<typename T>
istream& operator >> (istream& is, vector<T>& vec){
  for(T& x: vec) {is >> x;} return is;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'['; for(T i: a) {out<<i<<',';} out<<']'; return out;
}
 
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0}; // DRUL
const int INF = 1<<30;
const ll LLINF = 1LL<<60;
const int MOD = 1000000007;

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

  ll n;
  string s;
  cin >> n >> s;
  ll q;
  cin >> q;
  vector<ll> k(q);
  REP(i, q) cin >> k[i];

  // vector<ll> num(n);
  // num[0] = (s[0]=='M'?1:0);
  // FOR(i, 1, n) num[i] = num[i-1] + (s[i]=='M'?1:0);

  REP(i, q) {
    ll ret = 0;
    ll mnum = 0, cnum = 0, now = 0;
    REP(j, k[i]) {
      if(s[j] == 'M') mnum++;
      else if(s[j] == 'C') cnum++, now += mnum;
    }
    for(ll l=0, r=k[i]-1; l<n; ++l, ++r) {
      if(s[l]!='D') {
        if(s[l]=='M') mnum--, now -= cnum;
        else if(s[l]=='C') cnum--;
        if(r+1 < n) {
          if(s[r+1] == 'M') mnum++;
          else if(s[r+1] == 'C') cnum++, now += mnum;
        }
        continue;
      }

      // cout << l << " " << r << " " << mnum << " " << cnum << " " << now << endl;
      ret += now;

      if(r+1 < n) {
        if(s[r+1] == 'M') mnum++;
        else if(s[r+1] == 'C') cnum++, now += mnum;
      }
    }

    // ll r = 0, mnum = s[0]
    // REP(l, n) {
    //   if(r < l) {
    //     r = l;
    //     mnum = s[l]=='M'?1:0;
    //     cnum = s[l]=='C'?1:0;
    //   }
    //   if(s[l] != 'D') {
    //     if(s[l] == 'M') mnum--, now -= cnum;
    //     else if(s[l] == 'C') cnum--;
    //     continue;
    //   }
    //   while(r < n && r-l < k[i]) {
    //     if(s[r] == 'C') now += mnum, cnum++;
    //     else if(s[r] == 'M') mnum++;
    //     ++r;
    //   }
    //   ret += now;
    // }
    cout << ret << endl;
  }

  return 0;
}


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

using namespace std;

typedef long long ll;
const int maxn=1e5+5;
int a[maxn];

int main()
{
    ios::sync_with_stdio(false);
    int n;
    while(cin>>n){
        ll sum=0;
        for(int i=0;i<n;i++){
            cin>>a[i];
            if(!i) sum+=abs(a[i]);
            else sum+=abs(a[i]-a[i-1]);
        }
        sum+=abs(a[n-1]);
        ll sum2;
        for(int i=0;i<n;i++){
            if(!i){
                sum2=sum-abs(a[i])-abs(a[1]-a[0])+abs(a[1]);
            }
            else if(i==n-1){
                sum2=sum-abs(a[n-1]-a[n-2])-abs(a[n-1])+abs(a[n-2]);
            }
            else{
                sum2=sum-abs(a[i]-a[i-1])-abs(a[i+1]-a[i])+abs(a[i+1]-a[i-1]);
            }
            cout<<sum2<<endl;
        }
    }
    return 0;
}


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

using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> p(n + 2);
  // sentinels
  p[0] = 0;
  p[n + 1] = n + 1;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  int c = 0;
  int i = 0;
  int j = 0;
  for (int k = 1; k < n; k++) {
    if (p[k] < p[k + 1]) {
      continue;
    }
    if (c == 0) {
      i = k;
    } else if (c == 1) {
      j = k + 1;
    } else if (c == 2) {
      cout << "NO" << endl;
      return 0;
    }
    c++;
  }
  bool ok = c == 0 || ((p[i - 1] < p[j] && p[j] < p[i + 1]) &&
                       (p[j - 1] < p[i] && p[i] < p[j + 1]));
  cout << (ok ? "YES" : "NO") << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;cin>>N;int X[N],COPY[N];
  for(int i=0;i<N;i++){cin>>X[i];COPY[i]=X[i];}
  sort(COPY,COPY+N);
  for(int i=0;i<N;i++){
    if(X[i]>COPY[N/2-1])cout<<COPY[N/2-1]<<endl;
    else cout<<COPY[N/2]<<endl;
  }
}

Measured memory_bytes: 3712
s = input()
x, y = map(int, input().split())


def part_sum0(a, A):
    # 初期化
    N = len(a)
    dp = [[0 for i in range(A + 1)] for j in range(N + 1)]
    dp[0][0] = 1

    # DP
    for i in range(N):
        for j in range(A + 1):
            if a[i] <= j:
                dp[i + 1][j] = dp[i][j - a[i]] or dp[i][j]
            else:
                dp[i + 1][j] = dp[i][j]
    return dp[N][A]


fs = s.split('T')
f_num = [len(f) for f in fs]
dir_lst = [[], []]
for i in range(len(f_num)):
    dir_lst[i % 2].append(f_num[i])
x_lst = dir_lst[0]
x -= x_lst[0]
x_lst = x_lst[1:]
y_lst = dir_lst[1]

can_x = False
can_y = False

dx = abs(sum(x_lst)) - abs(x)
if (dx % 2 == 1) or (dx < 0):
    pass
elif dx == 0:
    can_x = True
else:
    if part_sum0(x_lst, dx // 2):
        can_x = True

if can_x:
    dy = abs(sum(y_lst)) - abs(y)
    if (dy % 2 == 1) or (dy < 0):
        pass
    elif dy == 0:
        can_y = True
    else:
        if part_sum0(y_lst, dy // 2):
            can_y = True

if can_y:
    print('Yes')
else:
    print('No')


Measured memory_bytes: 20212