akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+5;
char s[N];
int l[N];
int r[N];
int a[N];
int main()
{
    int n,k,c,cnt = 0;
    scanf("%d %d %d %s",&n,&k,&c,s+1);
    for(int i=1;i<=n;i++)
    {
        if(s[i]=='x')   continue;
        a[++cnt] = i;
        int idx = upper_bound(a+1,a+cnt+1,a[cnt]-c-1)-a-1;
        l[cnt] = l[idx]+1;
    }
    for(int i=cnt;i>=1;i--)
    {
        int idx = lower_bound(a+1,a+cnt+1,a[i]+c+1)-a;
        r[i] = r[idx]+1;
    }
    for(int i=1;i<=cnt;i++)
    {
        if(l[i]+r[i]-1==k&&l[i]!=l[i-1]&&r[i]!=r[i+1])  printf("%d\n",a[i]);
    }
    return 0;
}


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

int main(){
  int N;
  cin >> N;
  vector<int> A(N), B(N), C(N);
  for(int i = 0; i < N; ++i) cin >> A[i];
  for(int i = 0; i < N; ++i) cin >> B[i];
  for(int i = 0; i < N; ++i) cin >> C[i];
  long long ans = 0;
  sort(A.begin(), A.end());
  sort(B.begin(), B.end());
  sort(C.begin(), C.end());
  for(int i = 0; i < N; ++i){
    long long l = upper_bound(A.begin(), A.end(), B[i]-1) - A.begin();
    long long r = lower_bound(C.begin(), C.end(), B[i]+1) - C.begin();
    ans += l*(N-r);
  }
  cout << ans << endl;
  return 0;
}


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

int main(){
	int x,y,n,a,b;
	cin >> x >> y;
	if(y>0){
		if(x>0){
			if(y==x){
				cout << 0;
			}
			else if(y>x){
				cout << y - x;
			}
			else {
				cout << x - y + 2;
			}
		}
		else if(x==0){
			cout << y;
		}
		else {
			// x<0 and y>0
			// x=-3 and y=3, x=-2 and y=4, x=-4 and y=2 
			if(abs(y)==abs(x)){
				cout << 1;
			}
			else 
				cout << abs(abs(y) - abs(x)) + 1;
		}
	}
	else if(y==0){
		if(x>0){
			cout << x + 1;
		}
		else if(x==0) cout << 0;
		else {
			cout << -x;
		}
	}
	else {
		// y<0
		if(x>0){
			// y<0 and x>0
			// x=2 and y=-2, x=4 and y=-2, x=2 and y=-4
			if(abs(y)==abs(x)) cout << 1;
			else cout << abs(abs(x) - abs(y)) + 1;
		}
		else if(x==0) cout << abs(y) + 1;
		else {
			// y<0 and x<0
			// x=-2 and y=-2, x=-4 and y=-2, x=-2 and y=-4
			if(y==x) cout << 0;
			else if(abs(x)>abs(y)) cout << abs(x) - abs(y);
			else cout << abs(y) - abs(x) + 2;
		}
	}
	return 0;
}


Measured memory_bytes: 256
from collections import deque
h, w = map(int, input().split())
maze = [[] for _ in range(h)]
seen = [[0 for i in range(w)] for _ in range(h)]
dx = deque()
dy = deque()
for i in range(h):
    l = input()
    for k in range(w):
        maze[i].append(l[k])
dx.append(0)
dy.append(0)
seen[0][0] = 1
while len(dx) != 0 and len(dy) != 0:
    nx = dx.popleft()
    ny = dy.popleft()
    if nx == w-1 and ny == h-1:
        black_all = 0
        for i in range(h):
            for k in range(w):
                if maze[i][k] == '#':
                    black_all += 1
        all_masu = h*w
        print(all_masu-black_all-seen[ny][nx])
        exit()
    for next_m in [(1,0), (-1, 0), (0, 1), (0, -1)]:
        next_x = nx + next_m[0]
        next_y = ny + next_m[1]
        if 0 <= next_x < w and 0 <= next_y < h:
            if maze[next_y][next_x] == '.' and seen[next_y][next_x] == 0:
                dx.append(next_x)
                dy.append(next_y)
                seen[next_y][next_x] = seen[ny][nx] + 1
print(-1)

Measured memory_bytes: 3316
#include <bits/stdc++.h>
using namespace std;
int main(){
  int K;
  cin >> K;
  long long sum = 0;
  for (int a = 1; a <= K; a++){
    for (int b = 1; b <= K; b++){
      for (int c = 1; c <= K; c++){
        sum += gcd(a, gcd(b, c));
      }
    }
  }
  cout << sum;
}

Measured memory_bytes: 3624
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define vi vector<ll>
#define vvi vector<vi>
#define DBG_N(hoge) cerr<<" "<<(hoge)<<endl;
#define DBG cerr<<"!"<<endl;
#define BITLE(n) (1LL<<((ll)n))
#define BITCNT(n) (__builtin_popcountll(n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
ll gcd(ll a,ll b){
  ll r=a%b;
  while(r>0){
    a=b;
    b=r;
    r=a%b;
  }
  return b;
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll a,b;cin>>a>>b;
  ll num=gcd(a,b);
  vi prime;
  prime.pb(2);
  for(ll i=3;i<=sqrt((ll)1e12+10);i+=2){
    bool fl=true;
    for(ll j=2;j<=sqrt(i);j++){
      if(i%j==0){fl=false;break;}
    }
    if(fl)prime.pb(i);
  }
  ll cnt=0;
  ll ans=0;
  while(cnt<prime.size()&&num>0){
    //cout<<cnt<<endl;
    if(num%prime[cnt]==0){
      ans++;
      while(num%prime[cnt]==0){
	num/=prime[cnt];
      }
    }
    cnt++;
  }
  if(num>1){
    ans++;
  }
  cout<<ans+1<<endl;
  return 0;
}


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

int main() {
  string s;
  cin >>s;
  int n =s.size();
  int a =0;
  
  for(int i=0;i < n;i++){
    if(s[i]==s[n-1-i]){
      a++;
    }
  }
  
  for(int i =0;i < n/2;i++){
    if(s[i]==s[n/2-1-i]){
      a++;
    }
  }
  
  if(a==n+n/2)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  
    
}

