akhauriyash/Code-Regression huggingface dataset:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

N = int(input())
S = list(input())
Q = int(input())


alphabet = "abcdefghijklmnopqrstuvwxyz"

bag = { l:[] for l in alphabet}

for i in range(N):
  bag[S[i]].append(i+1)


import bisect

for _ in range(Q):
  t,a,b = input().split()
  a = int(a)
  if int(t) == 1:
    if S[a-1]!=b:
      bag[S[a-1]].remove(a)
      S[a-1] = b
      bisect.insort(bag[b],a)
  else:
    count = 0
    b = int(b)
    for l in alphabet:
      if len(bag[l])>0:
        left = bisect.bisect_left(bag[l],a)
        if left != len(bag[l]):
          if bag[l][left] <= b:
            count += 1
    print(count)


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

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

  int left = A + B;
  int right = C + D;
  
  if (left > right ){
    cout << "Left" << endl;
  }
  
  else if ( right > left ){
    cout << "Right" << endl;
  }
  
  else {
    cout << "Balanced" << endl;
  }
  
}

Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10000000)
def input():
    return sys.stdin.readline()[:-1]
from bisect import *
from collections import *
from heapq import *
from math import *
from itertools import *

N, M = map(int, input().split())
es = [[] for i in range(N+1)]
for i in range(M):
    a, b = map(int, input().split())
    es[a].append(b)
    es[b].append(a)
path = [[], []]
used = [0] * (N+1)
used[1] = 1
for i in range(2):
    dq = deque()
    dq.append(1)
    while len(dq):
        v = dq.popleft()
        for u in es[v]:
            if used[u] == 0:
                used[u] = 1
                dq.append(u)
                path[i].append(u)
                break
path = path[0][::-1] + [1] + path[1]
print(len(path))
print(*path)


Measured memory_bytes: 25532
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    A = list(map(int, readline().split()))
    A.sort()
    if A[0] == A[1]:
        print(A[2])
    else:
        print(A[0])
    
    return


if __name__ == '__main__':
    main()


Measured memory_bytes: 9096
#include <algorithm>
#include <bitset>
#include <complex>
#include <cstdio>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

using namespace std;

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

typedef long long ll;

const int Inf = 100100100;
const double EPS = 1e-9;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, c;
    cin >> n >> m >> c;
    int b[m], a[n][m];
    rep(i, m) {
      cin >> b[i];
    }
    rep(i, n) {
      rep(j, m) {
        cin >> a[i][j];
      }
    }
    int ans = 0;
    rep(i, n) {
      int sum = 0;
      rep(j, m) {
        sum += a[i][j] * b[j];
      }
      sum += c;
      if (sum > 0) ans++;
    }

    cout << ans << '\n';

    return 0;
}


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

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
#define REP(i, n) for(int i=0; i<n; i++)
#define REPi(i, a, b) for(int i=int(a); i<int(b); i++)
#define MEMS(a,b) memset(a,b,sizeof(a))
#define mp make_pair
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; }
const ll MOD = 1e9+7;

ll X[10][10];
string S;

int main(){
    ll N;
    cin >> N;

    REPi(k,1,N+1){
        ll right = k%10; 
        ll y = k;
        ll left = 0;
        while(y > 0){
            left = y;
            y /= 10;
        }
        X[left][right]++;
    }

    ll ans = 0;
    REPi(i,1,10){
        REP(k,10){
            ll num = X[i][k] * X[k][i];
            ans += num; 
        }
    }
    cout << ans << endl;
    return 0;
}


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

int f(int n){
  int ans;
  while(n != 0){
  	ans += n%10;
  	n /= 10;
  }
  return ans;
}

int main(){
  int n;
  cin >> n;
  if(n%f(n) == 0){
  	cout << "Yes" << endl;
  }
  else{
  	cout << "No" << endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <functional>
#include <bitset>
#include <cassert>
using namespace std;
using ll = long long;


int main() {
	int H, W, N; cin >> H >> W >> N;
	int sr, sc; cin >> sr >> sc;
	string s, t; cin >> s >> t;
	string a = "UDU", b = "LRL";
	for (int k = 0; k < 2; k++) {
		int pos = sr;
		for (int i = 0; i < N; i++) {
			if (s[i] == a[k]) pos = a[k] == 'D' ? pos + 1 : pos - 1;
			if (pos > H || pos < 1) {
				cout << "NO" << endl;
				return 0;
			}
			if (t[i] == a[k + 1]) pos = a[k + 1] == 'D' ? min(H, pos + 1) : max(1, pos - 1);
		}
	}
	for (int k = 0; k < 2; k++) {
		int pos = sc;
		for (int i = 0; i < N; i++) {
			if (s[i] == b[k]) pos = b[k] == 'R' ? pos + 1 : pos - 1;
			if (pos > W || pos < 1) {
				cout << "NO" << endl;
				return 0;
			}
			if (t[i] == b[k + 1]) pos = b[k + 1] == 'R' ? min(W, pos + 1) : max(1, pos - 1);
		}
	}
	cout << "YES" << endl;
	return 0;
}

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

using namespace std;

#define ll long long

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

    int a, b;

    cin >> a >> b;
    if (a < b) swap(a,b);

    for (int i = 0; i < a; i++) cout << b;

    return 0;
}

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

int main(){
  int n;
  cin >> n;
  string s;
  cin >> s;
  long long ans=1;
  char tmp = s.at(0);
  for(int i=1; i<n; i++){
    //cout << tmp << endl;
    if(tmp!=s.at(i))ans++;
    tmp = s.at(i);
  }
  cout << ans << endl;
}


Measured memory_bytes: 512
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
char s1[305][305]={0};
int main(){
  ll n;
  cin>>n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++) cin>>s1[i][j];
  }
  ll ans=0;
  for(int del=-n;del<=n;del++){
    bool f=true;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
        ll nx=(j+n-del)%n,ny=(i+n+del)%n;
        if(s1[i][j]!=s1[nx][ny]) f=false;
      }
    }
    if(f) ans+=n-abs(del);
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 384
fn read_stdin() -> String {
    use std::io::Read;
    let mut input = String::new();
    std::io::stdin().read_to_string(&mut input).unwrap();
    input
}

fn scan<'a, I: Iterator<Item = &'a str>, T: std::str::FromStr>(tokens: &mut I) -> T {
    tokens.next().unwrap().parse().ok().unwrap()
}

