akhauriyash/Code-Regression huggingface dataset:
#include <stdio.h>
  int xy[15][15]={};
  int main(){
      int x,y,s,i,j,num=0;
      while(scanf("%d,%d,%d",&x,&y,&s) != EOF){
          if(s==1){
              xy[x][y]++;
              xy[x-1][y]++;
              xy[x+1][y]++;
              xy[x][y-1]++;
              xy[x][y+1]++;
          }else if(s==2){
              xy[x-1][y-1]++;
              xy[x-1][y]++;
              xy[x-1][y+1]++;
              xy[x][y-1]++;
              xy[x][y]++;
              xy[x][y+1]++;
              xy[x+1][y-1]++;
              xy[x+1][y]++;
              xy[x+1][y+1]++;
          }else if(s==3){
              xy[x-2][y]++;
              xy[x-1][y-1]++;
              xy[x-1][y]++;
              xy[x-1][y+1]++;
              xy[x][y-2]++;
              xy[x][y-1]++;
              xy[x][y]++;
              xy[x][y+1]++;
              xy[x][y+2]++;
              xy[x+1][y-1]++;
              xy[x+1][y]++;
              xy[x+1][y+1]++;
              xy[x+2][y]++;
          }
      }
      int max=xy[0][0];
      for(i=0;i<10;i++){
          for(j=0;j<10;j++){
              if(xy[i][j]==0) num++;
              if(xy[i][j]>max) max=xy[i][j];
          }
      }
      printf("%d\n%d\n",num,max);
      return 0;
  }

Measured memory_bytes: 1028
# /usr/bin/python
# -*- coding: utf-8 -*-
import math



N,K = map(int, input().split())

ans = 0
for i in range(1,N+1):
  if i < K:
    cnt = math.ceil(math.log2(K/i))
  else:
    cnt = 0
  ans += (1/2)**cnt

print(ans/N)

Measured memory_bytes: 3188
from math import sqrt

while True:
    inp = raw_input()
    if inp == "0 0 0": break
    cs = map(int,inp.split())
    cs.sort()
    c = sqrt(cs[0]**2 + cs[1]**2)/2

    n=int(raw_input())

    for gomi in xrange(n):
        r = int(raw_input())
        if c<r: print "OK"
        else: print "NA"

Measured memory_bytes: 4352
#include <stdio.h>
#define EXCHANGE(a, b){ int t; t = a; a = b; b = t; }

int GCD(int a,  int b)
{
  return b == 0 ? a : GCD(b, a % b);
}

int main()
{
  int a, b, gcd;

  while (scanf("%d %d", &a, &b) > 0) {
    if (a < b) { EXCHANGE(a, b) };
    gcd = GCD(a, b);
    printf("%ld %ld\n", gcd, a  / gcd * b);
  }

  return 0;
}

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

main()
{
   long long int n,i,j,k,sum=0;
   cin>>n;
   long long int a[n];
   for(i=0;i<n;i++)
   {
       cin>>a[i];
       sum+=a[i]-1;
   }
   
   cout<<sum<<endl;
}

Measured memory_bytes: 256
n, k = map(int, input().split())
p = list(map(int, input().split()))
c = list(map(int, input().split()))
INF = 10 ** 18


def solve(v):
    res = []
    set_ = set()
    tot = 0
    while True:
        nxt_v = p[v] - 1
        cost = c[nxt_v]
        if nxt_v not in set_:
            set_.add(nxt_v)
            tot += cost
            res.append(tot)
            v = nxt_v
        else:
            break
    return res, tot


ans = -INF

# 1回移動する場合
for v in range(n):
    ans = max(c[v], ans)

for v in range(n):
    res, tot = solve(v)
    if tot < 0:
        for i in range(min(k, len(res))):
            ans = max(res[i], ans)
    else:
        lap, rem = divmod(k, len(res))
        tmp = lap * tot
        if lap > 0:
            ans = max(ans, tmp)
        for i in range(min(rem, len(res))):
            ans = max(tmp + res[i], ans)
            
        if lap == 0:
            continue
        lap -= 1
        tmp = lap * tot
        for i in range(min(k, len(res))):
            ans = max(tmp + res[i], ans)
            
print(ans)

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

typedef long long ll;

template<typename T>
void out(T x) { cout << x << endl; exit(0); }
#define watch(x) cout << (#x) << " is " << (x) << endl

const int maxn = 1e6 + 5;

typedef pair<int,int> pt;

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

  int n;
  cin>>n;
  vector<pt> red(n);
  for (int i=0; i<n; i++) {
    int x,y; cin>>x>>y;
    red[i].first=y;
    red[i].second=x;
  }

  vector<pt> blue;
  for (int i=0; i<n; i++) {
    int x,y; cin>>x>>y;
    blue.push_back({x,y});
  }

  sort(blue.begin(), blue.end());
  sort(red.begin(),red.end(),greater<pt>());

  
  vector<bool> used(n);
  int res = 0;

  
  for (int j=0; j<n; j++) {
    pt p = blue[j];
    for (int i=0; i<n; i++) {
      pt r = red[i];
      if (r.second < p.first && r.first < p.second && !used[i]) {
	res++;
	used[i]=true;
	break;
      }
    }
  }


  cout<<res<<endl;

  return 0;
}


Measured memory_bytes: 256
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <queue>

using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) for(int i=0;i<n;++i)
#define mod (ll)(1e9+7)
#define FIX(a) ((a)%mod+mod)%mod
#define ALL(obj) (obj).begin(), (obj).end()
#define rALL(obj) (obj).rbegin(), (obj).rend()
#define INF 1000000000 //1e9
#define LLINF 2000000000000000000LL //2e18
#define fi first
#define se second
#define pb push_back
int dy[]={0, 0, 1, -1};
int dx[]={1, -1, 0, 0};

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    s[3] = '8';
    cout << s << endl;

    return 0;
}

Measured memory_bytes: 3588
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define pb emplace_back
typedef long long ll;
typedef pair<int,int> pint;

