akhauriyash/Code-Regression huggingface dataset:
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <limits>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; }
template<class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); }

//math
//-------------------------------------------
template<class T> inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define EXISTch(s,c) ((((s).find_first_of(c)) != std::string::npos)? 1 : 0)//cがあれば1 if(1)
#define SORT(c) sort((c).begin(),(c).end())

#define REP(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = (int)1000000007;
const LL MOD = (LL)1000000007;//10^9+7
const LL INF2 = (LL)100000000000000000;//10^18

int main() {
	string s; cin >> s;
	int n = s.size();
	int cnt = 0;
	int ans = 0;
	bool f = true;

	for (int i = 0; i < n; i++) {
		cnt = 0;
		f = true;
		for (int j = i; j < n; j++) {
			if(s[j] == 'A' || s[j] == 'C' || s[j] == 'G' || s[j] == 'T') {
				cnt++;
			}
			else {
				ans = max(ans, cnt);
				break;
			}
		}

		ans = max(ans, cnt);
	}

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



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

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int X, Y, Z, K;
    cin >> X >> Y >> Z >> K;
    vector<long long> A(X), B(Y), C(Z);
    for (int i = 0; i < X; ++i) cin >> A[i];
    for (int i = 0; i < Y; ++i) cin >> B[i];
    for (int i = 0; i < Z; ++i) cin >> C[i];
    sort(A.begin(), A.end());
    sort(B.begin(), B.end());
    sort(C.rbegin(), C.rend());
    long long l = 3, r = 3e10, res;
    while (l <= r) {
        long long mid = (l + r) / 2;
        int num = 0;
        for (int i = 0; i < X; ++i) {
            int k = 0;
            for (int j = 0; j < Y; ++j) {
                long long sum = A[i] + B[j];
                while (C[k] + sum >= mid && k < Z) ++k;
                num += k;
            }
        }
        if (num >= K) {
            res = mid;
            l = mid + 1;
        } else {
            r = mid - 1;
        }
    }
    vector<long long> ans;
    for (int i = 0; i < X; ++i) {
        for (int j = 0; j < Y; ++j) {
            for (int k = 0; k < Z; ++k) {
                if (A[i] + B[j] + C[k] > res) {
                    ans.push_back(A[i] + B[j] + C[k]);
                } else {
                    break;
                }
            }
        }
    }
    sort(ans.rbegin(), ans.rend());
    for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i] << endl;
    }
    for (int i = ans.size(); i < K; ++i) {
        cout << res << endl;
    }
    return 0;
}



Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep1(i, n) for(int i = 1; i < n+1; i++)
#define all(A) A.begin(),A.end()
typedef long long ll;

int main(){
  int h,w,m;
  cin >> h >> w >> m;
  vector<int> sumH(h+1,0),sumW(w+1,0);
  set<pair<int,int>> st;
  rep(i,m){
    int bh,bw;
    cin >> bh >> bw;
    sumH[bh] ++;
    sumW[bw] ++;
    st.insert(make_pair(bh,bw));
  }
  int maxH = *max_element(all(sumH));
  int maxW = *max_element(all(sumW));
  int ans = maxH + maxW;
  vector<int> candH(0),candW(0);
  rep1(i,h) if(sumH[i]==maxH) candH.push_back(i);
  rep1(i,w) if(sumW[i]==maxW) candW.push_back(i);
  for (int i : candH){
    for (int j : candW){
      auto tmp = make_pair(i,j);
      if(st.count(tmp) == 0){
        cout << ans << endl;
        return 0;
      }
    }
  }
  ans --;
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 23880
#include <iostream>
#include <iomanip>
#include <cmath>
#include <vector>
#include <queue>
#include <limits>
#include <algorithm>
#include <string>
using namespace std;

static bool is_match(string S, string word, size_t i)
{
	bool matched = true;
	for (size_t j = 0; j < word.size(); j++) {
		if (S[i - word.size() + j] != word[j]) {
			matched = false;
			break;
		}
	}
	return matched;
}

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

	string dream = "dream";
	string erase = "erase";
	string eraser = "eraser";
	string dreamer = "dreamer";
	size_t N = S.size(), i = N;
	while (i > 0) {
		if (i < dream.size())
			break;

		if (is_match(S, dream, i)) {
			i -= dream.size();
			continue;
		}

		if (is_match(S, erase, i)) {
			i -= erase.size();
			continue;
		}

		if (i < eraser.size())
			break;

		if (is_match(S, eraser, i)) {
			i -= eraser.size();
			continue;
		}

		if (i < dreamer.size())
			break;

		if (is_match(S, dreamer, i)) {
			i -= dreamer.size();
			continue;
		}

		break;
	}

	cout << (i == 0 ? "YES" : "NO") << endl;

	return 0;
}

Measured memory_bytes: 512
# -*- coding:utf-8 -*-

def solve():
    N = int(input())
    dic = {}

    max_num = -1
    for i in range(N):
        S = input()
        if not S in dic: dic[S] = 0
        dic[S] += 1

        if dic[S] > max_num:
            max_num = dic[S]

    anss = []
    for key, value in dic.items():
        if value == max_num:
            anss.append(key)

    anss.sort()

    for ans in anss:
        print(ans)


if __name__ == "__main__":
    solve()


Measured memory_bytes: 32096
#include "bits/stdc++.h"

//#include <atcoder/all>

using namespace std;
//using namespace atcoder;

//#define int long long
#define ll long long

ll INF = (1LL << 60);
int mod = 998244353;
using P = pair<ll, ll>;

signed main() {
	string s; cin >> s;
	if (s[0] == s[1] || s[1] == s[2] || s[2] == s[3]) {
		cout << "Bad" << endl;
	}
	else cout << "Good" << endl;
	return 0;
}



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

int main(){
  char b;cin>>b;
  if(b=='A')
    cout<<"T"<<endl;
    else if(b=='T')
    cout<<"A"<<endl;
       else if(b=='G')
    cout<<"C"<<endl;
          else if(b=='C')
    cout<<"G"<<endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, a, b) for(int i = a; i < b; i++)
