akhauriyash/Code-Regression huggingface dataset:
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;
import java.util.Scanner;

//方針はDFS(一気に全部突っ込むDFSの場合)
public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        List<List<Integer>> edge = new ArrayList<>(N+1);
        List<List<Integer>> cost = new ArrayList<>(N+1);
        for(int i=1;i<=N+1;i++){
          edge.add(new ArrayList<>());
          cost.add(new ArrayList<>());
        }

        for(int i=1;i<=N;i++){
          edge.get(i).add(0);
          cost.get(i).add(0);
        }

        for(int i=1;i<=N-1;i++){
          int u = sc.nextInt();
          int v = sc.nextInt();
          int w = sc.nextInt();

            edge.get(u).add(v);
            edge.get(v).add(u);
            cost.get(u).add(w);
            cost.get(v).add(w);

        }


        Deque <Integer> dq = new ArrayDeque<>();
        int status [] = new int[N+1];
        int ans [] = new int[N+1];
        int chakushuzumi = 0;


        for(int i=1;i<=N;i++){
            ans[i]=0;
            status[i]=0;
        }
        dq.push(1);
        status[1]=1;
        while(!dq.isEmpty()){
            int last_in = dq.pollFirst();
            for(int i = 1; i <= edge.get(last_in).size()-1;i++){
                if(status[edge.get(last_in).get(i)] == 0){
                    int u = edge.get(last_in).get(i);
                    int w = cost.get(last_in).get(i);
                    ans[edge.get(last_in).get(i)] = (ans[last_in] + w) % 2;
                    status[edge.get(last_in).get(i)]=1;
                    dq.push(edge.get(last_in).get(i));
                }
            }

        }
        for (int i=1;i<=N ;i++) {
            System.out.println(ans[i]);
        }
    }
    }


Measured memory_bytes: 116984
#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll c[100005];
int main(){
  ll n,k;cin>>n>>k;
  ll sum=0;
  for(int i=0;i<n;++i){
    int a,b;cin>>a>>b;
    c[a]+=b;
  }
	
  for(int i=0;i<100005;++i){
    sum+=c[i];
    if(sum>=k){
      cout<<i<<endl;
      return 0;
    }
  }
}

Measured memory_bytes: 1024
#include<bits/stdc++.h>
using namespace std;
#define int long long
int32_t main()
{
  int n,m,i,j,k,l,w,l1=0,l2,l3;
  cin>>n>>m;
  cin>>w;
  map<int,set<int> > m1;
  vector<int> v(n+1),v1(m+1);
  for(i=1;i<=w;i++) {
    cin>>j>>k;m1[j].insert(k);
    v[j]++;
    v1[k]++;
  }
  l=0;
  for(i=1;i<=n;i++)
  {
    if(l<=v[i])
    {
      l=v[i];
    }
  }
  for(i=1;i<=m;i++)
  {
    if(l1<=v1[i]) {l1=v1[i];}
  }
    set<int> s1,s2;
    for(i=1;i<=n;i++)
    {
      if(l==v[i]) s1.insert(i);
    }
    for(i=1;i<=m;i++)
    {
      if(l1==v1[i]) s2.insert(i);
    }
    bool hi=false;
   for(auto it=s1.begin();it!=s1.end();it++){
     for(auto it1=s2.begin();it1!=s2.end();it1++)
     {
       j=*it;k=*it1;
        if(!m1.count(j)||m1[j].find(k)==m1[j].end())
        {
          hi=true;
          break;
        }
     }
     if(hi) break;
   }
  if(hi) cout<<l+l1;
  else cout<<l+l1-1;
}

Measured memory_bytes: 78232
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public class AtCoderA
{
    static void Main()
    {
        var sc = new Scanner();

        int a = sc.NextInt();
        int b = sc.NextInt();
        int c = sc.NextInt();

        string answer = (b - a == c - b) ? "YES" : "NO";

        Console.WriteLine(answer);
    }

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}

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

using namespace std;

typedef long long ll;

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

namespace Printer{
    void print(vector<int> v){
        rep(i, v.size()){
            if(i) cout << " ";
            cout<< v[i];
        }
        cout << endl;
    }

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

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

//--global--//



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

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

    ll a, b, q;
    cin >> a >> b >> q;
    vector<ll> s;
    vector<ll> t;
    vector<ll> x;
    rep(i, a){
        ll tmp;
        cin >> tmp;
        s.push_back(tmp);
    }    
    rep(i, b){
        ll tmp;
        cin >> tmp;
        t.push_back(tmp);
    }    
    rep(i, q){
        ll tmp;
        cin >> tmp;
        x.push_back(tmp);
    }

    rep(i, q){
        ll now = x[i];
        auto its = lower_bound(ALL(s), now) - s.begin();
        auto itt = lower_bound(ALL(t), now) - t.begin();
        ll ans = LLINF;

        // cout << "info" << endl;
        if(its == -1) its = s.size()-1;
        if(itt == -1) itt = t.size()-1;
        // cout << its << endl;
        // cout << itt << endl;
        if(its-1 >= 0 && itt-1 >=0){
            ans = min(ans, abs(min(s[its-1], t[itt-1]) - now));
        }
        if(its < a && itt < b){
            ans = min(ans, abs(max(s[its], t[itt]) - now));
        }
        if(its -1 >= 0 && itt < b){
            ans = min(ans, abs(t[itt] - s[its-1]) + min(abs(t[itt] - now), abs(s[its-1] - now)));
        }
        if(itt -1 >= 0 && its < a){
            ans = min(ans, abs(t[itt-1] - s[its]) + min(abs(s[its] - now), abs(t[itt-1] - now)));
        }
        cout << ans  << endl;
        // cout << "--" << endl;
    }


    
    return 0;
}


