akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;
#define int ll

const ll MOD = 1000000007;
const ll INF_LL = (ll)1000000007 * 1000000007;
const int INF_INT = (int)1000000007;
const double PI = 3.14159265358979323846;

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

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for (int i = ((int)(n)-1); i >= 0; i--)
#define irep(itr, st) for (auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for (auto itr = (st).rbegin(); itr != (st).rend(); ++itr)

#define m0(x) memset((x), 0, sizeof((x)))
#define m1(x) memset((x), -1, sizeof((x)))

// xにはvectorなどのコンテナ
#define ALL(x) (x).begin(), (x).end()  // sortなどの引数を省略したい
#define RALL(x) (x).rbegin(), (x).rend()  // sortなどの引数を省略したい
#define SIZE(x) ((ll)(x).size())     // sizeをsize_tからllに直しておく
#define MAX(x) *max_element(ALL(x))  //最大値を求める
#define MIN(x) *min_element(ALL(x))  //最小値を求める
#define all(x) (x).begin(), (x).end()

//略記
#define pb emplace_back  // vectorヘの挿入
#define mp make_pair     // pairのコンストラクタ
#define F first          // pairの一つ目の要素
#define S second         // pairの二つ目の要素

#define BITCOUNT __builtin_popcount
#define BITCOUNT_LL(x) __builtin_popcountll(x)

#define perm(c)             \
  sort(all(c));             \
  for (bool c##p = 1; c##p; \
       c##p = next_permutation(all(c)))  //順列 123 132 213 231 312 321

#define BIT(n) (1LL << (n))

#ifdef DEBUG
#define PRINT(A) std::cout << (#A) << ": " << (A) << std::endl;
#else
#define PRINT(A)
#endif

//入力高速化
struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
  }
} iosetup;

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

signed main() {
  int N; cin >> N;
  int ans = 0;
  for(int i = 1; i <= N; i++) {
    int b = N/i;
    ans += i*(b + 1)*b /2;
  }
  cout << ans << endl;
}

Measured memory_bytes: 3644
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int v[1005],w[1005],dp[1005][1005];
int main(void){
    int N,W;cin>>N>>W;
    for(int i=0;i<N;i++){
        cin>>v[i]>>w[i];
    }
    for(int i=N-1;i>=0;i--){
        for(int j=0;j<=W;j++){
            if(j<w[i]){
                dp[i][j]=dp[i+1][j];
            }else{
                dp[i][j]=max(dp[i+1][j],dp[i+1][j-w[i]]+v[i]);
            }
        }
    }
    cout<<dp[0][W]<<endl;
    return 0;
}



Measured memory_bytes: 3532
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define per(i, n) for (int i = (int)(n-1); i >= 0; i--)
#define sz(x) int(x.size())
#define all(x) (x).begin(), (x).end()
#define INF 2e9
typedef long long ll;
typedef pair<int, int> P;

int dp[101][4][4][4];

int main() {
    string s; cin >> s;
    int n = sz(s);
    int mod = 1e9+7;
    vector<vector<int> > dp(n+1, vector<int>(13));

    rep(i, n) {
        if (i == 0) {
            if (s[0] == '?') {
                rep(i, 10) {
                    dp[1][i] =  1;
                }
            }
            else {
                dp[1][int(s[0]-'0')] = 1;
            }
            continue;
        }
        rep(j, 13) {
            if (s[i] == '?') {
                rep(k, 10) {
                    int m = 10*j+k;
                    m %= 13;
                    dp[i+1][m] += dp[i][j];
                    dp[i+1][m] %= mod;
                }
            }
            else {
                int m = 10*j+(int)(s[i]-'0');
                m %= 13;
                dp[i+1][m] += dp[i][j];
                dp[i+1][m] %= mod;
            }
        }
    }

    cout << dp[n][5] << endl;

}

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

#define for(i, a, b) for(ll (i)=a;(i)<(b);++(i))
#define rep(i, n)    for(i, 0, n)
#define sz(x)        ((ll)(x).size())

const ll inf = 1 << 25;
const ll mod = 1e9 + 7;

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

int main() {
    string s;
    cin >> s;
    
    cout << s[0] + to_string(s.length() - 2) + s[s.length() - 1] << endl;
    
    return 0;
}

Measured memory_bytes: 256
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Ribhav
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        FastReader in = new FastReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        BSumo solver = new BSumo();
        solver.solve(1, in, out);
        out.close();
    }

    static class BSumo {
        public void solve(int testNumber, FastReader s, PrintWriter out) {
            String str = s.nextString();
            int countO = 0;
            int length = str.length();
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) == 'o') {
                    countO++;
                }
            }

            if (countO + (15 - length) >= 8) {
                out.println("YES");
            } else {
                out.println("NO");
            }
        }

    }

    static class FastReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private FastReader.SpaceCharFilter filter;

        public FastReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public String nextString() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            StringBuilder res = new StringBuilder();
            do {
                if (Character.isValidCodePoint(c)) {
                    res.appendCodePoint(c);
                }
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}



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

#define int long long

#define ii pair<int,int>
#define fi first
#define sc second

#define all(x) (x).begin(),(x).end()

int type_one(int h, int w) {
  int a = h / 3;
  int mi = a * w;
  if (h % 3) a++;
  int ma = a * w;
  return ma - mi;
}

int type_two(int h, int w) {
  int a = w / 3 + (w % 3 == 2);
  vector<int> v(3);
  v[0] = a * h;
  v[1] = (w - a) * (h / 2);
  v[2] = (w - a) * (h / 2 + h % 2);
  sort(all(v));
  return v[2] - v[0]; 
}

void solve() {
  int h, w;
  cin >> h >> w;
  // cout << type_one(w, h) << '\n';
  // cout << type_one(h, w) << '\n';
  // cout << type_two(w, h) << '\n';
  // cout << type_two(h, w) << '\n';
  cout << min(min(type_one(w, h), type_one(h, w)), min(type_two(w, h), type_two(h, w))) << '\n';
}

signed main() {
#ifdef _DEBUG
  // freopen("in" , "r", stdin );
  // freopen("out", "w", stdout);
#endif
  ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--) solve();
}