fn vec<T, F: FnMut(usize) -> T>(n: usize, mut f: F) -> Vec<T> {
    (0..n).map(|i| f(i)).collect()
}

fn main() {
    let input = read_stdin();
    let tokens = &mut input.split_whitespace();
    let h: usize = scan(tokens);
    let n: usize = scan(tokens);
    let a: Vec<usize> = vec(n, |_| scan(tokens));
    let sum_a: usize = a.iter().sum();
    println!("{}", if h <= sum_a { "Yes" } else { "No" });
}


Measured memory_bytes: 6396
#include <cstdio>
#include <algorithm>
using namespace std;
#define N 100050
double a[N],c[N],d[N];
int p[N],b[N],n,m,i,j,top,sta[N];
long long k;
bool vis[N];
int main()
{
	scanf("%d",&n);
	for (i=1;i<=n;i++) scanf("%lf",&a[i]);
	for (i=1;i<n;i++) p[i]=i;
	scanf("%d%lld",&m,&k);
	while (m--){
		int x;scanf("%d",&x);
		swap(p[x-1],p[x]);
	}
	for (i=1;i<n;i++) if (!vis[i]){
		top=0;
		sta[top]=i;vis[sta[top]]=true;
		while (p[sta[top]]!=i) sta[++top]=p[sta[top-1]],vis[sta[top]]=true;
		for (j=0;j<=top;j++) b[sta[j]]=sta[(j+k%(top+1))%(top+1)];
	}
	for (i=1;i<n;i++) c[i]=a[i+1]-a[i];
	for (i=1;i<n;i++) d[i]=c[b[i]];
	for (i=2;i<=n;i++) a[i]=a[i-1]+d[i-1];
	for (i=1;i<=n;i++) printf("%.10f\n",a[i]);
	return 0;
}

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

int main() {
	int n;
	cin >> n;
	vector<char>tmp(1000);
	vector<string>word(1000);
	for (int i = 0; i < n; i++) {
		cin >> word.at(i);
		tmp.at(i) = word.at(i).at(word.at(i).size() - 1);
		//cout << tmp.at(i)<<endl;
	}
	int count = 0;
	for (int i = 1; i < n; i++) {
		if (word.at(i).at(0) != tmp.at(i - 1)) {
			cout << "No";
			count++;
			break;
		}
		for (int m = 0; m < i; m++) {
			if (word.at(i) == word.at(m)) {
				cout << "No";
				count++;
				break;
			}
		}
	}
	if (count == 0) {
		cout << "Yes";
	}

}

Measured memory_bytes: 256
n, a, b = map(int, input().split())
ans = []

for i in range (n+1):
    i_10000 = i//10000
    i_1000 = i%10000//1000
    i_100 = i%1000//100
    i_10 = i%100//10
    i_1 = i%10
    i_sum = i_10000 + i_1000 + i_100 + i_10 + i_1
    if a<=i_sum<=b:
        ans.append(i)

print(sum(ans))

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

using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> TUP;
#define rep(i,n) for(int i=0;i<n;i++)
#define mp(a,b) make_pair(a,b)
const ll MOD=1000000007;

int main(){
	int K,S; cin>>K>>S;
  
  int cnt=0;
  rep(X,K+1){
  	rep(Y,K+1){
    	if(S-X-Y>=0 && S-X-Y<=K) cnt++;
    }
  }
  
  printf("%d",cnt);
}

Measured memory_bytes: 256
package main

import "fmt"

func main() {
	var s string
	fmt.Scan(&s)

	for i := 0; i < len(s)-1; i++ {
		f := s[i:i+1]
		s := s[i+1:i+2]
		if f == s {
			fmt.Println("Bad")
			return
		}
	}
	fmt.Println("Good")
}


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

int main() {
  long N, K;
  cin >> N >> K;
  vector<pair<long, long>> V(N);
  for (long i = 0; i < N; i++) {
    long a, b;
    cin >> a >> b;
    V.at(i) = {a, b};
  }
  sort(V.begin(), V.end());
  for (auto [a, b] : V) {
    K -= b;
    if (K <= 0) return cout << a << "\n", 0;
  }
}

Measured memory_bytes: 4820
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
  long long n,m,ans;
  cin>>n>>m;
  if(n==1&&m==1)
    ans=1;
  else if(n==1||m==1)
    ans=n*m-2;
  else
    ans=(n-2)*(m-2);
  cout<<ans;
  return 0;
}

Measured memory_bytes: 256
#include <stdio.h>

int fibonacci(int n);
int F[44];

int main(){
    int n;        /* n????????£??????????????°????±??????? */

    scanf("%d", &n);

      /*  ?????£??????????????°?????¨??? */
    printf("%d\n", fibonacci(n));

    return 0;
}

int fibonacci(int n){

    int i;

    F[0] = 1;	/* n ??? 1 ?????? result[1] ??? 1 */
    F[1] = 1;	/* n ??? 2 ?????? result[2] ??? 1 */

      /* n?????????????????????????????????????????????????????? */
    for (i = 2; i <= n; i++){/* ?¨?????????????????????????????¨???????????????? */
        F[i] = F[i - 1] + F[i - 2];
}
    return (F[n]);
}

Measured memory_bytes: 596
import bisect
import cmath
import heapq
import itertools
import math
import operator
import os
import random
import re
import string
import sys
from collections import Counter, defaultdict, deque
from copy import deepcopy
from decimal import Decimal
from functools import lru_cache, reduce
from math import gcd
from operator import add, itemgetter, mul, xor

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(10 ** 9)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7
# MOD = 998244353


N = int(sys.stdin.buffer.readline())
C = sys.stdin.buffer.readline().decode().rstrip()

ans = 0
l = 0
r = N - 1
while l < r:
    while r >= 0 and C[r] == 'W':
        r -= 1
    while l < N and C[l] == 'R':
        l += 1
    if l < r:
        ans += 1
        l += 1
        r -= 1
print(ans)