Measured memory_bytes: 3828
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int x, y, z, len;
int main(){
	scanf("%d%d%d", &x, &y, &z);
	len = y - x + 1;
	if((z << 1) >= len){
		for(int i = x; i <= y; ++i) printf("%d\n", i);
	}
	else{
		for(int i = x; i <= x + z - 1; ++i) printf("%d\n", i);
		for(int i = y - z + 1; i <= y; ++i) printf("%d\n", i);
	}
	return 0;
} 

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<map>
#include<iomanip>
#define rip(i,n) for(int i=0;i<n;i++)
#define ll long long
#define lb long double
#define vec vector<int>
#define vec2 vector<vector<int>>
#define set setprecision
#define mod 1000000007
#define pi 3.14159265
#include<numeric>
using namespace std;
int gcd(int a, int b)
{
	if (a % b == 0)	return(b);
	else return(gcd(b, a % b));
}
int lcm(int a, int b)
{
	return a * b / gcd(a, b);
}
ll pow(ll a, ll b) {
	ll k = b % 30000, h = b / 30000, s = 1;
	rip(i, 30000)s = (s * a) % mod; ll j = 1;
	if (h != 0)rip(i, h)j = j * s % mod;
	if (k != 0)rip(i, k)j = j * a % mod;
	return(j);
}
int main() {
	int n;
	cin >> n;
	vec a(n);
	int k;
	rip(i, n)cin >> a[i];
	if (n < 10000) {
		int maxxx = 0;
		rip(i, n) {
			if (i != 0) k = a[0];
			else  k = a[1];
			rip(j, n) {
				if (j != i)k = gcd(k, a[j]);
			}
			maxxx = max(maxxx, k);
		}
		cout << maxxx << endl;
	}
	else {
		int p[100009], r[100009];
		int l = a[0];//subetenosaidaikouyakusuu
		rip(i, n) {
			l = gcd(l, a[i]);
			p[i] = l;
		}
		int k = a[n - 1];
		rip(i, n) {
			int j = n - 1 - i;
			k = gcd(k, a[j]);
			r[j] = k;
		}
		p[-1] = l;
		r[n] = l;
		int maxx = 0;
		rip(i, n) {
			maxx = max(maxx, gcd(p[i - 1], r[i + 1]));
		}
		cout << maxx << endl;
	}
}

Measured memory_bytes: 1408
using System;
 
namespace aizu
{
    class OnlineJudge
    {
        static void Main()
        {
            int count;
            while((count = int.Parse(Console.ReadLine())) != 0) {
                var value = new int[7];
                for(int i = 0; i < count; ++i) {
                    ++value[Math.Min(int.Parse(Console.ReadLine()) / 10, 6)];
                }
                foreach(var item in value) {
                    Console.WriteLine(item);
                }
            }
        }
    }
}

Measured memory_bytes: 5812
#k = int(input())
#s = input()
#a, b = map(int, input().split())
#l = list(map(int, input().split()))


s = input()

if s[0] != s[1] or s[1] != s[2] or s[0] != s[2]:
    print ("Yes")
else:
    print("No")



Measured memory_bytes: 8984
open Core
open Stdio

let read_line () = In_channel.(input_line_exn stdin)
let rep i0 n f = for i = i0 to n - 1 do
        f ()
    done
let t2_map f (a, b) = (f a, f b)

let money_inf = 50 * 50
let dist_inf = Int64.(10L ** 15L)
type edge_t = { cod : int; unchin : int; jikan : Int64.t }

let [n;m;s] = read_line () |> String.split ~on:' '
let n = Int.of_string n
let m = Int.of_string m
let s = Int64.of_string s
let s = Int64.(to_int_exn @@ min (of_int money_inf) s)

let edges = Array.create ~len:n []

let () =
    for i = 1 to m do
        let [s_u; s_v; s_un; s_ji] = read_line () |> String.split ~on:' ' in
        let u = Int.of_string s_u - 1 in
        let v = Int.of_string s_v - 1 in
        let un = Int.of_string s_un in
        let ji = Int64.of_string s_ji in
        edges.(u) <- { cod = v; unchin = un; jikan = ji } :: edges.(u);
        edges.(v) <- { cod = u; unchin = un; jikan = ji } :: edges.(v)
    done

let maisu_jikan_s = Array.init n ~f:(fun _ ->
    read_line () |> String.lsplit2_exn ~on:' ' |> (fun (x,y) ->
    let x = Int.(min (of_string x) money_inf) in
    let y = Int64.of_string y in
    (x,y)))

let used = Array.make_matrix ~dimx:n ~dimy:(money_inf+1) false
let res = Array.make_matrix ~dimx:n ~dimy:(money_inf+1) dist_inf

let rec dijk mins = match Set.min_elt mins with
    | None -> ()
    | Some ((dist, v, money) as tup) ->
        let mins = Set.remove mins tup in
        if used.(v).(money) then dijk mins else begin
        used.(v).(money) <- true;
        res.(v).(money) <- Int64.min res.(v).(money) dist;
        (* 銀貨に交換 *)
        let mins = let money = money + fst maisu_jikan_s.(v) in
            let dist = Int64.(dist + snd maisu_jikan_s.(v)) in
            if money <= money_inf && not used.(v).(money)
            then Set.add mins (dist, v, money) 
            else mins in
        (* 別の都市に移動 *)
        let mins = List.fold edges.(v) ~init:mins ~f:(fun mins ch ->
            let money = money - ch.unchin in
            let dist = Int64.(dist + ch.jikan) in
            let v = ch.cod in
            if money >= 0 && not used.(v).(money)
            then Set.add mins (dist, v, money)
            else mins) in
        dijk mins
        end

module DijkData : (Comparator.S with type t = (int64 * int * int)) = struct
    type t = (int64 * int * int)
    include Comparator.Make (struct
        type t = (int64 * int * int)
        let compare = Tuple3.compare ~cmp1:Int64.compare ~cmp2:compare ~cmp3:compare
        let sexp_of_t = Tuple3.sexp_of_t Int64.sexp_of_t Int.sexp_of_t Int.sexp_of_t
    end)
end

let () = dijk (Set.singleton (module DijkData) (0L, 0, s));
    for v = 1 to n - 1 do
        printf "%Ld\n" @@ Option.value_exn (Array.min_elt res.(v) ~compare:Int64.compare)
    done