Measured memory_bytes: 3628
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int top;
int M[1000];
void push(int X) {
	M[++top] = X;
}
int pop() {
	top--;
	return M[top+1];
}
int main() {
	top = 0;
	int A, B;
	char s[100];
	while (scanf("%s", s) != EOF) {
		if (s[0] == '+') {
			A = pop();
			B = pop();
			push(A + B);
		}
		else if (s[0] == '-') {
			B = pop();
			A = pop();
			push(A - B);
		}
		else if (s[0] == '*') {
			A = pop();
			B = pop();
			push(A * B);
		}
		else {
			push(atoi(s));
		}
	}
	printf("%d\n", pop());
	
	return 0;
}



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

int n, m;
char map[510][510], map1[510][510], map2[510][510];

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; ++i)
    scanf("%s", map[i] + 1);
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      if (map[i][j] == '#') {
        map1[i][j] = '#';
        map2[i][j] = '#';
      } else if (j == 1) {
        map1[i][j] = '#';
        map2[i][j] = '.';
      } else if (j == m) {
        map1[i][j] = '.';
        map2[i][j] = '#';
      } else if (i & 1) {
        map1[i][j] = '#';
        map2[i][j] = '.';
      } else {
        map1[i][j] = '.';
        map2[i][j] = '#';
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j)
      putchar(map1[i][j]);
    putchar('\n');
  }
  putchar('\n');
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j)
      putchar(map2[i][j]);
    putchar('\n');
  }
  return 0;
}

Measured memory_bytes: 1536
#include <bits/stdc++.h>
#if 1
#define int long long
#define MAX LLONG_MAX
#define MIN LLONG_MIN
#define stoi stoll
#else
#define MAX INT_MAX
#define MIN INT_MIN
#endif
#define INF MAX/2
#define ALL(obj) (obj).begin(), (obj).end()
#define fi first
#define se second
const int NIL = -1;
const int MOD = 1000000007;
#define REP(i,n) for (int i=0; i < (n); ++i)
#define FOR(i,a,b) for (int i=(a); i < (b); ++i)
using namespace std;

int pow(int x, int n) {int res=1; while(n){if(n&1)res*=x;x*=x;n>>=1;} return res;}
int get_gcd(int a, int b) {while(1){if (a < b) swap(a, b); if (b == 0) return a; a %= b;}}
int get_lcm(int a, int b) {return a * b / get_gcd(a, b);}
bool chmin(int &a, int b) {if(a>b){a=b; return true;} else return false;}
bool chmax(int &a, int b) {if(a<b){a=b; return true;} else return false;}
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};

//-------------------------テンプレここまで-------------------------//

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

  int H, W;
  cin >> H >> W;
  vector<string> field(H);
  REP(i,H) cin >> field[i];

  // dp[i][j] ... (i,j) (0-index)までの道のり組み合わせ数
  static int dp[1000][1000];
  dp[0][0] = 1;
  REP(i,H) {
    REP(j,W) {
      if (field[i][j] == '#') continue;
      if (i>0) dp[i][j] += dp[i-1][j];
      if (j>0) dp[i][j] += dp[i][j-1];
      dp[i][j] %= MOD;
    }
  }

  cout << dp[H-1][W-1] << endl;

  return 0;
}


Measured memory_bytes: 9088
#-*- coding: utf-8 -*-
# １００でちょうどD回割り切れる正の整数のうちN番目に小さいものを求める
D, N = map(int, input().split())
# D = 0, 1, 2のいずれか
# N は1以上100以下の整数
if N != 100:
    print(100 ** D * N)
else:
    print(100 ** D * (N +1))

Measured memory_bytes: 2940
#include <iostream>
#include <vector>
#include <string>


using namespace std;

int n, q, A[25];
char t[25][2010];

bool check(int i, int m) {
	if (m == 0) return true;
	if (i >= n) return false;
	if (t[i][m] > 0) return t[i][m] == 2;
	bool res = check(i + 1, m) || check(i + 1, m - A[i]);
	t[i][m] = res ? 2 : 1;
	return res;
}

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

	cin >> q;
	for(int i=0;i<q;i++) {
		int m;
		cin >> m;
		if (check(0, m)) {
			cout << "yes" << endl;
		} else {
			cout << "no" << endl;
		}
	}

	return 0;
}

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

int main() {
  int N;
  cin >> N;
  vector<int> vec(N);
  for(int i=0;i<N;i++){
    cin >> vec.at(i);
  }
  sort(vec.begin(),vec.end());
  
  int A=1;
  for(int i=0;i<N-1;i++){
    if(vec.at(i)<vec.at(i+1)){A++;}
  }  
  cout << A << endl;
}


Measured memory_bytes: 3624
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int INF = 1001001001;
const ll LINF = 1001001001001001001;
#define fori(i,a,b) for(int i=(a);i<(int)(b);i++)
#define repi(i,n) fori(i,0,n)
#define forr(i,a,b) for(int i=int(b-1);i>=int(a);i--)
#define repr(i,n) forr(i,0,n)
#define all(x) (x).begin(),(x).end()
#define fill(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define mp make_pair
#define pcnt __builtin_popcount
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;}

void solve(ll a, ll b){
    bool ans[3];
    repi(i,3)ans[i]=true;
    ans[a-1]=false;
    ans[b-1]=false;
    repi(i,3)if(ans[i])cout<<i+1<<endl;
}

int main(){
    ll a;
    scanf("%lld",&a);
    ll b;
    scanf("%lld",&b);
    solve(a, b);
    return 0;
}


Measured memory_bytes: 256
//QWsin
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,x,y) for(int i=x;i<=y;++i)
#define out(i,u) for(int i=first[u];i!=-1;i=nxt[i])
#define repvc(i,vc) for(int i=0,Sz=vc.size();i<Sz;++i)
using namespace std;
inline int read()
{
	char ch=getchar();int ret=0,f=1;
	while(ch<'0' || ch>'9') {if(ch=='-')f=-1;ch=getchar();}
	for(;ch>='0' && ch<='9';ch=getchar()) ret=ret*10+ch-'0';
	return ret*f;
}

typedef long long ll;
const int INF=1<<30;