Measured memory_bytes: 27364
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace CsProgramSpace
{
    public partial class Pencil
    {

        partial class Program
        {
            static void Main(string[] args)
            {
                int n = int.Parse(Console.ReadLine());
                int[] a = Console.ReadLine().Split(' ').Select(_ => int.Parse(_)).ToArray();
                int t, j;
                string s="";
                show(ref a, ref s);
                for (int i = 1;i<n;i++)
                {
                    t = a[i];
                    j = i - 1;
                    while(j>=0 && a[j]>t)
                    {
                        a[j + 1] = a[j];
                        j--;
                    }
                    a[j + 1] = t;
                    show(ref a, ref s);
                }
            }
            static void show(ref int[] a,ref string s)
            {
                foreach (int tm in a)
                    s += $"{tm} ";
                s = s.Remove(s.Length - 1);
                Console.WriteLine(s);
                s = "";
            }
        }
    }
}


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

#############
# Libraries #
#############
import time
 
START = (int(time.time() * 1000))

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

D = int(input().strip())
C = list(map(int,input().split()))
S = [list(map(int, input().split())) for _ in range(D)]

ans = 0
score = 0
last = [[-1] for i in range(26)]
schedule = []

for d in range(D):
  t = d%26
  last[t].append(d)
  score += S[d][t]
  schedule.append(t)

for i in range(26):
  last[i].append(D)
  for k in range(len(last[i])-1):
    g = last[i][k+1]-last[i][k]-1
    score -= C[i]*g*(g+1)//2


import random
while True:
  prev = score
  d = random.choice(range(D))
  c = (d + random.choice(range(1,26)))%D
  p = schedule[d]
  q = schedule[c]
  schedule[d] = q
  schedule[c] = p
  score -= S[d][p]
  score += S[d][q]
  score -= S[c][q]
  score += S[c][p]
  
  pos = last[p].index(d)
  score -= C[p]*(last[p][pos]-last[p][pos-1])*(last[p][pos+1]-last[p][pos])
  last[p].remove(d)
  
  pos = bisect.bisect(last[q],d)
  score += C[q]*(d-last[q][pos-1])*(last[q][pos]-d)
  bisect.insort(last[q],d)
  
  pos = last[q].index(c)
  score -= C[q]*(last[q][pos]-last[q][pos-1])*(last[q][pos+1]-last[q][pos])
  last[q].remove(c)
  
  pos = bisect.bisect(last[p],c)
  score += C[p]*(c-last[p][pos-1])*(last[p][pos]-c)
  bisect.insort(last[p],c)
  t = int(time.time() * 1000)-START
  if score<prev or random.random() < ((2000-t)/2000)**2*0.5:
    score = prev
    bisect.insort(last[p],d)
    last[q].remove(d)
    bisect.insort(last[q],c)
    last[p].remove(c)
    schedule[d] = p
    schedule[c] = q
  if t>1900:
    break
    
for i in schedule:
  print(i+1)


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

using namespace std;

int ispr(int x)
{
	if(x==1) return 0;
	for(int i=2;i*i<=x;i++)
		if(x%i==0)
			return 0;
	return 1;
}

int main()
{
	int x;
	cin >> x;
	while(!ispr(x)) x++;
	cout << x << endl;
}

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

using namespace std;

int main() {
	int ans = 0;
	string str;
	while (cin >> str) {
		int l = 0;
		int r = str.size()-1;
		bool flag = true;
		while (l <= r) {
			if (str[l] != str[r]) {
				flag = false;
				break;
			}
			l++;
			r--;
		}
		if (flag == true)
			ans++;
	}
	cout << ans << endl;
	return 0;
}

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

F = []
for _ in range(N):
    tmp = list(map(int, input().split()))
    v = 0
    for i in range(5 * 2):
        v += tmp[i] << i
    F.append(v)

P = []
for _ in range(N):
    p = list(map(int, input().split()))
    P.append(p)

ans = -1 * (10**12)
for bit in range(1, 1 << (5 * 2)):
    v = 0
    for i in range(len(F)):
        f = F[i]
        product = bit & f
        cnt = 0
        for j in range(5 * 2):
            if product & (1 << j):
                cnt += 1
        v += P[i][cnt]
    ans = max(ans, v)
print(ans)


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

// 参考：http://drken1215.hatenablog.com/entry/2019/09/16/014600

vector<int> Z(string S) {
  // L[i] SとS[i:]との一致数
    vector<int> L(S.size());
      L[0] = S.size();
      int i = 1, j = 0;
      while (i < S.size()) {
        while (i+j < S.size() && S[j] == S[i+j]) j++;
        L[i] = j;
        if (j == 0) {
           i++;
           continue;
         }
        int k = 1;
        while (i+k < S.size() && k+L[k] < j){
          L[i+k] =L[k];
          k++;
        }
        i += k; j -= k;
    }
    return L;
}

int main() {
    int N;
    string s;
    cin >> N >> s;
    int ans = 0;
    for (int i = 0; i < N; i++) {
        string t = s.substr(i);
        vector<int> l(t.size());
        l = Z(t);
        for (int j = 0; j < t.size(); j++) {
          // 文字列の被りが起こることがある
          // 例えばt = "orangeorangeor"
          // このとき l[0]=14 l[6]=8 l[12]=2 他0となるが
          // 答えは明らかに6("orange")
          // l[0]ではすべての文字が、l[6]では"orangeor"の"or"が被っている
          // ここでj文字目を境界に二つの文字列に分ける
          // j=0のとき　""と"orangeorangeor"
          // j=6のとき　"orange"と"orangeor"
          // j=12のとき "orangeorange"と"or"
          // このようにl[j]!=0のとき先頭からj文字をとるか
          // l[j]をそのままとるかの大きくない方を選ぶ
            int a = min(l[j], j);
            ans = max(ans, a);
        }
    }

    cout<<ans<<endl;
}


Measured memory_bytes: 384
#include<bits/stdc++.h>    
using namespace std;
#define ll long long 
#define pi pair<ll,ll>
#define pd pair<ld,ld>
#define pb push_back
#define inf 1e16
#define mod 998244353
#define en '\n'
#define ld long double
#define ff first
#define ss second
#define ull unsigned long long 
ll pw(ll b, ll r, ll md){
	ll ans = 1;
	while(r){
		if(r&1)ans = (ans*b)%md;
		b = (b*b)%md;
		r>>=1;
	}
	return ans;
}
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	ll n,k;cin>>k>>n;
	ll arr[n];for(int i = 0 ; i < n ; i ++ )cin>>arr[i];
	sort(arr,arr+n);
	ll mx = 0 ; 
	for(int i = 0 ; i < n-1 ; i ++ )mx = max(mx,arr[i+1]-arr[i]);
	mx = max(mx,k-arr[n-1]+arr[0]);
	cout<<k-mx<<en;
}

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