#define  all(x) (x).begin(),(x).end()     // 昇順ソート
#define  rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define  FastIO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
typedef long long ll;
using P = pair<int,int>;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }

int main(){
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    rep(i, n) cin >> a[i];
    rep(i, k){
        vector<int> b(n+2);
        rep(j, n){
            int v = a[j];
            int l = max(j+1-v, 0), r = min(j+1+v+1, n+1);
            b[l]++; b[r]--;
        }
        int mn = n+1;
        rep(j,n){
            b[j+1] += b[j];
            a[j] = b[j+1];
            mn = min(mn, a[j]);
        }
        if (mn == n) break;
    }
    rep(i, n) printf("%d%c", a[i], i == n-1 ? '\n':' ');
    return 0;
}

Measured memory_bytes: 4968
N = int(input())
Mylog = []
cnt = 0
x = list(input())
Mylog.append(x)
for i in range(1,N):
  x = list(input())
  Mylog.append(x)
  if (x in Mylog[:-1]) or (Mylog[i-1][-1] != Mylog[i][0]):
    cnt += 1

print("Yes" if cnt==0 else "No")

Measured memory_bytes: 3064
n = int(input())
r = 0
a = 0
b = 0
ab = 0
for _i in range(n):
    s = input()
    r += s.count("AB")
    B = 0
    A = 0
    if s[-1]=="A":
        a += 1
    if s[0]=="B":
        b += 1
    if s[-1]=="A" and s[0]=="B":
        ab += 1
if ab == a and ab == b:
    print(r+max(0, ab-1))
else:
    print(r+min(a, b))

Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define long long long

using namespace std;

long n;
long a1, a2, a3, b1, b2, b3;

long trade(long m, pair<long, long> ratio) {
    return ((m / ratio.first) * ratio.second) + (m % ratio.first);
}

long calc() {
    vector<pair<long, long>> v;
    if (b1 > a1) v.push_back({a1, b1});
    if (b2 > a2) v.push_back({a2, b2});
    if (b3 > a3) v.push_back({a3, b3});

    if (v.size() == 0) return n;
    if (v.size() == 1) return trade(n, v[0]);
    if (v.size() == 2) {
        long res = 0;
        for (long l = 0; l <= n; ++l) {
            res = max(res, trade(l, v[0]) + trade(n-l, v[1]));
        }
        return res;
    }
    if (v.size() == 3) {
        long res = 0;
        for (long l = 0; l <= n; ++l) {
            for (long r = 0; r <= n - l; ++r) {
                res = max(res, trade(l, v[0]) + trade(r, v[1]) + trade(n-l-r, v[2]));
            }
        }
        return res;
    }
}

int main() {
    cin >> n >> a1 >> a2 >> a3 >> b1 >> b2 >> b3;
    n = calc();
    swap(a1, b1); swap(a2, b2); swap(a3, b3);
    n = calc();
    cout << n << "\n";
}


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

typedef long long ll;

const ll MOD = 1e9 + 7;

ll mod_pow(ll x, ll n, ll mod) {
    ll res = 1;
    while (n > 0) {
        if (n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}

ll inv(int x) {
    return mod_pow(x, MOD - 2, MOD);
}

ll fct[200001];

ll c(ll n, ll k) {
    return ((fct[n] * inv(fct[k]) + MOD) % MOD * inv(fct[n - k]) + MOD) % MOD;
}

void solve() {
    ll B, W;
    cin >> B >> W;
    fct[0] = 1;
    rep(i, 200000) fct[i + 1] = (fct[i] * (i + 1) + MOD) % MOD;
    ll p[100001], q[100001];
    p[0] = inv(mod_pow(2, B, MOD));
    rep(i, W - 1) p[i + 1] = (p[i] + (c(B + i, i + 1) * inv(mod_pow(2, B + i, MOD)) + MOD) % MOD * inv(2) + MOD) % MOD;
    q[0] = inv(mod_pow(2, W, MOD));
    rep(i, B - 1) q[i + 1] = (q[i] + (c(W + i, i + 1) * inv(mod_pow(2, W + i, MOD)) + MOD) % MOD * inv(2) + MOD) % MOD;
    rep(i, B + W) {
        if (i < min(B, W)) cout << inv(2) << endl;
        else if (i >= max(B, W)) cout << (q[i - W] + (1 - p[i - B] - q[i - W] + MOD + MOD) * inv(2) + MOD) % MOD << endl;
        else if (B < W) cout << ((1 - p[i - B] + MOD) * inv(2)) % MOD << endl;
        else cout << (q[i - W] + (1 - q[i - W] + MOD) * inv(2)) % MOD << endl;
    }
    return;
}

int main() {
    solve();
}

Measured memory_bytes: 5376
#include <stdio.h>
#include <algorithm>

typedef long long ll;


int N,x;
int a[100];
int main(){
        scanf("%d %d",&N,&x);
        for(int i = 0; i < N; i++){
                scanf("%d",a+i);
        }
        std::sort(a,a+N);

        int c = 0;
        while(x >= a[c] && c < N){
                x -= a[c];
                c++;
        }
        if(c == 0){
                 puts("0");
                 return 0;
        }

        if(c >= N){
                if(x == 0) printf("%d\n",N);
                else       printf("%d\n",N-1);
        }else{
                printf("%d\n",c);
        }
}

Measured memory_bytes: 128
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
//#include <regex>

/* g++ -g -std=c++0x */
/* g++ -g -std=c++11 */

using namespace std;

// freopen("input.txt", "rt", stdin);
// freopen("output.txt", "wt", stdout);

#define ALL(c)          (c).begin(), (c).end()
#define ALLR(c)         (c).rbegin(), (c).rend()
#define FOR(i,a,b)      for (int i=(a); i < (b); ++i)
#define FORR(i,a,b)     for (int i=(a); i > (b); --i)
#define FOR_ALL(i,c)    for (__typeof((c).begin()) i=(c).begin();   \
                             i != (c).end(); ++i)
#define FOR_ALLR(i,c)   for (__typeof((c).rbegin()) i=(c).rbegin(); \
                             i != (c).rend(); ++i)