typedef pair<pint,pint> ppi;
int t[10001];
vector<pint> vp[10001];
int d[10001][201][3];
int INF=1000100010;
int n,x;
int dijkstra(int st){
    priority_queue<ppi,vector<ppi>,greater<ppi> > pq;
    rep(i,n)rep(j,201)rep(k,3) d[i][j][k]=INF;
    d[st][0][0]=0;
    pq.push({{0,st},{0,0}});
    while(!pq.empty()){
        ppi pi=pq.top();pq.pop();
        int ti=pi.first.second;
        int di=pi.first.first;
        pint ui=pi.second;
        if(d[ti][ui.second][ui.first]<di) continue;
        for(auto it:vp[ti]){
            if(t[it.second]==1){
                int nx=min(x,ui.second+it.first);
                if(d[it.second][nx][ui.first]>di+it.first){
                    d[it.second][nx][ui.first]=di+it.first;
                    pq.push({{d[it.second][nx][ui.first],it.second},{ui.first,nx}});
                }
            }
            else{
                if(t[it.second]==ui.first||ui.second+it.first>=x){
                    if(d[it.second][0][t[it.second]]>di+it.first){
                        d[it.second][0][t[it.second]]=di+it.first;
                        pq.push({{d[it.second][0][t[it.second]],it.second},{t[it.second],0}});
                    }
                }
            }
        }
    }
}
int main(){
    int m;
    cin>>n>>m>>x;
    rep(i,n) cin>>t[i];
    int sr,dt,ds;
    rep(i,m){
        cin>>sr>>dt>>ds;
        --sr,--dt;
        vp[sr].pb(ds,dt);
        vp[dt].pb(ds,sr);
    }
    dijkstra(0);
    int mn=1000100010;
    rep(i,201)rep(j,3) mn=min(mn,d[n-1][i][j]);
    cout<<mn<<endl;
    return 0;
}


Measured memory_bytes: 27656
// A

#include <bits/stdc++.h>
using ll = long long;
using namespace std;
ll mod=1e9+7;

int main(){
  int H,W,N;
  cin>>H>>W>>N;
  cout << (N+max(H,W)-1) / max(H,W) << endl;
  return 0;
}



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

yaks = []

for i in range(1, N + 1) :
    cnt = 0
    
    for j in range(1, i) :
        if i % j == 0 :
            cnt += 1
            
    if cnt == 7 and i % 2 != 0:
        yaks.append(i)

print(len(yaks))

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

int main() {
    int a, b, c;
    cin >> a >> b >> c;
    
    bool flg = false;
    if ((a == 5 and b == 5 and c == 7)
        or (a == 5 and b == 7 and c == 5)
        or ((a == 7 and b == 5 and c == 5))) {
        flg = true;
    }
    
    cout << (flg ? "YES" : "NO") << endl;
}


Measured memory_bytes: 256
import sys
input = sys.stdin.readline



def main():
    n, k = map(int, input().split())
    p = [int(x)-1 for x in input().split()]
    c = list(map(int, input().split()))
    
    doubling = [[0]*n for i in range(31)]
    doubling_max = [[0]*n for i in range(31)]
    place = [[0]*n for i in range(31)]
    
    ans = -1000000000
    for i in range(n):
        sub = c[p[i]]
        if sub >= 0:
            break
        if ans < sub:
            ans = sub
        if i == n-1:
            print(ans)
            sys.exit()
            
    
    for i in range(31):
        if i == 0:
            for j in range(n):
                doubling[i][j] = c[p[j]]
                place[i][j] = p[j]
            continue
        for j in range(n):
            place[i][j] = place[i-1][place[i-1][j]]
            doubling[i][j] = doubling[i-1][j]+doubling[i-1][place[i-1][j]]
    
    for i in range(31):
        if i == 0:
            for j in range(n):
                doubling_max[i][j] = doubling[i][j]
            continue
        for j in range(n):
            doubling_max[i][j] = max(doubling_max[i-1][j], doubling[i-1][j]+doubling_max[i-1][place[i-1][j]])
    
    ans = 0
    judge = []
    key = 0
    while k:
        if k%2:
            judge.append(key)
        k //= 2
        key += 1
    judge.reverse()
    
    for i in range(n):
        pl = i
        key = 0
        for j in range(len(judge)):
            if ans < key+doubling_max[judge[j]][pl]:
                ans = key+doubling_max[judge[j]][pl]
            key += doubling[judge[j]][pl]
            pl = place[judge[j]][pl]
        

    print(ans)
        
    


    
    
if __name__ == "__main__":
    main()


Measured memory_bytes: 72532
#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main(){
    int n;
    double a[1001],s,e;
    do{
    cin >>n;
    if(n==0)
        break;
    s=0;
    e=0;
    for(int i=0;i<n;i++)  
    {
        cin >>a[i];
        s+=(a[i]/n);
    }
    for(int i=0;i<n;i++){
      e+=((a[i]-s)*(a[i]-s))/n;  
    }
    cout<<fixed;
    cout <<setprecision(8)<<sqrt(e)<<endl;}while(true);
    return 0;
}

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

int n;

template<typename Drake>
void read(Drake &x){
	x=0;char ch=getchar();bool f=1;
	while(!isdigit(ch)){
		if(ch=='-')f^=1;
		ch=getchar();
	}
	while(isdigit(ch)){
		x=(x<<1)+(x<<3)+(ch^48);
		ch=getchar();
	}
	x*=(2*f-1);
}

template<typename Drake>
void write(Drake x){
	if(x<0){x=-x;putchar('-');}
	if(x>9)write(x/10);
	putchar(x%10+'0');
}

template<typename Drake>
void writeln(Drake x){
	write(x);
	puts("");
}

void init(){
	read(n);
}