const int maxn=200000+10;
int h[maxn];
int cmp(int a,int b){return a>b;}
int main()
{
	int n,k;
	cin>>n>>k;
	rep(i,1,n) cin>>h[i];
	sort(h+1,h+n+1,cmp);
	ll sum=0;
	rep(i,k+1,n) sum+=h[i];
	cout<<sum<<endl;
	return 0;
}


Measured memory_bytes: 1024
// 7/25 リアルタイム参加
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < (n); ++i)
using ll = long long;
using P = pair<int, int>;

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

    int count = 0;

    while (A >= B) {
        B *= 2;
        count++;
    }

    while (B >= C) {
        C *= 2;
        count++;
    }

    if (count > K) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;
    }

    return 0;
}

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

//typedef
//------------------------------------------
typedef long long LL;
typedef vector<LL> VL;
typedef vector<VL> VVL;
typedef vector<string> VS;
typedef pair<LL, LL> PLL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

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

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

//main code

int main(int argc, char const* argv[])
{
	int n;
	cin >> n;
	VL a(n),b(n);
	vector<tuple<LL,LL,LL>> tp(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> b[i];
		tp[i] = make_tuple(a[i]+b[i],a[i],b[i]);
	}
	sort(RALL(tp));
	LL ta = 0,ao = 0;
	for (int i = 0; i < n; i++) {
		if (i % 2 == 0) {
			ta += get<1>(tp[i]);
		}
		else {
			ao += get<2>(tp[i]);
		}
	}
	cout << ta - ao << endl;
	return 0;

}


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

#define REP(i, x, n) for (int i = x; i < (int)(n); ++i)
#define RREP(i, x, n) for (int i = x - 1; i >= (int)(n); --i)
#define rep(i, n) REP(i, 0, n)
#define rrep(i, n) RREP(i, n, 0)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define MAX 1e9 + 7

using namespace std;

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

int main(void)
{
    vector<int> num(3);
    rep(i, 3)
    {
        cin >> num[i];
    }
    sort(all(num));
    cout << ((num[2] * 10 + num[1]) + num[0]) << endl;
    return 0;
}

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

int main(){
int total;
int change;
int count=0;
while(true){
 cin>>total;
 if(total==0){
    break;
  } 
 change=1000-total;
 while(true){
   if(change>=500){
     change-=500;
     count++;
   }else if(change>=100){
     change-=100;
     count++;
   }else if(change>=50){
     change-=50;
     count++;
   }else if(change>=10){
     change-=10;
     count++;
   }else if(change>=5){
     change-=5;
     count++;
   }else{
     change--;
     count++;
    }
  if(change==0){
    break;
  }
 }
 cout<<count<<"\n";
 count=0;
}
return 0;
}

Measured memory_bytes: 1160
class Problem
  attr_accessor *(?a..?z).to_a.map(&:to_sym)
  def initialize
    @h,@w,@m = gets.split.map(&:to_i)
    @a = Array.new(m){ gets.split.map(&:to_i).map{_1-1}}
  end

  def solve
    row = Array.new(h,0)
    col = Array.new(w,0)
    chk = Hash.new(false)
    a.each do |y,x|
      row[y] += 1
      col[x] += 1
      chk[[y,x]] = true
    end

    maxy = row.max
    maxx = col.max
    xs = []
    ys = []

    row.each_with_index do |y,i|
      ys << i if y == maxy
    end

    col.each_with_index do |x,i|
      xs << i if x == maxx
    end

    ys.each do |y|
      xs.each do |x|
        return maxy + maxx if !chk[[y,x]]
      end
    end
    return maxy + maxx - 1
  end

  def show(ans)
    puts ans
  end
end

Problem.new.instance_eval do
  show(solve)
end

Measured memory_bytes: 69980
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <queue>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <functional>
#include <iomanip>
#define vll vector<ll>
#define vvv vector<vvl>
#define vvi vector<vector<int> >
#define vvl vector<vector<ll> >
#define vv(a, b, c, d) vector<vector<d> >(a, vector<d>(b, c))
#define vvvl(a, b, c, d) vector<vvl>(a, vvl(b, vll (c, d)));
#define rep(c, a, b) for(ll c=a;c<b;c++)
#define re(c, b) for(ll c=0;c<b;c++)
#define all(obj) (obj).begin(), (obj).end()
typedef long long int ll;
typedef long double ld;
using namespace std;

vvl prime_factor(ll n) {
  vvl d = vv(0, 2, 0, ll);
  for(ll i = 2; i * i <= n; i++) {
    ll c = 0;
    while(n % i == 0) n /= i, c++;
    if(c!=0) d.push_back(vll{i, c});
  }
  if(n != 1) d.push_back(vll{n, 1});
  return d;
}
ll mpow(ll a, ll b, ll p = -1){
  ll ret = 1, num = a;
  if(p==-1){
    while(b>0){
      if(b%2) ret *= num;
      num = num * num;
      b /= 2;
    }
  }else{
    while(b>0){
      if(b%2) ret = (ret*num)%p;
      num = (num*num)%p;
      b /= 2;
    }
  }
  return ret;
}
//-------------------------------------------------------------------
#define P 1000000007
#define N_MAX 2000000
typedef long long int ll;
ll fac[N_MAX+1];
ll inv[N_MAX+1];
ll finv[N_MAX+1];

ll comb(ll n, ll k){
  if(n<0||k<0||n<k) return 0;
  return (((fac[n]*finv[n-k])%P)*finv[k])%P;
}

void init(){
  fac[0] = finv[0] = fac[1] = finv[1] = inv[1] = 1;
  for(int i = 2; i <= N_MAX; i++){
    fac[i] = (fac[i-1]*i)%P;
    inv[i] = ((-(P/i)*inv[P%i])%P+P)%P;
    finv[i] = (finv[i-1]*inv[i])%P;
  }
}
ll pp(ll a, ll b){
  return (a * b)%P;
}
//-------------------------------------------------------------------
int main(int argc, char const *argv[]) {
  ll ans = 0, n;std::cin >> n;
  vll a(n);re(i, n) std::cin >> a[i];
  map<ll, ll> mp;
  init();
  re(i, n){
    vvl tmp = prime_factor(a[i]);
    re(j, tmp.size()){
      ll now = tmp[j][0], cnt = tmp[j][1];
      if(mp.find(now)==mp.end()) mp.emplace(now, cnt);
      else{
        ll z = mp.at(now);
        mp.erase(now);
        mp.emplace(now, max(z, cnt));
      }
    }
  }
  ll mul = 1;
  for(auto itr=mp.begin();itr!=mp.end();itr++){
    ll now = (*itr).first, cnt = (*itr).second;
    mul = (mul * mpow(now, cnt, P))%P;
  }

  re(i, n){
    vvl tmp = prime_factor(a[i]);
    ll x = mul;
    re(j, tmp.size()){
      ll now = tmp[j][0], cnt=tmp[j][1];
      re(k, cnt) x = pp(x, inv[now]);
    }
    ans = (ans + x)%P;
  }
  std::cout << ans << '\n';
  return 0;
}