#define SZ(array)       (sizeof(array)/sizeof(array[0]))
#define lc(x)           (x<<1)     /* 2*x */
#define rc(x)           (x<<1 | 1) /* 2*x+1 */
#define lowbit(x)       (x & (-x)) /* 0b10100 -> 0b100 */

typedef long long       LL;
typedef map<int,int>    MII;
typedef pair<int,int>   PII;
typedef set<int>        SI;
typedef vector<bool>    VB;
typedef vector<double>  VD;
typedef vector<int>     VI;
typedef vector<string>  VS;

/* check if a key is in container C */
template <class C>
inline bool in_(const typename C::key_type& k, const C& A)
{ return A.find(k) != A.end(); }
inline bool in_(const string& s, const string& S)
{ return S.find(s) != string::npos; }

int main()
{
/* #ifndef ONLINE_JUDGE */
/*     freopen("foo", "rt", stdin); */
/* #endif */
    std::ios::sync_with_stdio(false);

    int d; int n;
    cin >> d >> n;
    int cnt = 0;
    const int maxn = 2e6+6;
    int ans = 0;
    FOR(x,1,maxn) {
        if (d == 0) {
            cnt += (x%100 != 0);
        }
        else {
            int y = x;
            int c = 0;
            while (y && y%100 == 0) {
                y /= 100;
                c++;
            }
            if (c == d)
                cnt++;
        }
        if (cnt == n) {
            ans = x;
            break;
        }            
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
#include<iostream>
#define REP(i,n) for(int i=0;i<(int)n;++i)
#define REP(i,a,b) for(int i=(int)a;i<(int)b;++i)
using namespace std;
void solver(){
    REP(i,1,10){
        REP(j,1,10){
            cout<<i<<"x"<<j<<"="<<i*j<<endl;
        }
    }

}

int main() {
    solver();
}

Measured memory_bytes: 3044
#include <stdio.h>
  #define MAX 2000000
   
  int main() {
    int i, n, R[MAX], max, min;
   
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
      scanf("%d", &R[i]);
    }

   min = R[0];
   max = R[1] - R[0];

    for(i = 1; i < n; i++) {
      if(max < R[i] - min){
       max = R[i] - min;
     }
      if(min >= R[i]) {
        min = R[i];
      }
    }
   
    printf("%d\n", max);
    return 0;
  }

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

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll,int> plli;
typedef pair<double, int> pdbi;
typedef pair<int,pii> pipii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const ll INF = 1<<30;
const ll mod = 10000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

//#define int long long
#define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod

int n;
string joi;
int a[100000 + 5];
int dp[1005][1<<3];

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n;
    cin >> joi;
    rep(i, n) {
        if (joi[i] == 'J') a[i] = 0;
        else if (joi[i] == 'O') a[i] = 1;
        else a[i] = 2;
    }
    dp[0][1] = 1;
    rep(i, n)rep(j, 1<<3) {
        if (!(j>>a[i] & 1)) continue;
        rep(l, 1<<3) {
            if ((j&l) == 0) continue;
            addm(dp[i + 1][j], dp[i][l]);
        }
    }
    int ans = 0;
    rep(j, 1<<3) addm(ans, dp[n][j]);

    cout << ans << endl;
}

Measured memory_bytes: 3168
#pragma target("avx")
#include<bits/stdc++.h>
using ll = int_fast64_t;
using P = std::pair<ll,ll>;
using V = std::vector<ll>;
template<typename T> using pq = std::priority_queue<T>;
template<typename T> using rpq = std::priority_queue<T, std::vector<T>, std::greater<T>>;

#define rep(i,b,e) for(ll i=b; i<e; i++)
#define be(vec) vec.begin(),vec.end()
#define print(vec) printf("[ ");for(auto &i:vec)printf("%ld ",i);puts("]");
#define scan(vec) for(auto &i:vec)scanf("%ld",&i)
#define fi first
#define se second

const int MOD = 1e9+7;
const ll INF = 1e18;
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

int main(){
	int n;
	scanf("%d", &n);
	int a[n], max = 0;
	rep(i, 0, n){
		scanf("%d", &a[i]);
		if(a[i]>max) max = a[i];
	}
	double mid = max / 2.0;
	int idx = (a[0]==max);
	rep(i, 0, n){
		if(a[i]==max) continue;
		if(abs(a[idx]-mid)>abs(a[i]-mid)) idx = i;
	}
	printf("%d %d\n", max, a[idx]);
	return 0;
}


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

void println(std::vector<int> &vec) {
  for (int i = 0; i < vec.size(); i++) {
    std::cout << vec[i];
    if (i != vec.size() - 1) {
      std::cout << ' ';
    } else {
      std::cout << std::endl;
    }
  }
}

int main() {
  int n;
  std::cin >> n;
  std::vector<int> vec;
  while (n-- > 0) {
    int buf;
    std::cin >> buf;
    vec.push_back(buf);
  }

  println(vec);
  for (int i = 1; i < vec.size(); i++) {
    int key = vec[i];
    int j = i - 1;
    while (j >= 0 && vec[j] > key) {
      vec[j + 1] = vec[j];
      j--;
    }
    vec[j + 1] = key;
    println(vec);
  }
  return 0;
}



Measured memory_bytes: 3132
// ref. https://juppy.hatenablog.com/entry/2019/03/19/Educational_DP_Contest%28EDPC%29_M~P_Python_%E7%AB%B6%E6%8A%80%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0_Atcoder
// ref. https://atcoder.jp/contests/dp/submissions/3936122

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <unordered_map>
#include <vector>
#include <string.h>
#include <set>

using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> void printvec(const vector<T>& v) {
  for (auto x : v) { cout << x << " "; }
  cout << endl;
}
template<class T> void printtree(const vector< vector<T> >& tree) {
  for (long long i = 0; i < tree.size(); ++i) {
    cout << i + 1 << ": "; printvec(tree[i]);
  }
}
template<class T, class U> void printmap(const map<T, U>& mp) {
  for (auto x : mp) { cout << x.first << "=>" << x.second << endl; }
}

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