int main(){
    ll n;
    cin>>n;
    ll A[n+1][3];
    ll dp[n+1][3];

    for(ll i = 1; i <= n; i++){
        for(ll j = 0; j < 3; j++){
            cin>>A[i][j];
        }
    }
    dp[1][0] = A[1][0];
    dp[1][1] = A[1][1];
    dp[1][2] = A[1][2];
    for(ll i = 2; i <= n; i++){
        dp[i][0] = max(dp[i-1][1] + A[i][0],dp[i-1][2] + A[i][0]);
        dp[i][1] = max(dp[i-1][0] + A[i][1],dp[i-1][2] + A[i][1]);
        dp[i][2] = max(dp[i-1][1] + A[i][2],dp[i-1][0] + A[i][2]);
    }
    cout<<max(dp[n][0],max(dp[n][1],dp[n][2]));
    return 0;
}




//freopen("input.in","r",stdin);
//freopen("output.out","w",stdout);
//ios::sync_with_stdio(0);cin.tie(0);


Measured memory_bytes: 4992
#include <stdio.h>
#define N 100

int bubbleSort(int *a,int n);

main()
{
  int a[N],n,i,count;

  scanf("%d",&n);

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

  count = bubbleSort(a,n);
  printf("%d",a[0]);

  for(i=1; i<n; i++){
    printf(" %d",a[i]);
  }

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

  return 0;
}

int bubbleSort(int *a,int n)
{
  int i,j,temp,count=0;
  for(i=0; i<n; i++){
    for(j=n-1; j>i; j--){
      if(a[j]<a[j-1]){
	temp = a[j];
	a[j] = a[j-1];
	a[j-1] = temp;
	count++;
      }
    }
  }
  return count;
}

Measured memory_bytes: 600
#include<bits/stdc++.h>
#define pb push_back
#define x first
#define y second
using namespace std;
const int N = 1010, Mod = 1e9 + 7;
int n, m, A[N], B[N], C[N], cnt[N * N];
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++)
    {
        scanf("%d", &A[i]);
        cnt[A[i]] ++;
        if (cnt[A[i]] > 1)
            return !printf("0");
    }
    memset(cnt, 0, sizeof(cnt));
    for (int i = 1; i <= m; i++)
    {
        scanf("%d", &B[i]);
        cnt[B[i]] ++;
        if (cnt[B[i]] > 1)
            return !printf("0");
    }
    for (int i = 1; i <= n; i++)
        cnt[A[i]] ++;
    int tot = 1;
    for (int i = 1; i <= n; i++)
        if (cnt[A[i]] == 1)
        {
            int c = 0;
            for (int j = 1; j <= m; j++)
                if (B[j] > A[i])
                    c ++;
            tot = 1ll * tot * c % Mod;
        }
    for (int i = 1; i <= m; i++)
        if (cnt[B[i]] == 1)
        {
            int c = 0;
            for (int j = 1; j <= n; j++)
                if (A[j] > B[i])
                    c ++;
            tot = 1ll * tot * c % Mod;
        }
    sort(A + 1, A + n + 1);
    sort(B + 1, B + m + 1);
    int _cnt = 0, ccc = 0;;
    for (int i = n * m; i; i--)
        if (cnt[i] == 2)
            C[++ ccc] = i;
    sort(C + 1, C + ccc + 1);
    for (int i = n * m; i; i--)
        if (cnt[i] == 0)
        {
            int la = upper_bound(A + 1, A + n + 1, i) - A;
            int lb = upper_bound(B + 1, B + m + 1, i) - B;
            la = n - la + 1; lb = m - lb + 1;
            int kk = la + lb;
            kk -= ccc + 1 - (int)(upper_bound(C + 1, C + ccc + 1, i) - C);
            tot = 1ll * tot * (la * lb - _cnt - kk) % Mod;
            _cnt ++;
        }
    printf("%d\n", tot);
    return (0);
}

Measured memory_bytes: 4224
#include<iostream>
#include<algorithm>
using lint=int64_t;
using namespace std;
#include<vector>
template<typename T>
void LIS(vector<T>& src,vector<T>& dst)
{
	dst.clear();

	for(auto&& i:src)
	{
		auto itr=upper_bound(dst.begin(),dst.end(),i);
		if(itr==dst.end())
			dst.push_back(i);
		else
			*itr=i;
	}

	return;
}

int main()
{
	int N;
	vector<int> A;

	cin >> N;
	for(int i=0;i<N;i++)
	{
		int a;
		cin >> a;
		A.push_back(-a);
	}

	vector<int> lis;
	LIS(A,lis);

	cout << lis.size() << endl;
	return 0;
}


Measured memory_bytes: 1400
using System;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.IO;

namespace SolutionCS
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            int N = int.Parse(s[0]);
            int M = int.Parse(s[1]);

            int[] c = new int[M];

            for (int i = 0; i < N; i++)
            {
                s = Console.ReadLine().Split(' ');
                int K = int.Parse(s[0]);

                for (int j = 0; j < K; j++)
                {
                    int a = int.Parse(s[j + 1]);

                    c[a - 1]++;
                }
            }

            int ans = 0;
            for (int i = 0; i < M; i++)
            {
                if (c[i] == N)
                {
                    ans++;
                }
            }

            Console.WriteLine(ans);
        }
    }
}



Measured memory_bytes: 11092
N, M = [int(t) for t in input().split()]
R = []
for i in range(M):
    a, b = [int(t) for t in input().split()]
    R.append((a, b))

R.sort(key=lambda r: r[1])

last  = -1
count = 0
for a, b in R:
    if last < a:
        last = b - 1
        count += 1
print(count)