Measured memory_bytes: 256
use std::io::*;
use std::str::FromStr;

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("filed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

fn main() {
    let x: usize = read();
    println!("{}", 1000 * (x / 500) + 5 * ((x % 500) / 5));
}


Measured memory_bytes: 2172
#include<iostream>
#include <stdlib.h>
#include <algorithm>
#include <string>
#include <math.h>
typedef long long int ll;
using namespace std;
int main(){
	int N,count=0;
	string S;
	cin>>N>>S;
	for(int i=0;i<N-2;i++){
		if(S[i]=='A'){
			if(S[i+1]=='B'&&S[i+2]=='C'){
				count++;
			}
		}
	}
	cout<<count<<endl;
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;

int n, xx, yy;
vector<int> a[N];

int dxx[N];
void dfsxx(int x, int p)
{
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (h == p)
            continue;
        dxx[h] = dxx[x] + 1;
        dfsxx(h, x);
    }
}

int dyy[N];
void dfsyy(int x, int p)
{
    for (int i = 0; i < a[x].size(); ++i)
    {
        int h = a[x][i];
        if (h == p)
            continue;
        dyy[h] = dyy[x] + 1;
        dfsyy(h, x);
    }
}

int main()
{
    scanf("%d", &n);
    scanf("%d%d", &xx, &yy);
    for (int i = 0; i < n - 1; ++i)
    {
        int x, y;
        scanf("%d%d", &x, &y);
        a[x].push_back(y);
        a[y].push_back(x);
    }
    dfsxx(xx, xx);
    dfsyy(yy, yy);
    int ans = 0;
    for (int i = 1; i <= n; ++i)
    {
        if (dxx[i] < dyy[i])
        {
            if ((dyy[i] - dxx[i]) % 2 == 0)
                ans = max(ans, dyy[i] - 1);
            else
                ans = max(ans, dyy[i] - 1);
        }
    }
    printf("%d\n", ans);
    return 0;
}


Measured memory_bytes: 11648
//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
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 pair<long, long> PLL;
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 SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
#define INF 10000000
#define MAX_V 101
struct edge{int to;LL cost;};
typedef pair<LL,int> P;//firstは最短距離,secondは頂点番号
int V;
vector<edge> G[MAX_V];
LL d[MAX_V];

void dijkstra(int s){
	priority_queue<P,vector<P>,greater<P> > que;
	fill(d,d+V,INF);
	d[s]=0;
	que.push(P(0,s));
	
	while(!que.empty()){
		P p=que.top();que.pop();//仮の最短距離が短い順に取り出す
		int v=p.second;
		if(d[v]<p.first)continue;
		REP(i,G[v].size()){
			edge e=G[v][i];
			if(d[e.to]>d[v]+e.cost){
				d[e.to]=d[v]+e.cost;
				que.push(P(d[e.to],e.to));//仮の最短距離と頂点の組を更新が行われる度に追加していく
			}
		}
	}
}

int main(){
	long n,k;
	while(cin>>n>>k){
		if(n==0&&k==0)break;
		V=n;
		REP(i,n)G[i].clear();
		REP(i,k){
			int ch;
			cin>>ch;
			if(ch){
				int ct,dt;
				LL et;
				cin>>ct>>dt>>et;
				ct--;dt--;
				edge ed={dt,et};
				G[ct].PB(ed);
				ed.to=ct;
				G[dt].PB(ed);
			}else{
				int a,b;
				cin>>a>>b;
				a--;b--;
				dijkstra(a);
				if(d[b]<INF)cout<<d[b]<<endl;
				else cout<<"-1"<<endl;
			}
		}
	}
	
	return 0;
}

Measured memory_bytes: 1288
#include <bits/stdc++.h> 
#define FOR(i,a,b) for(int i=(a);i<=(b);++i) 
#define RFOR(i,a,b) for(int i=(a);i>=(b);--i) 
#define REP(i,n) for(int i=0;i<(n);++i)
#define RREP(i,n) for(int i=(n);i>0;--i)
#define CI cin>> 
#define CO cout<<
#define EN <<endl 
#define all(x) (x).begin(),(x).end()
using namespace std;
template <typename T> istream& operator >> (istream& is, vector<T>& vec) { for (T& x : vec) is >> x; 	return is; }
using ll = long long;
using VI = vector<int>;

int main() {
	string s; CI s;
	REP(i, 3) {
		if (s[i] == '9') s[i] = '1';
		else if (s[i] == '1')s[i] = '9';
	}
	CO s EN;
}

Measured memory_bytes: 256
#include <vector>
#include <list>
#include <map>
#include <climits>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <queue>
#include <random>
#include <cctype>
#include <complex>
#include <regex>
#include <unordered_map>



using namespace std;


using namespace std;

int main() {
   int A, B, C, D;
   cin>>A>>B>>C>>D;

   cout<<A*B-B*C-A*D+C*D<<endl;

}

Measured memory_bytes: 256
//https://atcoder.jp/contests/dp/tasks/dp_h
#include<bits/stdc++.h>
#define forz(i,n) for(int i=0;i<n;i++)
#define foro(i,n) for(int i=1;i<=n;i++)
#define mod 1000000007
#define pb push_back
#define vi vector<int>
#define lli long long int
using namespace std;
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);
int main()
{
     faster
     int h,w;
     cin>>h>>w;
      char grid[h][w];
      for(int i=0;i<h;i++)
      {
           for(int j=0;j<w;j++)
           {
                cin>>grid[i][j];
           }
      }
    lli dp[h][w];
    for(int i=0;i<h;i++)
    {
         for(int j=0;j<w;j++)
         {
              dp[i][j]=0;
         }
    }
    dp[0][0]=1;
    for(int i=1;i<w;i++)
    {
         if(grid[0][i]=='#')
         {
              dp[0][i]=0;
         }
         else{
         dp[0][i]=dp[0][i-1];
         }
    }
  for(int i=1;i<h;i++)
  {
     if(grid[i][0]=='#')
         {
              dp[i][0]=0;
         }
         else{
         dp[i][0]=dp[i-1][0];  
         }
  }
for(int i=1;i<h;i++)
 {
      for(int j=1;j<w;j++)
      {
           if(grid[i][j]=='#')
           {
                dp[i][j]=0;
           }
           else{
                dp[i][j]=dp[i-1][j]+dp[i][j-1];
                dp[i][j]=dp[i][j]%mod;
             }
      }
 }
cout<<dp[h-1][w-1];
     return 0;
}