Measured memory_bytes: 47872
#include <iostream>
using namespace std;
#include <cmath>
#include <cstdio>


int main(){

  int a , b ;
  double C , S = 0.0 , L = 0.0 , h = 0.0;

  cin >> a >> b >> C;
  C = M_PI * C / 180;
  S = ( a * b * sin(C) ) / 2;
  L = sqrt( a * a + b * b - 2 * a * b * cos(C) ) + a + b;
  h = (double)( 2 * S )/ a;

  printf("%.8f\n%.8f\n%.8f\n",S,L,h);
  // cout <<  sin() << endl;


  return 0;
}

Measured memory_bytes: 1252
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main(void){
    // Your code here!
    int n;
    
    cin >> n;
    for(int i = 1; i < 10; i++){
        if(i * 111 >= n){
            cout << i * 111 << endl;
            break;
        }
    }
    
    return 0;
}


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

#define INF 10000000

using namespace std;
using P = pair<int, int>;

int main(void){

    int a, b, m;
    cin >> a >> b >> m;

    vector<int> vA(a), vB(b);
    vector<int> x(m), y(m), c(m);

    for(int i = 0;i < a;i++) cin >> vA[i];
    for(int i = 0;i < b;i++) cin >> vB[i];

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

    vector<int> value(m);
    for(int i = 0;i < m;i++){
        value[i] = vA[x[i]-1] + vB[y[i]-1] - c[i];
    }

    int ans = INF;

    sort(vA.begin(), vA.end());
    sort(vB.begin(), vB.end());

    for(int i = 0;i < m;i++){
        ans = min(ans, value[i]);
    }

    ans = min(ans, vA[0]+vB[0]);

    cout << ans << endl;

    return 0;
}

Measured memory_bytes: 2560
from heapq import heappop, heappush

n, m = map(int ,input().split())
MAP = [[] for _ in range(n)]
for i in range(m):
    u, v = map(int, input().split())
    MAP[u-1].append(v-1)

INF = 10 ** 15

dd = [[INF, INF, INF] for _ in range(n)]
visited = [[False, False, False] for _ in range(n)]

s, t = map(int, input().split())
s -= 1
t -= 1
dd[s] = [0, 0, 0]
q = [(0, s)]
ans = -1

while q:
    d, v = heappop(q)
    if visited[v][d%3]:
        continue
    visited[v][d%3] = True
    if v == t and d % 3 == 0:
        ans = d//3
        break
    to_d = d+1
    for to_v in MAP[v]:
        if visited[to_v][to_d % 3]:
            continue
        heappush(q, (to_d, to_v))

print(ans)


Measured memory_bytes: 38616
import java.lang.reflect.Array;
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		// 入力
		int A1 = sc.nextInt();                
		int A2 = sc.nextInt();
		int A3 = sc.nextInt();
		
		int B[] = new int[3];
		B[0] = Math.abs(A1 - A2);
		B[1] = Math.abs(A3 - A2);
		B[2] = Math.abs(A3 - A1);
		
		int max = 0;
		for (int i = 0; i< 3; i++) {
			if (max < B[i]) {
				max = B[i];
			}
		}
		System.out.println(B[0]+B[1]+B[2]-max);			
	}	
}


Measured memory_bytes: 23764
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <deque>
using namespace std;
using P = pair<int,int>;
typedef long long int ll;
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)

int main(){ 
    int n;cin >> n;
    double ans = 0.0,a;
    rep(i,n){
        cin >> a;
        ans += 1.0/a;
    }
    ans = 1.0 /ans;
    cout<<setprecision(16) << ans<<endl;
    return 0;
}




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

namespace AtCoder
{
	public class ABC
	{
		static readonly int mod = 1000000007;  // 10^9+7

		static void Main(string[] args)
		{
			Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });


			//string s = Console.ReadLine();
			int n = int.Parse(Console.ReadLine());
			//long x = long.Parse(Console.ReadLine());
			//int[] vs = Console.ReadLine().Split().Select(int.Parse).ToArray();
			//long[] vs = Console.ReadLine().Split().Select(long.Parse).ToArray();
			int[][] a = new int[n][];
			for (int i = 0; i < n; i++) {
				a[i] = Console.ReadLine().Split().Select(int.Parse).ToArray();
			}
#if true
			long[,] dp = new long[n + 1, 1 << n];
			dp[0, 0] = 1;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < dp.GetLength(1); j++) {
					if (i == PopCount(j)) {
						//System.Diagnostics.Debug.Write($"dp[{i},{j,2}({Convert.ToString(j, 2).PadLeft(n, '0')})]{dp[i, j]} -> ");
						for (int k = 0; k < n; k++) {
							int msk = 1 << k;
							if (a[i][k] == 1 && (j & msk) == 0) {
								dp[i + 1, j | msk] += dp[i, j];
								dp[i + 1, j | msk] %= mod;
							}
							//System.Diagnostics.Debug.Write($"dp[{i + 1},{j | msk,2}({Convert.ToString(j | msk, 2).PadLeft(n, '0')})]{dp[i + 1, j | msk]} ");
						}
						//System.Diagnostics.Debug.WriteLine("");
					}
				}
				//System.Diagnostics.Debug.WriteLine("");
			}
			//System.Diagnostics.Debug.WriteLine($"dp[{n},{(1 << n) - 1,2}({Convert.ToString((1 << n) - 1, 2).PadLeft(n, '0')})] = {dp[n, (1 << n) - 1]}");
			Console.WriteLine(dp[n, (1 << n) - 1]);