Measured memory_bytes: 50204
#include <bits/stdc++.h>
using namespace std;
#define rng(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) rng(i,0,b)
#define gnr(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define per(i,b) gnr(i,0,b)
#define ALL(x) (x).begin(),(x).end()
#define RALL(x) (x).rbegin(),(x).rend()
#define IDX(vec, element_iter) distance((vec).begin(), element_iter)
#define pb push_back
#define mp make_pair
#define INF9 1000000000 //10^9
#define MOD 1000000007 //10^9+7
#define fi first
#define sc second
using ll=long long;
using pii=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;

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

    int a,b,c;cin >> a >> b >> c;
    int x = a;
    bool can = false;
    while(x<=a*b){
        if(x%b==c) {
            can = true;
            break;
        }
        x += a;
    }
    cout << (can?"YES":"NO") << endl;;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <memory.h>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <set>
#include <map>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <cctype>
#include <fstream>
#include <cmath>
using namespace std;

#define rep(i, n) for(int i = 0; i< (int)(n); i++)
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define pb(e) push_back(e)
#define foreach(c, i) for(iter(c) i = (c).begin(); i != c.end(); ++i)
#define mp(a, b) make_pair(a, b)

typedef long long ll;
typedef pair<ll, ll> P;
const int INF = 1000 * 1000 * 1000 + 7;
const double EPS = 1e-10;

const int maxn = 11;
const int maxl = 11;

string dp[1 << maxn][maxn][maxl];
int next_str[maxn][maxl][maxn];
int next_len[maxn][maxl][maxn];
string cost[maxn][maxl][maxn];

string needed_str(const string &s1, const string &s2){
  for(int i = min(s1.size(), s2.size()); i > 0; i--){
    if(s1.substr(s1.size()-i, i) == s2.substr(0, i)) return s2.substr(i);
  }
  return s2;
}

void set_next(int &strID, int &len, const string &str, const vector<string> &vs){
  int n = vs.size();
  int best = 0;
  rep(i, n) {
    for(int j = min(str.size(), vs[i].size()); j > 0; j--){
      if(str.substr(str.size() - j, j) == vs[i].substr(0, j) && best < j){
	strID = i;
	len = best = j;
      }
    }
  }
}

void init_table(const vector<string> &vs){
  int n = vs.size();
  rep(i, n)rep(j, vs[i].size() + 1){
    string s1 = vs[i].substr(0, j);
    rep(k, n){
      string s2 = needed_str(s1, vs[k]);
      cost[i][j][k] = s2;
      set_next(next_str[i][j][k], next_len[i][j][k], s1 + s2, vs);
    }
  }
}

vector<string> erase_substr(vector<string> vs){
  int n = vs.size();
  
  rep(i, n)rep(j,n){
    if(i == j) continue;
    
    if(vs[i] == vs[j]){
      if(i < j) vs[j] = "";
    }else if(vs[i].find(vs[j]) != string::npos){
      vs[j] = "";
    }
  }
  vector<string> res;
  rep(i, n){
    if(vs[i] != "") res.pb(vs[i]);
  }
  return res;
}

int main(){
  int N;
  while(cin >> N && N){
    vector<string> vs;
    
    rep(i, N){
      string s;
      cin >> s;
      vs.pb(s);
    }
    
    vs = erase_substr(vs);
    N = vs.size();

    init_table(vs);
    fill(&dp[0][0][0], &dp[0][0][0] + (1 << 10) * 11 * 11, "");
    
    rep(mask, 1 << N)rep(i, N)rep(j, vs[i].size() + 1){
      if(mask > 0 && dp[mask][i][j] == "" || (mask == 0 && j > 0)) continue;
      
      rep(k, N){
	int s = next_str[i][j][k];
	int l = next_len[i][j][k];
	
	string new_str = dp[mask][i][j] + cost[i][j][k];
	string &str = dp[mask | (1 << k)][s][l];
	if(str == "" || str.size() > new_str.size() ||
	   (str.size() == new_str.size() && str > new_str)){
	  str = new_str;
	}
      }
    }

    string res;
    
    rep(i, N)rep(j, vs[i].size() + 1){
      string s = dp[(1 << N) - 1][i][j];
      if(s == "") continue;
      if(res == "") res = s;
      
      if(s.size() < res.size()){
	res = s;
      }else if(s.size() == res.size()){
	res =  min(res, s);
      }
    }
    cout << res << endl;
  }
  return 0;
}

Measured memory_bytes: 4956
#include <iostream>

using namespace std;

int main()
{
    int a,b;
    cin>>a>>b;
    int result = 0;
		if(a > b){
		    result = a + a - 1;
		}else if(a < b){
		    result = b + b - 1;
		}else{
		    result = a + b;
		}
		cout<<result;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<vector>
#include<math.h>
#include<algorithm>
#define ll long long
using namespace std;
struct dog {
	string a;
	int b;
};
int main()
{
	//int h, a;
	//cin >> h >> a;
	//int ans;
	//ans = h / a;
	//if (h - ans*a == 0) {}
	//else { ans += 1; }
	//cout << ans;

	//int h, n;
	//cin >> h >> n;
	//for (int i = 0; i < n; i++)
	//{
	//	int a;
	//	cin >> a;
	//	h -= a;
	//	if (h <= 0) { cout << "Yes"; exit(0); }
	//}
	//cout << "No";

	ll n, k;
	cin >> n >> k;
	vector<ll>QQ;
	for (int i = 0; i < n; i++)
	{
		ll hea;
		cin >> hea;
		QQ.push_back(hea);
	}
	sort(QQ.begin(), QQ.end());
	int key = n - 1;
	if (k > n) { cout << 0; exit(0); }
	for (int i = 0; i < k; i++)
	{
		QQ[key] = 0;
		key--;
		//cout << key << endl;
	}
	ll ans = 0;
	int OO = n - k;
	for (int i = 0; i < OO; i++)
	{
		ans += QQ[i];
	}
	cout << ans;
}



Measured memory_bytes: 5364
#include <iostream>
#include <algorithm>
#include <ostream>
#include <vector>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <cstring>

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

template <typename T, typename U>
std::ostream& operator << (std::ostream& out, std::pair<T, U> p) {
  out << "(" << p.first << ", " << p.second << ")";
  return out;
}

template <typename T>
std::ostream& operator << (std::ostream& out, std::vector<T> v) {
  out << "[";
  for (int i = 0; i < v.size(); ++i) {
    out << v[i] << (i == (v.size() - 1) ? "" : ", ");
  }
  out << "]";
  return out;
}

int main() {
  int ans = int(1e9);
  std::string s;
  cin >> s;

  for (int i = 0; i < s.size() - 2; ++i) {
    int tmp = std::stoi(s.substr(i, 3));

    if (abs(753 - tmp) < ans) {
      ans = abs(753 - tmp);
    }
  }

  cout << ans << endl;

  return 0;
}

Measured memory_bytes: 256
//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
  input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
  obj = init(inLine);
  console.error('\n↑入力 ↓出力');
  Main();
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){  
  return {
    list : input, index : 0, max : input.length,
    hasNext : function(){return (this.index < this.max);},
    next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
  };
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
    var N = nextInt();
    var mod = 1000000007;
    var dp = new Array(200005).fill(0);
    dp[0] = 1;
    var mae = 0;
    for(var i = 0; i < N; i++){
      var C = nextInt();
      if(C == mae){
        continue;
      }
      dp[C] = (dp[C] + dp[mae]) % mod;
      mae = C;
    }
    myout(dp[mae]);
}



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