#define rep(i, n) for(ll i = 0; i < n; ++i)
#define all(s) s.begin(), s.end()
#define sz(x) (ll)(x).size()
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> triple;
typedef double D;
typedef vector<ll> vl;
typedef vector<P> vp;

const ll INF = 1e15;
const ll MOD = 1000000007;  // 1e9 + 7

const ll N = 405;
// dp[i][j] .. min cost of [i, j]
ll dp[N][N];

int main(int argc, char** argv) {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  //cout << setprecision(10) << fixed;

  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) {
    cin >> a[i];
  }
  vector<ll> s(n+1);
  rep(i, n) {
    s[i+1] = s[i] + a[i];
  }

  rep(i, n) rep(j, n) {
    dp[i][j] = INF;
  }
  rep(i, n) {
    dp[i][i] = 0;
  }
  for (ll d = 2; d <= n; ++d) { // distance. d=r-l+1
    rep(l, n) {
      ll r = l+d-1;
      if (r >= n) { continue; } // invalid
      for (ll k = l; k < r; ++k) {
        chmin(dp[l][r], dp[l][k] + dp[k+1][r]);
      }
      dp[l][r] += s[r+1] - s[l]; // sum of [l, r]
    }
  }
  cout << dp[0][n-1] << endl;
}


Measured memory_bytes: 1536
def f(n):
    if n%2 == 0:
        return int(n/2)
    else:
        return int(3*n+1)


s = int(input())

collatz = [s]
count = 1
continue_flg = True
while continue_flg:
    collatz_nums = set(collatz)
    count += 1
    s = f(s)
    for collatz_num in collatz_nums:
        if collatz_num == s:
            continue_flg = False
    collatz.append(s)
print(count)

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

const int N = 3e5;


struct SegmentTree {
  int n;
  vector<int> st;
  const int INF = numeric_limits<int>::min();
  SegmentTree(int N) {
    n = N;
    st.resize(4 * N);
  }

  inline int L(int n) {
    return (n << 1);
  }

  int query(int l, int r) {
    l = max(l, 0);
    r = min(r, n - 1);
    return rmq(0, n - 1, l, r, 1);
  }

  void update(int ind, int k) {
    upd(0, n - 1, ind, k, 1);
  }

  int upd(int l, int r, int ind, int val, int node) {
    if (l > ind || r < ind)
      return st[node];
    if (l == r) {
      st[node] = max(st[node], val);
      return st[node];
    }
    int mid = l + r >> 1;
    int left = upd(l, mid, ind, val, L(node));
    int right = upd(mid + 1, r, ind, val, L(node) + 1);
    return st[node] = max(left, right);
  }
  
  int rmq(int l, int r, int lb, int rb, int node) {
    if (l > rb || r < lb) 
      return INF;
    if (l >= lb && r <= rb)
      return st[node];
    int mid = l + r >> 1;
    int left = rmq(l, mid, lb, rb, L(node));
    int right = rmq(mid + 1, r, lb, rb, L(node) + 1);
    return max(left, right);
  }
};


int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
  vector<int> a(n);
  for (int i = 0; i < n; i++) 
    cin >> a[i];
  SegmentTree st(N + 1);
  vector<int> dp(n);
  for (int i = 0; i < n; i++) {
    dp[i] = max(dp[i], 1 + st.query(a[i] - k, a[i] + k));
    st.update(a[i], dp[i]);
  }
  cout << *max_element(dp.begin(), dp.end());
  return 0;
}

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

struct UnionFind{
    vector<int> d;
    UnionFind(int n=0): d(n, -1) {}
    int find(int x){
        if(d[x]<0)return x;
        return d[x] = find(d[x]);
    }
    bool unite(int x, int y){
        x = find(x); y = find(y);
        if(x==y) return false;
        if(d[x]>d[y]) swap(x,y);
        d[x] += d[y]; d[y] = x;
        return true;
    }
    bool same(int x, int y){return find(x) == find(y);}
    int size(int x){return -d[find(x)];}
};

int deg[100005];//友達の人数
//vector<int> to[100005];

int main(){
    int n,m,k;cin>>n>>m>>k;
    UnionFind uf(n);
    
    rep(i,m){
        int a,b;
        cin>>a>>b;
        --a; --b;
        deg[a]++;
        deg[b]++;
        uf.unite(a,b);
    }
    rep(i,k){
        int a,b;
        cin>>a>>b;
        --a; --b;
        if(uf.same(a,b)){
            deg[a]++;
            deg[b]++;
        }
        //to[a].push_back(b);
        //to[b].push_back(a);
    }
    
    rep(i,n){
        int ans = uf.size(i)-1-deg[i];
        /*
        for(int u: to[i]){
            if(uf.same(i,u)) --ans;
        }
        */
        printf("%d%c", ans, i==n-1?'\n':' ');
    }
    
    return 0;
}

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

