akhauriyash/Code-Regression huggingface dataset:
import collections
n,m,k = map(int, raw_input().split(' '))
ais = map(int,raw_input().split(' '))
bis = map(int,raw_input().split(' '))
cumul = 0
pais = [0]
for ai in ais:
	cumul +=ai
	pais.append(cumul)

cumul = 0
pbis = [0]
for ai in bis:
	cumul +=ai
	pbis.append(cumul)
	


q = 0
for j,bi in enumerate(pbis):

	if k >= bi:
		j
		qq = k - bi
		lo,hi = 0, len(pais)-1
		while(lo < hi):
			med = (lo + hi + 1) >> 1
			if pais[med] <= qq:
				lo = med
			else:
				hi = med -1
		
		ans = j + lo
		q = max(ans, q)
print q




Measured memory_bytes: 122660
n, A, B = map(int, input().split())

def ok(a):
    s = 0
    while a > 0:
        s += a % 10
        a //= 10
    return (A <= s and s <= B)

s = 0
for i in range(1, n+1):
    if ok(i):
        s += i
print(s)

Measured memory_bytes: 2940
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 main() {
    use std::collections::HashSet;
    let input = read_stdin();
    let tokens = &mut input.split_whitespace();
    let n: usize = scan(tokens);
    let mut set: HashSet<usize> = HashSet::with_capacity(n);
    for _ in 0..n {
        let a: usize = scan(tokens);
        if !set.insert(a) {
            println!("NO");
            return;
        }
    }
    println!("YES");
}


Measured memory_bytes: 10492
/*
Code by b21
Time: 19:03 Sun 01/03/2020
*/

#include <bits/stdc++.h>

using namespace std;

const int mn = 1 * (int)(1e5) + 10;
const int mod = 1 * (int)(1e9) + 7;
const int mm = 1 * (int)(1e3) + 10;
const int base = 1 * (int)(1e9);
const bool aNs = 0;

int tt, ntest = 1;
void docfile()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    if (ifstream("test.inp"))
    {
       freopen("test.inp", "r", stdin);
       if (!aNs) freopen("test.out", "w", stdout);
       else freopen ("test.ans", "w", stdout);
    }
   else if (ifstream("abc157_b.inp"))
    {
        freopen("abc157_b.inp", "r", stdin);
        freopen("abc157_b.out", "w", stdout);
    }
}

template <typename T>
void read(T& x)
{
    x = 0; T f = 1;
    char ch = getchar();
    while (!isdigit(ch)) f = ch == '-' ? - f : f, ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
    x *= f;
}

template <typename T>
void write (T a)
{
    if (a < 0)
    {
        putchar ('-');
        write (-a);
        return;
    }
    if (a < 10)
    {
        putchar ('0' + a);
        return;
    }
    write (a / 10);
    putchar ((char)('0' + (a % 10)));
}

int a[5][5], ok[5][5];

void enter()
{
    for (int i = 0; i < 3; ++ i)
    for (int j = 0; j < 3; ++ j)
    cin >> a[i][j];
    int n;
    cin >> n;
    for (int i = 0; i < n; ++ i) {
        int x;
        cin >> x;
        for (int i = 0; i < 3; ++ i)
        for (int j = 0; j < 3; ++ j)
        if (a[i][j] == x)
        ok[i][j] = 1;
    }
    int sol = 0;
    for (int i = 0; i < 3; ++ i)
    {
        int s = 0;
        for (int j = 0; j < 3; ++ j)
        s += ok[i][j];
        if (s == 3) sol = 1;
    }
    for (int i = 0; i < 3; ++ i)
    {
        int s = 0;
        for (int j = 0; j < 3; ++ j)
        s += ok[j][i];
        if (s == 3) sol = 1;
    }
    if (ok[0][0] && ok[1][1] && ok[2][2]) sol = 1;
    if (ok[0][2] && ok[1][1] && ok[2][0]) sol = 1;
    cout << (sol ? "Yes" : "No");
}

void solve()
{

}

void print_result()
{

}

main()
{
    docfile();
    //cin>>ntest;
    for (tt = 1; tt <= ntest; ++ tt)
    {
        enter();
        solve();
        print_result();
    }
}