#else
			long ans = 0;
			bool[] xf = new bool[n];
			bool[] yf = new bool[n];
			for (int i = 0; i < n; i++) {
				ans += saiki(a, i, 0, xf, yf, "");
			}
			Console.WriteLine(ans);
#endif


			Console.Out.Flush();
		}

		static int PopCount(long x)
		{
			int ret = 0;
			while (x != 0) {
				x &= x - 1;
				ret++;
			}
			return ret;
		}

		static long saiki(int[][] a, int x, int y, bool[] xf, bool[] yf, string s)
		{
			long result = 0;
			if (a[x][y] == 0 || xf[x] || yf[y]) {
				return 0;
			}
			xf[x] = true;
			yf[y] = true;
			s += $"a[{x},{y}]:{a[x][y]} ";
			if (y == a.Length - 1) {
				result = 1;
				System.Diagnostics.Debug.WriteLine(s);
			} else {
				for (int i = 0; i < a.Length; i++) {
					result += saiki(a, i, y + 1, xf, yf, s);
				}
			}
			xf[x] = false;
			yf[y] = false;
			return result;
		}

	}
}


Measured memory_bytes: 322400
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<string>
#include<stack>
#include<set>
#include<unordered_map>
#include<time.h>
#include<cstdlib>
typedef long long ll;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define mm(a) memset(a,0,sizeof(a))
#define lr rt<<1
#define rr rt<<1|1
#define sync std::ios::sync_with_stdio(false);std::cin.tie(0);
#define inf 0x3f3f3f3f
#define eqs 1e-8
#define lb(x) (x&(-x))
#define ch(a) (int(a-'a')+1)
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef pair<int,int> pii;
const double pi=acos(-1);
const int maxn=20010;;
//const ll Mod=1000000007;
const ll Mod=998244353;
using namespace std;

int a[maxn];
int b[maxn];

int main()
{
    sync;
    int n,x,y;
    cin>>n>>x>>y;
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            int s;
            if(i<=x)
            {
                s=min(j-i,abs(j-y)+1+x-i);
            }
            else
            {
                s=min(j-i,abs(j-y)+1+i-x);
                s=min(s,j+1+i-x);
            }
            b[s]++;
        }
    }
    for(int i=1;i<n;i++)
        cout<<b[i]<<endl;
    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<map>
#include<string>
#include<utility>
#include<algorithm>
#include<cstdio>
#include<iomanip>
using namespace std;
#define ll int64_t
#define REP(i, n) for (ll i=0; i<n; i++)

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

    ll r, D, x2000;
    cin >> r >> D >> x2000;

    REP (i, 10) {
        x2000 = r * x2000 - D;
        cout << x2000 << "\n";
    }
}

Measured memory_bytes: 256
#define George_Plover
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <iostream>
#include <iomanip>
#include <algorithm>
#define MAXN 1001
#define MAXM 600001
#define MOD 100000007
#define EPS (1e-8)
#define LL long long
using namespace std;
int n,m;
bool vis[200][200];
int main()
{
    cin>>n>>m;
    for(int i=1;i<=m;i++)
    {
        int tx,ty;
        cin>>tx;
        for(int j=1;j<=tx;j++)
        {
            cin>>ty;
            vis[ty][i]=1;
        }
    }
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        int tx=0;
        for(int j=1;j<=m;j++)
        {
            if(vis[i][j])tx++;
        }
        if(!tx)
            sum++;
    }
    cout<<sum<<endl;
    return 0;
}


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

using namespace std;
const int MAXN = 3e5+5;
pair<pair<int,int>,int> q[MAXN];
int ans[MAXN];
int main(){
    int X,k;
    cin>>X>>k;
    for(int i=1;i<=k;i++){
        cin>>q[i].first.first;
        q[i].second = -1;
    }
    int Q;
    cin>>Q;
    for(int i=1;i<=Q;i++){
        cin>>q[k+i].first.first>>q[k+i].first.second;
        q[k+i].second = i;
    }
    sort(q+1,q+k+Q+1);
    int l = 0;
    int r = X;
    int a = 0;
    int b = X;
    int sign = -1;
    int prev = 0;
    for(int i=1;i<=k+Q;i++){
        //cout<<1234<<" "<<q[i].first.first<<" "<<q[i].first.second<<endl;
        if(q[i].second != -1){
            //query
            int diff = q[i].first.first - prev;
            int res;
           
            if(q[i].first.second<=l){
                res = a;
            }else if(q[i].first.second>=r){
                res = b;
            }else{
                res=a+q[i].first.second-l;
                //cout<<123<<" "<<res<<endl;
                //cout<<q[i].first.second<<endl;
               
            }
           
             res+=diff*sign;
            if(res>X){
                res = X;
            }
            if(res< 0){
                res = 0;
            }
            
             
            ans[q[i].second] = res;
            
        }else{
            int diff = q[i].first.first - prev;
            if(sign == -1){
                if(a-diff<0){
                    l=min(X,l+diff-a);
                    a=0;
                    b-=diff;
                    b=max(b,0);
                    r=max(l,r);
                    
                }else{
                    a-=diff;
                    b-=diff;
                }
            }else{
                if(b+diff>X){
                    r=max(0,r-diff+X-b);
                    a=min(X,a+diff);
                    l=min(l,r);
                    b=X;
                }else{
                    a+=diff;
                    b+=diff;
                }
            }
            sign*=-1;
            prev = q[i].first.first;
        }
    }
    for(int i=1;i<=Q;i++){
        cout<<ans[i]<<" ";
    }
    
}

Measured memory_bytes: 4480
#include<bits/stdc++.h>
#include <utility>
using namespace std;
typedef long long ll;
string s,t;
int s_size;
int t_size;

int main(){
	cin >> s;
	cin >> t;
	s_size = s.size();
	t_size = t.size();
	if (s_size < t_size){
		cout << "UNRESTORABLE" << endl;
        return 0;
	}
	int start_point = -1;
	for (int i = s_size + 1 - t_size; i >= 0 ;i--){
		bool flag = true;
		for (int j=0;j<t_size;j++){
			if (s[i+j] == '?') continue;
			else if (s[i+j] != t[j]) {
				flag = false;
				break;
			}
		}
		if (flag==true){
          start_point = i;
          break;
        }
	}
	if (start_point == -1){
		cout << "UNRESTORABLE" << endl;
		return 0;
    }
	for (int i = 0;i < t_size;i++){
		s[start_point + i] = t[i];
	}
	for (int i = 0;i < s_size;i++){
		if (s[i] == '?') s[i] = 'a';
	}
	cout << s << endl;
}