int main()
{
    string s1,s2,s3;
    cin>>s1>>s2>>s3;
    int a=s1.length(),b=s2.length();
    if(s2[0]==s1[a-1]&&s3[0]==s2[b-1]) cout<<"YES";
    else cout<<"NO";

    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main(void){
    int n;

    cin >> n;

    if(n == 25){
        cout << "Christmas" << endl;
    }
    else if(n == 24){
        cout << "Christmas Eve" << endl;
    }
    else if(n == 23){
        cout << "Christmas Eve Eve" << endl;
    }
    else{
        cout << "Christmas Eve Eve Eve" << endl;
    }
    
    return 0;
}

Measured memory_bytes: 256
import sys
N=int(input())
A=list(map(int, input().split()))
for i in range(N):
    if A[i]==0:
        print(0)
        sys.exit()
ans=1
flg=0
for i in range(N):
    ans*=A[i]
    if ans>10**18:
        flg=1
        break
if flg==0:
    print(ans)
else:
    print(-1)


Measured memory_bytes: 21788
#include <iostream>

using namespace std;

int main() {
    int x, y;

    while (cin >> x >> y)
        if (x == 0 && y == 0)
            break;
        else if (x < y)
            cout << x << " " <<  y << endl;
        else
            cout << y << " " <<  x << endl;

    return 0;
}

Measured memory_bytes: 3080
#include <iostream>

using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  
  if(N < M) {
    int tmp = N;
    N = M;
    M = tmp;
  }
  
  if(N - M >= 2) {
    cout << "0" << endl;
    return 0;
  }
  
  long long ans = 0;
  long long Nkaijo = 1;
  long long Mkaijo = 1;
  for(int i = 1; i <= N; i++) {
    Nkaijo *= i;
    Nkaijo %= 1000000007;
  }
  for(int i = 1; i <= M; i++) {
    Mkaijo *= i;
    Mkaijo %= 1000000007;
  }
  
  ans = (Nkaijo * Mkaijo) % 1000000007;
  if(N == M) {
    ans *= 2;
    ans %= 1000000007;
  }
  cout << ans << endl;
  
  return 0;
}
  
    
    

Measured memory_bytes: 256
#include<cstdio>
#include<algorithm>
#include<cstring>
#define MN 20005
using namespace std;
int n,a[MN],ax[MN];
int main(){
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	scanf("%d",&n);
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)ax[a[i]]=i;
	for(int i=1;i<=n;i++)printf("%d ",ax[i]+(i*30000));puts("");
	for(int i=1;i<=n;i++)printf("%d ",ax[i]+(n-i+1)*30000);
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
 
int main(){
	long long int N,L;
	cin >> N >> L;
	long long int a[N];
    for(int i=0;i<N;i++){
      cin >> a[i];
    }
    long long int c = N*(N-1)*(N-2) / 6;
    long double x = 0;
    long double y = 0;
    long double pi = 3.14159265358979323846264338327950288;
    for(int j=0;j<N-1;j++){
          for(int k=j+1;k<N;k++){
      x = x + (N - 2 * (k - j)) * cos(pi * (a[j] + a[k]) / L) / c;
      y = y + (N - 2 * (k - j)) * sin(pi * (a[j] + a[k]) / L) / c;
          }
    }
    cout << fixed;
	cout << setprecision(10) << x << " " << y <<endl;
}

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main(){

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

  function< int(vector<int>) > rec =
  [&](vector<int> a){
    int ans = a[0];
    int res = a[0];
    for(int i = 0; i < n; i++){
      if(__gcd(ans,a[i]) <= __gcd(ans,res)){
        ans = __gcd(ans,res);
        res = a[i];
      }
      else {
        ans = __gcd(ans,a[i]);
      }
    }
    return ans;
  };

  int ans = 1;
  ans = max(ans,rec(a));
  reverse(a.begin(),a.end());
  ans = max(ans,rec(a));
  cout << ans << endl;

  return 0;
}


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

using namespace std;
using ll =long long;

#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cout << #x << " = " << (x) << endl;
void coY() {cout <<"Yes"<<endl;}
void coN(){cout <<"No"<<endl;}

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
const ll INF = 1LL << 60;

//Write From this Line

int main()
{
	ll n; 
	cin >>  n; 
	ll a , b , c; 
	for(a = 1 ; a <= 3500 ; a ++) {
		for( b=  1 ; b <= 3500 ; b++){
			ll mother = 4 * a * b -(n*a) - (n*b);
			ll child = n * a * b ;
			if(mother == 0) continue;
			if(child % mother != 0 ){
				continue;
			}
			else{
				c = child / mother ;
				if( c <= 0 ) continue;
				cout << a << " " << b << " " ;
				cout << child/mother  << endl;
				return 0 ;
			}

		}
	}
	cout << - 1<< endl;

}


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

int main(void)
{
	int L[10];
	int V[2];
	char C[11];

	while(cin >> L[0] >> C[0])
	{
		for(int i=1; i<10; i++)
			cin >> L[i] >> C[i];
		cin >> V[0] >> C[10] >> V[1];

		int sum = 0;
		for(int i=0; i<10; i++)
			sum += L[i];

		double col_point = 0;
		col_point = ((double)sum)/((double)V[0]+V[1])*V[0];

		int ruikei = 0;
		for(int i=0; i<10; i++)
		{
			ruikei += L[i];
			if(col_point <= ruikei)
			{
				cout << (i+1) << endl;
				break;
			}
		}
	}
	return 0;
}

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

int main() {

	int n, m, c;
	scanf("%d %d %d", &n, &m, &c);
	int a[n][m];
	int b[m];
	int sig = 0;
	int ans = 0;
	for(int i = 0; i < m; i++) {
		scanf("%d", &b[i]);
	}
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			scanf("%d", &a[i][j]);
		}
	}
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			sig += a[i][j] * b[j];
		}
		if(sig + c > 0) {
			ans++;
		}
		sig = 0;
	}
	printf("%d\n", ans);
}

Measured memory_bytes: 256
//専用ライブラリ
#include <iostream>
#include <iomanip>
#include <istream>
#include <ostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <ctime>
#include <cctype>
#include <cstdlib>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll>P;
#define INF 10e9
#define MOD 1000000007
#define rep(i, a, n) for (ll i = a; i < (ll)(n); i++)
#define fi first
#define se second
#define mmax(x,y)(x>y?x:y)
#define mmin(x,y)(x<y?x:y)
#define PI acos(-1.0)
long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; }
long long LCM(long long a, long long b)  {return a/GCD(a,b)*b;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
int cmp(P a,P b){
        if(a.fi!=b.fi)
        return a.fi>b.fi;
        else
        return a.se<b.se;
}

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    int ans=0;
    vector<int> nums;
    
    if(s[0]=='0')
       nums.push_back(0);//最开始的0和最后面的0不需要计算，所以加2个0当作首尾1的个数
    for(int i=0;i<s.length();)
    {
        int j=i;
        while(j<s.length()&&s[j]==s[i])
            j++;
        nums.push_back(j-i);
        i=j;
    }
    if(s.back()=='0')
        nums.push_back(0);//最开始的0和最后面的0不需要计算，所以加2个0当作首尾1的个数
    vector<int> sums(nums.size()+1,0);
    for(int i=0;i<nums.size();i++)
        sums[i+1]=sums[i]+nums[i];//DP思想
    for(int left=0;left<sums.size();left+=2)
    {
        int right=left+k*2+1;
        if(right>=sums.size())
            right=sums.size()-1;
        ans=max(ans,sums[right]-sums[left]); //普通的搜索
    }
    cout<<ans;
    return 0;
}