using namespace std;

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

    vector<string> s(n);

    for(int i=0; i<n; i++)
    {
        cin >> s.at(i);
    }

    sort(s.begin(),s.end());

    int cnt=1;
    for(int i=1; i<n; i++)
    {
        if(s.at(i) != s.at(i-1))
            cnt++;
    }

    cout << cnt << endl;

    return 0;

    
}

Measured memory_bytes: 9536
#include <algorithm>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
ll const INF = 1LL << 60;

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

    int X[N];
    int Y[N];
    int A[N];
    for (int i = 0; i < N; i++) {
        cin >> X[i] >> Y[i] >> A[i];
    }

    int visit[W][H];
    for (int x = 0; x < W; x++) {
        for (int y = 0; y < H; y++) {
            visit[x][y] = 0;
        }
    }
    

    for (int i = 0; i < N; i++) {
        if (A[i] == 1) {
            for (int x = 0; x < X[i]; x++) {
                for (int y = 0; y < H; y++) {
                    visit[x][y] = 1;
                }
            }
        } else if (A[i] == 2) {
            for (int x = X[i]; x < W; x++) {
                for (int y = 0; y < H; y++) {
                    visit[x][y] = 1;
                }
            }
        } else if (A[i] == 3) {
            for (int x = 0; x < W; x++) {
                for (int y = 0; y < Y[i]; y++) {
                    visit[x][y] = 1;
                }
            }
        } else if (A[i] == 4) {
            for (int x = 0; x < W; x++) {
                for (int y = Y[i]; y < H; y++) {
                    visit[x][y] = 1;
                }
            }
        }
    }

    int ans = 0;
    for (int y = H-1; y >=0; y--) {
        for (int x = 0; x < W; x++) {
            if (visit[x][y] == 0)
                ans++;
            // cout << visit[x][y] << " ";
        }
        // cout << endl;
    }

    cout << ans << endl;


   return 0;
}

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

int main() {
  int A, B, C; cin >> A >> B >> C;
  int K; cin >> K;
  vector<int> v;
  v.push_back(A); v.push_back(B) ;v.push_back(C);
  for (int i = 0; i < K; i++) {
    sort(v.begin(), v.end());
    v[2] = 2 * v[2];
  }
  cout << v[0] + v[1] + v[2] << endl;
}


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

int main(){
    int64_t N, sum = 0, memo = 100000, B, i;
    cin >> N;
    for(i = 0; i < N - 1; i++){
        cin >> B;
        if(memo > B)sum += B;
        else sum += memo;
        memo = B;
    }
    sum += memo;
    cout << sum << endl;
}

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

int main() {
  int k, a, b;
  int i = 1;
  int sum;
cin >> k >> a >> b;

    while(sum < a)
    {
        sum = k * i;
        i++;
    }
    if(sum <= b){
        cout << "OK" << endl;
    }
    else
        cout << "NG" << endl;
}

Measured memory_bytes: 3620
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;

LL n,A,B;
LL a[N];
bool judge(LL x){
    LL temp=x*B;
    LL sub=A-B;
    LL sum=0;
    for(LL i=n;i>=1;i--){
        if(a[i]>temp){
            if((a[i]-temp)%sub)
                sum+=(a[i]-temp)/sub+1;
            else
                sum+=(a[i]-temp)/sub;
        }
    }
    if(sum<=x)
        return true;
    return false;
}
int main() {
    scanf("%lld%lld%lld",&n,&A,&B);

    LL right=0;
    for(LL i=1;i<=n;i++){
        scanf("%lld",&a[i]);
        right+=a[i]/B+1;
    }
    sort(a+1,a+1+n);

    LL left=1;
    while(left<right){
        LL mid=(left+right)>>1;
        if(judge(mid))
            right=mid;
        else
            left=mid+1;
    }
    printf("%lld\n",left);

    return 0;
}

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

using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using ld = long double;

#define     _test int _TC; cin >> _TC; while(_TC--)
#define     CS(x) cout << "Case " << x << ": ";
#define     precision(n) cout << fixed << setprecision(n);
#define     Clear(a, b) memset(a, b, sizeof(a))
#define     sq(x) ((x) * 1LL * (x))
#define     eb emplace_back
#define     pb push_back
#define     endl "\n"
#define     all(_n) _n.begin(), _n.end()
#define     rall(_n) _n.rbegin(), _n.rend()
#define     show(x) cout << x << endl
#define     nl cout << endl
#define     sz(x) int(x.size())
#define     gcd(a, b) __gcd(a, b)
#define     lcm(a, b) (abs((a) * (b)) / gcd(a, b))
#define     parity(a, b) ((a&1) ^ (b&1))
#define     IN(a) for(auto &it : a) cin >> it