Measured memory_bytes: 256
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <iomanip>
#include <complex>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f3f3f3f3fLL
#define SIZE 100005

using namespace std;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
typedef pair<int,ll> pill; typedef pair<ll,int> plli; 
typedef pair<double, int> pdi;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }
typedef complex<double> P;
long long int MOD = 1000000007;
int K;
ll A[SIZE];
int main(){
	int K;
	cin>>K;
	
	for(int i=0;i<K;i++){
		cin>>A[K-i-1];
	}
	ll mians=2;
	ll maans=2;
	bool flag=true;
	for(int i=0;i<K;i++){
		ll x=A[i];
		mians=((mians+x-1)/x)*x;
		maans=(maans/x)*x;
		if(mians>maans)flag=false;
		else maans=maans+x-1;
	}
	if(flag)cout<<mians<<" "<<maans<<endl;
	else cout<<-1<<endl;
}


Measured memory_bytes: 1024
import java.util.*;

public class Main{
  public static void main(String[]args){
    Scanner sc = new Scanner(System.in);
    int[] n = new int[4];
    for(int i = 0; i < 6; i++){
      int m = sc.nextInt();
      n[m-1]++;
    }
    
    String ans = "YES";
    if(n[0] == 3 || n[1] == 3 || n[2] == 3 || n[3] == 3){
      ans = "NO";
    }
    
    System.out.println(ans);
  }
}

Measured memory_bytes: 25172
#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstdlib>
#include <list>
#include <numeric>
#include <map>	
#include <cmath>
#include <string>
#define rep(i,n) for(int i=0;i<n;i++)
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define Cout(s) cout<<s<<endl
using namespace std;
typedef long long ll;
string Yes(bool b) { if (b) { return "Yes"; } else { return "No"; } }
string YES(bool b) { if (b) { return "YES"; } else { return "NO"; } }
//================================================
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }		

int a = 0, b = 0, c, d, n, k = 0, ans = 0;
string s, t;