Measured memory_bytes: 256
(defmacro while (test &body body)
  `(do ()
       ((not ,test))
     ,@body))

(defun digit-end (n)
  (rem n 10))
 
(defun digit-first (n)
  (while (>= n 10)
    (setf n (floor n 10)))
  n)
 
 
(defun  handstand2 (n)
  (let ((ans 0)
        (c (make-array '(10 10))))
    (dotimes (i n)
      (incf (aref c (digit-first (1+ i)) (digit-end (1+ i)))))
    (dotimes (i 10)
      (dotimes (j 10)
        (setf ans (+ ans (* (aref c i j) (aref c j i))))))
    ans))
(format t "~D~%" (handstand2 (read))) 

Measured memory_bytes: 19300
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 1000
#define C 100

int top,S[N];

void push(int x) {
  S[++top] = x;
}

int pop() {

  top--;

  return S[top+1];
} 

int main(void) {

  int a,b;
  top = 0;
  char c[C];

  while(scanf("%s",c) != EOF) {
    if(c[0] == '+') {
      a = pop();
      b = pop();
      push(a+b);
    }
    else if(c[0] == '-') {
      b = pop();
      a = pop();
      push(a-b);
    }
    else if(c[0] == '*') {
      a = pop();
      b = pop();
      push(a*b);
    }
    else {
      push(atoi(c));
    }
  }
  printf("%d\n",pop());

  return 0;
}

Measured memory_bytes: 548
n, lw = gets.split.map(&:to_i)
h = Hash.new { |bh, k| bh[k] = [] }

n.times do
  w, v = gets.split.map(&:to_i)
  h[w] << v
end

w = h.keys
(4 - w.size).times { w << 0 }

cum = Array.new(4) { [0] }
h.each_value.with_index do |v, idx|
  v.sort.reverse_each { |e| cum[idx] << cum[idx][-1] + e }
end

ans = 0

cum[0].size.times do |i|
  cum[1].size.times do |j|
    cum[2].size.times do |k|
      cum[3].size.times do |l|
        next if w[0] * i + w[1] * j + w[2] * k + w[3] * l > lw

        ans = [ans, cum[0][i] + cum[1][j] + cum[2][k] + cum[3][l]].max
      end
    end
  end
end

puts ans


Measured memory_bytes: 14376
import java.util._

object Main {
  def main(args:Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    val a = sc.nextInt
    val b = sc.nextInt
    val c = sc.nextInt
    val list = Array(a, b, c)
    val max = list.max
    var ans = max * 9
    for(v <- list) {
      ans += v
    }
    println(ans)
  }
}

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

using pii = pair<int, int>;
using vi = vector<int>;
using ll = long long;

int main() {
    int x = 0, y = 0, z = 0;
    int a, b, c;
    while(cin >> a >> b >> c) {
        int a1 = min({a, b, c}), a3 = max({a, b, c}), a2 = a + b + c - a1 - a3;
        if(a1 + a2 <= a3) {
            break;
        }
        if(a1 * a1 + a2 * a2 == a3 * a3) {
            x += 1;
        } else if(a1 * a1 + a2 * a2 > a3 * a3) {
            y += 1;
        } else {
            z += 1;
        }
    }
    cout << x + y + z << ' ' << x << ' ' << y << ' ' << z << endl;
}


Measured memory_bytes: 3068
#include <iostream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <utility>
using namespace std;
using ll = long long;
 
int main() {
	int a,b;
	cin >> a >> b;
	if (a <= b) cout << a << endl;
	else cout << a-1 << endl;
	return 0;
}


Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=n-1;i>=0;i--)
#define vi vector<int>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define inf (1ll<<60)
#define Yes(f) cout<<(f?"Yes":"No")<<endl
#define yes(f) cout<<(f?"yes":"no")<<endl
#define YES(f) cout<<(f?"YES":"NO")<<endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod=1e9+7;

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

void setup(string s,string &t,int n){
    Rep(i,2,n){
        if(s[i-1]=='o'){
            if(t[i-1]=='S')t+=t[i-2];
            else t+=(t[i-2]=='S'?'W':'S');
        }
        else{
            if(t[i-1]=='S')t+=(t[i-2]=='S'?'W':'S');
            else t+=t[i-2];
        }
    }
}
void run(){
    int n;
    string s;
    cin>>n>>s;
    string tmp="SS";
    setup(s,tmp,n);
    if(s[0]=='o'&&tmp[n-1]=='S'&&(s[n-1]=='o'&&tmp[n-2]==tmp[0]||s[n-1]=='x'&&tmp[n-2]!=tmp[0])
        ||s[0]=='x'&&tmp[n-1]=='W'&&(s[n-1]=='o'&&tmp[n-2]!=tmp[0]||s[n-1]=='x'&&tmp[n-2]==tmp[0])){
        cout<<tmp<<endl;
        return;
    }
    tmp="SW";
    setup(s,tmp,n);
    if(s[0]=='o'&&tmp[n-1]=='W'&&(s[n-1]=='o'&&tmp[n-2]!=tmp[0]||s[n-1]=='x'&&tmp[n-2]==tmp[0])
        ||s[0]=='x'&&tmp[n-1]=='S'&&(s[n-1]=='o'&&tmp[n-2]==tmp[0]||s[n-1]=='x'&&tmp[n-2]!=tmp[0])){
        cout<<tmp<<endl;
        return;
    }
    tmp="WW";
    setup(s,tmp,n);
    if(s[0]=='o'&&tmp[n-1]=='S'&&(s[n-1]=='o'&&tmp[n-2]==tmp[0]||s[n-1]=='x'&&tmp[n-2]!=tmp[0])
        ||s[0]=='x'&&tmp[n-1]=='W'&&(s[n-1]=='o'&&tmp[n-2]!=tmp[0]||s[n-1]=='x'&&tmp[n-2]==tmp[0])){
        cout<<tmp<<endl;
        return;
    }
    tmp="WS";
    setup(s,tmp,n);
    if(s[0]=='o'&&tmp[n-1]=='W'&&(s[n-1]=='o'&&tmp[n-2]!=tmp[0]||s[n-1]=='x'&&tmp[n-2]==tmp[0])
        ||s[0]=='x'&&tmp[n-1]=='S'&&(s[n-1]=='o'&&tmp[n-2]==tmp[0]||s[n-1]=='x'&&tmp[n-2]!=tmp[0])){
        cout<<tmp<<endl;
        return;
    }
    cout<<-1<<endl;
}

Measured memory_bytes: 768
from queue import Queue
# from queue import LifoQueue as Stack


N,q = list(map(int,input().split()))

Q=Queue()
for i in range(N):
    Q.put(list(map(str,input().split())))


ans=[]

time=0
while len(ans)!=N:
    p,t=Q.get()
    t=int(t)
    if t-q>0:
        Q.put([p,str(t-q)])
        time+=q
    else:
        time+=t
        ans.append((p,time))

for p,t in ans:
    print(p,t)

    






Measured memory_bytes: 16216
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef pair<int,int> Pi;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define fi first
#define se second
#define endl "\n"

template<typename T> inline bool chmax(T &a, T b){if(a<b){a=b;return true;}return false;}
template<typename T> inline bool chmin(T &a, T b){if(a>b){a=b;return true;}return false;}
template<typename T> ostream& operator<<(ostream& s,const complex<T>& d) {return s<<"("<<d.real()<<", "<<d.imag()<< ")";}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const pair<T1,T2>& d) {return s<<"("<<d.first<<", "<<d.second<<")";}
template<typename T> ostream& operator<<(ostream& s, const vector<T>& d){
	int len=d.size();
	rep(i,len){
		s<<d[i];if(i<len-1) s<<" ";
	}
	return s;
}
template<typename T> ostream& operator<<(ostream& s,const vector<vector<T>>& d){
	int len=d.size();
	rep(i,len){
		s<<d[i]<<endl;
	}
	return s;
}
template<typename T> ostream& operator<<(ostream& s,const set<T>& v){
	s<<"{ ";
	for(auto itr=v.begin();itr!=v.end();++itr) {
		if (itr!=v.begin()) {s<< ", ";}
		s<<(*itr);
	}
	s<<" }";
	return s;
}
template<typename T1, typename T2> ostream& operator<<(ostream& s,const map<T1,T2>& m){
	s<<"{"<<endl;
	for(auto itr=m.begin();itr!=m.end();++itr){
		s<<" "<<(*itr).first<<" : "<<(*itr).second<<endl;
	}
	s<<"}"<<endl;
	return s;
}

const ll mod=1e9+7;
const ll inf=1e17;
const int INF=1e9;
const double PI=acos(-1);
const double EPS=1e-10;

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	char a,b;
	cin>>a>>b;
	int A=(int)a-'0',B=(int)b-'0';
	string s,t;
	rep(i,B) s+=a;
	rep(i,A) t+=b;
	cout<<min(s,t)<<endl;
}

Measured memory_bytes: 256
#include<stdio.h>
int main(){
  int n,i=-2,s=0,a[5],b[5],d[5],c[]={10,50,100,500};
  while(scanf("%d",&n),n){
    if(i+2)printf("\n");
    for(i=0;i<4;i++){
      scanf("%d",&a[i]);
      s+=a[i]*c[i];
    }
    s-=n;
    while(i--){
      b[i]=s/c[i];
      s-=b[i]*c[i];
    }
    for(i=0;i<4;i++){
      if(a[i]>b[i])printf("%d %d\n",c[i],a[i]-b[i]);
    }
  }
  return 0;
}
  

Measured memory_bytes: 604
while True:
 a,op,b = input().split()
 a = int(a)
 b = int(b)
 if op == "?":
  break
 elif op == "+":
  print(a+b)
 elif op == "-":
  print(a-b)
 elif op == "*":
  print(a*b)
 else:
  print(a//b)


Measured memory_bytes: 5596
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=1005;
char s[maxn],t[maxn];
int main()
{
	scanf("%s %s",s,t);
	int n=strlen(s),m=strlen(t);
	int ans=maxn;
	for(int i=0;i+m<n+1;i++){
		int cnt=0;
		for(int j=0;j<m;j++){
			if(s[i+j]!=t[j]) cnt++;
		}
		ans=min(ans,cnt);
	}
	printf("%d",ans);
}

Measured memory_bytes: 3240
#define PROBLEM "http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0558"

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

#define call_from_test
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
struct FastIO{
  FastIO(){
    cin.tie(0);
    ios::sync_with_stdio(0);
  }
}fastio_beet;
//END CUT HERE
#ifndef call_from_test
signed main(){
  return 0;
}
#endif

#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
vector< vector<int> >
bfs(vector<string> &st,int sy,int sx,char wall,int dir){
  int h=st.size(),w=st.front().size();
  vector<vector<int> > dp(h,vector<int>(w,-1));
  using P = pair<int, int>;
  queue<P> qu;

  dp[sy][sx]=0;
  qu.emplace(sy,sx);

  int dy[]={1,-1,0,0,1,1,-1,-1};
  int dx[]={0,0,1,-1,1,-1,1,-1};
  auto in=[&](int y,int x){return 0<=y&&y<h&&0<=x&&x<w;};

  while(!qu.empty()){
    int y,x;
    tie(y,x)=qu.front();qu.pop();
    for(int k=0;k<dir;k++){
      int ny=y+dy[k],nx=x+dx[k];
      if(!in(ny,nx)||st[ny][nx]==wall) continue;
      if(~dp[ny][nx]) continue;
      dp[ny][nx]=dp[y][x]+1;
      qu.emplace(ny,nx);
    }
  }

  return dp;
}
//END CUT HERE
#ifndef call_from_test
//INSERT ABOVE HERE
signed ABC088_D(){
  int h,w;
  cin>>h>>w;
  vector<string> s(h);
  for(int i=0;i<h;i++) cin>>s[i];
  auto dp=bfs(s,0,0,'#',4);

  if(dp[h-1][w-1]<0){
    cout<<-1<<endl;
    return 0;
  }

  int cnt=0;
  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
      cnt+=s[i][j]=='.';
  cout<<cnt-(dp[h-1][w-1]+1)<<endl;
  return 0;
}
/*
  verified on 2019/12/10
  https://atcoder.jp/contests/abc088/tasks/abc088_d
*/

signed main(){
  ABC088_D();
  return 0;
}
#endif

#undef call_from_test

signed main(){
  int h,w,n;
  cin>>h>>w>>n;

  vector<string> st(h);
  for(int i=0;i<h;i++) cin>>st[i];

  for(int i=0;i<h;i++)
    for(int j=0;j<w;j++)
      if(st[i][j]=='S') st[i][j]='0';

  vector<int> ys(n+1),xs(n+1);
  for(int i=0;i<h;i++){
    for(int j=0;j<w;j++){
      if(!isdigit(st[i][j])) continue;
      int p=st[i][j]-'0';
      ys[p]=i;xs[p]=j;
    }
  }

  int ans=0;
  for(int k=0;k<n;k++)
    ans+=bfs(st,ys[k],xs[k],'X',4)[ys[k+1]][xs[k+1]];

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



Measured memory_bytes: 7824
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, const char * argv[]) {
    unsigned long N;
    scanf("%lu\n", &N);
    unsigned long *A = (unsigned long *) malloc(sizeof(unsigned long) * N);
    
    unsigned long num = 0;
    for(unsigned long i = 0; i < N; i++) {
        scanf("%lu", A + i);
        if(*(A + i) % 2 != 0) {
            num++;
        }
    }
    
    free(A);
    
    printf("%s\n", num % 2 == 0 ? "YES" : "NO");
    return 0;
}

Measured memory_bytes: 896
import sys

input = sys.stdin.readline


def main():
    K, A, B = map(int, input().split())

    if A + 2 < B:
        if A - 1 <= K:
            K -= (A - 1)
            ans = A
            q, r, = divmod(K, 2)
            ans += (B - A) * q + r
        else:
            ans = 1 + K
    else:
        ans = 1 + K

    print(ans)


if __name__ == "__main__":
    main()


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

using namespace std;

int n, answer;
vector < int > G[100020];
int A[100020][30];

void go(int v, int p = 0)
{
  for(int i = 0; i < G[v].size(); ++ i)
  {
    int to = G[v][i];
    if(to == p)
      continue;
    go(to, v);
    for(int k = 0; k < 30; ++ k)
      A[v][k] += A[to][k];
  }
  int x = 0;
  for(int i = 29; i >= 0; -- i)
    if(A[v][i] > 1)
    {
      x = i; break;
    }
  while(A[v][x])
    x ++;
  answer = max(x, answer);
  for(int i = 0; i < x; ++ i)
    A[v][i] = 0;
  A[v][x] = 1;
  for(int i = x; i < 30; ++ i)
    if(A[v][i])
      A[v][i] = 1;
}

int main()
{
  scanf("%d", &n);
  for(int i = 1; i < n; ++ i)
  {
    int x, y;
    scanf("%d %d", &x, &y);
    G[x].push_back(y);
    G[y].push_back(x);
  }

  go(1);

  printf("%d\n", answer);
}


Measured memory_bytes: 24320
#include <bits/stdc++.h>
using namespace std;
int n;
void get(int *p, int &pc, char *s) {
    if (s[1] == '0') p[++pc] = 0;
    p[++pc] = 0;
    for (int i = 1; i < n; ++i)
        if (s[i] != s[i + 1]) p[++pc] = i;
    if (s[n] == '0') p[++pc] = n;
    p[++pc] = n;
}

char s[5003], t[5003];
int p[5003], q[50003], pc, qc;
int res = 1000000000;
int qry(int *p, int pc, int pos) {
    return pos < 1 ? 0 : (pos > pc ? n : p[pos]);
}
int solve(int x) {
    int rt = 0;
    for (int i = min(1 - x, 1), en = max(qc - x, pc); i <= en; ++i)
        rt += abs(qry(p, pc, i) - qry(q, qc, i + x));
    return rt;
}
int main() {
    scanf("%d%s%s", &n, s + 1, t + 1);
    get(p, pc, s), get(q, qc, t);
    for (int i = -pc; i <= qc; i += 2) res = min(res, solve(i));
    printf("%d\n", res);
    return 0;
}


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

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define rep3(i,m,n) for(int (i)=m;(i)<=(n);(i)++)
#define rep3rev(i,m,n) for(int (i)=m;(i)>=(n);(i)--)
#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> pii;

void Main(){
    int n;
    cin >> n;
    vi a(n), plus(n), minus(n);
    map<int, int> mp;
    rep(i,n) {
        cin >> a[i];
        plus[i] = i + 1 + a[i];
        minus[i] = i + 1 - a[i];
        mp[minus[i]] ++;
    }
    ll ans = 0;
    rep(i,n){
        ans += mp[plus[i]];
    }
    
    cout << ans << endl;
	return;
}

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


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

using namespace std;
#define all(x) (x).begin(),(x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define endl "\n"
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {os << "["; for (const auto &v : vec) {os << v << ","; } os << "]"; return os;}
template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) {os << "(" << p.first << ", " << p.second << ")"; return os;}

const int mod = 1e9 + 7;

void solve() {
    ll N;
    cin >> N;
    vector<ll> A(N);
    ll sum = 0;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
        sum += A[i];
        sum %= mod;
    }

    ll ans_ = 0;
    for (int i = 0; i < N; i++) {
        ans_ += sum * A[i];
        ans_ %= mod;
    }
    for (int i = 0; i < N; i++) {
        ans_ -= A[i] * A[i] % mod;
        ans_ = (ans_ + mod) % mod;
    }

    cout << ans_ * (ll)(5 * 1e8 + 4) % mod << endl;

}

int main() {
    #ifdef LOCAL_ENV
    cin.exceptions(ios::failbit);
    #endif
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout.setf(ios::fixed);
    cout.precision(16);
    
    solve();
}

Measured memory_bytes: 4776
n = int(input())
creatures = list(map(int, input().split()))

creatures.sort()

accumulation = []

for i in range(len(creatures)):
    if i == 0:
        accumulation.append(0)
    else:
        accumulation.append(creatures[i-1]+accumulation[i-1])
        
creatures.reverse()
accumulation.reverse()

ans = 0

for i in range(len(creatures)):
    if i == 0:
        ans += 1
    else:
        if creatures[i-1] > (creatures[i] + accumulation[i])*2:
            break
        else:
            ans += 1
            
print(ans)

Measured memory_bytes: 14508
#include<bits/stdc++.h>
using namespace std;
typedef long long int uli;
const int mx=2e5+10;
const int mxl=21;
int a[mxl][mx];
int qry(int l,int r){
  int len=32-__builtin_clz(r-l+1)-1;
  return min(a[len][l],a[len][r-(1<<len)+1]);
}
int main(){
  int n;
  scanf("%d",&n);
  for(int i=0;i<n;i++)scanf("%d",&a[0][i]);
  for(int l=1;l<mxl;l++){
    for(int i=0;i<n;i++){
      a[l][i]=a[l-1][i];
      int j=i+(1<<(l-1));
      if(j<n){
        a[l][i]=min(a[l][i],a[l-1][j]);
      }
    }
  }
  uli ans=0ll;
  for(int i=0;i<n;i++){
    int l=0,r=i,lft=i,rht=i; 
    while(l<=r){
      int mid=(l+r)>>1;
      if(qry(mid,i)==a[0][i])lft=mid,r=mid-1;
      else l=mid+1;
    }
    l=i,r=n-1;
    while(l<=r){
      int mid=(l+r)>>1;
      if(qry(i,mid)==a[0][i])rht=mid,l=mid+1;
      else r=mid-1;
    }
    ans+=uli(a[0][i])*uli(i-lft+1)*uli(rht-i+1);
  }
  printf("%lld\n",ans);
  return 0;
}


Measured memory_bytes: 16640
/*
 Author: Vedang Karwa
 */

//#include <bits/stdc++.h>
#include<iostream>
#include <iosfwd>
#include<limits.h>
#include <sstream>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include<stdio.h>
#include <queue>
#include <stack>
#include <math.h>
#include<string.h>
#include <map>
#include <set>
#include<vector>
#include <chrono>
#include <random>
#include <bitset>
using namespace std;
#define fr(i,j,n)   for(lli i=j;i<(n);++i)
#define bk(i,j,n)   for(lli i=j;i>=n&&j>=n;--i)
#define pb push_back
#define F first
#define S second
#define endl "\n"
#define mem(p,v) memset(p,v,sizeof(p))
#define present(c,x)    ((c).find(x) != (c).end())
#define run ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
//#define ordered_set tree<lli, null_type,less<lli>, rb_tree_tag,tree_order_statistics_node_update>
#define debug1(x) cout<<#x<<" "<<x<<endl;
#define debug2(x,y) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<endl;
#define debug3(x,y,z) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<endl;
#define debug4(x,y,z,a) cout<<#x<<" "<<x<<" "<<#y<<" "<<y<<" "<<#z<<" "<<z<<" "<<#a<<" "<<a<<endl;
#define ub upper_bound
#define lb lower_bound
typedef long long int lli;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef vector<lli> vl;
typedef pair<lli,lli> pl;
inline lli max(lli a,lli b){ return a>b?a:b;}
inline lli min(lli a,lli b){ return a<b?a:b;}
const long long inf = 1e18+5;
const lli M =1e9+7 ;
void show(vector<lli> v){fr(i,0,v.size())cout<<v[i]<<" ";cout<<endl;}
void show(vector<int> v){fr(i,0,v.size())cout<<v[i]<<" ";cout<<endl;}
void show(int a[], int arraysize){for (int i = 0; i < arraysize; ++i)cout << a[i] << " ";cout<<endl;}
void show(lli a[], lli arraysize){for (lli i = 0; i < arraysize; ++i)cout << a[i] << " ";cout<<endl;}
void show(bool a[], lli arraysize){for (lli i = 0; i < arraysize; ++i)cout << a[i] << " ";cout<<endl;}
void show(set<int> s){for (auto it = s.begin(); it != s.end(); it++){cout << *it << " ";}cout<<endl;}
void show(map<lli,lli> mp){for (auto it = mp.begin(); it != mp.end(); it++){cout << it->first << " "<<it->second<<endl;}cout<<endl;}
lli modpow(lli x,lli n){lli res=1;while(n>0){if(n&1) res=((res%M)*(x%M))%M;x=((x%M)*(x%M))%M;n>>=1;}return res;}
lli power(lli x, lli n){lli res=1;while(n>0){if(n&1) res=res*x;x=x*x;n>>=1;}return res;}
const int N =1e5+5;
/////// ------ Main code starts here ------ /////

void solve()
{
    lli n;
    cin>>n;
    lli a[n];
    lli mn[n];
    lli ans=0;
    fr(i,0,n)
    {
        cin>>a[i];
        if(i==0)
        {
            mn[i]=INT_MIN;
        }
        else
        {
            mn[i]=max(mn[i-1],a[i-1]);
        }
        if(a[i]<mn[i])
        {
            ans+=mn[i]-a[i];
        }
    }
    cout<<ans<<endl;
}
int main()
{
    run;cout<<fixed<<setprecision(0);
    int t;
    t=1;
    while(t){solve();t--;}
    return 0;
}


Measured memory_bytes: 6780
#include<bits/stdc++.h>
using namespace std;
int main(){
  int h1,h2,m1,m2,k;
  cin>>h1>>m1>>h2>>m2>>k;
  int ans;
  ans=60*(h2-h1)+(m2-m1)-k;
  if (ans>=0){
    cout<<ans<<endl;
  }else{
    cout<<0<<endl;
  }

  
}

Measured memory_bytes: 3592
n, m = map(int, input().split())
ps = [[i for i in input().split()] for i in range(m)]


correct = 0
penalty = 0
true_flag = [True] * (10 ** 5)
d = {}

for p, s in ps:
    p = int(p)

    # 初回でACのときはスルー
    if p not in d and s == 'AC':
        d[p] = 1
        correct += 1
        true_flag[p] = False
    # 2回目以降でACのときpenaltyを加算
    if not true_flag[p]:
        continue  # 「trueだったら以下の処理を行う」を裏返してこう書けばネストが1つ減らせる

    if s == 'AC':
        penalty += d[p]
        correct += 1
        true_flag[p] = False
    # WAのときはdに追加していく
    elif p not in d:
        d[p] = 1
    else:
        d[p] += 1

print(correct, penalty)


Measured memory_bytes: 38780
#include <cstdio>
#include <cstring>

#define iter(i, n) for (int i = 1; i <= n; ++i)
#define iter0(i, n) for (int i = 0; i < n; ++i)

#define NR 22
#define SR (1 << 20)

const int mod = 1000000007;

int n, m, K;
int must[NR][NR], f[2][NR][SR];

inline void inc(int &x, int y) { x = (x + y) % mod; }
inline int remove_lowbit(int x) { return x ^ (x & -x); }

int main() {
	scanf("%d%d%d", &n, &m, &K); --n;
	int M = 1 << n;
	memset(must, -1, sizeof(must));
	iter(i, K) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		must[a - 1][b - 1] = c;
	}
	f[0][0][0] = 1;
	iter0(i, m) {
		bool d = i % 2;
		memset(f[!d], 0, sizeof(f[!d]));
		iter0(j, n) {
			bool _d = (d ^ (j + 1 == n));
			int _j = (j + 1) % n;
			iter0(s, M) if (f[d][j][s]) {
				if (!(s >> j & 1) && must[i][j] != 1)
					inc(f[_d][_j][s], f[d][j][s]);
				int tmp = (1 << j) - 1;
				int _s = remove_lowbit(s & ~tmp) ^ (1 << j) ^ (s & tmp);
				//printf("%d -%d-> %d\n", s, j, _s);
				if (must[i][j] != 0) inc(f[_d][_j][_s], f[d][j][s]);
				//printf("f[%d][%d][%d]=%d\n", i, j, s, f[d][j][s]);
			}
		}
	}
	int ans = 0;
	iter0(s, M) {
	//	printf("f[%d][0][%d]=%d\n", m, s, f[m%2][0][s]);
		inc(ans, f[m % 2][0][s]);
	}
	printf("%d\n", ans);
	return 0;
}

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


def solve(X: int):
    while True:
        f = 1
        for i in range(2, int(X**0.5) + 1):
            if X % i == 0:
                X += 1
                f = 0
                break
        if f == 1:
            print(X)
            exit()
    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()
    X = int(next(tokens))  # type: int
    solve(X)

if __name__ == '__main__':
    main()


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

#define ll long long

int const N = 301;

double n;

double dp[N][N][N];
bool vis[N][N][N];

double pound(int ones, int twos, int threes)
{
	if (ones == 0 && twos == 0 && threes == 0)
		return 0;
	
	if (vis[ones][twos][threes])
		return dp[ones][twos][threes];
	
	vis[ones][twos][threes] = 1;
	
	double s = (ones + twos + threes);
	double sum = n / s;
	
	pair<double, int> temp;
	
	if (ones)
	    sum += pound(ones - 1, twos, threes) * (ones / s);
	
	if (twos)
	    sum += pound(ones + 1, twos - 1, threes) * (twos / s);
	
	if (threes)
		sum += pound(ones, twos + 1, threes - 1) * (threes / s);
	
	return dp[ones][twos][threes] = sum;
}

int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int m[3] = {};
	cout << setprecision(9) << fixed;
	
    cin >> n;
    for (int i = 0; i < n; ++i)
    {
        int k;
        cin >> k;
        
        ++m[k - 1];
    }
    
    cout << pound(m[0], m[1], m[2]);;
    
    return 0;
}

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

int main()
{
	int n, m, s;

	cin >> n >> m;

	s = m / n;

	for (int i = s; i > 0; i--) {
		if (m % i == 0) {
			cout << i << endl;
			break;
		}
	}

	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = (0); i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
#define rng(x) (x).begin(),(x).end()
#define rrng(x) (x).rbegin(),(x).rend()
#define limit(x,l,r) max(l,min(x,r))
#define lims(x,l,r) (x = max(l,min(x,r)))
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define show(x) cout << #x << " = " << (x) << endl
#define show2(x,y) cout << #x << " = " << (x) << ", " << #y << " = " << (y) << endl
#define show3(x,y,z) cout << #x << " = " << (x) << ", " << #y << " = " << (y) <<  ", " << #z << " = " << (z) << endl
#define showv(v) rep(i,v.size()) printf("%d%c", v[i], i==v.size()-1?'\n':' ')
#define showv2(v) rep(j,v.size()) showv(v[j])
#define showt(t,n) rep(i,n) printf("%d%c", t[i], i==n-1?'\n':' ')
#define showt2(t,r,c) rep(j,r) showt(t[j],c)
#define showvp(p) rep(i,p.size()) printf("%d %d\n", p[i].first, p[i].second);
#define printv(v) rep(i,v.size()) printf("%d\n", v[i])
#define printt(t,n) rep(i,n) printf("%d\n", t[i])
#define incl(v,x) find(rng(v),x)!=v.end()
#define incls(s,c) s.find(c)!=string::npos
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define bit(n,k) ((n>>k)&1) // nのk bit目
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define newline puts("")
#define uni(x) x.erase(unique(rng(x)),x.end())
#define SP << " " <<
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using vl = vector<ll>;
using vs = vector<string>;
using P = pair<int, int>;
using T = tuple<int, int, int>;
using vp = vector<P>;
using vt = vector<T>;
const int mod = 1000000007;
const double EPS = 1e-9;
//const long double EPS = 1e-14;
const int INF = (1<<30)-1;
const ll LINF = (1LL<<62)-1;
#define dame { puts("No"); return 0;}
#define yn {puts("Yes");}else{puts("No");}
inline int in() { int x; scanf("%d",&x); return x;}
inline ll lin() { ll x; scanf("%lld",&x); return x;}
inline char chin() { char x; scanf("%c",&x); return x;}
inline string stin() { string x; cin >> x; return x;}
inline double din() { double x; scanf("%lf",&x); return x;}
//template<class T = int> inline T in() { T x; cin >> x; return (x);}
template<typename T>inline ll suma(const v(T)& a) { ll res(0); for (auto&& x : a) res += x; return res;}
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
char itoa(int n) { return n + '0';}
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;}

int main () {
  int r = in();
  double ans = 2*M_PI*r;
  cout << fixed << setprecision(10) << ans << endl;
  return 0;
}

Measured memory_bytes: 3800
#include<stdio.h>
int main(){
	int a,b,c;
	scanf("%d %d %d",&a,&b,&c);
if(a>=b&&a>=c){
	if(b>=c){
		printf("%d %d %d\n",c,b,a);
		}
	else{
		printf("%d %d %d\n",b,c,a);
		}
}else if(b>=a&&b>=c){
	if(a>=c){
		printf("%d %d %d\n",c,a,b);
		}
	else{
		printf("%d %d %d\n",a,c,b);
		}
}else if(c>=a&&c>=b){
	if(a>=b){
		printf("%d %d %d\n",b,a,c);
		}
	else{
		printf("%d %d %d\n",a,b,c);
		}
}
	return 0;
}

Measured memory_bytes: 596
import math
n = input()
ai = list(int(i) for i in input().split())
li = [0] * (max(ai)+10)
for i in range(len(ai)):
    p = ai[i]
    p = p+1
    li[p-1] = li[p - 1] + 1
    li[p] = li[p] + 1
    li[p + 1] = li[p + 1] + 1
print(max(li))

Measured memory_bytes: 13964
n = int(input())
a = [int(input()) for i in range(n)]
ans = 0
now = 1
for i in a:
    if i == now:
        now += 1
    if i < now:
        continue
    if i%now == 0:
        ans += i//now-1
    else:
        ans +=  i//now
    if now == 1:
        now += 1
print(ans)

Measured memory_bytes: 49880
#ifdef LOCAL
#  define _GLIBCXX_DEBUG
#  define _GLIBCXX_DEBUG_PEDANTIC
#endif

#include "bits/stdc++.h"
#define ff first
#define ss second
#define szof(_x) ((int) (_x).size())
#define TASK_NAME ""

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int INF = 1e9 + 7;
const ll INFL = 1e18 + 123;
const double PI = atan2(0, -1);
mt19937 tw(960172);
ll rnd(ll x, ll y) { static uniform_int_distribution<ll> d; return d(tw) % (y - x + 1) + x; }


void solve() {
	int n, m;
	cin >> n >> m;
	string s;
	cin >> s;
	vector<vector<int>> graph(n * 4);
	for (int i = 0; i < m; ++i) {
		int a, b;
		cin >> a >> b;
		--a; --b;
		if (s[a] > s[b]) {
			swap(a, b);
		}
		if (s[a] == 'A' && s[b] == 'A') {
			graph[a].push_back(b + n);
			graph[b].push_back(a + n);
		} else if (s[a] == 'A' && s[b] == 'B') {
			graph[a + n].push_back(b + n * 2);
			graph[b + n * 3].push_back(a);
		} else if (s[a] == 'B' && s[b] == 'B') {
			graph[a + n * 2].push_back(b + n * 3);
			graph[b + n * 2].push_back(a + n * 3);
		}
	}

	vector<int> used(n * 4);

	// vector<int> cycle;

	function<bool(int)> dfs = [&](int v) {
		// cycle.push_back(v);
		used[v] = 1;
		for (int to : graph[v]) {
			if (!used[to]) {
				if (dfs(to)) {
					return true;
				}
			} else if (used[to] == 1) {
				/*reverse(cycle.begin(), cycle.end());
				while (cycle.back() != to) {
					cycle.pop_back();
				}
				reverse(cycle.begin(), cycle.end());
				for (int num : cycle) {
					cerr << num % n + 1 << " ";
				}
				cerr << endl;*/
				return true;
			}
		}
		used[v] = 2;
		// cycle.pop_back();
		return false;
	};

	for (int i = 0; i < n * 4; ++i) {
		if (!used[i]) {
			if (dfs(i)) {

				cout << "Yes\n";
				return;
			}
		}
	}

	cout << "No\n";
}


int main() {
	//freopen(TASK_NAME ".in", "r", stdin);
	//freopen(TASK_NAME ".out", "w", stdout);
	cerr << fixed << setprecision(15);
	cout << fixed << setprecision(15);
	ios::sync_with_stdio(false);

	int tests = 1;
	// cin >> tests;
	for (int it = 1; it <= tests; ++it) {
		solve();
	}
	
	#ifdef LOCAL
		cerr << "time: " << clock() << " ms\n";
	#endif
	return 0;
}

Measured memory_bytes: 34512
#include<stdio.h>

int main(void){
	int n;
	int i,j,z,y;
	int cnt=0;
	
	while(scanf("%d",&n)==1){
		for(i=0;i<=9;i++){
			for(j=0;j<=9;j++){
				for(z=0;z<=9;z++){
					for(y=0;y<=9;y++){
						if(n==i+j+z+y)
							cnt++;
					}
				}
			}
		}
		printf("%d\n",cnt);
		cnt=0;
	}
	
	return 0;
}

Measured memory_bytes: 0
#include "bits/stdc++.h"
using namespace std;
typedef long long li;

const li mod = 1e9 + 7;

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

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

    if (xs[0] != 0) {
        cout << -1 << endl;
        return 0;
    }

    for (int i = 0; i < n - 1; ++i) {
        if (xs[i+1] - xs[i] >= 2) {
            cout << -1 << endl;
            return 0;
        }
    }

    vector<li> maxd(2 * n);
    for (int i = 0; i < n; ++i) {
        maxd[i - xs[i]] = max(maxd[i - xs[i]], xs[i]);
    }

    li ans = 0;
    for (int i = 0; i < 2 * n; ++i) {
        ans += maxd[i];
    }


    cout << ans << endl;


    return 0;
}


Measured memory_bytes: 4992
#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
#define int long long
using P = pair<int,int>;
//#########################################
signed main(){
	string s;cin >> s;
	int k;cin >> k;
	vector<string> res;
	int n = s.size();
	rep(i,n)rep(j,k+1){
		if(j != 0)res.push_back(s.substr(i,j));
	}
	sort(res.begin(),res.end());
	int cnt = 0;
	map<string,int> mp;
	rep(i,res.size()){
		if(mp[res[i]])continue;
		cnt++;
		// trace(cnt);
		if(cnt == k){
			cout << res[i] << endl;
			return 0;
		}
		mp[res[i]]++;
	}
	return 0;
}

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

int main() {
  string S,T;
  cin >> S >> T;
  long long count = 0;
  for (long long i = 0; i < S.size(); i++){
    if (S.at(i) != T.at(i)){
      count++;
    }
  }
  cout << count;
}

Measured memory_bytes: 3880
int p[1<<17];
int find(int a){return a-p[a]?p[a]=find(p[a]):a;}
int n,m,x,y,cnt;
int P[1<<17];
main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i++<n;)
	{
		p[i]=i;
		scanf("%d",P+i);
	}
	for(;~scanf("%d%d",&x,&y);)
	{
		x=find(x);y=find(y);
		if(x-y)p[x]=y;
	}
	for(int i=0;i++<n;)cnt+=find(P[i])==find(i);
	printf("%d",cnt);
}


Measured memory_bytes: 896
import java.util.*;
import java.lang.*;
import java.io.*;

public class Main {
    public static void main(String args[]) {
        Scanner scan = new Scanner(System.in);
        
        int a = scan.nextInt();
        int b = scan.nextInt();
        
        System.out.println(cardDown(a,b));
    }
    
    private static long cardDown(int a, int b){
        if(a == 1 && b == 1) return 1;
        if(a == 1 || b == 1) return a*b - 2;
        
        return (long)a*b - 2*a - 2*b + 4;
    }
}


Measured memory_bytes: 21844
#include<cstdio>
#include <iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<list>
#include<set>
#include<numeric>

#define MOD 1000000007

using namespace std;
typedef long long ll;

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

	int x[1010], y[1010];

	for (int i = 0; i < N; i++) {
		cin >> x[i] >> y[i];
	}

	ll ans = 0;
	for (int i = 0; i < N-1; i++) {
		if ((x[i] <= x[i+1] && y[i] <= y[i+1])||
			(x[i] >= x[i + 1] && y[i] >= y[i + 1])){
			ans += max(abs(x[i] - x[i + 1]), abs(y[i] - y[i + 1]));
		}
		else 
			ans += abs(x[i] - x[i + 1]) + abs(y[i] - y[i + 1]);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3024
# -*- coding: utf-8 -*-
"""
Created on Sat Sep  1 18:57:18 2018
ABC108C
@author: maezawa
"""

n, k = list(map(int, input().split()))

if k % 2 != 0:
    r = n // k
    print(r**3)
else:
    r1 = n // k
    r2 = n // (k//2) - r1
    print(r1**3+r2**3)


Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define REP2(i,x,n) for(int i=x; i<(n); i++)
int main()
{
    int A, B;
    cin >> A>>B;

    string ans = "Impossible";
    if(A%3==0) ans = "Possible";
    if(B%3==0) ans = "Possible";
    if((A+B)%3==0) ans = "Possible";
    cout << ans<< endl;

   return 0;
}

Measured memory_bytes: 256
/*
 ID: bradyawn
 PROG: contest
 LANG: C++11
 */

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <vector>
#include <deque>
#include <string>
#include <cmath>
#include <map>
#include <utility>
#include <algorithm>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <bitset>
#include <random>
#include <cstring>

using namespace std;

typedef long long ll;
typedef pair<int,int> i2;
typedef pair<ll,ll> ll2;

vector<vector<i2>> adj(100001); //node, dist

bool vis[100001];

int val[100001];

bool ok = true;

void dfs(int cur)
{
    if (!ok) return;
    vis[cur] = true;
    for (auto e : adj[cur])
    {
        if (!vis[e.first]) //not visited
        {
            val[e.first] = val[cur] + e.second;
            dfs(e.first);
        }
        else if (val[cur]+e.second != val[e.first]) { ok = false; return; }
    }
}

int main()
{
    //ifstream inf("");
    //ofstream outf("");
    //cout << setprecision(10);
    //ios_base::sync_with_stdio(0);
    //cin.tie(0);
    
    int n, m;
    cin >> n >> m;
    
    while (m--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        adj[a].push_back({b,c});
        adj[b].push_back({a,-c});
    }
    
    for (int i = 1; i <= n; i++)
    {
        if (!vis[i]) //check is component reliable
        {
            val[i] = 0;
            dfs(i);
        }
        if (!ok)
        {
            cout << "No\n";
            return 0;
        }
    }
    
    cout << "Yes\n";
    return 0;
    
}




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

using namespace std;

#define pb push_back

#define ll long long int
vector<pair<int, int>> graph[100000];
#define max 1000000

int main(){
    
    ll n;
    cin>>n;
    string s;
    while(n){
        // if(n<=0)
        //     break;
        int f=n%26;
        
        char ch = f+'a'-1;
        if(f==0 ){
            ch='z';
            
        }
        // cout<<f;
        s.pb(ch);
        
        n=(n-1)/26;
        // if(n<=0)
        //     break;
    }
    
    reverse(s.begin(), s.end());
    cout<<s;
    return 0;
}

Measured memory_bytes: 5976
#include <stdio.h>

int a[26] = {};
char x[101][101] = {};
int main()
{
	int h, w;
	int cnt_4 = 0,cnt_2 = 0,cnt_1 = 0;
	int ans = 0;
	scanf("%d %d",&h, &w);
	for (int i = 0; i < h; i++)
	{
			scanf("%s",x[i]);
			for (int j = 0; j < w; j++)
			{
				a[x[i][j] - 'a']++;
			}
	}

	for (int i = 0; i < 26; i++)
	{
		int x, y;
		x = 0;
		y = 0;
		x = a[i];
		cnt_4 = cnt_4 + x / 4;
		if (x % 4 != 0)
		{
			y = x % 4;
			cnt_2 = cnt_2 + y / 2;
		}
		if (x % 4 != 0 && y % 2 != 0)
		{
			cnt_1 = cnt_1 + y % 2;
		}
	}
	if (h % 2 != 0 && w % 2 != 0)
	{
		if (cnt_1 == 1 && cnt_4 >= ((w-1) / 2) * ((h-1) / 2))
		{
			int q;
			q = cnt_4 - ((w-1) / 2) * ((h-1) / 2);
				if (cnt_2 + (q * 2) == (w-1) / 2 + (h-1) / 2)
				{
					ans = 1;
				}
		}
	}
	else if (h % 2 == 0 && w % 2 != 0)
	{
		if (cnt_1 == 0 && cnt_4 >= ((w-1) / 2) * (h / 2))
		{
			int q;
			q = cnt_4 - ((w-1) / 2) * (h / 2);
			if (cnt_2 + (q * 2) == h / 2)
			{
				ans = 1;
			}
		}
	}
	else if (h % 2 != 0 && w % 2 == 0)
	{
		if (cnt_1 == 0 && cnt_4 >= (w / 2) * ((h-1) / 2))
		{
			int q;
			q = cnt_4 - (w / 2) * ((h-1) / 2);
			if (cnt_2 + (q * 2) == w / 2)
			{
				ans = 1;
			}
		}
	}
	else if (h % 2 == 0 && w % 2 == 0)
	{
		if (cnt_1 == 0 && cnt_2 == 0 && cnt_4 == (w / 2) * (h / 2))
		{
			ans = 1;
		}
	}
	if (ans == 1)
	{
		printf("Yes");
	}
	else
	{
		printf("No");
	}
	return 0;
}

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

int n, A[50];

int solve(int i, int m) {
  if (m == 0) return 1;
  if (i >= n) return 0;
  int res = solve(i + 1, m) || solve(i + 1, m - A[i]);
  return res;
}

int main() {
  int q, M, i;

  cin >> n;
  for (i = 0; i < n; i++) scanf("%d", &A[i]);
  cin >> q;
  for (i = 0; i < q; i++) {
    cin >> M;
    if (solve(0, M)) cout << "yes" << endl;
    else cout << "no" << endl;
  }

  return 0;
}


Measured memory_bytes: 3244
import java.util.Arrays;
import java.util.Scanner;

public class Main {

	
	public static boolean nextPermutation(int[] arr) {
		
		int i = arr.length-1;
		
		while (i > 0 && arr[i-1] > arr[i]) {
			--i;
		}
		
		if (i == 0) return false;
		
		int j = arr.length - 1;
		int head = arr[i-1];
		while (j > i && arr[j] < head) {
			--j;
		}
		
		int tmp = arr[i-1];
		arr[i-1] = arr[j];
		arr[j] = tmp;
		
		j = arr.length - 1;
		
		while (i < j) {
			tmp = arr[i];
			arr[i] = arr[j];
			arr[j] = tmp;
			++i;
			--j;
		}
		
		
		return true;
	}
	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int N = Integer.parseInt(sc.nextLine());
		
		int[] arr = new int[N];
		int[][] input = new int[N][];
		double fact = 1;
		
		for (int i=0; i<N; ++i) {
			arr[i] = i+1;
			fact *= (i+1);
			input[i] = new int[N];
			
			for (int j=0; j<2; ++j) {
				input[i][j] = sc.nextInt();
			}
		}
		
		double sum = 0;
		double avg = 0;
	
		do {
			
			int[] now = input[arr[0]-1];
			int x1 = now[0];
			int y1 = now[1];
			for (int cur=1; cur < N; ++cur) {
				
				now = input[arr[cur]-1];
				int y2 = now[1];
				int x2 = now[0];
				
				int left = (y2-y1) * (y2-y1);
				int right = (x2-x1) * (x2- x1);
				sum += Math.sqrt(left + right);
				
				x1 = x2;
				y1 = y2;
			}
			
		}while(nextPermutation(arr));
		
		System.out.printf("%.10f\n",sum / fact);
		
		sc.close();

	}

}


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

int g[15][15] ; 

int main() {
  int n ; 
  cin >> n ; 
  for(int i = 0 ; i < n ; i++){
    for(int j = 0 ; j < n ; j++){
      g[i][j] = -1 ; 
    }
  }
  
  for(int i = 0 ; i < n ; i++){
    int m ;
    cin >> m ; 
    for(int j = 0 ; j < m ; j++){
      int a , x ; 
      cin >> a >> x ; 
      --a ; 
      g[i][a] = x ; //入力でiがjについてなんて言っているか
    }
  }
  
  int ans = 0 ; 
  for(int i = 0 ; i < (1<<n) ; i++){//ex)  000000 , 000001 , 000010 ....
    vector<int> d(n) ; 
    for(int j = 0 ; j < n ; j++) if(i >>j&1){
      d[j] = 1 ; 
    }
  
  
    bool ok = true ; 

    for(int j = 0 ; j <n ; j++){
      if(d[j]){
        for(int k = 0 ; k <n ; k++){
          if(g[j][k] == -1) continue ; 
          if(g[j][k] != d[k]) ok = false ;         
        }
      }
    }
    if(ok) ans = max(ans , __builtin_popcount(i)) ; 
  }
  cout << ans << endl ; 
  return 0 ; 
}

Measured memory_bytes: 3596
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = (0); i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define DEBUG(val) cout << #val << " : " << val << endl
#define MOD 1000000007

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = (0); i < (n); ++i)
#define ALL(obj) (obj).begin(), (obj).end()
#define DEBUG(val) cout << #val << " : " << val << endl
#define MOD 1000000007

int find_all(string str, string subStr) {
  int result = 0;
  int subStrSize = subStr.size();
  int pos = str.find(subStr);

  while (pos != std::string::npos) {
    result++;
    pos = str.find(subStr, pos + subStrSize);
  }
  return result;
}

int main() {
  int n;
  cin >> n;
  int a_count = 0;
  int b_count = 0;
  int w_count = 0;
  int ab_count = 0;
  REP(i, n) {
    string s;
    cin >> s;
    ab_count += find_all(s, "AB");
    if (s[0] == 'B' && s[s.length() - 1] == 'A') {
      w_count++;
    } else if (s[0] == 'B') {
      b_count++;
    } else if (s[s.length() - 1] == 'A') {
      a_count++;
    }
  }
  while (a_count != 0 || b_count != 0) {
    if (a_count < b_count) {
      swap(a_count, b_count);
    }
    if (w_count > 0) {
      w_count--;
      ab_count++;
    } else if (a_count != 0 && b_count != 0) {
      a_count--;
      b_count--;
      ab_count++;
    } else {
      break;
    }
  }
  if (w_count > 0) ab_count += (w_count - 1);
  cout << ab_count << endl;
}

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

int main(){
    int x, y; cin >> x >> y;
    int ans = 0;
    if(x == 1) ans += 300000;
    if(x == 2) ans += 200000;
    if(x == 3) ans += 100000;
    if(y == 1) ans += 300000;
    if(y == 2) ans += 200000;
    if(y == 3) ans += 100000;
    if(x == 1 && y == 1) ans += 400000;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256