Measured memory_bytes: 18220
#define DEBUG
#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std; using __gnu_cxx::power;
using ll = long long; using ull = unsigned long long; using ld = long double;
const ll llinf = (1ll<<61)-1; const double eps = 1e-6, ldeps = 1e-9;
#define all(x) begin(x), end(x)
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
#define TCC template<class C
struct Dbg { static constexpr auto &os = cout;   // replace by cerr in interactive.
#define ENI typename enable_if<
#define DTOR , Dbg&>::type operator<<
#define RIS return *this
TCC> static auto o(C *x)->decltype(os << *x, 0);
TCC> static char o(...);
TCC> ENI sizeof o<C>(0)!=1 DTOR(const C &x) {os<<x; RIS;}
TCC> Dbg &dump(C b, C e) {
    int ii = 0;
    for(C i = (os << "\n[", b); i!=e and ii<26; i++, ii++) *this << ", "+2*(i==b) << *i;
    RIS << ']';
}
TCC> ENI sizeof o<C>(0)==1 DTOR(const C &x) {
    return dump(all(x));
}
TCC, size_t X> ENI !is_same<C, const char>::value DTOR(C (&x)[X]) {
    return dump(all(x));
}
TCC, class D> Dbg &operator<<(const pair<C, D> &x) {RIS<<'{'<<x.first<<", "<<x.second<<'}';}
Dbg &operator<<(ostream&(*x)(ostream&)) { os << x; RIS; }
} dbg;
void err(istringstream *iss) { delete iss; }
TCC, class... Args> void err(istringstream *iss, const C &val, const Args &... args) {
    string name; *iss >> name; if (name.back()==',') name.pop_back();
    dbg << name << " = " << val << "; ", err(iss, args...);
}
#ifdef DEBUG
#define bug(args...) dbg << __LINE__ << ": ", err(new istringstream(#args), args), dbg << endl
#else
#define bug(args...) 0
#endif
#define ASSERT(a, o, b, args...) if (!((a)o(b))) bug(a, b, ##args), assert((a)o(b));
int T = 1, Q, cn;
const int inf = 1000000007, mxn = -1;

signed main() { cin.tie(0)->sync_with_stdio(0); cin.exceptions(cin.failbit); cout.precision(11), cout.setf(ios::fixed);
    // cin >> T;
    auto inputAll = [&]()->void {
        
    };
    auto kase = [&]()->void {
        // cout << "Case " << cn << ": ";
        int x;
        cin>>x;
        cout<<(x>=30 ? "Yes":"No")<<'\n';
    };
    auto resetAll = [&]()->void {
        
    };
    while (cn++!=T) inputAll(), kase(), resetAll();
}


Measured memory_bytes: 3660
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

typedef long long ll;
typedef pair<int,int> ii;
typedef tree<ii,null_type,less<ii>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;

const int mod=1e9+7;

int main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	long long a=2,b=1;
	string s; cin>>s;
	for (int i=1; i<s.size(); i++) {
		b=(b+b+b)%mod;
		if(s[i]=='0')
			continue;
		b=(b+a)%mod;
		a=(a+a)%mod;
	}
	cout<<(a+b)%mod<<"\n";
	return 0;
}

Measured memory_bytes: 512
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define M 1000000007
//#define FILE_A_IN

void solve() {
    int n;
    cin >> n;
    unordered_set<string> us;
    for (int i = 0; i < n; ++i) {
        char c[15]={0};
        cin >> c;
        string s = c;
        us.insert(s);
    }
    cout << us.size() << endl;
}
int main() {
#ifdef FILE_A_IN
    freopen("a.in", "r", stdin);
#endif
    fio;
        solve();
    return 0;
}

Measured memory_bytes: 17768
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int MAX_N = 500005;
ll bit[MAX_N+5],N;

ll bit_sum(int i)
{
	ll res = 0;
	while(i > 0)
	{
		res += bit[i];
		i -= i & -i;
	}
	return res;
}

void bit_add(int i, ll x)
{
	while(i <= N)
	{
		//cout << "index:" << i << endl;
		bit[i] += x;
		i += i & -i;
	}
	return;
}

int main()
{
	int Q;
	cin >> N >> Q;
	rep(i,N)
	{
		ll a;
		cin >> a;
		bit_add(i+1,a);
	}
	/*
	rep(i,N)
	{
		cout << bit[i+1] << " ";
	}
	*/
	cout << endl;
	rep(i,Q)
	{
		int t;
		cin >> t;
		if (t == 0)
		{
			int p;
			ll x;
			cin >> p >> x;
			p++;
			bit_add(p,x);
			/*
			rep(i,N)
			{
				cout << bit[i+1] << " ";
			}
			cout << endl;
			*/
		}
		else
		{
			int l,r;
			cin >> l >> r;
			cout << bit_sum(r) - bit_sum(l) << endl;
		}
	}
	return 0;
}

Measured memory_bytes: 8216
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int a = sc.nextInt();
		int p = sc.nextInt();

		sc.close();

		p = a * 3 + p;

		System.out.print(p / 2);

	}

}


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

#pragma region Macros
using ll = long long;
#define int ll
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
template<class T = ll> using V = vector<T>;
template<class T = ll> using VV = V<V<T>>;
#define IOS\
    ios::sync_with_stdio(false);\
    cin.tie(0);\
    cout.tie(0);
#define FOR(i,l,r) for(ll i=(l);i<(r);++i)
#define REP(i,n) FOR(i,0,n)
#define REPS(i,n) FOR(i,1,n+1)
#define RFOR(i,l,r) for(ll i=(l);i>=(r);--i)
#define RREP(i,n) RFOR(i,n-1,0)
#define RREPS(i,n) RFOR(i,n,1)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define SORT(name) sort(name.begin(), name.end())
#define RSORT(name)\
    SORT(name);\
    reverse(all(name));
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
inline void Yes(bool b = true) {cout << (b ? "Yes" : "No") << '\n';}
inline void YES(bool b = true) {cout << (b ? "YES" : "NO") << '\n';}
template <class T> inline void print(T x){ cout << x << '\n';}
template<typename T1,typename T2> inline void chmin(T1 &a, T2 b){ if(a > b) a = b; }
template<typename T1,typename T2> inline void chmax(T1 &a, T2 b){ if(a < b) a = b; }

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const double DINF = std::numeric_limits<double>::infinity();
#pragma endregion

#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const int MOD = 1000000007;
const int MAX_N = 100010;

class UnionFind {
    public:
        UnionFind(){}
        UnionFind(int size) { Init(size); }
        // size: Union-Find で扱いたい要素の数
        void Init(int size) { m_Data.assign(size, -1); }