template <typename T> void print (T t){ for(const auto &it : t) cout << it << ' '; nl; }
template <typename T> using OS = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; // order_of_key find_by_order

const    double Pi = acos(-1.0);
const    int Mod = 1e9 + 7;
const    int Inf = 1 << 30; // INT_MAX = (1LL << 31) -1 // Inf -> 1e9
const    int Maxx = 1e5+10;

/************************************[Code Starts Here]************************************/
vector<int> fnd[Maxx], foe[Maxx];
bool vis[Maxx];
vector<int> comp_no(Maxx), compNodeCnt(Maxx);
int cmp;

void dfs(int s)
{
    vis[s] = 1; comp_no[s] = cmp; compNodeCnt[cmp]++;
    for (auto it : fnd[s]) {
        if (vis[it] == 0) {
            dfs(it);
        }
    }
}


void _case()
{
    int n, m, k; cin >> n >> m >> k;
    for (int i = 0; i < m; i++) {
        int x, y; cin >> x >> y;
        fnd[x].eb(y);
        fnd[y].eb(x);
    }
    for (int i = 0; i < k; i++) {
        int x, y; cin >> x >> y;
        foe[x].eb(y);
        foe[y].eb(x);
    }
    
    for (int i = 1; i <= n; i++) {
        if (vis[i] == 0) {
            dfs(i);
            int ans = compNodeCnt[cmp] - sz(fnd[i]) - 1;
            for (auto it : foe[i]) {
                if (vis[it] == 1 && comp_no[it] == cmp) ans--;
            }
            cout << ans << ' ';
            cmp++;
        } else {
            int ans = compNodeCnt[comp_no[i]] - sz(fnd[i]) - 1;
            for (auto it : foe[i]) {
                if (vis[it] == 1 && comp_no[it] == comp_no[i]) ans--;
            }
            cout << ans << ' ';
        }
    }
}

int32_t main()
{
    ios::sync_with_stdio(false), cin.tie(nullptr);
    _case();
    return 0;
}




Measured memory_bytes: 12672
#!/usr/bin/env python3
import sys

sys.setrecursionlimit(2000000)