Measured memory_bytes: 1272
#include <iostream>
#include <string> 
#include <math.h>
#include <algorithm>
#include <cstdlib>
#include <vector>
using namespace std;

int main()
{
	int A, B, C;
	cin >> A;
	cin >> B;
	cin >> C;
	if (C >= A && C <= B)
	{
		cout << "Yes" << endl;
	}
	else
	{
		cout << "No" << endl;

	}
    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,c) for(int i=0;i<c;++i)
int main(){
int a,b,c,d;
cin >> a>> b>> c>>d;
int e = a+c;
int f = a+d;
int g = b+c;
int h = b+d;
cout << min(min(e,f),min(g,h)) << endl;
return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main() {
    int N, cnt = 0;
    char c;
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> c;
        if (c == 'R') cnt++;
        else cnt--;
    }
    cout << (cnt > 0 ? "Yes" : "No") << endl;
}

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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1e9
typedef long long ll;


int main() {
    int N,X; cin >> N >> X;
    vector<int> m(N);

    REP(i,N){
        cin >> m[i];
        X -= m[i];
    }
    sort(ALL(m));

    cout << N + X/m[0] << endl;
    return 0;
}

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

int main() {
  int N; cin >> N;
  int x=0;
  for (int i=1;i<=N;i+=2) {
    int count=0;
    for (int j=1;j<=i;j+=2) {
      if (i % j == 0) count++;
    }
    if (count>=8) x++;
  }
  cout << x << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<climits>
#include<bitset>
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define int long long
#define mod 1000000007
using namespace std;
inline int read(){
    int f=1,ans=0;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){ans=ans*10+c-'0';c=getchar();}
    return f*ans;
}
const int MAXN=2e5+11;
int N,M,X[MAXN],Y[MAXN],Sx[MAXN],Sy[MAXN],Ans1,Ans2;
int Mod(int x){return ((x%mod)+mod)%mod;}
signed main(){
    N=read(),M=read();for(int i=1;i<=N;i++) X[i]=read();for(int i=1;i<=M;i++) Y[i]=read();
    for(int i=N;i>=1;i--) Sx[i]=Sx[i+1]+X[i];for(int i=M;i>=1;i--) Sy[i]=Sy[i+1]+Y[i];
    for(int i=1;i<=N;i++) Ans1+=Mod(Sx[i+1]-X[i]*(N-i)),Ans1%=mod;for(int i=1;i<=M;i++) Ans2+=Mod(Sy[i+1]-Y[i]*(M-i)),Ans2%=mod;
    printf("%lld\n",Ans1*Ans2%mod);return 0;
}



Measured memory_bytes: 6264
l=[]
for _ in range(int(input())):
    s = input()
    l.append(s)
ac,wa,tle,reg=l.count('AC'),l.count('WA'),l.count('TLE'),l.count('RE')
print("AC x {}".format(ac))
print("WA x {}".format(wa))
print("TLE x {}".format(tle))
print("RE x {}".format(reg))


Measured memory_bytes: 16224
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <cassert>
#include <unordered_map>

#define _(x) { cout << #x << " = " << x << " "; }

const double E = 1e-8;
const double PI = acos(-1);

using namespace std;

bool isp(string s) {
    string t(s.rbegin(), s.rend());
    return t == s;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s, s1, s2;
    cin >> s;
    int n = s.length();

    if (isp(s) && isp(s.substr(0, (n-1)/2)) && isp(s.substr((n+1)/2))) {
        cout << "Yes\n";
        return 0;
    }

    cout << "No\n";
    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
	string s;
	cin >> s;
	string a="KIHBR...";
	int flag = 0;
	int x = 0;
	int ans = 1;
	for(int i = 0; i < s.size(); i++){
		if(s[i] == 'A'){
			if(flag == 0){
				flag = 1;
			}else ans = 0;
		}else
		if(s[i] == a[x]){
			if(x == 0 || x == 1)flag = 1;
			else flag = 0;
			x++;
		}else
		ans = 0;
	}
	if(ans && x ==5)cout <<"YES"<<endl;
	else cout <<"NO"<<endl;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a[5];
    int ans=0;
    int sum=0;
    for(int i=0; i<5;i++){
        cin>>a[i];
        if((a[i]%10) && (ans<10-a[i]%10)) ans = 10-a[i]%10;
        if(a[i]%10) sum += (a[i]/10+1)*10;
        else sum += a[i];
    }
    cout << sum -ans << endl;
    
    
}


Measured memory_bytes: 256
n, m = map(int, input().split())

dp = [float('inf') for _ in range(2 ** n)]
dp[0] = 0

for i in range(m):
    a, b = map(int, input().split())
    c = list(map(int, input().split()))
    k = 0
    p = 1
    for x in c:
        k += 2 ** (x-1)
    for j in range(2 ** n):
        L = j | k
        dp[L] = min(dp[L], dp[j] + a)

#print(dp)

if dp[-1] == float('inf'):
    print(-1)
else:
    print(dp[-1])

Measured memory_bytes: 49372
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<map>
#include<queue>
#include<set>
#include<vector>
#include<cstdio>

using namespace std;