        // x と y の属する集合を併合
        // 既に一緒な集合内にいるなら false を
        // 新たに併合されたなら true を返す
        bool Unite(int x, int y) {
            x = Find(x), y = Find(y);
            if(x == y) { return (false); }
            if(m_Data[x] > m_Data[y]) { swap(x, y); }
            m_Data[x] += m_Data[y];
            m_Data[y] = x;
            return (true);
        }

        // 要素 k が属する集合の根を返す
        int Find(int k) {
            if(m_Data[k] < 0) { return (k); }
            return (m_Data[k] = Find(m_Data[k]));
        }

        // 要素 k と同じ集合の要素数を返す
        int Size(int k) { return (-m_Data[Find(k)]); }

        // 要素のうち独立しているグループの数 O(NlogN)
        int GetGroupNum() {
            map<int, bool> groups;
            for(int i = 0; i < m_Data.size(); ++i) { groups[Find(i)] = true; }
            return static_cast<int>(groups.size());
        }

    private:
        vector<int> m_Data;
};

signed main() {
    IOS;

    int N, Q;
    cin >> N >> Q;

    UnionFind uf(N);

    REP(i, Q) {
        int t, u, v;
        cin >> t >> u >> v;
        if(t == 0) {
            uf.Unite(u, v);
        }
        else {
            if(uf.Find(u) == uf.Find(v)) {
                print(1);
            }
            else {
                print(0);
            }
        }
    }

    return 0;
}


Measured memory_bytes: 4696
#!usr/bin/env python3
import sys
import math
import string
import collections
import fractions
import random
from operator import itemgetter
import itertools
from collections import deque
import copy
import heapq
import bisect

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

sys.setrecursionlimit(10 ** 8)

List = [str(i) for i in range(0, 10)]
prev_num = 10
K = int(input())
if K <= 9:
    print(List[K])
else:
    while True:
        List2 = []
        for i in List:
            if i != "0":
                List2.append(i + str(i[-1]))
                if i[-1] != "9":
                    List2.append(i + str(int(i[-1]) + 1))
                if i[-1] != "0":
                    List2.append(i + str(int(i[-1]) - 1))
        List = List2
        if prev_num + len(List)-1 >= K:
            List = list(map(int, List))
            List.sort()
            print(List[K - prev_num])
            sys.exit()
        prev_num += len(List)


Measured memory_bytes: 20324
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;

public class Main {
   public static void main (String[] args) {
      Scanner input = new Scanner(System.in);
      ArrayList<Integer> t = new ArrayList<Integer>();
      for (int i=0; i<10; i++) t.add(input.nextInt());
      Collections.sort(t);
      for (int i=0; i<3; i++) System.out.println(t.get(9-i));
}
}

Measured memory_bytes: 17176
import itertools
N = int(input())
L = list(map(int, input().split()))

cnt = 0
for i in range(N-2):
    a = L[i]
    for j in range(i+1, N-1):
        b = L[j]
        for k in range(j+1, N):
            c = L[k]
            if a != b and b != c and c != a:
                m = [a, b, c]
                o = max(m)
                m.remove(o)
                if o < sum(m):
                    cnt += 1
#cnt = list(map(list, set(map(tuple, cnt))))
print(cnt)

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

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

ll f(ll x, int c, int d){
  ll res = x; 
  res -= x/c;
  res -= x/d;
  res += x/lcm(c,d);
  return res;
}

int main() {
  ll a,b;
  int c,d;
  cin >> a >> b >> c >> d;
  
  ll ans = f(b,c,d) - f(a-1,c,d);
  cout << ans << endl;
  
  return 0;
  
}


Measured memory_bytes: 256
// 2011/11/14 Tazoe

#include <iostream>
using namespace std;

int main()
{
	int W[10];
	for(int i=0; i<10; i++)
		cin >> W[i];

	int K[10];
	for(int i=0; i<10; i++)
		cin >> K[i];

	for(int i=0; i<9; i++)
		for(int j=i+1; j<10; j++)
			if(W[i]<W[j]){
				int tmp = W[i];
				W[i] = W[j];
				W[j] = tmp;
			}

	for(int i=0; i<9; i++)
		for(int j=i+1; j<10; j++)
			if(K[i]<K[j]){
				int tmp = K[i];
				K[i] = K[j];
				K[j] = tmp;
			}

	cout << W[0]+W[1]+W[2] << ' ' << K[0]+K[1]+K[2] << endl;

	return 0;
}

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

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

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

int N;
long long W, weight[110], value[110];

long long dp[110][100100] = {0};


void solve(){
    for(int i = 0; i < N; i++){
        for(int sum_w = 0; sum_w <= W; sum_w++){

            if(sum_w - weight[i] >= 0){
                chmax(dp[i+1][sum_w], dp[i][sum_w-weight[i]] + value[i]);
            }

            chmax(dp[i+1][sum_w], dp[i][sum_w]);

        }
    }
    cout << dp[N][W] << endl;
}

// Generated by 1.1.3 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    cin >> N >> W;
    for(int i = 0; i < N; ++i) cin >> weight[i] >> value[i];
    solve();
    return 0;
}


Measured memory_bytes: 79360
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

a,b,c = inpl()
x = [-INF] + inpln(a) + [INF]
y = [-INF] + inpln(b) + [INF]
z = inpln(c)

def f(x,y,q):
    res = INF
    qq = []
    cs = bisect.bisect_left(x,q)
    qq.append((x[cs],abs(q - x[cs])))
    qq.append((x[cs-1],abs(q - x[cs-1])))
    while qq:
        now, dist = qq.pop()
        cs = bisect.bisect_left(y,now)
        res = min(res,dist+abs(now - y[cs]))
        res = min(res,dist+abs(now - y[cs-1]))
    return res

for q in z:
    print(min(f(x,y,q),f(y,x,q)))



Measured memory_bytes: 20312
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int a[202020];
int main()
{
	int n;
	long long ans=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;i++)
		if(a[i]>i-1)
		{
			printf("-1");
			return 0;
		}
	a[n+1]=0;
	for(int i=n;i>=1;i--)
		if(a[i+1]&&a[i]<a[i+1]-1)
		{
			printf("-1");
			return 0;
		}
		else if(a[i]!=a[i+1]-1)
			ans+=a[i];
	printf("%lld",ans);
	return 0;
}

Measured memory_bytes: 1024
def gcd_core(a, b):
    if b == 0:
        return a
    else:
        return gcd_core(b, a % b)