int main(){
	init();
	int Ans=0;
	for(register int i=105;i<=n;i+=2){
		int tmp=0;
		for(register int j=1;j<=i;j++){
			tmp+=((i%j)==0);
		}
		Ans+=(tmp==8); 
	}
	cout<<Ans<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main() {
	string s;
	int add = 0;
	cin >> s;
	for (int i = 0; i < 3; i++) {
		if (s[i] == 'o') {
			add++;
		}
	}
	cout << 700 + add * 100 << endl;

	return 0;
}

Measured memory_bytes: 256
#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;i++)
typedef long long ll;

int main(){
  int a,b;
  cin >> a >> b;
  if(a==b)cout << 'a' << ' ' << "==" << ' ' <<  'b' << endl;
  else if(a<b)cout << 'a' << ' ' << '<' << ' ' <<  'b' << endl;
  else  cout << 'a' << ' ' << '>' << ' ' <<  'b' << endl;
}



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

int main() {
	int N;
	cin >> N;
	int n = N;
	int fx = 0;
	for (int i=1000000000; i>0; i/=10) {
		if (N>=i) {
			fx +=  n/i;
			n -= i*(n/i);
		}
	}
	if (N%fx==0) puts("Yes");
	else puts("No");
}

Measured memory_bytes: 256
N = int(input())
A = [int(x) for x in input().split()]

S = 0
r = 0
ans = 0
for l, a in enumerate(A):
	while r < N and S + A[r] == S ^ A[r]:
		S += A[r]
		r += 1
	ans += r - l	
	S -= a
print(ans)

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

// types
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;

// macros
#define ALL(a) a.begin(),a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i,n) for(int i=0;i<((int)n);i++)
#define REP1(i,n) for(int i=1;i<((int)n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define PB push_back
#define EB emplace_back
#define MP(a,b) make_pair(a,b)
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()), c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
#define GET_POS(c,x) (lower_bound(c.begin(),c.end(),x)-c.begin())
#define Decimal fixed<<setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int mod = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

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

  ll n;
  cin >> n;

  vector<ll> a(n);
  REP(i, n)
    cin >> a[i];

  //sort(a.begin(), a.end());
  map<ll,ll> amap;
  REP(i, n)
    amap[a[i]]++;

  ll ans = 0;
  REP(i, 1e5){
    ll sum = 0;
    REP(j, 3)
      sum += amap[i+j];

    ans = max(ans, sum);
  }

  cout << ans << endl;


  
}


Measured memory_bytes: 7296
var n,k,i,min:longint;
h:array[1..100200]of longint;
procedure qsort(l,r:longint);
var i,j,k,mid:longint;
begin 
	i:=l;j:=r;
	mid:=h[(i+j) div 2];
	repeat
		while h[i]<mid do inc(i);
		while h[j]>mid do dec(j);
		if i<=j then 
		begin 
			k:=h[i];h[i]:=h[j];h[j]:=k;
			inc(i);
			dec(j);
		end;
	until i>j;
	if i<r then qsort(i,r);
	if j>l then qsort(l,j);
end;
begin 
	read(n,k);
	min:=maxlongint;
	for i:=1 to n do read(h[i]);
	qsort(1,n);
	for i:=1 to n-k+1 do
		if h[i+k-1]-h[i]<min then min:=h[i+k-1]-h[i];
	write(min);
end.

Measured memory_bytes: 512
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int i=0; i<(int)(n); i++)
#define intt int64_t
#define all(vec) vec.begin(), vec.end()
#define INF 2000000000
#define MOD 1000000007

int main() {
    int n, m;
    scanf("%d", &n);
    vector<int> time(n);
    int sum=0;
    rep(i,n) {
        scanf("%d", &time.at(i));
        sum+=time.at(i);
    }
    scanf("%d", &m);
    rep(i,m) {
        int c, d;
        scanf("%d %d", &c, &d); c--;
        printf("%d\n", sum+d-time.at(c));
    }
}

Measured memory_bytes: 256
st, q = input(), int(input())
for _ in range(q):
    order = list(map(str, input().split()))
    a, b = int(order[1]), int(order[2])
    if order[0] == "print":
        print(st[a:b+1])
    elif order[0] == "reverse":
        st = st[:a] + st[a:b+1][::-1] + st[b+1:]
    else:
        st = st[:a] + order[3] + st[b+1:]

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

int n;
vector<vector<vector<double>>> dp;

double rec(int i, int j, int k)
{
  if (dp[i][j][k] != -1)
    return dp[i][j][k];
  double d = i + j + k;
  dp[i][j][k] = (n - d) / d;
  if (i > 0)
    dp[i][j][k] += (rec(i - 1, j, k) + 1) * i / d;
  if (j > 0)
    dp[i][j][k] += (rec(i + 1, j - 1, k) + 1) * j / d;
  if (k > 0)
    dp[i][j][k] += (rec(i, j + 1, k - 1) + 1) * k / d;
  return dp[i][j][k];
}

int main()
{
  cin >> n;
  map<int, int> m;
  rep(i, n)
  {
    int a;
    cin >> a;
    m[a]++;
  }
  dp.resize(n + 1);
  rep(i, n + 1)
  {
    dp[i].resize(n + 1);
    rep(j, n + 1)
    {
      dp[i][j].resize(n + 1);
      fill(dp[i][j].begin(), dp[i][j].end(), -1);
    }
  }
  dp[0][0][0] = 0;
  printf("%.10lf\n", rec(m[1], m[2], m[3]));
  return 0;
}

Measured memory_bytes: 216192
#include <bits/stdc++.h>
using namespace std;
const int maxn=200005;
const int inf=0x3f3f3f3f;
int n,m,l,r;
vector<int> v[maxn];
struct node {
    int l,r,lz,mmax;
    inline void update(int v) {
        lz+=v;
        mmax+=v;
    }
} seg[maxn<<2];
inline void pushup(int x) {
    seg[x].mmax=max(seg[x<<1].mmax,seg[x<<1|1].mmax);
}
inline void pushdown(int x) {
    int lz=seg[x].lz;
    if (lz) {
        seg[x<<1].update(lz);
        seg[x<<1|1].update(lz);
        seg[x].lz=0;
    }
}
void build(int x,int l,int r) {
    seg[x].l=l,seg[x].r=r;
    if (l==r) {
        seg[x].mmax=l;
        return;
    }
    int m=(l+r)>>1;
    build(x<<1,l,m);
    build(x<<1|1,m+1,r);
    pushup(x);
}
void update(int x,int l,int r,int v) {
    int L=seg[x].l,R=seg[x].r;
    if (l<=L&&r>=R) {
        seg[x].update(v);
        return;
    }
    pushdown(x);
    int m=(L+R)>>1;
    if (m>=l)
        update(x<<1,l,r,v);
    if (m<r)
        update(x<<1|1,l,r,v);
    pushup(x);
}
int query(int x,int l,int r) {
    int L=seg[x].l,R=seg[x].r;
    if (l<=L&&r>=R)
        return seg[x].mmax;
    pushdown(x);
    int ret=-inf,m=(L+R)>>1;
    if (m>=l)
        ret=max(ret,query(x<<1,l,r));
    if (m<r)
        ret=max(ret,query(x<<1|1,l,r));
    pushup(x);
    return ret;
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=0;i<n;++i) {
        scanf("%d%d",&l,&r);
        v[l].push_back(r);
    }
    build(1,0,m+1);
    int res=max(0,n-m);
    for (int i=0;i<=m;++i) {
        for (auto x:v[i])
            update(1,0,x,1);
        res=max(res,query(1,i+1,m+1)-m-i-1);
    }
    printf("%d\n",res);
    return 0;
}