Measured memory_bytes: 9088
n = gets.to_i
n.times{|ii|
  gets
  mat = []
  8.times{|i|
    mat[i] = gets.chomp.split("").map(&:to_i)
  }
  x = gets.to_i - 1
  y = gets.to_i - 1
  q = []
  q << [y, x]
  while q.size > 0
    y, x = q.shift
    mat[y][x] = 0
    [-3,-2,-1,1,2,3].each{|dx|
      if x+dx < 0 || x+dx > 7
        next
      else
        if mat[y][x+dx] == 1
          q << [y,x+dx]
          mat[y][x+dx] = 0
        end
      end
    }
    [-3,-2,-1,1,2,3].each{|dy|
      if y+dy < 0 || y+dy > 7
        next
      else
        if mat[y+dy][x] == 1
          q << [y+dy,x]
          mat[y+dy][x] = 0
        end
      end
    }
  end
  puts "Data #{ii+1}:"
  8.times{|i|
    8.times{|j|
      print mat[i][j]
    }
    puts
  }
}

Measured memory_bytes: 8764
import sys
import math
import string
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
from bisect import bisect, bisect_left, bisect_right

MOD = 10 ** 9 + 7
INF = float('inf')
input = lambda: sys.stdin.readline().strip()

sys.setrecursionlimit(10 ** 8)