def count_linear_process(lis):
    length = len(lis)
    return [l // length for l in lis]


def linear_process(lis):
    length = len(lis)
    return [l % length for l in lis]


def check_end(lis):
    return max(lis) < len(lis)


def add_processed(lis, counts):
    sum_cnt = sum(counts)
    return [l + sum_cnt - cnt for l, cnt in zip(lis, counts)]


def update(lis, n_process=0):
    if check_end(lis):
        return n_process
    next_lis = linear_process(lis)
    counts = count_linear_process(lis)
    return update(
        add_processed(next_lis, counts), 
        n_process + sum(counts)
    )


def solve(N: int, a: "List[int]"):
    print(update(a))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()


Measured memory_bytes: 12148
//contest 162 at coder
#include<bits/stdc++.h>
using namespace std;
#define rep(n) for(int i=0;i<n;i++)
#define pb push_back
int main(){
	int n,m;
	cin>>n>>m;
	int sum=0;
	for(int i=0;i<m;i++){
		int x;
		cin>>x;
		sum+=x;
	}
	if(n-sum<0) cout<<-1<<endl;
	else cout<<n-sum<<endl;

	return 0;
}

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

using namespace std;

int dp[3007][3007];

int main(){
	memset(dp, 0, sizeof(dp));
	string s, t;
	cin>>s>>t;
	for(int x=1;x<=s.size();x++){
		for(int j=1;j<=t.size();j++){
			if(s[x-1] == t[j-1]) dp[x][j] = 1 + dp[x-1][j-1];
			else dp[x][j] = max(dp[x-1][j], dp[x][j-1]);
		}
	}
	int i = (int)s.size(), j = (int)t.size();
	string ans;
	while(i>0 && j>0) {
		if(dp[i][j] == dp[i-1][j]){
			i--;
		} else if(dp[i][j] == dp[i][j-1])j--;
		else if(dp[i][j] == 1 + dp[i-1][j-1]){
			ans.push_back(s[i-1]);
			i--;
			j--;
		}
	}
	reverse(ans.begin(), ans.end());
	cout<<ans<<"\n";
	return 0;
}

Measured memory_bytes: 35584
import sys
from collections import Counter
from collections import deque
import heapq
import math
import fractions
import bisect
import itertools
def input(): return sys.stdin.readline().strip()
def mp(): return map(int,input().split())
def lmp(): return list(map(int,input().split()))

n,q=mp()
tree=[[] for i in range(n+1)]
c=[0]*(n+1)
done=[0]*(n+1)
for i in range(n-1):
    a,b=mp()
    tree[a].append([a,b])
    tree[b].append([b,a])
for i in range(q):
    p,x=mp()
    c[p]+=x
que=deque()
que.append(tree[1])
done[1]=1
while len(que):
    q=que.popleft()
    for v in q:
        if done[v[1]]==0:
            done[v[1]]=1
            c[v[1]]+=c[v[0]]
            que.append(tree[v[1]])
print(*c[1:])

Measured memory_bytes: 126024
fun main() {
    val mod = 1_000_000_007L
    val (h, w, a, b) = nextIntList()
    val factList = mutableListOf(1L)
    val modFactList = mutableListOf(modinv(1L, mod))

    var f = 1L
    (1..(h + w)).forEach {
        f = f.times(it).rem(mod)
        factList.add(f)
        modFactList.add(modinv(f, mod))
    }

    fun combi(i: Int, j: Int): Long {
        return factList[i + j].times(modFactList[i]).rem(mod).times(modFactList[j]).rem(mod)
    }
    val hn = h - a - 1
    var s = 0L
    (0 until b).forEach {
        s = s.plus(combi(hn, it).times(combi(a - 1, w - it - 1)).rem(mod)).rem(mod)
    }
    println(combi(h - 1, w - 1).minus(s).plus(mod).rem(mod))
}

// # Utils
// ## Input
fun next() = readLine()!!
fun nextInt() = next().toInt()
fun nextLong() = next().toLong()
fun nextDouble() = next().toDouble()
fun nextList() = next().split(" ")
fun nextIntList() = next().split(" ").map(String::toInt)
fun nextLongList() = next().split(" ").map(String::toLong)
fun nextDoubleList() = next().split(" ").map(String::toDouble)

// ## Array
inline fun <reified T> sizedArray(size: Int, default: T) = Array(size) { default }
inline fun <reified T> sized2DArray(row: Int, column: Int, default: T) = Array(row) { Array(column) { default } }

// ## mod^-1
//    [NOTE] CANNOT use this for 0, mod, 2mod, 3mod ...(return wrong value)
fun modinv(num: Long, mod: Long): Long {
    var a = num
    var b = mod
    var u = 1L
    var v = 0L
    while (b > 0) {
        val t = a.div(b)
        a -= t * b
        a = b.also { b = a } // swap a and b
        u -= t * v
        u = v.also { v = u } // swap u and v
    }
    u %= mod
    if (u < 0) u += mod
    return u
}

fun gcd(x: Int, y: Int): Int {
    fun innerGcd(x: Int, y: Int): Int {
        return if (x % y == 0) y else innerGcd(y, x % y)
    }
    return if (x >= y) innerGcd(x, y) else innerGcd(y, x)
}

fun gcd(x: Long, y: Long): Long {
    fun innerGcd(x: Long, y: Long): Long {
        return if (x % y == 0L) y else innerGcd(y, x % y)
    }
    return if (x >= y) innerGcd(x, y) else innerGcd(y, x)
}

// classes
class UnionFindTree(val size: Int) {
    private val r = sizedArray(size, -1)
    fun root(x: Int): Int {
        if (r[x] < 0) return x
        return root(r[x])
    }

    fun unite(x: Int, y: Int) {
        var rx = root(x)
        var ry = root(y)
        if (rx == ry) return
        if (r[rx] > r[ry]) {
            rx = ry.also { ry = rx }
        }
        r[rx] += r[ry]
        r[ry] = rx
    }

    fun size(x: Int) = -r[root(x)]

    override fun toString(): String {
        return r.joinToString(separator = ", ")
    }
}

Measured memory_bytes: 62660
from collections import Counter

N = int(input())
a=[]
for i in range(N):
    s=input()
    s="".join(sorted(s)).split()
    a.append(s[0])
num=Counter(a).values()
#print(list(num))
ans=0
for i in (list(num)):
    ans+=i*(i-1)//2
print(ans)


Measured memory_bytes: 69080
N = int(input())
X = []
Y = []
H = []
xc = None
yc = None
hc = None
for _ in range(N):
    x, y, h = map(int,input().split())
    X.append(x)
    Y.append(y)
    H.append(h)
    if h > 0:
        xc = x
        yc = y
        hc = h

for pos_x in range(101):
    for pos_y in range(101):
        center_h = hc + abs(xc - pos_x) + abs(yc - pos_y)

        flag = True
        for n in range(N):
            if H[n] != max(center_h - abs(X[n] - pos_x) - abs(Y[n] - pos_y), 0):
                flag = False
                break
        if flag:
            print(pos_x, pos_y, center_h)
            exit()


Measured memory_bytes: 3064
(() => {
    const memoize = function(proc){
        const cache = {};

        const helper = (n) => {
            if(!cache[n]){
                cache[n] = proc(n);
            }
            return cache[n];
        }

        return helper;
    }

    const fib = memoize((n) => {
        if(n < 2){
            return 1;
        }
        return fib(n - 1) + fib(n - 2);
    });

    let input = require('fs').readFileSync('/dev/stdin', 'utf8').split("\n");
    console.log(fib(Number(input[0])));
})();


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

#define REP(i ,n) for(int i=0 ;i < n; i++)
#define REPB(i ,n) for(int i=n; i >= 0; i--)
#define FOR(i ,m ,n) for(int i=m; i < n; i++)
#define FORB(i, m, n) for(int i=m; i>= n; i--)
#define ll long long
#define pb push_back
#define popb pop_back
#define debug(i) cout << i << endl
using namespace std;
int main(){
  ll x, y;
  cin >> x >> y;
  int ans = 0;
  for(ll i = x; i <= y; i *= 2) ans++;
  cout << ans << endl;
  return 0;
}

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

#define MAX_N 3000

int main() {
	int N, a[MAX_N];
	int res;

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

	res = 0;
	for (int i = 0; i < N; i++) {
		res += a[i] - 1;
	}

	printf("%d\n", res);

	return 0;
}

Measured memory_bytes: 128
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using static System.Math;

namespace CSharpSpiral {
    class Program {
        static void Main(string[] args) {
            var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            new Program().Solve(new ConsoleInput(Console.In, ','));
            Console.Out.Flush();
        }
		public void Solve(ConsoleInput cin) {
			int caseCount = 0;
			while(true){
				var W = cin.ReadInt;
				if(W == 0) break;
				caseCount++;
				var N = cin.ReadInt;
				var dp = new long[W + 1];
				for(int i = 0; i < N; i++) {
					var d = cin.ReadIntArray(2);
					var wi = d[1];
					for(int j = W-wi; j >= 0; j--) 
						dp[j + wi] = Max(dp[j + wi], dp[j] + d[0]);
				}
				var index = Enumerable.Range(0, W + 1).Aggregate((y, j) => dp[y] < dp[j] ? j : y);
				Console.WriteLine($"Case {caseCount}:");
				Console.WriteLine(dp[index]);
				Console.WriteLine(index);
			}
		}

	}
	static class ExtendClass {
		public static string Join<T>(this IEnumerable<T> X, string separator = " ") => string.Join(separator, X);
	}
	class ConsoleInput {
		private readonly TextReader _stream;
		private readonly char _separator = ' ';
		private readonly Queue<string> inputStream;
		public ConsoleInput(TextReader stream, char separator = ' ') {
			this._separator = separator;
			this._stream = stream;
			inputStream = new Queue<string>();
		}
		public string Read {
			get {
				if(inputStream.Count != 0) return inputStream.Dequeue();
				string[] tmp = _stream.ReadLine().Split(_separator);
				for(int i = 0; i < tmp.Length; ++i)
					inputStream.Enqueue(tmp[i]);
				return inputStream.Dequeue();
			}
		}
		public string ReadLine { get { return _stream.ReadLine(); } }
		public int ReadInt { get { return int.Parse(Read); } }
		public long ReadLong { get { return long.Parse(Read); } }
		public double ReadDouble { get { return double.Parse(Read); } }
		public string[] ReadStrArray(long N) {
			var ret = new string[N];
			for(long i = 0; i < N; ++i)
				ret[i] = Read;
			return ret;
		}
		public int[] ReadIntArray(long N) { var ret = new int[N]; for(long i = 0; i < N; ++i) ret[i] = ReadInt; return ret; }
		public long[] ReadLongArray(long N) { var ret = new long[N]; for(long i = 0; i < N; ++i) ret[i] = ReadLong; return ret; }
		public double[] ReadDoubleArray(long N) { var ret = new double[N]; for(long i = 0; i < N; ++i) ret[i] = ReadDouble; return ret; }
	}

}



Measured memory_bytes: 18700
import sys
from collections import deque
input = sys.stdin.buffer.readline
from collections import defaultdict
N = int(input())
g = defaultdict(list)
color = defaultdict(int)
gn = [0]*(N+1)
edges = []
for i in [0]*(N-1):
    a,b = map(int,input().split())
    edges.append((a,b))
    g[a].append(b)
    g[b].append(a)
    gn[a]+= 1
    gn[b]+= 1

M = max(gn)

dq = deque([1])
visited = [False] * (N+1)
ncolor = [-1]*(N+1)
ncolor[1] = 0
while dq:
    s = dq.popleft()
    visited[s] = True
    s_clr = ncolor[s]
    for t in g[s]:
        if not visited[t]:
            s_clr = (s_clr + 1) % M
            ncolor[t] = s_clr
            if s < t:
                color[(s,t)] = s_clr
            else:
                color[(t,s)] = s_clr
            dq.append(t)
print(M)
for edge in edges:
    print(color[edge]+1)








Measured memory_bytes: 55500
N = input()
num = input().split(" ")
count = int(0)
for i in range(int(N)):
  if (int(num[i]) % 2) == 1:
    pass
  elif (int(num[i]) % 3) != 0 and (int(num[i]) % 5) != 0:
    print("DENIED")
    count = count + 1
    break
  else:
    pass

if count == 0:
    print("APPROVED")
    

    

Measured memory_bytes: 3060
#include<stdio.h>

int main()
{

  int n, m, a[ 100000 ];
  int i;
  int MAX = 0, tmp;

  scanf( "%d%d", &n, &m );

  for( i = 0; i < m; i++ ){
    scanf( "%d", &a[ i ] );
  }

  if( a[ 0 ] != 1 ) MAX = a[ 0 ] - 1;

  for( i = 0; i < m; i++ ){
    tmp = ( a[ i ] - a[ i - 1 ] ) / 2;
    if( MAX < tmp ) MAX = tmp;
  }

  tmp = n - a[ m - 1 ];
  if( MAX < tmp ) MAX = tmp;

  
  printf( "%d\n", MAX );

  return 0;

}

Measured memory_bytes: 1876
N, C = gets.split.map(&:to_i)
D = C.times.map { gets.split.map(&:to_i) }
grid = N.times.map { gets.split.map(&:to_i) }

counter = Hash.new { |h, k| h[k] = Hash.new(0) }

grid.each.with_index(1) do |row, i|
  row.each.with_index(1) do |c, j|
    id = (i + j) % 3
    counter[id][c] += 1
  end
end

ans = Float::INFINITY

[*1..C].permutation(3).each do |colors|
  cost = 0

  3.times do |id|
    counter[id].each do |k, cnt|
      cost += cnt * D[k - 1][colors[id] - 1]
    end
  end

  if ans > cost
    ans = cost
  end
end

puts ans


Measured memory_bytes: 4348
#include <bits/stdc++.h>
#define st first
#define nd second

using namespace std;

void debug_out() { cerr << endl; }
template<class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for(auto e : v) out << e << '\n'; return out;}
template<class T> ostream& operator<<(ostream& out, vector <T> v) { return prnt(out, v); }
template<class T> ostream& operator<<(ostream& out, set <T> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, map <T1, T2> v) { return prnt(out, v); }
template<class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out  << p.st << ' ' << p.nd; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)
#define dbg_v(x, n) do{cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)
#define dbg_ok cerr<<"OK!\n"