Measured memory_bytes: 18944
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)
int main(){
    string s;
    cin >> s;
    int mn=1000,tmp;
    rep(i,s.length()-2){
        tmp=(s[i]-'0')*100+(s[i+1]-'0')*10+s[i+2]-'0';
        mn=min(mn,abs(tmp-753));
    }
    cout << mn << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 

using namespace std;
using namespace __gnu_pbds;

#define pbds tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> 
#define ll long long 
#define inf 0x3f3f3f3f
#define infll 0x3f3f3f3f3f3f3f3f
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define ii pair<int,int>
#define vii vector<ii>
#define vi vector<int>

#define F_OR(i, a, b, s) for (int i=(a); (s)>0?i<(b):i>(b); i+=(s))
#define F_OR1(e) F_OR(i, 0, e, 1)
#define F_OR2(i, e) F_OR(i, 0, e, 1)
#define F_OR3(i, b, e) F_OR(i, b, e, 1)
#define F_OR4(i, b, e, s) F_OR(i, b, e, s)
#define GET5(a, b, c, d, e, ...) e
#define F_ORC(...) GET5(__VA_ARGS__, F_OR4, F_OR3, F_OR2, F_OR1)
#define FOR(...) F_ORC(__VA_ARGS__)(__VA_ARGS__)
#define EACH(x, a) for (auto& x: a)
 
// debug templates 
#define debug(x)       cerr<< #x <<" : "<< x << endl;
#define debuga(A,N)    cerr<< #A <<" : [";for(int i = 0; i<N;i++) cerr<<A[i]<<" "; cerr<<"]\n";
#define debuga2(A,N,M) cerr<< #A << " : \n"; for(int i=0;i<N;i++){cerr<<"[";for(int j=0;j<M;++j) cerr<<A[i][j]<<" ";cerr<<"]\n";}
#define debugp(p)      cerr<< #p <<" : "<<"("<<(p).first<<","<<(p).second<<")\n";
#define debugv(v)      cerr<< #v <<" : "<<"[";for(int i = 0; i< (v).size(); i++) cerr<<v[i]<<" "; cerr<<"]\n";
#define debugv2(v)     cerr<< #v << " : \n"; for(int i=0;i<v.size();i++){cerr<<"[";for(int j=0;j<(v[0].size());++j) cerr<<v[i][j]<<" ";cerr<<"]\n";}
#define debugs(m)      cerr<< #m <<" : [ "; for(auto itr = m.begin(); itr!=m.end();itr++) cerr<<*itr<<" "; cerr<<"]\n";
#define debugm(m)      cerr<< #m <<" : [ "; for(auto itr = m.begin();itr!=m.end(); itr++) cerr<<"("<<itr->first<<","<<itr->second<<") ";cerr<<"]\n";
 
const int mxN = 55;
// dp[i][j] -> i operations A's and j operation B's
multiset<ll> dp[mxN][mxN];
// first take the jewels out, and then in the end we put the negative valued jewels back
void solve(){
    int n,cnt; cin >> n >> cnt;
    ll a[n],b[n];
    FOR(n){
        cin >> a[i];
        b[i] = a[i];
    }
    reverse(b,b+n);

    FOR(i,n+1){
        FOR(j,n+1){
            if(i+j < n){
                FOR(k,i) dp[i][j].insert(a[k]);
                FOR(k,j) dp[i][j].insert(b[k]);
            } else{
               FOR(k,n) dp[i][j].insert(a[k]); 
            }
        }   
    }

    ll ans = 0;
    FOR(i,n+1){
        FOR(j,n+1){
            if(i+j > min(cnt,n)) continue;
            int operations_left = cnt - (i+j);
            while(operations_left){
                if(dp[i][j].empty()) break;
                auto itr = dp[i][j].begin();
                if(*itr < 0) dp[i][j].erase(itr);
                else break;
                --operations_left;
            }
            ll temp_ans = 0;
            for(ll v : dp[i][j]) temp_ans += v;
            ans = max(ans,temp_ans);
        }
    }
    cout << ans;

}
 
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	
	solve();
	return 0;
}
 


Measured memory_bytes: 8844
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <cstdio>
#include <string>
#include <cmath>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repr(i, n) for (int i = (n) - 1; i >= 0; i--)
#define FOR(i,i0,n) for(int (i)=(i0);(i)<(n);(i)++)
#define rn return
typedef std::pair<int,int> P;
using namespace std;
using ll = long long;


map<int, int> dp;

int main(){
    int a,b; cin >> a >> b;
    cout << a*b-(a+b-1);
    rn 0;
}

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


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

Measured memory_bytes: 512
import java.util.*;
 
 
class Main {
    static int abc[];
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);

        int N = sc.nextInt();
        if(N%2==1) {
            System.out.println("No");
            return;
        }
        String S = sc.next();

        String str = S.substring(0,N/2);

        str = str + str;

        if(str.equals(S)){
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }

    }

}

Measured memory_bytes: 23380
#define LOCAL

#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <vector>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <string>
#include <cstring>
#include <sstream>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <cmath>
#include <iomanip>

using namespace std;