N, A, B = map(int, input().split())
dis = B - A - 1
if dis % 2 == 0:
    print(min((N - max(A, B) + 1) + (N - min(A, B) - (N - max(A, B) + 1)) // 2,
          min(A, B) + ((max(A, B) - min(A, B) - 1) // 2)))
else:
    print((dis + 1) // 2)


Measured memory_bytes: 6220
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define db double
#define pii pair<int,int>
#define pli pair<ll,int>
#define pil pair<int,ll>
#define pll pair<ll,ll>
#define mat vector<vector<int>>
const int inf = 1 << 30;
const ll linf = 1e18;
const db EPS = 1e-7;
template<class T> void chmin(T& x, T y){if(x > y) x = y;}
template<class T> void chmax(T& x, T y){if(x < y) x = y;}

int N, M;
int edge[710][710];
int st[710];
bool flag = true;
vector<pii> ps;

void dfs(int v, int* dat){
    dat[st[v]]++;
    int to = (st[v] + 1) & 1;
    for(int i = 1; i <= N; ++i){
        if(edge[v][i] == 0){
            if(st[i] == -1) st[i] = to, dfs(i, dat);
            else if(st[i] != to) flag = false;
        }
    }
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin >> N >> M;
    for(int i = 1; i <= N; ++i) st[i] = -1, edge[i][i] = 1;
    for(int i = 0; i < M; ++i){
        int A, B;
        cin >> A >> B;
        edge[A][B] = 1;
        edge[B][A] = 1;
    }
    for(int i = 1; i <= N; ++i){
        if(st[i] != -1) continue;
        
        int dat[2] = {};
        st[i] = 0;
        dfs(i, dat);
        ps.push_back({dat[0], dat[1]});
    }
    if(flag){
        int t = 0, h = 0;
        bitset<710> res = {};
        res[0] = 1;
        for(auto u : ps){
            res = (res << u.first) | (res << u.second);
        }

        int ans = inf;
        for(int i = 0; i <= N; ++i){
            if(res[i] == 1){
                auto culc = [] (int a){return a * (a - 1) / 2;};
                chmin(ans, culc(i) + culc(N - i));
            }
        }
        cout << ans << endl;
    }else{
        cout << -1 << endl;
    }
    return 0;
}

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

template <typename T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
if (!v.empty()) {
    out << '[';
    std::copy(v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
    out << "\b\b]";
  }
  return out;
}
template <typename T1, typename T2>
std::ostream &operator<<(std::ostream &out, const std::pair<T1, T2> &p) {
    out << "[" << p.first << ", " << p.second << "]";
    return out;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> as(n);
    for(int i = 0; i < n; i++){
        cin >> as[i];
    }
    
    int cost = 0;
    int ans = 10000000;
    for(int i = -100; i <= 100; i++){
        for(int j = 0; j < n; j++){
            cost += pow(as[j] - i, 2);
        }
        if(cost < ans){
            ans = cost;
        }
        cost = 0;
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for (int i = a; i < b; i++)
#define REP(i,b) FOR(i,0,b)
#define RFOR(i,a,b) for (int i = a-1; i >= b; i--)
#define RREP(i,a) RFOR(i,a,0)
#define REPALL(i,v) for (int i = 0; i < v.size(); i++)
#define SORT(v) sort(v.begin(), v.end())
#define FIND(v,n) find(v.begin(), v.end(), n)
#define MIN_ELEMENT(v) min_element(v.begin(), v.end())
#define MAX_ELEMENT(v) max_element(v.begin(), v.end())
#define COUNT(v,n) count(v.begin(), v.end(), n);
typedef long long ll;
typedef unsigned long long ull;
const int INF = 1e7;
const ll MOD = 1e9 + 7;

ll p[111111], y[111111];
vector<ll> v[111111];

ll getid(int i)
{
    //vector<ll> t = v[p[i]];
    ll l = 0, r = v[p[i]].size();
    ll mid, val;
    ll k = v[p[i]].size();
    REP(j, k) {
        mid = (l + r) / 2;
        val = v[p[i]][mid] - y[i];
        if (val == 0) {
            break;
        } else if (val > 0) {
            r = mid;
        } else {
            l = mid;
        }
    }
    return mid + 1;
}

int main()
{
    int n, m;
    cin>>n>>m;

    REP(i,m) {
        cin>>p[i]>>y[i];
        v[p[i]].push_back(y[i]);
    }

    REP(i,n+1) {
        SORT(v[i]);
    }

    REP(i,m) {
        printf("%06d%06d\n", p[i], getid(i));
    }

    return 0;
}


Measured memory_bytes: 8832
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;
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;
}

int diff(int i, int j) {
    if((i & 1) != (j & 1)) return 1;
    return diff(i / 2, j / 2) + 1;
}

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
int ans[505][505];
int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    for(int i = 1; i <= N-1; i++) {
        for(int j = i + 1; j <= N; j++) {
            cout << diff(i, j);
            if(j != N) cout << " ";
        }
        cout << endl;
    }
    return 0;
}


Measured memory_bytes: 512
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
/*int main()
{
    char s[4];
    cin>>s;
    if((s[0]=='7')|| (s[1]=='7') || (s[2]=='7'))
    {
         cout<<"YES";
    }
    else{
         cout<<"NO";
    }*/
int main()
{
    char s[4];
    scanf("%s",s);
    if(s[0]=='7'||s[1]=='7'||s[2]=='7')
        puts("Yes");
    else
        puts("No");
}


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

int main() {
  int n;
  cin>>n;

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

  sort(cards.begin(), cards.end());
  reverse(cards.begin(), cards.end());

  int alice = 0, bob = 0;
  for(int i=0; i<cards.size(); i++) {
    if (i % 2 == 0) alice += cards[i];
    else bob += cards[i];
  }

  cout << alice - bob << endl;

  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <string.h>
#define N 1500000
#define L 13
 
char moji[N][L],order[7];
 
int h1(int key) { return key % N; }
int h2(int key) { return  1 + (key % (N-1)); }
 
int ctoi(char str){
    if(str == 'A') return 1;
    else if(str == 'C') return 2;
    else if(str == 'G') return 3;
    else if(str == 'T') return 4;
    else return 0;
}
 
int getKey(char str[]){
    int sum=0,time=1,i;
    for(i=0; i<strlen(str); i++){
        sum += time * (ctoi(str[i]));
        time *= 4;
    }
    return sum;
}
 
void insert(char str[]){
    int i,key,h;
    key = getKey(str);
    for(i=0; ; i++){
    h = (h1(key) + i * h2(key)) % N;
    if(strcmp(moji[h],str) == 0) break;
    else if(strlen(moji[h]) == 0){
    strcpy(moji[h],str);
    break;
    }
    }
}
 
int find(char str[]){
    int key,i,h;
    key = getKey(str);
    for(i=0; ; i++){
        h = (h1(key) + i * h2(key)) % N;
        if(strcmp(moji[h],str) == 0) return 1;
        else return 0;
    }
}
 
int main(){
    int i,j,num;
    char str[L];
    scanf("%d",&num);
    for(i=0; i<N; i++) moji[i][0] = '\0';
    for(i=0; i<num; i++){
    scanf("%s%s",order,str);
    if(strcmp(order,"insert") == 0) insert(str);
    else{ 
    if(find(str) == 1) printf("yes\n"); 
    else  printf("no\n");
    }
    }
    return 0;
}

Measured memory_bytes: 19648
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		String name=sc.next();
		char[] nameC=name.toCharArray();
		int namelen=name.length();
		int cnt=0;
		A:for(int i=0;i<n;i++){
			String str=sc.next();
			char[] strC=str.toCharArray();
			int strlen=str.length();
			boolean flag=false;
			int j=0;
			for(;j<strlen;j++){
				if(strC[j]==nameC[0]){
					int k=j;
					for(;k<strlen;k++){
						if(strC[k]==nameC[1]){
							int x=k-j;
							for(int l=k+x,m=2;l<strlen;l+=x,m++){
								flag=false;
								if(strC[l]==nameC[m]){
									flag=true;
									if(m==namelen-1){
										cnt++;
										continue A;
									}
								}
								if(flag==false){
									break;
								}
							}
						}
					}
				}
			}
		}
		System.out.println(cnt);
	}
}

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

typedef pair<int,int> P;

void debug(vector<P>& v){
	cout << "[debug]" << endl;
	for(int i=0 ; i < v.size() ; i++ ){
		cout << "(" << v[i].first << "," << v[i].second << ")" << endl;
	}
	cout << endl;
}

vector<P> remove(vector<P>& a){
	bool flag = false;
	vector<P> a_;
	for(int i=0 ; i < a.size() ; i++ ){
		if( a[i].second <= 0 || a[i].second >= 4 ){
			flag = true;
		}else{
			if( !a_.empty() && a_[a_.size()-1].first == a[i].first ){
				flag = true;
				a_[a_.size()-1].second += a[i].second;
			}else{
				a_.push_back( a[i] );
			}
		}
	}
	if( flag ) return remove(a_);
	return a_;
}

int sum(vector<P>& a){
	int s = 0;
	for(int i=0 ; i < a.size() ; i++ ){
		s += a[i].second;
	}
	return s;
}

int solve(vector<P>& a){
	int ans = sum(a);
	for(int i=0 ; i < a.size() ; i++ ){
		if( 0 <= i-1 ){
			// 1つ前の色に変える.
			int c = a[i-1].first;
			vector<P> a_ = a;
			a_[i].second--;
			a_[i-1].second++;
			a_ = remove(a_);
			/*if( sum(a_) < ans ){
				cout << "change:" << i << " color:" << c << endl;
				debug(a_);
			}*/
			ans = min( ans , sum(a_) );
		}
		if( i+1 < a.size() ){
			// 1つ後ろの色に変える.
			int c = a[i+1].first;
			vector<P> a_ = a;
			a_[i].second--;
			a_[i+1].second++;
			a_ = remove(a_);
			/*if( sum(a_) < ans ){
				cout << "change:" << i << " color:" << c << endl;
				debug(a_);
			}*/
			ans = min( ans , sum(a_) );
		}
	}
	return ans;
}

int main(){
	int n;
	while( cin >> n , n ){
		vector<int> v(n);
		for(int i=0 ; i < n ; i++ ){
			cin >> v[i];
		}
		vector<P> a;
		for(int i=0 ; i < v.size() ; i++ ){
			if( i == 0 ){
				a.push_back( P(v[i],1) );
			}else if( v[i-1] == v[i] ){
				a[a.size()-1].second++;
			}else{
				a.push_back( P(v[i],1) );
			}
		}
		cout << solve( a ) << endl;
	}
}

Measured memory_bytes: 24712
#include <algorithm>
#include <bitset>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
using namespace std;

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

typedef long long ll;

string A;
ll ans = 0;

int main() {
  cin >> A;

  ll s = 0;
  char a = 'a';
  REP(i, 26) {
    char c = a+i;
    ll cnt = count(A.begin(), A.end(), c);
    s += cnt*(cnt-1)/2;
  }

  ll n = A.length();
  ans = n*(n-1)/2 - s + 1;

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


Measured memory_bytes: 640
using System;
using System.Collections.Generic;
class Solve{
    int N;
    long[] a;
    public Solve(){}
    public static int Main(){
        new Solve().Calc();
        return 0;
    }
    void Calc(){
        getinput();
        int[] b = new int[N];
        for(int i=0;i<N;i++){
            b[i] = i;
        }
        int count = 0;
        Array.Sort(b,(i,j) => c(i,j));
        for(int i=0;i<N;i++){
            if(b[i] % 2 != i % 2){
                count++;
            }
        }
        Console.WriteLine(count/2);
    }
    int c(int i,int j){
        if(a[i] - a[j] > 0){
            return 1;
        }
        else if(a[i] == a[j]){
            return 0;
        }
        else{
            return -1;
        }
    }
    void getinput(){
        N = int.Parse(Console.ReadLine());
        a = new long[N];
        for(int i=0;i<N;i++){
            a[i] = Int64.Parse(Console.ReadLine());
        }
    }
}

Measured memory_bytes: 8024
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>

using namespace std;
void countersort(int A[],int B[], int n){
    int c[10001];
    for(int i = 0; i < 10001 ; i++){
       c[i] = 0;
    } 
    for(int i = 0; i < n ; i++){
        c[A[i]]++;
    }

    for(int i = 1; i < 10001 ; i++){
       c[i] = c[i-1]+c[i]; 
    }

    for(int i = 0; i < n ; i++){
        B[c[A[n-i-1]]-1]=A[n-i-1];
        c[A[n-i-1]]--;
    }
}
int main(){
    int n;
    cin >> n;
    int A[n];
    for(int i = 0; i < n ; i++){
        scanf("%d",&A[i]); 
    }
    int B[n];
    countersort(A,B,n);
    for(int i=0; i<n; i++){
        if(i>=1)
            printf(" ");
        printf("%d",B[i]);
    }
    cout << endl;
}


Measured memory_bytes: 18880
//C - Skill Up

#include<iostream>
#include<vector>

using namespace std;

int main(){
    int n,m,x;
    cin>>n>>m>>x;

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

    int best_cost=1e9;
    for(int i=0; i<(1<<n); i++){
        int cost=0;
        vector<int> understand(m,0);
        for(int j=0; j<n; j++){
            if(i>>j & 1){
                cost+=c[j];
                for(int k=0; k<m; k++){
                    understand[k]+=a[j][k];
                }
            }
        }
        bool flag=true;
        for(int j=0; j<m; j++){
            if(understand[j]<x){
                flag=false;
                break;
            }
        }
        if(flag){
            best_cost=min(best_cost,cost);
        }
    }
    if(best_cost!=1e9){
        cout<<best_cost<<endl;
    }else{
        cout<<-1<<endl;
    }
    return 0;
}

Measured memory_bytes: 3636
n = int(input())
for i in  range(1, n + 1):
    x = i
    if x % 3 == 0:
        print(" " + str(i), end = "")
    else:
        while True:
            if x % 10 == 3:
                print(" " + str(i), end = "")
                break
            x //= 10
            if x <= 0:
                break
print()        



Measured memory_bytes: 5868
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define st first
#define nd second 

int n, a, b;
int arr[3][100005];
int dp[3][100005];

int32_t main() {
	ios_base::sync_with_stdio(0);
    cin >> n;
    for(int i = 0; i < n; i++){
    	cin >> a;
    	arr[0][i] = a;
    	cin >> a;
    	arr[1][i] = a;
    	cin >> a;
    	arr[2][i] = a;
    }
    dp[0][0] = arr[0][0];
    dp[1][0] = arr[1][0];
    dp[2][0] = arr[2][0];
    for (int i = 1; i < n; i++){
    	for (int j = 0; j < 3; j++){
    		if(j == 0) dp[0][i] = max(dp[1][i-1]+arr[0][i], dp[2][i-1]+arr[0][i]);
    		if(j == 1) dp[1][i] = max(dp[0][i-1]+arr[1][i], dp[2][i-1]+arr[1][i]);
    		if(j == 2) dp[2][i] = max(dp[1][i-1]+arr[2][i], dp[0][i-1]+arr[2][i]);
    	}
    }
    cout << max(max(dp[0][n-1],dp[1][n-1]),dp[2][n-1]) << endl;
}

Measured memory_bytes: 4992
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int main()
{
	int X,Y,Z;
	int sum;
	scanf("%d %d %d",&X,&Y,&Z);
	sum=(X-Z)/(Y+Z);
	printf("%d\n",sum);
	return 0;
}


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

int main()
{
	int k;
	cin>>k;
	long long sum=0;
	for(long long i=1;i<=k;i++)
	{
		for(long long j=1;j<=k;j++)
		{
			for(long long l=1;l<=k;l++)
			{
				sum=sum+__gcd(i,__gcd(j,l));
			}
		}
	}
	cout<<sum;
	return 0;
}

Measured memory_bytes: 3624
#include<iostream>
#include <algorithm>
#include<string>
#include <numeric>
#include<math.h>
#include <bits/stdc++.h>
using namespace std;

int main(void) {
    int n;
    string ans="AGC";
    cin>>n;
    if(n<1200)ans="ABC";
    else if(n<2800)ans="ARC";
    else ans="AGC";
    cout<<ans<<endl;
}


Measured memory_bytes: 256
//
// Created by chen on 19-7-20.
//
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <cmath>
using namespace std;
int t[200010];
int main(){
    int n;
    cin>>n;
    int f=0,s=0;
    for(int i=1;i<=n;i++){
        scanf("%d",&t[i]);
        if(f<t[i]){
            s=f;
            f=t[i];
        }else{
            if(s<t[i]){
                s=t[i];
            }
        }
    }
    for(int i=1;i<=n;i++){
        if(t[i]==f){
            printf("%d\n",s);
        }else
            printf("%d\n",f);
    }
}

Measured memory_bytes: 2432
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
#include <deque>
#include <iomanip>
#include <limits>
#include <fstream>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n,m;cin>>n>>m;
  int A[n], B[m];
  FOR(i,0,n)cin>>A[i];
  FOR(i,0,m)cin>>B[i];
  int cnt[n+1];
  int ans = -1, num = 0;
  CLR(cnt);
  FOR(i,0,m){
      FOR(j,0,n){
          if(A[j] <= B[i]){
              cnt[j+1]++;
              if(cnt[j+1] > cnt[ans]) ans = j + 1;
              break;
          }
      }
  }
  cout << ans << endl;
}


Measured memory_bytes: 3160
fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map { it.toInt() }.toIntArray()

    println(a.joinToString(" "))

    for (i in 1 until n) {
        val v = a[i]
        var j = i - 1
        while (j >= 0 && a[j] > v) {
            a[j + 1] = a[j]
            j--
        }
        a[j + 1] = v
        println(a.joinToString(" "))
    }
}


Measured memory_bytes: 33480
/** Author: Dwai
 *  Omae wa mou shindeiru!
**/

#include <bits/stdc++.h>
using namespace std;

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define ff first
#define ss second
#define all(a) (a).begin(),(a).end()
#define fr(i,a,b) for (i = a; i <= b; i++)
#define frr(i,a,b) for (i = a; i >= b; i--)
#define rep(i,n) for (i = 0 ; i < n; i++)
#define repr(i,n) for (i = n - 1; i >= 0; i--)
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);srand(time(NULL));
#define flushinput   cin.ignore(numeric_limits<streamsize>::max(),'\n');

void debug_out() { cerr << endl; }

void clock_out(){
  cerr<<"\nTime Elapsed : " << 1.0*clock() / CLOCKS_PER_SEC << " s\n";
}

void fileio(){
  freopen("/home/dwai/Desktop/cp/input.txt","r", stdin);
  freopen("/home/dwai/Desktop/cp/output.txt", "w", stdout);
  freopen("/home/dwai/Desktop/cp/debug.txt", "w", stderr);
}

void ofileio(){
  freopen("input.txt","r", stdin);
  freopen("output.txt", "w", stdout); 
}

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

#ifndef ONLINE_JUDGE
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#define fio() fileio()
#define ofio() fileio()
#define clock() clock_out()
#else
#define debug(...) 42
#define fio() 42
#define ofio() ofileio()
#define clock() 42
#endif

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
typedef vector<pll> vll;
typedef map<ll,ll> mll;

int IT_MAX = 1 << 20;
const ll MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;



void solve()
{
	ll n;cin>>n;
	vector<pair<string,ll>> v;
	ll i;
	rep(i,n)
	{
		ll x;string s;
		cin>>s>>x;
		v.eb(mp(s,x));
	}
	ll ans=0;
	bool flag=false;
	string cs;
	cin>>cs;
	rep(i,n)
	{
		if(flag)
		{
			ans=ans+v[i].ss;
		}
		if(v[i].ff==cs)
			flag=true;
	}
	cout<<ans;	
}

int main()
{
//	fio();
	fast
	ll t=1;
	//cin>>t;

	while(t--){
		solve();
	}
	return 0;
}

Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

int main()
{
    vector<int> a(3), b(2);
    cin >> a[0] >> a[1] >> a[2] >> b[0] >> b[1];

    sort(a.begin(), a.end());
    sort(b.begin(), b.end());

    int ret = a[0] + b[0] - 50;
    cout << ret << endl;

    return 0;
}

Measured memory_bytes: 0
#include <bits/stdc++.h>
#define SLAY ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define PI 3.141592653589793238
#define ll long long
#define pii pair<int , int>
#define pll pair<long long , long long>
using namespace std;
int _=1 , tc=1;
int dx[] = { 0 , 0 , 1 , -1};
int dy[] = { 1 , -1 , 0 , 0};
int getmod(int x, int y , int mod ){return (((x - y)%mod)+mod)%mod;}
int MAXN = 46341 + 1;
int spf[46341+1];
int d,x,y;
int gcd(int a , int b){
	if(b==0)return a;
	return gcd(b , a%b);
}
void sieve(){
	for(int i = 1 ; i<MAXN ; i++)spf[i]=i;
	for(int i = 4 ; i<MAXN ; i+=2)spf[i]=2;
	for(int i = 3; i*i<MAXN; i+=2){
		if(spf[i]==i){
			for(int j = i*i ; j<MAXN ; j+=i){
				if(spf[j]==j)spf[j]=i;
			}
		}
	}
}
void extended_gcd(int a , int b){
	if(b==0){
		d=a;
		x=1;
		y=0;
	}else{
		extended_gcd(b,a%b);
		int temp = y;
		y = x - y * (a/b);
		x = temp;	
	}
}
ll gcd(ll a , ll b){
	if(b==0)return a;
	return gcd(b , a%b);
}
ll lcm(ll a, ll b){  
    return (a*b)/gcd(a, b);  
}

ll v[3001][3001];
ll dp[3001][3001][4];
ll r,c,k;

ll solve_(int i , int j , int left){
	if( i>r || j>c )return 0;
	if(left==-1)return LLONG_MIN;
	
	ll &ret = dp[i][j][left];
	if(~ret)return ret;
	ret =0;
	if(left == 0)ret = solve_(i+1 , j , 3);
	else{
		ret = max(ret , v[i][j]+solve_(i+1 , j , 3));
		ret = max(ret , v[i][j]+solve_(i,j+1  , left-1));
		ret = max(ret , solve_(i,j+1  , left));
	}
	return ret;
}
void solve(){
	memset(v,0,sizeof(v));
	memset(dp,-1,sizeof(dp));
	cin>>r>>c>>k;
	for(int i = 0 ; i<k ; i++){
		ll j,k,c;
		cin>>j>>k>>c;
		v[j][k]=c;
	}
	cout<<solve_(1 , 1, 3);
}
int main(){
	SLAY
	// cin>>_;
	while(_--){
		solve();
	}
	return 0;
}

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

#define MAX_N (22)
#define MAX_M (2010)
int A[MAX_N];
bool dp[2][MAX_M];
int main()
{
    int n, q;

    scanf("%d", &n);

    memset( dp, false, 2 * MAX_M * sizeof( bool ) );
    dp[0][0] = 1;

    for ( int i = 0; i < n; i++ ) {
	scanf("%d", &A[i]);
    }

    for ( int i = 0; i < n; i++ ) {
	int a = A[i];
	for ( int j = 0; j < MAX_M; j++ ) {
	    if ( dp[i % 2][j] ) {
		dp[( i + 1 ) % 2][j] = true;
		if ( j + a < MAX_M ) {
		    dp[( i + 1 ) % 2][j + a] = true;
		}
	    }
	}
    }

    scanf("%d", &q);

    for ( int i = 0; i < q; i++ ) {
	int m;
	scanf("%d", &m);
	if ( dp[n % 2][m] ) {
	    printf("yes\n");
	}
	else {
	    printf("no\n");
	}
    }

    return 0;
}

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

#define rep(i,n) for(int i = 0; i < n; i++)
#define REP(i,n) for(int i = 1; i < n; i++)
#define repll(i,n) for(ll i = 0; i < n; i++)
#define REPll(i,n) for(ll i = 1; i < n; i++)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl "\n"
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll a, const ll b){return ((a)+(b)-1)/b;}


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

  int n;
  cin >> n;
  ll ans = 0;
  rep(i,n) {
    ans += i;
  }
  cout << ans << endl;
}

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

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,a,n) for(int i=a;i<n;i++)