int main()
{
	ll n, k; cin >> n >> k;
	if (n % k == 0)
	{
		cout << 0 << endl; return 0;
	}
	if (n > k)
		n = n % k;
	cout << min(n, k - n) << endl;


	return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define P pair<int,int>
using ll=int64_t;
using namespace std;
#define ketasuu(n) fixed<<setprecision(n)
#define btoe(p) p.begin(),p.end()
#define etob(p) p.rbegin(),p.rend()




int main(){
    string t; cin>>t;
    int n=t.length();
    rep(i,n){
        if(t[i]=='?') t[i]='D';
    }
    cout<<t<<endl;
    return 0;
}

Measured memory_bytes: 3676
#include <iostream>
#include <string>

using namespace std;

int main() {
	int ans = 0;
	string s;
	cin >> s;
	for (int i = 0; i<s.size(); i++) {
		if (i % 2 == 0 && s[i] == 'p')ans--;
		else if (i % 2 == 1 && s[i] == 'g')ans++;
	}
	cout << ans << endl;
	return 0;
}
//g,pを交互に出す

Measured memory_bytes: 512
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include<cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include<iostream>
#include<queue>
#include<set>
#include<string>
#include <vector>

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

using namespace std;

int main(){
	int n, q, S[100001], T[50001], ans = 0;
	scanf("%d", &n);
	FOR(i, 1, n + 1){
		scanf(" %d", &S[i]);
	}
	scanf("%d", &q);
	FOR(i, 1, q + 1){
		scanf(" %d", &T[i]);
	}
	int left = 1, right = n;
	FOR(i, 1, q + 1){
		int t = 0;
		left = 1, right = n;
		while (t <= log2(n) + 1){
			if (T[i] == S[0] || T[i] == S[n]){
				ans++;
				break;
			}
			else if (T[i] < S[(left + right) / 2]){
				right = (right + left) / 2;
			}
			else if (T[i]>S[(left + right) / 2]){
				left = (right + left) / 2;
			}
			else if (T[i] == S[(left + right) / 2]){
				ans++;
				break;
			}
			t++;
		}
	}

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


	return 0;
}


Measured memory_bytes: 3892
// Author : Rifayat Samee (Sanzee)
// Problem :
// Algorithm:

/*

*/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxval = 200005;
ll tree[4*maxval+5];
void make_tree(int i,int low,int high,const vector<int>&A){
    if(low == high){
        tree[i] = 0;
        return;
    }
    int mid = (low + high) / 2;
    make_tree(2*i,low,mid,A);
    make_tree(2*i+1,mid+1,high,A);
    tree[i] = max(tree[2*i],tree[2*i+1]);
}
void update(int i,int low,int high,ll val,ll h,const vector<int>&A){
    if(low == high){
        tree[i] = val;
        return;
    }
    int mid = (low + high)/2;
    if(h <=mid) update(2*i,low,mid,val,h,A);
    else
        update(2*i+1,mid+1,high,val,h,A);
    tree[i] = max(tree[2*i],tree[2*i+1]);
}
ll query(int i,int low,int high,int a,int b){
    if(high < a || low > b) return 0;
    if(low>=a && high <=b){
        return tree[i];
    }
    int mid = (low + high)/2;
    return max(query(2*i,low,mid,a,b),query(2*i+1,mid+1,high,a,b));
}
int main(){

	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
    int n;
    scanf("%d",&n);
    vector<int>H(n+1);
    vector<int>A(n+1);
    int maxh = 0;
    for(int i=0;i<n;i++){
        scanf("%d",&H[i]);
        maxh = max(maxh,H[i]);
    }
    for(int i=0;i<n;i++){
        scanf("%d",&A[i]);
    }
    make_tree(1,0,maxh,A);
    ll res = 0;
    for(int i=0;i<n;i++){
        ll h = H[i];
        ll val = query(1,0,maxh,0,h-1);
        res = max(res,A[i]+val);
        update(1,0,maxh,val+A[i],h,A);
    }
    printf("%lld\n",res);
	return 0;
}


Measured memory_bytes: 7936
//Problem: ARC067 C
//Progress: Working
#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <cstring>
#define MOD 1000000007
typedef long long int ll;
using namespace std;

const ll maxN = 1e3 + 11;

vector<ll> vlp;
bool is_prime[maxN];
map<ll, ll> spf;

map<ll, ll> mpl;

vector<ll> vans;

void sieve(){
    memset(is_prime, true, sizeof(is_prime));
    is_prime[0] = is_prime[1] = false;
    for(ll i=2; i<maxN; i++){
        if(is_prime[i]){
            vlp.push_back(i);
            spf[i] = i;
        }
        for(ll j=0; j<(ll)vlp.size() && i*vlp[j] <= maxN && vlp[j] <= spf[i]; j++){
            is_prime[i*vlp[j]] = false;
            spf[i*vlp[j]] = vlp[j];
        }
    }
}

void solve(ll n){
    for(ll i=2; i<=n; i++){
        ll cop_i = i, j=0;
        while(cop_i != 1){
            while(cop_i%vlp[j] == 0){
                cop_i /= vlp[j];
                mpl[vlp[j]] += 1;
            }
            j++;
        }
    }
    ll anss = 1;
    for(map<ll, ll>::iterator it = mpl.begin(); it != mpl.end(); ++it)
        anss = (anss*(it->second + 1))%MOD;
    cout << anss;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    sieve();
    ll t;
    cin >> t;
    solve(t);
    return 0;
}

Measured memory_bytes: 384
// https://atcoder.jp/contests/abc140/tasks/abc140_e

//   4  1  5  2  3
// 4 -  14 45 45 45
// 1 -  -  15 25 35
// 5 -  -  -  25 35
// 2 -  -  -  -  23
// 3 -  -  -  -  -
//
//
//   7  8  6  2  1  4  3  5  9
// 7 -  78 78 78 78 78 78 78 89
// 8 -  -  68 68 68 68 68 68 89
// 6 -  -  -  26 26 46 46 56 69
// 2 -  -  -  -  12 24 34 45 59
// 1 -  -  -  -  -  14 34 45 59
// 4 -  -  -  -  -  -  34 45 59
// 3 -  -  -  -  -  -  -  35 59
// 5 -  -  -  -  -  -  -  -  59
// 9 -  -  -  -  -  -  -  -  -


#include <cstdio>
#include <functional>
#include <algorithm>
#include <set>

using namespace std;

#define REP(i,n)   for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for(int i=(b); i<=(int)(e); i++)
#define ITR(c,it)  for(auto it = c.begin(); it != c.end(); it++)
#define DUMP(a, n) REP(i, n) printf("%d%c", a[i], i + 1 == n ? '\n' : ' ')
#define DUMP2D(a, n, m) REP(i, n) REP(j, m) printf("%d%c", a[i][j], j + 1 == m ? '\n' : ' '); puts("")

const int N_MAX = 1e5;

typedef long long ll;
typedef pair<int, int> PAIR;

int N;
int P[N_MAX];

int L1[N_MAX];
int L2[N_MAX];
int R1[N_MAX];
int R2[N_MAX];

set<PAIR, greater<PAIR>> rst, lst;

void solve() {
  fill(R1, R1 + N, N);
  fill(R2, R2 + N, N);
  fill(L1, L1 + N, -1);
  fill(L2, L2 + N, -1);
  REP(i, N) {
    PAIR p = make_pair(P[i], i);
    auto it = rst.lower_bound(p);
    while (it != rst.end()) {
      int k = it->second;
      if (R2[k] == N) {
        if (R1[k] == N) R1[k] = i;
        else R2[k] = i;
        it++;
      } else {
        it = rst.erase(it);
      }
    }
    rst.insert(p);
  }
  for (int i = N - 1; i >= 0; i--) {
    PAIR p = make_pair(P[i], i);
    auto it = lst.lower_bound(p);
    while (it != lst.end()) {
      int k = it->second;
      if (L2[k] < 0) {
        if (L1[k] < 0) L1[k] = i;
        else L2[k] = i;
        it++;
      } else {
        it = lst.erase(it);
      }
    }
    lst.insert(p);
  }
  ll ans = 0;
  REP(i, N) {
    ll tmp = 0;
    tmp += (i - L1[i]) * (R2[i] - R1[i]);
    tmp += (L1[i] -L2[i]) * (R1[i] - i);
    // printf("%d %d %d %d %d => %lld\n", L2[i], L1[i], i, R1[i], R2[i], tmp);
    ans += tmp * P[i];
  }
  printf("%lld\n", ans);
}

void input() {
  scanf("%d", &N);
  REP(i, N) scanf("%d", P + i);
}

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


Measured memory_bytes: 6784
N = int(input())
L = list(map(int, input().split()))
L.sort()
A = 0
if N > 2:
    for i in range(N-2):
        for j in range(N-2-i):
            for k in range(i+1, N-1-j):
                if L[N-j-1]-L[i]<L[k]<L[i]+L[N-j-1] and L[N-j-1] != L[k] and L[k]!=L[i]:
                    A +=1
print(A)

Measured memory_bytes: 9152
'use strict'

function Main(input) {
    let S = input.trim()
    
    let p010=0;
    let p101=0;

    let checkbit = '0';
    for(let i=0,l=S.length;i<l;i++){
        if(S[i]===checkbit){
            p101++;
        } else{
            p010++;
        }
        checkbit = checkbit==='0' ? '1':'0';
    }
    console.log(Math.min(p010,p101));
}
//*この行以降は編集しないでください（標準入出力から一度に読み込み、Mainを呼び出します）
Main(require("fs").readFileSync("/dev/stdin", "utf8"));


Measured memory_bytes: 7500
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=int(a);i<int(b);i++)
typedef vector<int> vi;
typedef long long ll;



int main(){
    
    string s;cin>>s;
    int N=s.size(),a=0,b=0,c=0;
    if(N==1){
        cout<<"YES"<<endl;
        return 0;
    }
    rep(i,0,N){
        if(s.at(i)=='a')a++;
        else if(s.at(i)=='b')b++;
        else if(s.at(i)=='c')c++;
    }
    int MIN=9999999,MAX=-1;
    MIN=min(min(a,b),c);
    MAX=max(max(a,b),c);
    if(MAX-MIN<=1)cout<<"YES"<<endl;
    else cout<<"NO"<<endl;

    
}






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