// From Errichto's Youtube stream
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
    *this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

using int64 = long long;

/////////////////////
// Code starts here//
/////////////////////

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

    int64 N, M;

    cin >> N >> M;

    int64 l = 0, r = 2 * M;
    int64 sol;

    while (l <= r) {
        auto m = (l + r) >> 1;

        auto A = N, B = M;
        if (m > A) {
            B -= 2 * (m - A);
        }

        if (B >= 2 * m) {
            sol = m;
            l = m + 1;
        } else {
            r = m - 1;
        }
    }

    cout << sol << "\n";

    return 0;
}


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

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

    long long int a, b, c, d;
    long long int lower = 0, upper = 1e9;
    while(upper - lower > 1){
        long long int mid = (upper + lower) / 2;
        if(mid * mid <  s) lower = mid;
        if(mid * mid >= s) upper = mid;
    }
    a = upper;
    d = upper;
    s = a * d - s;

    if(s == 0){ b = 0; c = 0; }
    else{
        for (long long int i=1; i*i<=s; i++){
            if (s % i == 0){
                b = i;
                c = s / i;
            }
        }
    }

    cout << 0 << " " << 0 << " "
         << a << " " << b << " "
         << c << " " << d << endl;
    return 0;
}

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

int main()
{
  int N;
  cin >> N;
  vector<pair<int, int>> even_count_pair(pow(10, 5) + 1);
  vector<pair<int, int>> odd_count_pair(pow(10, 5) + 1);

  vector<int> even_count(pow(10, 5) + 1, 0);
  vector<int> odd_count(pow(10, 5) + 1, 0);

  rep(i, N)
  {
    int v;
    cin >> v;
    if ((i + 1) % 2 == 1)
      odd_count[v]++;

    if ((i + 1) % 2 == 0)
      even_count[v]++;
  }

  rep(i, pow(10, 5) + 1)
  {
    even_count_pair[i] = make_pair(even_count[i], i);
    odd_count_pair[i] = make_pair(odd_count[i], i);
  }
  sort(even_count_pair.rbegin(), even_count_pair.rend());
  sort(odd_count_pair.rbegin(), odd_count_pair.rend());

  int even_num = even_count_pair[0].second;
  int odd_num = odd_count_pair[0].second;

  int ans = 0;
  if (even_num != odd_num)
    ans = N - (even_count_pair[0].first + odd_count_pair[0].first);
  else
  {
    int temp_1 = N - (even_count_pair[1].first + odd_count_pair[0].first);
    int temp_2 = N - (even_count_pair[0].first + odd_count_pair[1].first);
    ans = min(temp_1, temp_2);
  }

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

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

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")

/* debugging */
#define TRACE

#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
	cout << name << ": " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
	const char* comma = strchr(names + 1, ',');cout.write(names, comma - names) << " : " << arg1<<"| ";__f(comma+1, args...);
}
#else
#define trace(...)
#endif


#define boost ios::sync_with_stdio(0); cin.tie(0);
#define int long long

const int N = 1e5 + 10;
const int mod = 1e9 + 7;

int32_t main(){
    boost;

  
    //code
    int n;
    cin >> n;
    int x , y;
    pair<int,int> a[n] , b[n];

    for(int i = 0 ; i < n ; ++i){
    	cin >> x >> y;
    	a[i] = {x,y};
    	b[i] = {y,x};
    }

    sort(a,a+n);
    sort(b,b+n);

    map<pair<int,int> , int> mp;

    for(int i = 0 ; i < n ; ++i){
    	for(int j = 0 ; j < n ; ++j){
    		if(i != j){
    			x = a[i].first - a[j].first;
    			y = a[i].second - a[j].second;

    			if(x or y){
    				mp[{x,y}]++;
    			}
    		}
    	}
    }

    int ans = n;

    for(auto i : mp){
    	ans = min(ans , n - i.second);
    }

    mp.clear();

    for(int i = 0 ; i < n ; ++i){
    	for(int j = 0 ; j < n ; ++j){
    		if(i != j){
    			x = b[i].first - b[j].first;
    			y = b[i].second - b[j].second;

    			if(x or y){
    				mp[{x,y}]++;
    			}
    		}
    	}
    }

    for(auto i : mp){
    	ans = min(ans , n - i.second);
    }
    
    cout << ans;
    return 0;
}

Measured memory_bytes: 384
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
	    Scanner sc=new Scanner(System.in);
	    int x=sc.nextInt();
	    int y=sc.nextInt();
	    
	    if(y%2==0 && y>=2*x && y<=4*x) {
	    	System.out.println("Yes");
	    }else {
	    	System.out.println("No");
	    }

	}
}


Measured memory_bytes: 35804
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<deque>
#include<iomanip>
using namespace std;
typedef long long int LL;
typedef pair<int,int> P;
typedef pair<int,pair<int,int> > PP;
typedef pair<LL,int> LP;
const int INF=1<<30;
const LL MAX=1e9+7;

void array_show(int *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%d%c",array[i],(i!=array_n-1?middle:'\n'));
}
void array_show(LL *array,int array_n,char middle=' '){
	for(int i=0;i<array_n;i++)printf("%lld%c",array[i],(i!=array_n-1?middle:'\n'));
}
void print_YES_NO(bool answer){cout<<(answer?"YES":"NO")<<endl;}
void print_Yes_No(bool answer){cout<<(answer?"Yes":"No")<<endl;}
void print_POSSIBLE_IMPOSSIBLE(bool answer){cout<<(answer?"POSSIBLE":"IMPOSSIBLE")<<endl;}

vector<P> v1;

bool exist(int a,int b){
	for(int i=0;i<v1.size();i++){
		if(v1[i].first==a && v1[i].second==b)return true;
	}
	return false;
}

int main(){
	int n,m;
	int i,j,k;
	int a,b,c;
	int s=100;
	cin>>n;
	for(i=0;i<n;i++){
		cin>>a>>b;
		v1.push_back(make_pair(a,b));
	}
	if(n==1){
		cout<<1<<endl;
		return 0;
	}
	for(i=0;i<n;i++){
		for(j=i+1;j<n;j++){
			a=v1[i].first-v1[j].first,b=v1[i].second-v1[j].second;
			c=0;
			for(k=0;k<n;k++){
				if(!exist(v1[k].first-a,v1[k].second-b))c++;
			}
			s=min(s,c);
		}
	}
	cout<<s<<endl;
}


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