typedef long long ll;

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; }

int a[100010];

void Main()
{
	int N; cin >> N;
	rep(i,N) {
		int _a; cin >> _a;
		--_a;
		a[i] = _a;
	}
	int current = 0;
	int cnt = 1;
	while (cnt < N) {
		if (a[current] == 1) {
			cout << cnt << endl;
			return;
		}
		current = a[current];
		++cnt;
	}
	cout << -1 << endl;
}

int main()
{
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();
}


Measured memory_bytes: 640
import sys

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
B = list(map(int, sys.stdin.readline().split()))

ans = 0
plus = 0
for i in range(N):
    if A[i] >= plus:
        A[i] -= plus
        ans += plus
    else:
        ans += A[i]
        A[i] = 0
    plus = 0

    if A[i] >= B[i]:
        ans += B[i]
    else:
        ans += A[i]
        plus = B[i] - A[i]
    # print(ans, plus)

if A[N] >= plus:
    ans += plus
else:
    ans += A[N]

print(ans)

Measured memory_bytes: 24540
#include <stdio.h>

int main(void){
    char s[200001];
    int a, z, i = 0;

    scanf("%s", s);

    while(s[i] != '\0'){
        if(s[i] == 'A'){
            a = i;
            break;
        }
        i++;
    }

    while(s[i] != '\0'){
        if(s[i] == 'Z') z = i;
        i++;
    }
    
    printf("%d", z - a + 1);

    
    return 0;
}