const long long N = 55;
long long n, mn = 1, mx = 1, a[N];
vector <pair<long long, long long> > ans;

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

	cin >> n;
	for(long long i = 1; i <= n; i++) {
		cin >> a[i];
		if(a[i] < a[mn]) mn = i;
		if(a[i] > a[mx]) mx = i;
	}

	long long who;
	if(a[mn] < 0 && a[mx] >= 0) {
		if(abs(a[mn]) > abs(a[mx]))
			who = mn;
		else 
			who = mx;

		for(long long i = 1; i <= n; i++) {
			if(i != who) {
				a[i] += a[who];
				ans.push_back({who, i});
			}
		}
	}

	if(a[1] >= 0)
		for(long long i = 2; i <= n; i++) {
			if(a[i] > a[i - 1]) continue;
			a[i] += a[i - 1];
			ans.push_back({i - 1, i});
		}
	else 
		for(long long i= n - 1; i >= 1; i--) {
			if(a[i] < a[i + 1]) continue;
			a[i] += a[i + 1];
			ans.push_back({i + 1, i});
		}

	// dbg_v(a, n/ + 1);
	cout << ans << '\n';
}	

	

Measured memory_bytes: 256
#include<iostream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<algorithm>
#include<functional>
#include<utility>
#include<cmath>
#include<complex>
#include<iomanip>