int main()
{
	int N, ans = 0, check = 0;
	char c;
	vector<int> S, a;

	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> c;
		if (c == 'W') {
			S.push_back(1);
		}
		else {
			S.push_back(0);
		}
	}

	int mark = 0;
	for (int j = 1; j < N; j++) {
		if (S[j] == 0) mark++;
	}
	ans = mark;
	check = mark;
	for(int i = 1; i < N; i++){
		if (S[i - 1] == 1) check++;
		if (S[i] == 0) check--;
		ans = min(ans, check);
	}
	


	cout << ans;

	char qq;
	cin >> qq;
	return 0;
}

Measured memory_bytes: 2420
#include <bits/stdc++.h>
#define rep(i,n) for (int i= 0;i<(n);++i)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const long long INF = 1LL << 60;
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; }

int main(){
    int n;cin>>n;
    vector<int> a(n);rep(i,n)cin>>a[i];
    int count =0;
    for(int i = 0;i<n;i+=2){
        if(a[i]%2==1)count++;
    }
    cout << count << endl;
}

Measured memory_bytes: 3620
n, m = map(int, input().rstrip().split())
h = list(map(int, input().rstrip().split()))
l = []
high = [0] * n
count = 0

for i in range(m):
    a, b = map(int, input().rstrip().split())
    l.append([a, b])

for i in l:
    if h[i[0]-1] < h[i[1]-1]:
        high[i[0]-1] += 1
    elif h[i[0]-1] == h[i[1]-1]:
        high[i[0]-1] += 1
        high[i[1]-1] += 1
    else:
        high[i[1]-1] += 1

for hi in high:
    if hi == 0:
        count += 1
print(count)

Measured memory_bytes: 28776
h,w = map(int,input().split())

arr = ["0" *(w + 2)]
for i in range(h):
    arr.append("0" + input() + "0")
arr.append("0" *(w + 2))

flg = [[-1 for i in range(w+2)] for j in range(h+2)]

ima = []
for i in range(h+2):
    for j in range(w+2):
        if arr[i][j] == "#":
            ima.append([i,j])
            flg[i][j] = 0

while len(ima) > 0:
    tugi = []
    for x,y in ima:
        if arr[x+1][y] == "." and flg[x+1][y] == -1:
            tugi.append([x+1,y])         
            flg[x+1][y] = flg[x][y] + 1
        if arr[x-1][y] == "." and flg[x-1][y] == -1:
            tugi.append([x-1,y])
            flg[x-1][y] = flg[x][y] + 1
        if arr[x][y+1] == "." and flg[x][y+1] == -1:
            tugi.append([x,y+1])
            flg[x][y+1] = flg[x][y] + 1
        if arr[x][y-1] == "." and flg[x][y-1] == -1:
            tugi.append([x,y-1])
            flg[x][y-1] = flg[x][y] + 1
    ima = tugi

ans = -1
for i in flg:
    for j in i:
        if ans < j:
            ans = j
print(ans)

Measured memory_bytes: 146132
#include<cstdio>
#include<algorithm>
using namespace std;
#define MAXN 100010
#define LL long long
int Adj[MAXN],V[MAXN*2],nxt[MAXN*2],W[MAXN*2],c=1;
void AddEdge(int u,int v)
{
	c++;V[c]=v,nxt[c]=Adj[u],W[c]=1;Adj[u]=c;
	c++;V[c]=u,nxt[c]=Adj[v],W[c]=2;Adj[v]=c;
}
int col[MAXN],vis[MAXN],vise[MAXN],cnt[3],flag,cV,E;
LL ans;
void dfs(int u)
{
	cV++;
	cnt[col[u]]++;
	vis[u]=1;
	for(int i=Adj[u];i;i=nxt[i])
	{
		if(vise[i>>1]) continue;
		vise[i>>1]=1;
		E++;
		if(!vis[V[i]])
		{
			col[V[i]]=(col[u]+W[i])%3;
			dfs(V[i]);
		}
		else if(col[V[i]]!=(col[u]+W[i])%3)
			flag=1;
	}
}
int x[MAXN],y[MAXN];
int main()
{
	int n,m,u,v;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		scanf("%d%d",&x[i],&y[i]);
		AddEdge(x[i],y[i]);
	}
	for(int i=1;i<=m;i++)
		if(!vise[i])
		{
			flag=cnt[0]=cnt[1]=cnt[2]=cV=E=0;
			dfs(x[i]);
			if(flag) ans+=1LL*cV*cV;
			else if(!cnt[0]||!cnt[1]||!cnt[2]) ans+=E;
			else ans+=1LL*cnt[0]*cnt[1]+1LL*cnt[1]*cnt[2]+1LL*cnt[2]*cnt[0];
		}
	printf("%lld\n",ans);
}

Measured memory_bytes: 7936
import java.io.*;
import java.util.*;


public class Main {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int h = sc.nextInt();
		int w = sc.nextInt();

		char[][] a = new char[h][];
		
		for (int i = 0; i < h; i++) {
			a[i] = sc.next().toCharArray();
		}

		Set<Integer> hsw = new HashSet<>();
		out:
		for (int i = 0; i < w; i++) {
			for (int j = 0; j < h; j++) {
				if (a[j][i] == '#') {
					continue out;
				}
			}
			
			hsw.add(i);
		}
		
		Set<Integer> hsh = new HashSet<>();
		out:
		for (int j = 0; j < h; j++) {
			for (int i = 0; i < w; i++) {
				if (a[j][i] == '#') {
					continue out;
				}
			}
			
			hsh.add(j);
		}

		for (int i = 0; i < h; i++) {
			if (hsh.contains(i)) {
				continue;
			}
			for (int j = 0; j < w; j++) {
				if (hsw.contains(j)) {
					continue;
				}
				
				pr.print(a[i][j]);
			}
			pr.println();
		}
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(INPUT == null ? System.in : new ByteArrayInputStream(INPUT.getBytes()));
		pr = new Printer(System.out);

		solve();

//		pr.close();
		pr.flush();
//		sc.close();
	}

	static String INPUT = null;

	private static class Printer extends PrintWriter {
		Printer(OutputStream out) {
			super(out);
		}
	}
}


Measured memory_bytes: 24020
#include <iostream>
#include <map>
#include <string>

using namespace std;