Measured memory_bytes: 1932
N = int(input())
D = []

count = 0
for i in range(N):
    Di = list(map(int, input().split()))
    D.append(Di)

for i in range(N):
    Di = D[i]
    if Di[0] == Di[1]:
        count += 1
    else:
        count = 0
    if count >= 3:
        print("Yes")
        break
else:
    print("No")

Measured memory_bytes: 9188
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,copy,time
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inp(): return int(input())
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

while True:
    N,M = inpl()
    if N == 0 and M == 0:
        break
    else:
        MINlist = [[0,i] for i in range(N)]
        MAXlist = [[0,i] for i in range(N)]
        for _ in range(M):
            arg = inpl()
            s = arg[0]
            k = arg[1]
            cc = arg[2:]
            for c in cc:
                MAXlist[c-1][0] += s
            if k == 1:
                MINlist[c-1][0] += s

        MINlist.sort()
        MAXlist.sort(reverse=True)
        MinVal1,MinId1 = MINlist[0]
        MinVal2,MinId2 = MINlist[1]
        MaxVal1,MaxId1 = MAXlist[0]
        MaxVal2,MaxId2 = MAXlist[1]
        if MinId1 != MaxId1:
            print(MaxVal1-MinVal1+1)
        else:
            print(max(MaxVal2-MinVal1,MaxVal1-MinVal2)+1)