def gcd(arr):
    g = gcd_core(arr[0], arr[1])
    for i in range(2, len(arr)):
        g = gcd_core(g, arr[i])
    return g

n = int(input())
arr = list(map(int,input().split()))
bef = arr[0]
cst = arr[0]
for i in range(1,n):
    if bef < arr[i]:
        cst += arr[i]-bef
    bef = arr[i]

print(cst)







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

#define rep(i,m,n) for(int (i)=(int)(m);i<(int)(n);++i)
#define rep2(i,m,n) for(int (i)=(int)(n)-1;i>=(int)(m);--i)
#define REP(i,n) rep(i,0,n)
#define REP2(i,n) rep2(i,0,n)
#define FOR(i,c) for(decltype((c).begin())i=(c).begin();i!=(c).end();++i)
#define all(hoge) (hoge).begin(),(hoge).end()
#define en '\n'
using ll = long long;
using ull = unsigned long long;
template <class T> using vec = vector<T>;
template <class T> using vvec = vector<vec<T>>;
typedef pair<ll, ll> P;
constexpr long long INF = 1LL << 60;
constexpr int INF_INT = 1 << 25;
constexpr long long MOD = (ll) 1e9 + 7;
//constexpr long long MOD = 998244353LL;
typedef vector<ll> Array;
typedef vector<Array> Matrix;


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

struct Edge {
	ll to, cap, rev;
	Edge(ll _to, ll _cap, ll _rev) {
	to = _to; cap = _cap; rev = _rev;
	}
};
using Edges = vector<Edge>;
using Graph = vector<Edges>;

void add_edge(Graph& G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
	G[from].push_back(Edge(to, cap, (ll)G[to].size()));
	if (revFlag)G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

ll n,m;
ll a[1010],b[1010],c[1010],d[1010],e[1010],f[1010];

bool line[6060][6060];
bool dp[6060][6060];

void solve(){
	cin>>n>>m;

	map<ll,ll> mpx,mpy;
	mpx[0]++;
	mpy[0]++;
	REP(i,n){
		cin>>a[i]>>b[i]>>c[i];
		mpx[a[i]]++;
		mpx[b[i]]++;
		mpy[c[i]]++;
	}
	REP(i,m){
		cin>>d[i]>>e[i]>>f[i];
		mpx[d[i]]++;
		mpy[e[i]]++;
		mpy[f[i]]++;
	}

	Array xs,ys;
	for(auto& i:mpx){
		i.second=xs.size();
		xs.push_back(i.first);
	}
	for(auto& i:mpy){
		i.second=ys.size();
		ys.push_back(i.first);
	}

	REP(i,n){
		ll aa=mpx[a[i]];
		ll bb=mpx[b[i]];
		ll cc=mpy[c[i]];
		rep(x,aa*2,bb*2+1) line[x][cc*2]=true;
	}
	REP(i,m){
		ll dd=mpx[d[i]];
		ll ee=mpy[e[i]];
		ll ff=mpy[f[i]];
		rep(y,ee*2,ff*2+1) line[dd*2][y]=true;
	}

	

	ll dx[4]={0,0,1,-1};
	ll dy[4]={1,-1,0,0};
	
	ll mx=xs.size()*2;
	ll my=ys.size()*2;

	ll ans=0;

	queue<pair<int,int>> que;
	que.push({mpx[0]*2,mpy[0]*2});
	dp[mpx[0]*2][mpy[0]*2]=true;

	while(que.size()){
		auto [x,y] = que.front();
		que.pop();
		if(x==0 || x==mx-1 || y==0 || y==my-1){
			cout<<"INF"<<en;
			return;
		}
		if(x%2==1 && y%2==1){
			ans += (xs[x/2+1]-xs[x/2])*(ys[y/2+1]-ys[y/2]);
		}

		REP(v,4){
			ll nx=x+dx[v];
			ll ny=y+dy[v];
			if(nx<0 || nx>=mx || ny<0 || ny>=my) continue;
			if(line[nx][ny]) continue;
			if(dp[nx][ny]) continue;
			dp[nx][ny]=true;
			que.push({nx,ny});
		}
	}
	cout<<ans<<en;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
	
	solve();
    //ll t;cin>>t;REP(i,t) solve();

    return 0;
}

Measured memory_bytes: 73612
#include <stdio.h>

int A[100005];

int min(int a, int b){ return a < b ? a : b; }

int main(){
    int N;
    scanf("%d", &N);
    for(int i=1; i<=N; i++) scanf("%d", &A[i]);
    long long res=0;
    for(int i=1; i<=N; i++){
        res += A[i]/2;
        A[i] -= A[i]/2 * 2;
        res += min(A[i], A[i+1]);
        A[i+1] -= min(A[i], A[i+1]);
    }
    printf("%lld", res);
    return 0;
}

Measured memory_bytes: 512
#include<stdio.h>

int main(){
  int sx = 0;
  int sy = 0;
  int tx = 0;
  int ty = 0;
  int i = 0;
  scanf("%d %d %d %d",&sx,&sy,&tx,&ty);
  for(i=0;i<ty-sy;i++){
    printf("U");
  }
  for(i=0;i<tx-sx;i++){
    printf("R");
  }
  for(i=0;i<ty-sy;i++){
    printf("D");
  }
  for(i=0;i<tx-sx;i++){
    printf("L");
  }
  printf("L");
  for(i=0;i<ty-sy+1;i++){
    printf("U");
  }
  for(i=0;i<tx-sx+1;i++){
    printf("R");
  }
  printf("D");
  printf("R");
  for(i=0;i<ty-sy+1;i++){
    printf("D");
  }
  for(i=0;i<tx-sx+1;i++){
    printf("L");
  }
  printf("U");
  return 0;
}
  
  
  
  
  

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define ll long long
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    ll x,n,q,sum=0,a,b;
    int arr[100005]={0};
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>x;
        sum += x;
        arr[x]++;
    }
 
    cin>>q;
    while(q--){
        cin>>a>>b;
        sum -= (a*arr[a]);
        sum -= (b*arr[b]);
        int temp = arr[a];
        arr[a] = 0;
        arr[b] += temp;
        sum += (b*arr[b]);
 
        cout << sum << "\n";
    }
    
    return 0;
}