vector<pair<int, int> > v;
int main() {
  int a, b, N;
  cin >> a >> b >> N;
  for(int i=0; i<N; i++){
    int s, f;
    cin >> s >> f;
    v.push_back(make_pair(s, f));
  }
  int ans=0;
  for(int i=0; i<N; i++){
    if(v[i].first < b && a < v[i].second) ans=1;
  }
  cout << ans << endl;
}

Measured memory_bytes: 3092
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
sort(arr,arr+n);
int sum = 0;
for(int i=0;i<k;i++)
{
sum+=arr[i];
}
cout<<sum;
return 0;
}

Measured memory_bytes: 3624
#include <iostream>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>

using namespace std;

vector <int> vec;

typedef long long ll;
typedef pair<ll, ll> pl;
typedef vector <ll> vl;
typedef set <ll> sl;
typedef vector <pair <ll, ll>> vll;
typedef map <string, int> msi;

int used[10];
int kol;

int color(int rate)
{
	if (rate < 400)
		return 0;
	if (rate < 800)
		return 1;
	if (rate < 1200)
		return 2;
	if (rate < 1600)
		return 3;
	if (rate < 2000)
		return 4;
	if (rate < 2400)
		return 5;
	if (rate < 2800)
		return 6;
	if (rate < 3200)
		return 7;
	return 8;

}


int main() {
	ios::sync_with_stdio(false);



	int n, temp, ans = 0;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> temp;
		if (color(temp) == 8)
		{
			kol++;
			continue;
		}
		if (!used[color(temp)])
		{
			ans++;
			used[color(temp)] = true;
		}
	}

	cout << max(1,ans) << ' ';
	cout << ans + kol;


	return 0;
}


Measured memory_bytes: 256
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
      
      int count=0;
      for(int i=0;i<N;i++){
        int a = sc.nextInt();
        if(i%2 ==0 && a%2 ==1){
          count++;
        }
      }
      System.out.println(String.valueOf(count));
    }
}

Measured memory_bytes: 35848
#include <iostream>
#include <algorithm>
#include <string>
#include <set>
using namespace std;

typedef unsigned long long ull;

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

int h,w;
string str[10];

void solve(){
  int y, x, ansn = 0;
  string ans = "0";
  set<string> s;
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      for(int k=0;k<8;k++){
	string st;
	st += str[i][j];
	y = i;
	x = j;
	while(1){
	  y = (y + h + dy[k]) % h;
	  x = (x + w + dx[k]) % w;
	  if(y == i && j == x) break;
	  else st += str[y][x];
	  if(!s.empty() && s.find(st) != s.end()){
	    if(ansn < st.size() || ansn == st.size() && ans > st){
	      ans = st;
	      ansn = st.size();
	    }
	  } else s.insert(st);
	}

      }
    }
  }
  cout << ans << endl;
}

int main(){
  while(cin >> h >> w && (h|w)){
    for(int i=0;i<h;i++){
      cin >> str[i];
    }
    solve();
  }
}

Measured memory_bytes: 33768
#include <bits/stdc++.h>
#include <assert.h>
using namespace std;
using i64 = long long;

int main()
{
  i64 n;
  cin >> n;
  vector<i64> h(n);
  for (i64 i = 0; i < n; i++)
    cin >> h[i];
  i64 ans = 0, now = 0;
  for (i64 i = n - 1; 0 < i; i--)
    if (h[i] <= h[i - 1])
      now++;
    else
    {
      ans = max(ans, now);
      now = 0;
    }
  cout << max(ans, now) << endl;
  return 0;
}


Measured memory_bytes: 1024
package main

import (
	"fmt"
)