Measured memory_bytes: 11100
#define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);

signed main() {
	int n, m;
	while (cin >> n >> m, n || m) {
		vector<int>M(n + 1), D(m + 1);
		rep(i, 1, n + 1)cin >> M[i];
		rep(i, 1, m + 1)cin >> D[i];
		int ans(1), i(1);
		while (i < n) {
			i += D[ans];
			if (i >= n) {
				ans++;
				break;
			}
			i += M[i];
			ans++;
		}
		cout << ans - 1 << endl;
	}
	return 0;
}

Measured memory_bytes: 2988
n=gets.to_i
a=gets.split.map &:to_i
b=[a[0]]
(n-1).times do |i|
  b<< b[-1]+a[i+1]
end
g=b[-1]
min=10**15
(n-1).times do |i|
  gg=(b[i]-(b[-1]-b[i])).abs
  min = gg if min>gg
end
p min


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

using namespace std;

typedef unsigned long long ull;
typedef long long ll;

#define MAX ((ll)(1e9))

int main(){
	int n, k;
	ll s;
	cin >> n >> k >> s;
	for(int i=0; i<k; i++) cout << s << " ";
	for(int i=k; i<n; i++){
		if(s!=MAX) cout << s+1 << " ";
		else cout << 1 << " ";
	}
	cout << endl;
	return 0;
}

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

#define FOR(i, a, b) for(int i=(a); i<(b); i++)
#define RFOR(i, a, b) for(int i=(a); i>=(b); i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, n, 0)
#define dump(x)  cout << #x << " = " << (x) << endl;

typedef long long ll;
typedef pair<int, int> ii;
const ll INF = 1e18;
const int inf = 1e8;


void solve()
{
  ll x;
  scanf("%lld",&x);
  ll a;
  scanf("%lld",&a);
  ll b;
  scanf("%lld",&b);
  if (abs(x-a)<abs(x-b)) cout << "A" << endl;
  else cout << "B" << endl;
}