#define rep(i,beg,end)   for (auto i=(beg);i<(end);i++)
#define rep2(name,container) for(auto name=begin(container); name != end(container);name++)
#define all(cont)           begin(cont), end(cont)
#define rall(cont)           rbegin(cont), rend(cont)
using ll = long long;
using vi = vector<ll>;
const ll INFMAX = LLONG_MAX;
const ll INFMIN = LLONG_MIN;
const ll MOD = 1000000007;

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

	ll n;
	cin >> n;
	vi d;
	rep(i, 0, n) {
		ll t;
		cin >> t;
		if (t > 0) d.push_back(t);
	}
	if (d.empty()) d.push_back(0);

	const ll SENTINEL_D = 101;
	ll dp[SENTINEL_D][SENTINEL_D] = { 0, };
	ll sum = 0;
	rep(i, 0, d.size() - 1) {
		ll x = d[i];
		rep(j, i + 1, d.size()) {
			ll y = d[j];
			if (dp[x][y])
				sum += dp[x][y];
			else
				sum += dp[x][y] = x * y;
		}
	}
	cout << sum << endl;

	return 0;
}


Measured memory_bytes: 384
#include <iostream>
#include <string>
#include <locale>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int main(){
  int a,b,c,d,e;
  cin >> a >> b >> c >> d >> e;
  if (a==0){
    cout << 1 << endl;
  }else if (b==0){
    cout << 2 << endl;
  }else if (c==0){
    cout << 3 << endl;
  }else if (d==0){
    cout << 4 << endl;
  }else if (e==0){
    cout << 5 << endl;
  }
  }
  

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

const int N = 105;
int n, m;
bool white[N][N];
int dp[N][N][2];

int solve(int i, int j, bool inv) {
    if(i > n || j > m) return 1e9;
    if(i == n && j == m) return (white[i][j] ? 0 : 1);
    if(dp[i][j][inv] != -1) return dp[i][j][inv];

    int goDown = 1e9, goRight = 1e9;
    if(i > n)
        goDown = 1e9;
    else if(white[i+1][j])
        goDown = (inv ? 1 : 0) + solve(i+1, j, 0);
    else
        goDown = solve(i+1, j, 1);

    if(j > m)
        goRight = 1e9;
    else if(white[i][j+1])
        goRight = (inv ? 1 : 0) + solve(i, j+1, 0);
    else
        goRight = solve(i, j+1, 1);

    return dp[i][j][inv] = min(goDown, goRight);
}

int main() {
    cin >> n >> m;
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= m; j++) {
            char c; cin >> c;
            if(c == '.') white[i][j] = 1;
            else white[i][j] = 0;
        }
    }

    memset(dp, -1, sizeof dp);
    int ans = solve(1, 1, !white[1][1]);
    cout << ans << '\n';

    return 0;
}


Measured memory_bytes: 384
#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

map<long long, int> factorization(long long N) {
	map<long long, int> re;
	while (true) {
		if (N % 2 == 0) {
			N /= 2;
			re[2]++;
			if (N == 1) break;
		}
		else {
			break;
		}
	}
	for (int loop = 3; (long long)loop * (long long)loop <= N; loop += 2) {
		if (N % loop == 0) {
			N /= loop;
			re[loop]++;
			loop -= 2;
			if (N == 1) break;
		}
	}
	if (N > 1) re[N]++;
	return re;
}

struct Memo {
	long long fact = 0;
	long long fact_inv = 0;
	long long inv = 0;
};
class Combination1 {
private:
	int m_module = 1000000007;
	vector<struct Memo> memo;
public:
	int calculate(int, int);
	Combination1(int, int);
};
int Combination1::calculate(int n, int k) {
	return ((memo[n].fact * memo[k].fact_inv) % m_module * memo[n - k].fact_inv) % m_module;
}
Combination1::Combination1(int memo_max, int module = 1000000007) {
	m_module = module;
	memo.push_back({ 1,1,0 });
	memo.push_back({ 1,1,1 });
	for (int loop = 2; loop <= memo_max; loop++) {
		struct Memo temp;
		temp.fact = (memo[loop - 1].fact * loop) % m_module;
		temp.inv = m_module - (memo[m_module % loop].inv * int(m_module / loop)) % m_module;
		temp.fact_inv = (memo[loop - 1].fact_inv * temp.inv) % m_module;
		memo.push_back(temp);
	}
}

class Combination2 {
private:
	int size = 0;
	bool add(int);
public:
	vector<int> sequence;
	bool next_combination(void);
	Combination2(int, int);
};
bool Combination2::add(int index) {
	if (index == (int)sequence.size() - 1 && sequence[index] == size - 1
		|| index < (int)sequence.size() - 1 && sequence[index] == sequence[index + 1] - 1) {
		if (index == 0) {
			return false;
		}
		else {
			bool re = add(index - 1);
			sequence[index] = sequence[index - 1] + 1;
			return re;
		}
	}
	else {
		sequence[index]++;
		return true;
	}
}
bool Combination2::next_combination(void) {
	return (sequence.size() == 0) ? false : add(sequence.size() - 1);
}
Combination2::Combination2(int n, int r) {
	size = n;
	for (int loop = 0; loop < r; loop++) sequence.push_back(loop);
	return;
}

void A_Password(void) {

	int N; cin >> N;

	cout << N * N * N << flush;
	return;
}

void B_Buffet(void) {

	int N; cin >> N;
	vector<int> A(N); for (int& it : A)cin >> it;
	int sum = 0;
	for (int loop = 0; loop < N; loop++) {
		int B; cin >> B;
		sum += B;
	}
	vector<int> C(N - 1); for (int& it : C)cin >> it;
	
	for (int index = 0; index < N - 1; index++) {
		if (A[index] + 1 == A[index + 1]) sum += C[A[index] - 1];
	}

	cout << sum << flush;
	return;
}

int main(void) {

	//A_Password();
	B_Buffet();
	return 0;
}


Measured memory_bytes: 256