func main() {
	var n int
	fmt.Scan(&n)
	h := make([]int, n)
	for i := range h {
		fmt.Scan(&h[i])
	}

	ans := h[0]
	for i := 1; i < n; i++ {
		ans += max(h[i]-h[i-1], 0)
	}
	fmt.Println(ans)
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func abs(n int) int {
	if n < 0 {
		return -n
	}
	return n
}


Measured memory_bytes: 1776
/******************************\
*  Everyone has a different way of thinking, so God Created us
Hope You Respect My Way..,Thank You ):  *
*                            *
*   Created by : Murad   *
\******************************/
//std::transform(s1.begin(), s1.end(), s1.begin(),::tolower);
#include<bits/stdc++.h>
using namespace std;
#define pi 3.1415926536
#define forn(i,a,b)for(int i=a;i<b;i++)
#define LL long long
#define MP make_pair
#define ff first
#define ss second
#define INF 2147483647
#define _INF -2147483647
#define pq priority_queue
#define MM multimap
#define PB push_back
#define ipair pair<int,int>
#define lpair pair<LL,LL>
#define MAXN 100004
#define El3zba ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define modd 1000000007
LL MMIN(LL a, LL b)
{
	if (a <= b)
		return a;
	else
		return b;
}
LL MMAX(LL a, LL b) {
	if (a >= b)
		return a;
	else
		return b;
}
int main()
{
	El3zba;
	int n;
	cin >> n;
	vector<int>a(n ), b(n-1);
	for (int i = 0; i <n-1; i++)
		cin >> b[i];
	a[0] = b[0];
	a[n - 1] = b[n - 2];
	int ans = 0;
	if (n >= 3)
	{
		for (int i = 1; i < n - 1; i++)
		{
			a[i] = min(b[i], b[i - 1]);
		}
	}
	for (int i = 0; i < n; i++)
		ans += a[i];
	cout << ans << endl;
	return 0;
}

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

// def
#define debug(x) cout << #x << ": " << x << endl
#define out(x) cout << x << endl
#define repeat(i, a, b) for (int i = (a); i < (b); i++)
#define revrepeat(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define revrep(i, n) for (int i = (n)-1; i >= 0; i--)
typedef long long ll;
typedef pair<int, int> P;
const int M = 1e9 + 7;
const int INF = 1e9;
const int MAX_V = 1000 + 5;
const int MAX_N = 1e5 + 5;

int main() {
    ll n;
    cin >> n;
    vector<P> p;
    rep(i, n) {
        ll a, b;
        cin >> a >> b;
        p.push_back(P(a, b));
    }
    ll ans;
    ll x, y;
    x = p[0].first;
    y = p[0].second;
    repeat(i, 1, n) {
        ll f = p[i].first;
        ll s = p[i].second;
        if (x > f || y > s) {
            ll a = (x + f - 1) / f;
            ll b = (y + s - 1) / s;
            ll mul = max(a, b);
            x = f * mul;
            y = s * mul;
        } else {
            x = f;
            y = s;
        }
    }
    ans = x + y;
    cout << ans << endl;
}


Measured memory_bytes: 256
#include <cstdio>

int num[10];
char str[10];

inline int calc(int bit) {
  int res = num[0];
  for (int i = 0; i < 3; ++i)
    if ((bit >> i) & 1)
      res += num[i + 1];
    else
      res -= num[i + 1];
  return res;
}

inline void print(int bit) {
  putchar(str[0]);
  for (int i = 0; i < 3; ++i) {
    if ((bit >> i) & 1)
      putchar('+');
    else
      putchar('-');
    putchar(str[i + 1]);
  }
  puts("=7");
}

signed main() {
  scanf("%s", str);
  for (int i = 0; i < 4; ++i)
    num[i] = str[i] - '0';
  for (int i = 0; i < 8; ++i)
    if (calc(i) == 7)
      return print(i), 0;
}


Measured memory_bytes: 128
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define MOD 1000000007
using namespace std;
typedef long long ll;

int main() {
  ll n;
  cin >> n;
  string s = "", t = "zabcdefghijklmnopqrstuvwxyz";
  while (n) {
    s += t.substr((n % 26LL), 1);
    if (n % 26LL) {
      n -= n % 26LL;
    } else {
      n -= 26LL;
    }
    n /= 26LL;
  }
  reverse(s.begin(), s.end());
  cout << s << endl;
}

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

#define ll long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long,long long>

#define fastio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl "\n"

#define Debug(x) cout<<(#x)<<" >> "<<(x)<<endl

using namespace std;

ll fastpow(ll angka,ll pangkat,ll MOD){
	ll res=1;
	while(pangkat){
		if(pangkat&1) res=(res*angka)%MOD;
		angka=(angka*angka)%MOD;
		pangkat=pangkat>>1;
	}
	return res;
}

ll fastpow(ll angka,ll pangkat){
	ll res=1;
	while(pangkat){
		if(pangkat&1) res=(res*angka);
		angka=(angka*angka);
		pangkat=pangkat>>1;
	}
	return res;
}

ll gcd(ll angka1,ll angka2){
	while(angka2){
		swap(angka1,angka2);
		angka2=angka2%angka1;
	}
	return angka1;
}

ll lcm(ll angka1,ll angka2){
	return (angka1/gcd(angka1,angka2))*angka2;
}

const ll mod=1000000007;

void debug(vector<ll>v){
	for(auto x:v) cout<<x<<" ";
	cout<<endl;
}

void solve(){
	
	int n;
	cin>>n;
	
	vector<ll>v(n);
	for(int i=0;i<n;i++) cin>>v[i];
	
	vector<ll>prefix(n);
	prefix[n-1]=(v[n-1])%mod;
	for(int i=n-2;i>=0;i--) prefix[i]=((v[i]%mod)+prefix[i+1])%mod;
	
	
	ll res=0;
	for(int i=0;i<n-1;i++){
//		cout<<v[i]<<" "<<prefix[i+1]<<endl;
		res=(res+(((v[i]%mod)*(prefix[i+1]%mod))%mod))%mod;
	}
	cout<<res<<endl;
	
}

int main(){
	
	fastio();

	int t=1;
//	cin>>t;
	
	for(int tt=0;tt<t;tt++){
//		cout<<"Case "<<tt+1<<":\n";solve();
		solve();
	}
	
	return 0;
}

Measured memory_bytes: 6308
#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
#include <algorithm>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int n, k;
	std::cin >> n >> k;
	std::vector<int> a(n);
	for(int i = 0; i < n; i++) std::cin >> a[i];
	std::sort(a.begin(), a.end());
	int sum = 0;
	for(int i = 0; i < k; i++) sum += a[i];
	std::cout << sum << '\n';
}

Measured memory_bytes: 3644
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using V = vector<int>;
using A = array<int, 2>;
using P = pair<int, int>;
constexpr int MOD{1000000000 + 7};
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
constexpr array<int, 4> dx{1, 0, -1, 0}, dy{0, 1, 0, -1};
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
constexpr int div_roundup(int a, int b)
{
    return (a + b - 1) / b;
}
// power with modulo
ll powMod(ll b, ll e, const ll m = MOD)
{
    ll r = 1;
    for (; e > 0; e >>= 1)
    {
        if (e & 1)
            r = (r * b) % m;
        b = (b * b) % m;
    }
    return r;
}
// factrial with modulo
ll factMod(const ll n, const ll m = MOD)
{
    ll x = 1;
    for (ll i = 1; i <= n; ++i)
        x = (x * i) % m;
    return x;
}
class Combi
{
private:
    const int n_, mod_;
    vector<ll> fac_, finv_;

public:
    Combi(int n = 10000, int mod = MOD);
    ll count(int n, int k);
};
Combi::Combi(int n, int mod) : n_(n), mod_(mod)
{
    fac_.resize(n_);
    finv_.resize(n_);
    vector<ll> inv(n_);

    fac_[0] = fac_[1] = 1;
    finv_[0] = finv_[1] = 1;
    inv[1] = 1;

    for (int i = 2; i < n_; i++)
    {
        fac_[i] = fac_[i - 1] * i % mod_;
        inv[i] = mod_ - inv[mod_ % i] * (mod_ / i) % mod_;
        finv_[i] = finv_[i - 1] * inv[i] % mod_;
    }
}
ll Combi::count(int n, int k)
{
    if (n < k)
        return 0;
    if (n < 0 || k < 0)
        return 0;
    return fac_[n] * (finv_[k] * finv_[n - k] % mod_) % mod_;
}