int main()
{
  solve();
  return 0;
}


Measured memory_bytes: 256
#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#define fi first
#define se second

using namespace std;

const long long MOD = (long long)1e9 + 7;
int n;
double p[3009], dp[3009][3009];
int main()
{
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> n;
	cout.precision(20);
	for (int i = 1; i <= n; i++) cin >> p[i];
	dp[0][0] = 1.0;
	for (int j = 1; j <= n; j++) {
		dp[0][j] = (1.0 - p[0 + j])*dp[0][j - 1];
	}
	for (int i = 1; i <= n; i++) {
		dp[i][0] = p[i + 0] * dp[i - 1][0];
		for (int j = 1; j <= n - i; j++) {
			dp[i][j] = p[i + j] * dp[i - 1][j] + (1.0 - p[i + j])*dp[i][j - 1];
		}
	}
	double ans = 0.0;
	for (int i = 1; i <= n; i++) {
		int j = n - i;
		if (i > j) ans += dp[i][j];
	}
	cout << ans << "\n";
	//system("pause");
	return 0;
}

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

int main() {
 int A,B;
  cin >> A >> B;
  if(A>12){
   cout << B << endl; 
  }
  else if(A<6){
   cout << 0 << endl; 
  }
  else{
   cout << B/2 << endl; 
  }
}


Measured memory_bytes: 256
import java.util.Scanner;

public class Main{


	public static boolean isPrime(int num){
		//?´???°????????¶
		//????????????????°?????????´??°??§??????????????????
		if(num==1)return false;
		if(num==2)return true;
		if(num%2==0)return false;

		for(int i=3;i*i<=num;i+=2){
			if(num%i==0)return false;
		}

		return true;
	}



	public static void main(String[] args){

		int N;
		int box, cnt;

		Scanner scan=new Scanner(System.in);
		N=scan.nextInt();

		cnt=0;
		for(int i=0;i<N;i++){
		box=scan.nextInt();

		if(isPrime(box)){cnt++;}
		}

		System.out.println(cnt);

		scan.close();

	}

}

Measured memory_bytes: 42332
s = input()

if s=='RRR':print(3)
if s=='SSS':print(0)

if s=='RRS':print(2)
if s=='SSR':print(1)

if s=='RSR':print(1)
if s=='SRS':print(1)

if s=='SRR':print(2)
if s=='RSS':print(1)


Measured memory_bytes: 9108
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner((System.in));
        String s = sc.next();

        if (Integer.valueOf(s.substring(0, 2)) <= 12 && Integer.valueOf(s.substring(0, 2)) > 0) {
            if (Integer.valueOf(s.substring(2, 4)) <= 12 && Integer.valueOf(s.substring(2, 4)) > 0) {
                System.out.println("AMBIGUOUS");
            } else {
                System.out.println("MMYY");
            }
        } else if (Integer.valueOf(s.substring(2, 4)) <= 12 && Integer.valueOf(s.substring(2, 4)) > 0) {
            System.out.println("YYMM");
        } else {
            System.out.println("NA");
        }
        

		sc.close();
	}
}


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

using ll = long long;
using ld = long double;

int main()
{
    int a,b;
    string op;
    cin >> a>>op>>b;
    if(op=="+")
    {
        cout << a+b<<endl;
    } 
    else
    {
        cout << a-b<<endl;    
    }
    
    return 0;
}

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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for(ll i=0;i<(ll)(n);i++)
#define rep2(i, m, n) for(ll i=m;i<(ll)(n);i++)
#define rrep(i, n, m) for(ll i=n;i>=(ll)(m);i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
    ll N, C;
    cin >> N >> C;
    vector<vector<P>> items(C);
    rep(i, N) {
        ll s, t, c;
        cin >> s >> t >> c;
        c--;
        items[c].emplace_back(s, t);
    }
    vector<vector<ll>> imos(C, vector<ll>(100005, 0));
    vector<ll> ans(100005, 0);
    rep(i, C) {
        rep(j, items[i].size()) {
            P p = items[i][j];
            imos[i][p.first - 1]++;
            imos[i][p.second]--;
        }
        rep(j, 100004) {
            imos[i][j + 1] += imos[i][j];
            ans[j + 1] += (imos[i][j + 1] >= 1 ? 1 : 0);
        }
    }
    cout << (*max_element(ans.begin(), ans.end())) << '\n';
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
    Main();
    return 0;
}


Measured memory_bytes: 27136
import bisect
import sys
sys.setrecursionlimit(10**7)

def dfs(v):
    pos=bisect.bisect_left(dp,arr[v])
    changes.append((pos,dp[pos]))
    dp[pos]=arr[v]
    ans[v]=bisect.bisect_left(dp,10**18)
    for u in g[v]:
        if checked[u]==0:
            checked[u]=1
            dfs(u)
    pos,val=changes.pop()
    dp[pos]=val


n=int(input())
arr=[0]+list(map(int,input().split()))
g=[[] for _ in range(n+1)]
for _ in range(n-1):
    a,b=map(int,input().split())
    g[a].append(b)
    g[b].append(a)
ans=[0]*(n+1)
checked=[0]*(n+1)
checked[1]=1
dp=[10**18 for _ in range(n+1)]
changes=[]
dfs(1)
for i in range(1,n+1):
    print(ans[i])

Measured memory_bytes: 239368
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define ALL(a) a.begin(), a.end()
ll dx[4] = { 0, 1, 0, -1 };
ll dy[4] = { 1, 0, -1, 0 };
const ll INF = 1e14;
const ll MOD = 1e9 + 7;

ll cal(ll n)
{
    return n * (n - 1) / 2;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    vector<string> s(N);
    rep(i, N)
    {
        cin >> s[i];
        sort(s[i].begin(), s[i].end());
    }
    sort(s.begin(), s.end());
    int l = 0, r = 1;
    ll cnt = 0;
    while (true) {
        if (s[l] == s[r]) {
            r++;
        } else {
            cnt += cal(r - l);
            l = r;
            r++;
        }
        if (r == N && s[r-1] == s[l]) {
            cnt += cal(r - l);
            break;
        }
    }
    cout << cnt << endl;
    return 0;
}

Measured memory_bytes: 5760