using namespace std;

#define REP(i,s,e) for(int i=int(s);i<=int(e);i++)
#define rep(i,n) for(int i=0;i<int(n);i++)
#define pi 3.14159265358979


int main(void){

	int n,m,b;
	string a,c;

	for(;;){
	cin >> n >> m;
	if(n==0 && m==0) break;

	vector<string>v1;
	vector<int>v2;
	rep(i,n){
		cin >> a >> b;
		v1.push_back(a);
		v2.push_back(b);
	}

	int x=0;
	rep(i,m){
		cin >> c;

	vector<int>s; s.push_back(0);
	for(int j=0;j<n;j++){
		bool f=true;
		for(int k=7;k>=0;k--){
			if(c[k]!=v1[j][k]){
				f=false;
				if(v1[j][k]=='*'){
					s.push_back(v2[j]); break;
				}
				else break;
			}
		}

		if(f) s.push_back(v2[j]);
	}

	sort(s.begin(),s.end());
	x+=s.back();

	}

	cout << x << endl;

	}

	return 0;
}

Measured memory_bytes: 1240
#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
	int n, m;
	cin >> n >> m;
	cout << n * (n - 1) / 2 + m * (m - 1) / 2 << endl;
}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using static System.Console;
using static System.Math;

namespace AtTest.TokioMarine_2020
{
    class D
    {
        static void Main(string[] args)
        {
            var sw = new System.IO.StreamWriter(OpenStandardOutput()) { AutoFlush = false };
            SetOut(sw);

            Method(args);

            Out.Flush();
        }

        static void Method(string[] args)
        {
            int n = ReadInt();
            int[][] vws = new int[n][];
            for (int i = 0; i < n; i++)
            {
                vws[i] = ReadInts();
            }
            int half = Min(n, 1 << 10 - 1);
            int[,] dp = new int[half, 100000 + 100];
            dp[0, vws[0][1]] = vws[0][0];
            for (int i = 1; i < 100000 + 100; i++)
            {
                dp[0, i] = Max(dp[0, i], dp[0, i - 1]);
            }
            for (int i = 1; i < half; i++)
            {
                for (int j = 0; j < 100000 + 100; j++)
                {
                    dp[i, j] = dp[(i - 1) / 2, j];
                    if (j >= vws[i][1])
                    {
                        dp[i, j] = Max(dp[i, j], dp[(i - 1) / 2, j - vws[i][1]] + vws[i][0]);
                    }
                }
            }

            int q = ReadInt();
            for (int i = 0; i < q; i++)
            {
                int[] vl = ReadInts();
                int v = vl[0] - 1;
                int l = vl[1];

                WriteLine(DFS(v, l, half, dp, vws));
            }
        }

        static int DFS(int now,int l,int half,int[,] dp,int[][]vws)
        {
            if (now < half)
            {
                return dp[now, l];
            }

            int parent = (now - 1) / 2;

            int res = DFS(parent, l, half, dp, vws);
            if (vws[now][1] <= l)
            {
                res = Max(res, DFS(parent, l - vws[now][1], half, dp, vws) + vws[now][0]);
            }

            return res;
        }

        private static string Read() { return ReadLine(); }
        private static char[] ReadChars() { return Array.ConvertAll(Read().Split(), a => a[0]); }
        private static int ReadInt() { return int.Parse(Read()); }
        private static long ReadLong() { return long.Parse(Read()); }
        private static double ReadDouble() { return double.Parse(Read()); }
        private static int[] ReadInts() { return Array.ConvertAll(Read().Split(), int.Parse); }
        private static long[] ReadLongs() { return Array.ConvertAll(Read().Split(), long.Parse); }
        private static double[] ReadDoubles() { return Array.ConvertAll(Read().Split(), double.Parse); }
    }
}


Measured memory_bytes: 259116
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    vector<string>v;
     map<string,ll>mp;
     map<char,ll>tap;
    char a[] = {'A','C','H','M','R'};
      do {
       string s = "" ;
       s += a[0] ;
       s += a[1] ;
       s += a[2] ;
       sort(s.begin(),s.end());
       if(!mp[s])
        v.push_back(s);
      mp[s]++;

    } while (next_permutation(a, a + 5));

    ll n;
    cin >> n;
    for(int i=0; i<n; i++)
    {
        string s;
        cin >> s;
        tap[s[0]]++;
    }

    ll ans = 0 ;
    for(int i=0; i<v.size(); i++)
    {
        ll x = tap[v[i][0]];
        ll y = tap[v[i][1]];
        ll z = tap[v[i][2]];
        ans += x*y*z ;
    }


     cout << ans << endl;


}


Measured memory_bytes: 3516
#include<stdio.h>
#include<string.h>
int main(){
	int n,x,i,j,k;
	char s[10];
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&x);
		printf("Case %d:\n",i+1);
		for(j=0;j<10;j++){
			x=x*x;
			for(k=0;k<8;k++){
				s[k]=x%10+'0';
				x=x/10;
			}
			x=0;
			for(k=5;k>=2;k--){
				x*=10;
				x+=s[k]-'0';
			}
			printf("%d\n",x);
		}
	}
	return 0;
}

Measured memory_bytes: 0
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <math.h>
#include <cstdint>
#include <cstring>

//#define SIZE_OF_ARRAY(array) (sizeof(array)/sizeof(array[0]))

using namespace std;

int main() {
    int n;
    cin >> n;
    if (n == 1) {
        cout << "Yes" << endl;
        return 0;
    }
 
    int* h = new int[n];
    for (int i = 0; i < n; i++) {
        cin >> h[i];
    }
 
    h[0] = max(h[0]-1, 1);
 
    for (int i = 1; i < n; i++) {
        int diff = h[i] - h[i - 1];
        if (diff >= 1) {
            h[i] = max(h[i]-1, 1);
        }
        else if (diff < 0) {
            cout << "No" << endl;
            return 0;
        }
    }
 
    cout << "Yes" << endl;
 
    return 0;
}

Measured memory_bytes: 640