// vector<vector<int>> vec(n_rows, vector<int>(n_cols, value));

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

void solve()
{
    int n, m, sv, tv;
    cin >> n >> m;
    vector<vector<int>> to(n);
    rep(i, m)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        to[a].push_back(b);
    }
    cin >> sv >> tv;
    sv--;
    tv--;

    vector<vector<int>> dist(n, vector<int>(3, MOD));
    queue<P> q;
    q.push(P(sv, 0));
    dist[sv][0] = 0;

    while (!q.empty())
    {
        const int v = q.front().first;
        const int l = q.front().second;
        q.pop();
        for (int u : to[v])
        {
            int nl = (l + 1) % 3;
            if (dist[u][nl] != MOD)
                continue;
            dist[u][nl] = dist[v][l] + 1;
            q.push(P(u, nl));
        }
    }

    int ans = dist[tv][0];
    if (ans == MOD)
        ans = -1;
    else
        ans /= 3;
    cout << ans << endl;
}

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

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << fixed << setprecision(0);

    solve();
}


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

#define rep(i,n) for(ll i=0;i<(n);++i)
#define reps(i,n) for(ll i=1;i<=(n);++i)
using ll = long long;
using str = string;
constexpr long long INF = (1LL<<60);
constexpr long long MOD = (1e9+7);
template<class T>inline T gcd(T a,T b){if(b==0)return a; return(gcd(b,a%b));}
template<class T>inline T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T>inline bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>inline bool chmin(T &a,const T &b){if(a>b){a=b;return true;}return false;}
inline void dump(){cout<<endl;}
template<class Head,class... Tail>inline void dump(Head&& head, Tail&&... tail){cout<<head<<", ";dump(forward<Tail>(tail)...);}
template<typename T>inline istream &operator>>(istream&input,vector<T>&v){for(auto &elemnt:v)input>>elemnt;return input;}

main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cout<<fixed<<setprecision(10);
  
  ll n;
  cin>>n;
  vector<ll> a(n),b(n);
  rep(i,n)cin>>a[i]>>b[i];
  vector<tuple<ll,ll,ll>> c(n);
  rep(i,n)c[i]=make_tuple(a[i]+b[i],a[i],b[i]);
  sort(c.begin(),c.end(),greater<>());
  ll ans=0;
  rep(i,n){
    ll x,y,z;
    tie(x,y,z)=c[i];
    if(i&1)ans-=z;
    else ans+=y;
  }
  cout<<(ans)<<endl;


}


Measured memory_bytes: 4224
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
#define F first
#define S second
#define pb push_back
mt19937 rnd;


int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
#endif

    ll a, b, k;
    cin >> a >> b >> k;
    if (k <= a) {
        cout << a - k << " " << b << "\n";
    }
    else {
        cout << 0 << " " << max(0ll, b - k + a) << "\n";
    }

#ifdef LOCAL
    cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
   return 0;
}


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

signed main(){
  
  int n;
  cin>>n;
  
  map<string,int> memo;
  
  for(int i=0;i<n;i++){
    string s;
    cin>>s;
    sort(s.begin(),s.end());
    memo[s]++;
  }
  
  typedef pair<string,int> P;
  
  int ans = 0;
  
  for(P p : memo ){

    int cnt = p.second;
    
    if( cnt >= 2 ) ans += cnt * ( cnt - 1 ) / 2;
    
  }

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


Measured memory_bytes: 11136
import scala.math.{min,max}

object Main {
  val sc = new java.util.Scanner(System.in)
  def main(args: Array[String]) {
    while(true) {
      val l = sc.nextInt
      if(l == 0) return

      val x = (1 to 12).map(_ => (sc.nextInt - sc.nextInt))
      val y = (1 to 12).map(n => x.take(n).sum).filter(_ < l)
      println( if(y.size == 12) "NA" else y.size + 1 )
    }
  }
}

Measured memory_bytes: 63432
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <stack>
#include <queue>

#define FOR(i, b, e) for(ll i = (ll)(b); i < (ll)(e); ++i)
#define RFOR(i, b, e) for(ll i = (ll)(e-1); i >= (ll)(b); --i)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define REPC(x,c) for(const auto& x:(c))
#define VS vector<string>
#define VL vector<long long>
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVL vector<vector<ll>>
#define ALL(x) (x).begin(),(x).end()
#define SORT(x) sort(ALL(x))
#define RSORT(x) sort((x).rbegin(),(x).rend())
#define PAIR pair<ll,ll>
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

using  ll = long long;
constexpr int MOD = 1000000007;
using namespace std;

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

VL prime_decomposition(ll n) {
	ll i = 2;
	VL table{};
	while (i * i <= n) {
		while (n % i == 0) {
			n /= i;
			table.emplace_back(i);
		}
		++i;
	}
	if (n > 1) {
		table.emplace_back(n);
		return table;
	}
	return table;
}

int main() {
	ll h, w;
	ll n;
	cin >> h >> w >> n;
	VL v(n);
	REP(i, n) { cin >> v[i]; }

	VVL mat(h,VL(w));

	ll x = 0, y = 0;
	ll d = 1;
	REP(i, v.size()) {
		REP(j, v[i]) {
			mat[y][x] = i + 1;
			x += d;
			if (x == w) { --x; ++y; d = -1; }
			if (x == -1) { ++x; ++y; d = 1; }
		}
	}
	REPC(y, mat) {
		REPC(x, y) {
			cout << x << " ";
		}
		cout << endl;
	}
}

Measured memory_bytes: 512
while 1:
    m, f, r = map(int, input().split())
    if m == f == r == -1:
        break
    elif m == -1 or f == -1:
        print("F")
    elif m + f >= 80:
        print("A")
    elif 65 <= m + f < 80:
        print("B")
    elif 50 <= m + f < 65:
        print("C")
    elif 30 <= m + f < 50:
        if 50 <= r:
            print("C")
        else:
            print("D")
    else:
        print("F")



Measured memory_bytes: 5596