Measured memory_bytes: 3976
#include <stdio.h>
#include <time.h>
void BubbleSort(int *p,int n);
int main ()
{
    int number,*p,i;
    scanf("%d",&number);
    p = (int *)malloc(number*sizeof(int));
    for(i = 0; i < number ; i++)
    {
        scanf("%d",&p[i]);
    }
    BubbleSort(p,number);
    free(p);
    return 0;
}
void BubbleSort(int *p,int n)
{
    int i,j,temp,count=0;
    for(i = 0;i < n - 1;i++)
    {
        for(j = n-1;j > i;j--)
        {
            if(p[j]<p[j-1])
            {
                temp = p[j];
                p[j] = p[j-1];
                p[j-1] = temp;
                count++;
            }
        }
    }
    for(i = 0;i < n;i++)
    {
        if(i > 0)printf(" ");
        printf("%d",*(p+i));
    }
    printf("\n%d\n",count);
}



Measured memory_bytes: 2120
import sys
import math
#a,b= map(int,input().split())
a=int(input())
li0=[]
li1=[]
for d in range(a):
  li0.append(input())
b=int(input())
for f in range(b):
  li1.append(input())
jug=0

for i in li0:
  count=0
  for j in li0:
    if i==j:
      count+=1
  for k in li1:
    if i==k:
      count-=1
  jug=max(jug,count)
#a, b, c = [list(map(int, input().split())) for _ in range(3)]
#li0= [int(x) for x in input().split()]
print(jug)

Measured memory_bytes: 3064
#include<iostream>
#include <algorithm>
#include <string>
#include <queue>
using namespace std;
int N_MAX = 1000;
int INF = 10000;
typedef pair<int,int> P;
int main()
{
  int h,w,i,j,ni,nj,res=0;
  string s,t;
  int d[N_MAX][N_MAX];
  int di[4]={1,0,-1,0}, dj[4]={0,1,0,-1};
  P p;
  cin >> h >> w;
  queue<P> que;
  for(i=0;i<h;i++){
    cin >> s;
    for(j=0;j<w;j++){
      t=s.substr(j,1);
      if(t==".")
        d[i][j]=INF;
      else{
        d[i][j]=0;
        que.push(P(i,j));
      }
    }
  }
  while(que.size()){
    p=que.front();
    que.pop();
    for(i=0;i<4;i++){
      ni=p.first+di[i];
      nj=p.second+dj[i];
      if(ni>=0 && ni<h && nj>=0 && nj<w && d[ni][nj]==INF){
        d[ni][nj]=res=d[p.first][p.second]+1;
        que.push(P(ni,nj));
      }
    }
  }
  cout << res << endl;
  return 0;
}


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

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef complex<double> point;

const ll N = 3005;
const ll INF = 1e9;
const int MOD = 1e9 + 7;
const double EPS = 1e-7;
const double PI = acos(-1.0);

string s, t;
ll n, m, dp[N][N];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(NULL), cout.tie(NULL);
	cin >> s >> t;

	n = s.size();
	m = t.size();

	for(int i = 1; i <= n; i++) {
		for(int j = 1; j <= m; j++) { 
			if(s[i - 1] == t[j - 1])
				dp[i][j] = dp[i - 1][j - 1] + 1;
			else
				dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
		}
	}

	stack<char> ans;
	while(n && m) {
		if(s[n - 1] == t[m - 1]) {
			ans.push(s[n - 1]);
			n--;
			m--;
		}
		else if(dp[n - 1][m] > dp[n][m - 1])
			n--;
		else
			m--;
	}
	while(!ans.empty()) {
		cout << ans.top();
		ans.pop();
	}
}


Measured memory_bytes: 70656
#include <bits/stdc++.h>
#define rep(i,n) for (long long i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll,ll>;
const ll MOD = 1000000007;
const ll INF = 1e18;
#define all(v) v.begin(), v.end()

int main(){
  string s,t;
  cin >> s >> t;
  vector<vector<ll>> A(26);
  rep(i,s.size()){
    A.at(s.at(i)-'a').push_back(i);
  }
  ll ans=0,k=0;
  rep(i,t.size()){
    if(A.at(t.at(i)-'a').size()==0){
      cout << -1 << endl;
      return 0;
    }
    if(lower_bound(all(A.at(t.at(i)-'a')),k)!=A.at(t.at(i)-'a').end()){
      k=*lower_bound(all(A.at(t.at(i)-'a')),k);
      k++;
      if(k==s.size()){
        k=0;
        ans++;
      }
    }else{
      ans++;
      k=*lower_bound(all(A.at(t.at(i)-'a')),0);
      k++;
    }
  }
  cout << ans*s.size()+k << endl;
}

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

#define rep(i, N) for (ll i = 0; i < N; i++)
#define rep1(i, N) for (ll i = 1; i <= N; i++)
#define repr(i, N) for (ll i = N-1; i >= 0; i--)
#define repr1(i, N) for (ll i = N; i > 0; i--)

#define INF 1000000007

using ll = long long;
using namespace std;

typedef pair<int,int> P;

int main(){
    string s[3];
    rep(i,3) cin >> s[i];
    int turn = 0;
    char ans;

    while(!s[turn].empty()){
        int nextturn = (s[turn][0] -'a');
        s[turn] = s[turn].substr(1);
        turn = nextturn;
    }

    ans = ('A'+turn);
    
    cout << ans;
    //printf("%.15lf",ans);
    cout << endl;
    return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
#define N 100



int main(){
  char stack[N];
  int STACK[N];
  int cal1=0,cal2=0,top=0;
  
  while(1){
    if((scanf("%s",stack)) ==EOF) break;
    if(stack[0]==' ') continue;
    else{
      if('0'<=stack[0] && stack[0]<='9'){
	STACK[top++]=atoi(stack);
      }
      
      else if(stack[0]=='+'){
	cal1=STACK[top-2];
	cal2=STACK[top-1];
	top-=2;
	STACK[top++]=cal1+cal2;
      }
      
      else if(stack[0]=='-'){
	cal1=STACK[top-2];
	cal2=STACK[top-1];
	top-=2;
	STACK[top++]=cal1-cal2;
      }
      
      else if(stack[0]=='*'){
	cal1=STACK[top-2];
	cal2=STACK[top-1];
	top-=2;
	STACK[top++]=cal1*cal2;
      }
    }
    
  }
  
  printf("%d\n",STACK[top-1]);
  
  return 0;
}

  



Measured memory_bytes: 2068