int main(void)
{
	string S;
	cin >> S;
	for (int i = 0; i < S.size(); i++){
		if (i % 2 == 0 && S[i] == 'L'){
			cout << "No" << endl;
			return 0;
		}
		else if (i % 2 == 1 && S[i] == 'R'){
			cout << "No" << endl;
			return 0;
		}
	}
	cout << "Yes" << endl;
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 7;

int32_t main()
{

    int n,k;
    cin >> n >> k;
    int h[n];
    for(int i = 0; i < n; i++) cin >> h[i];
    vector<int> dp(n, inf);
    dp[0] = 0;
    for(int i = 1; i < n; i++) {
        for(int j = 1; j <= min(i,k); j++){
            dp[i] = min(dp[i],dp[i-j] + abs(h[i] - h[i-j]));
           // cout<<i<<" "<<dp[i]<<endl;
        }
    }

    cout << dp[n-1] << endl;
}



Measured memory_bytes: 4016
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
const int INF = 1001001001;
const ll LINF = 1001001001001001;
const int MOD = 1000000007;

template <typename T>
void print(const T &v);

int main()
{
    ll n, m;
    cin >> n >> m;
    vector<ll> divs;
    for (int i = 1; i * i <= m; i++)
    {
        if (m % i == 0)
        {
            divs.push_back(i);
            divs.push_back(m / i);
        }
    }
    sort(divs.begin(), divs.end());

    // 二分探索
    ll left = 0;
    ll right = divs.size();

    while (right - left > 1)
    {
        ll mid = (left + right) / 2;
        if (m / divs[mid] < n)
        {
            right = mid;
        }
        else if (m / divs[mid] > n)
        {
            left = mid;
        }
        else
        {
            cout << divs[mid] << endl;
            return 0;
        }
    }
    cout << divs[int(left)] << endl;
    return 0;
}

// Use For Debug
template <typename T>
void print(T const &v)
{
    for (int i = 0; i < v.size(); i++)
    {
        if (i)
            cout << ' ';
        cout << v[i];
    }
    cout << endl;
}

Measured memory_bytes: 3588
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define MP make_pair
#define PB push_back
#define F first
#define S second
#define INF 1 << 30
#define EPS 1e-10

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N;
int k[10005];

int main(){
  while(scanf("%d", &N) && N){
    rep(i, N) scanf("%d", k+i);

    int f = 0;
    rep(i, N) if(k[i] > 1) f = 1;
    if(f){
      int c = 0;
      rep(i, N) if(k[i] == 0) c++;
      printf("%d\n", N+1-c);
    }else{
      puts("NA");
    }
  }
  return 0;
}

Measured memory_bytes: 1232
#include <stdio.h>
#include <string.h>

int main(void){
	char S[100],T[100];
	scanf("%s",&S);
	scanf("%s",&T);
	int size = strlen(S);//strlen()で文字列の長さ
	char U;
	int i,j;
	for(i=0;i<size;i++){
		for(j=0;j<size-1;j++){
			U = S[j];
			S[j] = S[j+1];
			S[j+1] = U;
		}
		if(strcmp(S,T) == 0){
			printf("Yes");
			return 0;
		}
	}
	printf("No");
	return 0;
}

Measured memory_bytes: 128
#include <stdio.h>

int main(void)
{
	double mt;
	double high;
	double low;
	
	scanf("%lf", &mt);
	high = mt;
	low = mt;
	
	while (scanf("%lf", &mt) != EOF){
		if(mt > high){
			high = mt;
		}
		if(mt < low){
			low = mt;
		}
	}
	
	printf("%.1lf\n", high - low);
	
	return (0);
}

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

int main(void){
    int N, X;
    int bound_count = 0;
    int i;
    vector<int> D;
    vector<int> L;
    D.emplace_back(NULL);
    L.emplace_back(NULL);
    cin >> N >> X;
    for(i = 1; i <= N; i++){
        int l;
        cin >> l;
        L.emplace_back(l);
    }
    for(i = 1; i <= N+1; i++){
        if(i == 1) D.emplace_back(0);
        else D.emplace_back(D[i-1] + L[i-1]);
        if(D[i] <= X) bound_count++;
        else break;
    }
    cout << bound_count << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int, int> P;

int h, w, ans;
int castle[1001][1001], times[1001][1001];
int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1}, dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};
char beach[1001][1001];

int main(){
    cin >> h >> w;
    for(int i = 0; i < h; i++){
        cin >> beach[i];
    }

    queue<P> Q;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(beach[i][j] == '.'){
                Q.push(P(i, j));
            } else{
                castle[i][j] = (int)beach[i][j] - 48;
            }
        }
    }

    while(Q.size()){
        P p = Q.front();
        Q.pop();
        ans = max(ans, times[p.first][p.second]);

        for(int i = 0; i < 8; i++){
            int y = p.first + dy[i], x = p.second + dx[i];
            if(y < 0 || y >= h || x < 0 || x >= w){
                continue;
            }

            castle[y][x]--;
            if(castle[y][x] == 0){
                Q.push(P(y, x));
                times[y][x] = times[p.first][p.second] + 1;
            }
        }
    }

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 10152
import java.io.BufferedInputStream;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    static final int MAX = 1_000_000_000;
    public static void main(String[] args) {
        //System.setIn(Main.class.getResourceAsStream("input"));
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        int  n = sc.nextInt();
        sc.nextLine();
        int[] a = toArray(sc.nextLine());
        int current = 1;
        for (int i = 0; i < a.length; i++) {
            if(a[i] == current) {
                current++;
            }
        }
        if(current == 1) System.out.println(-1);
        else System.out.println(n-current+1);
    }

    static long pgcd(long a, long b) {
        if(b == 0) return a;
        return pgcd(b, a%b);
    }
    private static int nextInt(Scanner sc) {
        return Integer.valueOf(sc.nextLine());
    }
    private static int[] toArray(String text) {
        return Arrays.stream(text.split("\\s")).mapToInt(Integer::valueOf).toArray();
    }

}

Measured memory_bytes: 57372