#include<algorithm>
#include<complex>
#include<ctype.h>
#include<iomanip>
#include<iostream>
#include<map>
#include<math.h>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<stdio.h>
#include<string>
#include<string>
#include<vector>

using namespace std;
typedef long long ll;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define ALL(v) (v).begin(), (v).end()
#define p(s) cout<<(s)<<endl
#define p2(s, t) cout << (s) << " " << (t) << endl
#define pn(s) cout << (#s) << " " << (s) << endl

const ll mod = 1e9 + 7;
const ll inf = 1e18;

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

    // input
    ll N, Y;
    cin >> N >> Y;

    FOR(i, 0, N+1){
        FOR(j, 0, N-i+1){
            ll k = N - i - j;
            if(Y == 10000LL*i + 5000LL*j + 1000LL*k){
                cout << i << " " << j << " " << k << endl;
                return 0;
            }
        }
    }

    p("-1 -1 -1");

    return 0;
}


Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 300 points

Problem Statement

The commonly used bills in Japan are 10000-yen, 5000-yen and 1000-yen bills. Below, the word "bill" refers to only these.

According to Aohashi, he received an otoshidama (New Year money gift) envelope from his grandfather that contained N bills for a total of Y yen, but he may be lying. Determine whether such a situation is possible, and if it is, find a possible set of bills contained in the envelope. Assume that his grandfather is rich enough, and the envelope was large enough.

Constraints

1 ≤ N ≤ 2000

1000 ≤ Y ≤ 2 × 10^7

N is an integer.

Y is a multiple of 1000.

Input

Input is given from Standard Input in the following format:

N Y

Output

If the total value of N bills cannot be Y yen, print -1 -1 -1.

If the total value of N bills can be Y yen, let one such set of bills be "x 10000-yen bills, y 5000-yen bills and z 1000-yen bills", and print x, y, z with spaces in between. If there are multiple possibilities, any of them may be printed.

Sample Input 1

9 45000

Sample Output 1

4 0 5

If the envelope contained 4 10000-yen bills and 5 1000-yen bills, he had 9 bills and 45000 yen in total. It is also possible that the envelope contained 9 5000-yen bills, so the output 0 9 0 is also correct.

Sample Input 2

20 196000

Sample Output 2

-1 -1 -1

When the envelope contained 20 bills in total, the total value would be 200000 yen if all the bills were 10000-yen bills, and would be at most 195000 yen otherwise, so it would never be 196000 yen.

Sample Input 3

1000 1234000

Sample Output 3

14 27 959

There are also many other possibilities.

Sample Input 4

2000 20000000

Sample Output 4

2000 0 0

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


#include <stdio.h>
int main(){
    int n, Y, x = 0, y, z;
    scanf("%d %d", &n, &Y);
    Y /= 1000;
    while(true){
        for(y = 0; y <= n - x; y++){
            z = n - x - y;
            if(10*x + 5*y + z == Y){
                printf("%d %d %d", x, y, z);
                return 0;
            }
        }
        if(x == n) break;
        else x++;
    }

    printf("-1 -1 -1");
    return 0;
}


#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <cfloat>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
//#include <random>
//#include <unordered_set>
#include <complex>
using namespace std;
int main()
{
    long long n,y,i,j,k;
    cin>>n>>y;
    for(i=0;i<=n;i++)
    {
        if((i*10000)>y) break;
        if((i*10000+(n-i)*5000)<y) continue;
        for(j=0;j<=n-i;j++)
        {
            if(((i*10000)+(j*5000))>y) break;
            for(k=0;k<=n-i-j;k++)
            {
                if(((i*10000)+(j*5000)+(k*1000))==y)
                {
                    cout<<i<<" "<<j<<" "<<k;
                    system ("pause");
                    return 0;
                }
                if(((i*10000)+(j*5000)+(k*1000))>y) break;
            }
        }
    }
    cout<<-1<<" "<<-1<<" "<<-1;
    system ("pause");
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define all(a) (a).begin(),(a).end()

using namespace std;

int main(int argc, char const *argv[]) {
    int N,Y;
    int a=0,b=0;
    int amari=0;
    bool cantflag=false;
    scanf("%d%d",&N,&Y);
    Y/=1000;
    const int right=Y-N;
    if(right>=9){
        a=(int)right/9;
    }
    if(right-(a*9)>=4){
        b=(int)((right-a)/4);
    }
    amari=right-(a*9)-(b*4);
    //ここでYはもう使わない
    //aが10000円、bが5000円、cが1000円の枚数

    while (1) {
        if(amari==0){
            printf("%d %d %d\n",a,b,N-(a+b));
            return 0;
        }else if(cantflag){
            printf("-1 -1 -1\n");
            return 0;
        }
        if (a>0) {
            --a;
            b+=2;
            ++amari;
            if(amari==4){
                amari-=4;
                ++b;
            }
        }else{
            cantflag=true;
        }
    }
    return 0;
}


#include <iostream>
#include <string>
#include <algorithm>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <iomanip>
#include <bitset>
#include <set>
#include <map>
#include <stdio.h>
#include <numeric>
#include <cstring>

#define rep(i,n) for (int i = 0; i < (n); i++)
#define rep1(i,n) for (int i = 1; i < (n); i++)
#define FOR(i,a,b) for (int i=(a); i < (b); i++)
#define MOD 1000000007 //10^9+7
#define ALL(a)  (a).begin(),(a).end()
using namespace std;
using ll = long long;
using PII = pair<int, int>;
using PLL = pair<long long, long long>;
const int INF = numeric_limits<int>::max();
const ll INFL = numeric_limits<ll>::max();
constexpr ll TEN(int n) { return (n==0) ? 1 : 10*TEN(n-1); }

// 負の数にも対応した % 演算
long long mod(long long val, long long m) {
    long long res = val % m;
    if (res < 0) res += m;
    return res;
}

//greatest common divisor
long long gcd(ll a, ll b)
{
    if (a % b == 0) {
        return b;
    } else {
        return gcd(b, a % b);
    }
}

//least common multiple
long long lcm(ll a, ll b)
{
    return a / gcd(a, b) * b ;
}

ll factorial(ll n) {
    ll res = 1;
    for (ll i = 1; i <= n; i++) {
        res *= i;
    }
    return res;
}

bool fn_is_prime(ll n) {
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return n != 1;
}

bool isOK(int index, int key, vector<ll>& a) {
    if (a[index] >= key) return true;
    else return false;
}

int binary_search(int key, vector<ll>& a) {
    int ng = -1;
    int ok = a.size();

    while (abs(ok - ng) > 1) {
        int mid = (ok + ng) / 2;

        if (isOK(mid, key, a)) ok = mid;
        else ng = mid;
    }
    return ok;
}

struct UnionFind {
    vector<int> r;

    UnionFind(int N) {
        r = vector<int>(N, -1);
    }

    int root(int x) {
        if (r[x] < 0) return x;
        return r[x] = root(r[x]);
    }

    bool unite(int x, int y) {
        x = root(x);
        y = root(y);
        if (x == y) return false;
        if (r[x] > r[y]) swap(x, y);
        r[x] += r[y];
        r[y] = x;
        return true;
    }

    int size(int x) {
        return -r[root(x)];
    }

    bool same(int x, int y) {
        return root(x) == root(y);
    }
};

//素因数分解
vector<pair<long long, long long> > prime_factorize(long long N) {
    vector<pair<long long, long long> > res;
    for (long long a = 2; a * a <= N; ++a) {
        if (N % a != 0) continue;
        long long ex = 0;
        while (N % a == 0) {
            ++ex;
            N /= a;
        }
        res.push_back({a, ex});
    }
    if (N != 1) res.push_back({N, 1});
    return res;
}
//const auto &pf = prime_factorize(i);
//for (auto p : pf) cout << p.first << " " << p.second;

int main()
{
    ll n, h; cin >>n >> h;
    vector<PLL> k(n);
    ll amax = 0;
    rep(i,n) {
        ll a, b; cin >> a >> b;
        k[i] = PLL{b, a};
        amax = max(amax, a);
    }

    sort(k.begin(), k.end());
    reverse(ALL(k));
    ll ans = 0;
    int index = 0;
    rep(i,n) {
        if (k[i].first < amax)
            break;
        h -= k[i].first;
        ans++;
        if (h <= 0) {
            cout << ans << endl;
            return 0;
        }
    }
    if (h % amax == 0)
        ans += h/amax;
    else
        ans += h/amax + 1;
    cout << ans << endl;

    return 0;
}

Score : 400 points

Problem Statement

You are going out for a walk, when you suddenly encounter a monster. Fortunately, you have N katana (swords), Katana 1, Katana 2, …, Katana N, and can perform the following two kinds of attacks in any order:

Wield one of the katana you have. When you wield Katana i (1 ≤ i ≤ N), the monster receives a_i points of damage. The same katana can be wielded any number of times.

Throw one of the katana you have. When you throw Katana i (1 ≤ i ≤ N) at the monster, it receives b_i points of damage, and you lose the katana. That is, you can no longer wield or throw that katana.

The monster will vanish when the total damage it has received is H points or more. At least how many attacks do you need in order to vanish it in total?

Constraints

1 ≤ N ≤ 10^5

1 ≤ H ≤ 10^9

1 ≤ a_i ≤ b_i ≤ 10^9

All input values are integers.

Input

Input is given from Standard Input in the following format:

N H
a_1 b_1
:
a_N b_N

Output

Print the minimum total number of attacks required to vanish the monster.

Sample Input 1

1 10
3 5

Sample Output 1

3

You have one katana. Wielding it deals 3 points of damage, and throwing it deals 5 points of damage. By wielding it twice and then throwing it, you will deal 3 + 3 + 5 = 11 points of damage in a total of three attacks, vanishing the monster.

Sample Input 2

2 10
3 5
2 6

Sample Output 2

2

In addition to the katana above, you also have another katana. Wielding it deals 2 points of damage, and throwing it deals 6 points of damage. By throwing both katana, you will deal 5 + 6 = 11 points of damage in two attacks, vanishing the monster.

Sample Input 3

4 1000000000
1 1
1 10000000
1 30000000
1 99999999

Sample Output 3

860000004

Sample Input 4

5 500
35 44
28 83
46 62
31 79
40 43

Sample Output 4

9


#include <bits/stdc++.h>

using namespace std;

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

int main() {
  int N, H;
  cin >> N >> H;
  vector<int> A(N), B(N);
  int max_a = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    chmax(max_a, A[i]);
  }
  sort(B.rbegin(), B.rend());
  vector<long long> BS(N + 1);
  for (int i = 0; i < N; i++) {
    BS[i + 1] = BS[i] + B[i];
  }
  long long ans = 1e15;
  for (int i = 1; i <= N; i++) {
    long long a = i + (H - BS[i] + max_a - 1) / max_a;
    if (BS[i] >= H) {
      a = i;
    }
    chmin(ans, a);
  }
  cout << ans << endl;

  return 0;
}
/* vim:set fdm=marker: */


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
typedef pair<int,int>pi;
#define ff first
#define ss second
#define inf 1000000000
#define IOS ios_base::sync_with_stdio(0)
#define bitseT(y,pos) (1<<pos)|y
#define bitchecK(y,pos) ((1<<pos)&y)
#define meM(y,a) memset(y,a,sizeof y)
#define sC(a) scanf("%d",&a)
#define alL(a) a.begin(),a.end()
#define prinT(a,sz)  cout<<a[0];for(int i=1;i<sz;i++)cout<<" "<<a[i];cout<<endl
int main()
{
    IOS;
    map<int,int>mp;
    multiset<int,greater<int > >st;
    int n,k;cin>>n>>k;
    for(int i=0;i<n;i++)
    {
        int a,b;cin>>a>>b;
        st.insert(a);
        st.insert(b);
        mp[a]=1;
    }
    int ans=0;
    int sum=0;
    for(auto it=st.begin();it!=st.end();it++){
        int val=*it;
//        cout<<"val = "<<val<<endl;
        if(mp[val]==1){
            ans+=ceil((1.0*k-sum)/val);
            break;
        }
        sum+=val;
        ans++;
        if(sum>=k)break;
    }
    cout<<ans<<endl;
}

/*
100 150
50 80
40 70

*/


#include <stdio.h>
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <tuple>

typedef std::tuple<long long, long long, long long> tri;

int main() {
    long long N, H, ai, bi;
    std::vector<tri> input_data;
    scanf("%lld %lld", &N, &H);
    for (long long i = 0; i < N; ++i) {
        scanf("%lld %lld", &ai, &bi);
        input_data.push_back(std::make_tuple(i, ai, bi));
    }

    std::sort(
            input_data.begin(),
            input_data.end(),
            [](tri const &lhs, tri const &rhs) {
                if (std::get<1>(lhs) != std::get<1>(rhs)) return std::get<1>(lhs) > std::get<1>(rhs);
                if (std::get<2>(lhs) != std::get<2>(rhs)) return std::get<2>(lhs) < std::get<2>(rhs);
                return std::get<0>(lhs) > std::get<0>(rhs);
            }
    );

    long long i_max = std::get<0>(input_data[0]);
    long long a_max = std::get<1>(input_data[0]);
    long long b_max = std::get<2>(input_data[0]);

    std::sort(
            input_data.begin(),
            input_data.end(),
            [](tri const &lhs, tri const &rhs) {
                if (std::get<2>(lhs) != std::get<2>(rhs)) return std::get<2>(lhs) > std::get<2>(rhs);
                if (std::get<1>(lhs) != std::get<1>(rhs)) return std::get<1>(lhs) > std::get<1>(rhs);
                return std::get<0>(lhs) > std::get<0>(rhs);
            }
    );

    long long i = 0;
    long long ans = 0;
    bool flag = false;
    while (H > 0) {
        if (std::get<2>(input_data[i]) <= a_max || i == N) {
            if (flag) {
                if (H - b_max <= 0) {
                    ans += 1;
                } else {
                    ans += ((H - b_max) / a_max + ((H - b_max) % a_max > 0) + 1);
                }
            } else {
                ans += (H / a_max + (H % a_max > 0));
            }
            H = -1;
        } else {
            if (i_max == std::get<0>(input_data[i])) {
                flag = true;
                i++;
            } else {
                H -= std::get<2>(input_data[i]);
                i++;
                ans++;
            }
        }
    }

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

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool cmp(int a,int b){
	return a>b;
}

int main(){
	int n,h,i,move=0,mx=-1,s=0;
	cin>>n>>h;
	int a[n],b[n];
	for(i=0;i<n;i++){
		cin>>a[i]>>b[i];
		if (mx<a[i]) mx=a[i];
	}
	sort(b,b+n,cmp);
	for(i=0;i<n;i++){
		if (h<=0){
			cout<<i;
			return 0;
		}
		if(mx>=b[i]) break;
		h-=b[i];
	}
	if(h>0) move=(h-1)/mx+1;
	cout<<move+i;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#  define DBG(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

#define MOD 1000000007
#define INF 1000000000
#define MAX_N 100010

ll N, H;
vector<ll> A, B;

int main()
{
    cin >> N >> H;
    REP(i, N) {
        ll a, b;
        cin >> a >> b;
        A.push_back(a);
        B.push_back(b);
    }
    sort(A.begin(), A.end(), greater<ll>());
    sort(B.begin(), B.end(), greater<ll>());
    ll ans = 0;
    ll b_i = 0;
    while(H > 0) {
        if(b_i < N && B[b_i] > A[0]) {
            H -= B[b_i];
            b_i++;
        } else {
            if(H % A[0] == 0) { ans += H / A[0]; }
            else { ans += H / A[0] + 1; }
            break;
        }
        ans++;
    }
    printf("%lld\n", ans);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
bool cmp(int a,int b) {
	return a>b;
}
int a[100001];
int b[100001];
int main() {
	int num,health;
	scanf("%d%d",&num,&health);
	for(int i=0; i<num; i++)
		scanf("%d%d",&a[i],&b[i]);
	sort(a,a+num,cmp);
	sort(b,b+num,cmp);
	int jb=-1;
	for(int v=0; v<num; v++) {
		if(a[0]>b[v])break;
		else jb=v;
	}
	int re=0,cur=0;
	while(jb>=cur) {
		health-=b[cur];
		cur++;
		re++;
		if(health<=0) {
			printf("%d\n",re);
			return 0;
		}
	}
	int ex=health%a[0]==0? 0:1;
	int ans=health/a[0]+ex;
	printf("%d\n",re+ans);
	return 0;
}


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

using vi = vector<int>;
using vvi = vector<vi>;

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

    int n, h; cin >> n >> h;

    vvi v(n, vi(2));

    int mx = 0;

    for(auto& e : v) cin >> e[1] >> e[0], mx = max(mx, e[1]);

    sort(begin(v), end(v));

    int ans = 0;

    for(int i = n - 1; i >= 0 && v[i][0] >= mx && h > 0; i--)
        ans++, h -= v[i][0];

    h = max(0, h);

    ans += h / mx + !!(h % mx);

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


#include <bits/stdc++.h>

using namespace std;

int main(){
	int n,h;
	cin >> n >> h;
	vector<int> a(n);
	vector<int> b(n);
	for(int i=0;i<n;i++){
		cin >> a[i] >> b[i];
	}
	sort(a.begin(),a.end());
	sort(b.begin(),b.end());
	int ans=0;
	int damage = 0;
	int i=n-1;
	while(damage < h){
		if(i >= 0 && a[n-1] < b[i]){
			damage += b[i];
			i--;
			ans++;
		} else {
			ans += (h-damage)/a[n-1] + ((h-damage)%a[n-1] != 0);
			break;
		}
	}
	cout << ans << endl;
	return 0;
}


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

int n,h,s,ans,ans2;
pair<int,int> arr[100005],arr2[100005];

int main() {
  scanf("%d%d",&n,&h);
  for(int i=0;i<n;i++){
    int a,b;
    scanf("%d%d",&a,&b);
    arr[i]=make_pair(a,b);
    arr2[i]=make_pair(b,a);
  }
  sort(arr2,arr2+n);
  reverse(arr2,arr2+n);
    sort(arr,arr+n);
  reverse(arr,arr+n);
  long long cur=0;
  for(int i=0;i<n;i++){
    if(arr2[i].first<arr[0].first) break;
    if(cur>=h){
      ans=i+1;
      break;
    }
    cur+=arr2[i].first;
  }
  cur = 0;
  for(int i=1;i<n;i++) if(arr[i].second>arr[0].first){ cur+=arr[i].second;ans2++;}
  cur+=arr[0].second;
  ans2++;
  //printf("%d",cur);
  ans2+=(h-cur+arr[0].first-1)/arr[0].first;
  printf("%d",min(ans,ans2));
}


#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mod %
using namespace std;
using ll = long long;

int main(){
    int ans = 0;
    int n, a, b; cin >> n >> a >> b;

    for(int i = 1; i <= n; i++){
        int tmp = 0, j = i;
        /*
        for(int j = i; j > 0; j /= 10){
            tmp += j % 10;
        }*/

        tmp += j % 10;
        j /= 10;
        tmp += j % 10;
        j /= 10;
        tmp += j % 10;
        j /= 10;
        tmp += j % 10;
        j /= 10;

        if(tmp >= a && tmp <= b){
            ans += i;
        }
    }

    cout << ans << endl;
}

Score : 200 points

Problem Statement

Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Constraints

1 \leq N \leq 10^4

1 \leq A \leq B \leq 36

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Sample Input 1

20 2 5

Sample Output 1

84

Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.

Sample Input 2

10 1 2

Sample Output 2

13

Sample Input 3

100 4 16

Sample Output 3

4554


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

int sumBit(int n){
  int sum = 0;
  while(n > 0){
    sum += n % 10;
    n /= 10;
  }
  return sum;
}

int main() {
  int n,a,b;
  long long total = 0;

  cin >> n >> a >> b;

  int tmpSum = 0;
  for(int i = 1; i <= n; i++){
    tmpSum = sumBit(i);
    if(a <= tmpSum && tmpSum <= b){
      total += i;
    }
  }

  cout << total;
  return 0;
}


#include<iostream>

int calc(int x){
    int ret = 0;
    while(x > 0){
        ret += x % 10;
        x /= 10;
    }
    return ret;
}

int main(){
    int N, A, B;
    std::cin >> N >> A >> B;
    long long sum = 0;
    for(int i = 1; i <= N; i++){
        int keta = calc(i);
        if(A <= keta && keta <= B) sum += i;
    }

    std::cout << sum << std::endl;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
using namespace std;

int main(void)
{
    int A, B, N, cnt = 0;

    cin >> N >> A >> B;

    for(int i = 1; i <= N; i++)
    {
        string s = to_string(i);
        int sum = 0;
        for(int j = 0; j < s.size(); j++) sum += s.at(i) - '0';

        if(sum >= A && sum <= B) cnt++;
    }

    cout << cnt << endl;

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> p_ll;

template<class T>
void debug(T itr1, T itr2) { auto now = itr1; while(now<itr2) { cout << *now << " "; now++; } cout << endl; }
#define repr(i,from,to) for (int i=(int)from; i<(int)to; i++)
#define rep(i,N) repr(i,0,N)
#define per(i,N) for (int i=(int)N-1; i>=0; i--)

const ll MOD = pow(10,9)+7;
const ll LLINF = pow(2,61)-1;
const int INF = pow(2,30)-1;

int main() {
    int N, A, B;
    cin >> N >> A >> B;

    int res = 0;
    repr(i,1,N+1){
        int sum = 0;
        int n = i;
        while (n > 0) {
            sum += n % 10;
            n = n / 10;
        }
        if (A <= sum && sum <= B) res += i;
    }

    cout << res << endl;
    return 0;
}


#include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;++i)
#include<string>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int Digit(int n) {
	int sum = 0;
	while (n > 0) {
		sum += n % 10;
		n /= 10;
	}
	return sum;
}

int main() {
	int n, a, b;
	cin >> n >> a >> b;
	int ans = 0;
	for (int i = 1;i <= n;++i) {
		if (a <= Digit(i) && Digit(i) <= b)ans+=i;
	}
	cout << ans << endl;
}


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

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

int main() {
	int n, a, b, z=0;
	cin >> n >> a >> b;
	REP(i, 1, 10001){
      int x=i, y=0;
      while(x){
        y=x%10+y;
        x=x/10;
      }
      if((a<=y) && (y<=b) && i<=n) z=z+i;
    }
  	cout << z << endl;
}


#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define NMAX 100000
#define INF 1e+6
#define scan(arg) cin>>arg
#define prin(arg) cout<<arg<<"\n"
#define prin2(arg1,arg2) cout<<arg1<<" "<<arg2<<"\n"
using namespace std;
typedef long long ll;
int n,a,b;
int main(){
  scan(n);scan(a);scan(b);
  int ans=0;
  rep1(i,n){
    int sum=0,j=i;
    sum+=j%10;
    while(j>=10){
      j/=10;
      sum+=j%10;
    }
    if(a<=sum&&sum<=b){
      ans+=i;
    }
  }
  prin(ans);

  return 0;
}


#include<iostream>
using namespace std;

int main() {
	int n, a, b, res = 0;
	cin >> n >> a >> b;
	for (int i = 1;i <= n;i++) {
		int c = i;
		int sum = 0;
		for (int j = 0;j < 4;j++) {
			sum += c % 10;
			c /= 10;
		}
		if (a <= sum && sum <= b)res += i;
	}
	cout << res << endl;
}


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

int main() {
 string s;
 int sum = 0;

 if(s.at(0) == '1'){
     sum++;
 }
 if(s.at(1) == '1'){
     sum++;
 }
 if(s.at(2) == '1'){
     sum++;
 }

 cout << sum << endl;
}

Score : 100 points

Problem Statement

Snuke has a grid consisting of three squares numbered 1, 2 and 3.
In each square, either 0 or 1 is written. The number written in Square i is s_i.

Snuke will place a marble on each square that says 1.
Find the number of squares on which Snuke will place a marble.

Constraints

Each of s_1, s_2 and s_3 is either 1 or 0.

Input

Input is given from Standard Input in the following format:

s_{1}s_{2}s_{3}

Output

Print the answer.

Sample Input 1

101

Sample Output 1

2

A marble will be placed on Square 1 and 3.

Sample Input 2

000

Sample Output 2

0

No marble will be placed on any square.


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

int main() {
  int s[3] = {0};
  char c[3] = {0};
  cin >> c;

  int ans = 0;
  for( int i = 0; i < 3; ++i){
    s[i] = atoi(&c[i]);
    if( s[i] == 1){
      ++ans;
    }
  }

  cout << ans << endl;
}


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

int main() {
  // ここにプログラムを追記
  int s1,s2,s3;
  cin>>s1>>s2>>s3;
  cout<<s1+s2+s3<<endl;
}


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

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  cout << a + b + c << endl;
}


#include<bits/stdc++.h>
using namespace std;
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    string s;
    cin >> s;
    int count = 0;
    for(int i = 0;i < 3;i++) {
        if(s[i] == '1') count++;
    }
    cout << count << endl;
}


// BEGINNING WITH NAME OF ALMIGHTY GOD ALLAH
// AUTHOR:: MOHAMMAD FAISAL
// ATCODER BEGINNERS PROBLEMS
//#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.flush();
   	char c;
    int ans =0;
  	while(cin >> c)
      if(c=='1') ans++;
    cout<<ans;
    return 0;
    // Bye Bye 2019
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  string s;
  cin>>s;
  cout<<int(s.at(0))+int(s.at(1))+int(s.at(2))<<endl;
  return 0;
}


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

            int main() {
              int a,s1,s2,s3;
                cin>>s1>>s2>>s3;
                a=100*s1+10*s2+s3;
                cout<<a;
            }


#include <stdio.h>
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string s;
  cin >> s;
  int first = stoi(s.substr(0, 1));
  int second = stoi(s.substr(1, 1));
  int third = stoi(s.substr(2, 1));
  cout << first + second + third << endl;
}


#include <iostream>
#include <string>
using namespace std;

int main() {
    string s;
    cin >> s;
    int counter = 0;
    if (s[0] == '1') ++counter;
    if (s[1] == '1') ++counter;
    if (s[2] == '1') ++counter;
    cout << counter << endl;
}


#include <iostream>
#include <string>

using namespace std;

int main(){
string s;
  cin>>s;
  int result=0;;
  for(int i=0;i<3;i++){
    if(s[i]=='1'){
    result++;
    }

  }
cout<<result<<endl;
}


#include <iostream>
#include <string>
using namespace std;

int main(){
  int a = 0;
  int s[3];
  for(int i =0; i<3; i++){
    cin>>s[i];
    if(s[i]==1){ a++;}
  }
  return 0;
}


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

main(){
  int a,b,c,d,e,f;
  cin>>a;
  b=a/100;
  c=a-b*100;
  d=c/10;
  e=c-d*10;
  f=b+d+e;
  cout<<f<<endl;
}


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

int main()
{
	string s;
	cin >> s;
	cout << ((s[0] == '1') + (s[1] == '1') + (s[2] == '1')) << endl;
	return 0;
}


#include<stdio.h>

int main()
{
  int i,res=0;
  char str[5];
  fgets(str,4,stdin);
  for(i=0;i<4;i++)
    if(str[i] == '1')
      res++;
  printf("%d\n",res);
  return 0;
}


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

int main() {

  string S, T;
  cin >> S >> T;

  string S_ = S;
  string answer = "UNRESTORABLE";

  bool flg = true;

  for (int i = S.size() - T.size(); 0 <= i; i--) {
    S_ = S;
    flg = true;
    for (int j = 0; j < T.size(); j++) {
      // cout << S_[i + j] << " " << T[j] << endl;
      if (S_[i + j] == T[j] || S_[i + j] == '?') {
        S_[i + j] = T[j];
      } else {
        flg = false;
        break;
      }
    }
    if (flg) {
      break;
    }
  }

  if (flg) {
    answer = "";
    for (int i = 0; i < S_.size(); i++) {
      if (S_[i] == '?') {
        answer += "a";
      } else {
        answer += S_[i];
      }
    }
  }

  cout << answer << endl;

  return 0;

}

Score : 300 points

Problem Statement

E869120 found a chest which is likely to contain treasure.

However, the chest is locked. In order to open it, he needs to enter a string S consisting of lowercase English letters.

He also found a string S', which turns out to be the string S with some of its letters (possibly all or none) replaced with ?.

One more thing he found is a sheet of paper with the following facts written on it:

Condition 1: The string S contains a string T as a contiguous substring.

Condition 2: S is the lexicographically smallest string among the ones that satisfy Condition 1.

Print the string S.

If such a string does not exist, print UNRESTORABLE.

Constraints

1 \leq |S'|, |T| \leq 50

S' consists of lowercase English letters and ?.

T consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S
T'

Output

Print the string S.

If such a string does not exist, print UNRESTORABLE instead.

Sample Input 1

?tc????
coder

Sample Output 1

atcoder

There are 26 strings that satisfy Condition 1: atcoder, btcoder, ctcoder,..., ztcoder.
Among them, the lexicographically smallest is atcoder, so we can say S = atcoder.

Sample Input 2

??p??d??
abc

Sample Output 2

UNRESTORABLE

There is no string that satisfies Condition 1, so the string S does not exist.


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const ll INF = 1e18;
const long double EPS = 1e-10;

int main() {
  string s, t;
  cin >> s >> t;

  int n = s.size(), m = t.size();
  reverse(s.begin(), s.end());
  reverse(t.begin(), t.end());
  rep(i, n - m + 1) {
    bool flag = true;
    FOR(j, i, i + m) {
      if (s[j] == '?') continue;
      if (s[j] != t[j - i]) {
        flag = false;
        break;
      }
    }
    if (flag) {
      FOR(j, i, i + m) s[j] = t[j - i];
      repr(j, n - 1) cout << (s[j] == '?' ? 'a' : s[j]);
      cout << endl;
      return 0;
    }
  }
  cout << "UNRESTORABLE" << endl;

  return 0;
}


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

int main(){
  string s,t;
  cin >> s >> t;
  bool flag = false;
  int p;
  for(int i=0; i<s.size()-t.size()+1; i++){
    string ssub = s.substr(i,t.size());
    for(int j=0; j<t.size(); j++){
      if(ssub.at(j)==t.at(j)||ssub.at(j)=='?'){
        flag = true;}
      if(ssub.at(j)!=t.at(j)&&ssub.at(j)!='?'){
        flag = false;
        goto end;}
    }
    p = i;
    end :
    int dummy = 1;
  }
  if(flag){
    for(int i=0; i<s.size(); i++){
      if(i==p){
        cout << t << endl;
        i += t.size()-1;
      }else{
        if(s.at(i)=='?'){
          cout << 'a';
        }else{
          cout << s.at(i);
        }
      }
    }
    cout << endl;
  }else{
    cout << "UNRESTORABLE" << endl;
  }
}


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s,t;
  cin >> s >> t;
  int s_l = (int)s.size();
  int t_l = (int)t.size();
  if(s_l<t_l){
    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  int p = -1;
  REPR(i,s_l-t_l){
    int cnt = 0;
    REP(j,t_l){
      if(s[i+j]!='?' && s[i+j]!=t[j]) break;
      else cnt++;
    }
    if(cnt==t_l){
      p = i;
      break;
    }
  }
  if(p==-1){
    cout << "UNRESTORABLE" << endl;
    return 0;
  }
  s.replace(p, p+t_l, t);
  int p2=0;
  int x;
  while((x = (int)s.find("?",p2))!=-1){
    s[x] = 'a';
    p2 += x;
  }
  cout << s << endl;
  return 0;
}


#include<bits/stdc++.h>
#define rep(i,n)for(ll i=0;i<n;i++)
#define ll long long
#define ggr getchar();getchar();return 0;
#define prique priority_queue
#define inf 1145141919364364
#define double long double
using namespace std;
typedef pair<ll, ll>P;
bool prime(int n) {
	int cnt = 0;
	for (int i = 1; i <= sqrt(n); i++) {
		if (n % i == 0)cnt++;
	}
	if (cnt != 1)return false;
	else return n != 1;
}
ll gcd(ll x, ll y) {
	if (y == 0)return x;
	return gcd(y, x % y);
}
ll lcm(ll x, ll y) {
	return x * y / gcd(x, y);
}
signed main() {
	ll memo=0;
	string s,t;
	cin>>s>>t;
	ll n=s.size(),m=t.size();
	string ans;
	rep(i,n){
		if(s[i]=='?')ans[i]='a';
		else ans[i]=s[i];
	}
	for(int i=0;i<=n-m;i++){
		memo=0;
		for(int j=i;j<i+m;j++){
			if(s[j]==t[j-i]||s[j]=='?')memo++;
		}
		if(memo==m){
			for(int k=i;k<i+m;k++){
				ans[k]=t[k-i];
			}
			break;
		}
		memo=-1;
	}
	if(memo==-1)puts("UNRESTORABLE");
	else rep(i,n)cout<<ans[i];
	ggr
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	string s, t;
	cin >> s >> t;
	for (int i = s.size()-1; i >= t.size(); i--)
	{
		bool isValid = true;
		for (int j = 0; j < t.size(); j++)
		{
			if (s[i-j] != '?' && s[i-j] != t[t.size()-j-1])
			{
				isValid = false;
				break;
			}
		}
		if (isValid)
		{
			s = s.substr(0, i + 1 - t.size()) + t + s.substr(i+1);
			replace(s.begin(), s.end(), '?', 'a');
			cout << s << endl;
			return 0;
		}
	}
	cout << "UNRESTORABLE" << endl;
	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main(int argc, const char *argv[])
{
	string S, T;

	cin >> S >> T;

	int a = S.size(), b = T.size();

	for (int i = a - b; i >= 0; i--) {
		bool unrestorable;
		for (int j = 0; j < b; j++) {
			if (S[i + j] != T[j] && S[i + j] != '?') {
                unrestorable = true;
                break;
            }
		}
		if (unrestorable) continue;

		for (int j = 0; j < b; j++) {
			S[i + j] = T[j];
		}

		for (int j = 0; j < a; j++) {
			if (S[j] == '?') S[j] = 'a';
		}

		cout << S << endl;
		return 0;
	}

	cout << "UNRESTORABLE" << endl;

	return 0;
}


#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <set>
#include <algorithm>
#include <array>
#include <complex>
#include <string>
#include <utility>
#include <map>
#include <queue>
#include <list>
int dx[4] = { -1,0,1,0 };
int dy[4] = { 0,1,0,-1 };
#define INF 1<<21
using pii = std::pair<int, int>;
#define SORT(v) std::sort(v.begin(), v.end())
int main(void) {
    int N;
    std::string ans;
    std::string s, t;
    std::cin >> s >> t;
    bool make = false;
    std::vector<std::string> ans_s;
    if (s.size() < t.size()) {
        std::cout << "UNRESTORABLE" << std::endl;
        return 0;
    }
    for (int i = 0; i <= s.size() - t.size(); i++) {
        make = true;
        for (int j = 0; j < t.size(); j++) {
            if (s[i + j] != '?'&&s[i + j] != t[j]) {
                make = false;
                break;
            }
        }
        if (make) {
            ans = s;
            for (int j = 0; j < t.size(); j++) {
                ans[i + j] = t[j];
            }
            for (int j = 0; j < ans.size(); j++) {
                if (ans[j] == '?') {
                    ans[j] = 'a';
                }
            }
            ans_s.push_back(ans);
        }

    }
    SORT(ans_s);
    if (!ans_s.empty()) {
        std::cout << ans_s[0] << std::endl;
    }
    else {
        std::cout << "UNRESTORABLE" << std::endl;
    }
    return 0;
}


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

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))

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

#define whole(f,x,...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)

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

using ll = long long;

int main() {
  string s, t; cin >> s >> t;
  vector<string> ans;
  REP(i,s.size()-t.size()+1) {
    bool f = true;
    REP(j, t.size()) {
      if (s[i+j] != t[j] && s[i+j] != '?') {
        f = false; break;
      }
    }
    if (f) {
      string a = s;
      REP(j,a.size()) if (a[j] == '?') a[j] = 'a';
      REP(j,t.size()) a[i+j] = t[j];
      ans.push_back(a);
    }
  }
  if (ans.empty()) cout << "UNRESTORABLE" << endl;
  else cout << *(ans.end()-1) << endl;
  return 0;
}


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

int main() {
	vector <int >vec;
	string str,str2;
	cin>>str>>str2;
	int i,j,count=0,k,count1=0,sw=0;

	for(i=1;i<str.size()-1;i++)
	   {
	   	  if(str[i]!='?')
	   	    vec.push_back(i);
	   }

	for(i=str.size()-1;i>=0;i--)
	  {
	  	for(j=0;j<str2.size();j++)
	  	   {
	  	   	   if(str[i]==str2[j])
	  	   	       {  count=0;
	  	   	          count1=0;
	  	   	          //cout<<i<<" "<<j<<" ";
	  	   	       	   for(k=i-1;k>=0;k--)
	  	   	       	      {
	  	   	       	      	  if(str[k]=='?')
	  	   	       	      		++count;
	  	   	       	      	  else
	  	   	       	      	     break;
	  	   	       	      }
	  	   	       	   for(k=i+1;k<str.size();k++)
	  	   	       	      {
	  	   	       	      	if(str[k]=='?')
	  	   	       	      	  ++count1;
	  	   	       	      	else
	  	   	       	      	   break;
	  	   	       	      }
	  	   	       	      //cout<<count<<" "<<count1<<" "<<str2.size()<<'\n';
	  	   	       }

	  	   	       	   if(count==j && count1==str2.size()-1-j && j>0 && j<str2.size()-1)
	  	   	       	     {	sw=1;
	  	   	       	     	//cout<<'a';
	  	   	       	     	break;

	  	   	       	     }

	  	   	       	    if(j==0 && count1==str2.size()-1-j)
	  	   	       	      {
	  	   	       	      	sw=1;
	  	   	       	      	//cout<<'b';
	  	   	       	      	break;
	  	   	       	      }
	  	   	       	   if(j==str2.size()-1 && count==j)
	  	   	       		{
	  	   	       			//cout<<'c';
	  	   	       			sw=1;
	  	   	       	      	            break;
	  	   	       		}
	  	   	       }
	  	   	      if(sw==1)
	  	   	        break;

	  }
	 //cout<<vec.size();
	 if(sw==0 && vec.size()==0 && str2.size()<=str.size())
	    {
	    	for(k=0;k<str.size()-str2.size();k++)
	    	  cout<<'a';
	    	cout<<str2;
	    }
	 else if(sw==0)
	   cout<<"UNRESTORABLE"<<'\n';
	 else
	   { //cout<<i;
	     for(k=0;k<i-j;k++)
	      {
	      	if(str[k]=='?')
	                 cout<<'a';
	         else
	      	cout<<str[k];
	      }
	      cout<<str2;
	    for(k=str2.size()+i-j;k<str.size();k++)
	      {
	      	if(str[k]=='?')
	                  cout<<'a';
	         else
	      	cout<<str[k];
	      }
	   }
	// your code goes here
	return 0;
}


#include<iostream>
#include<string>
int main(void) {
	std::string a ,b;
	std::cin >> a;
	std::cin >> b;
	int q = 0, q_max = 0;
	//std::cout << a.size() << std::endl;
	b.at((int)b.size() - 1 - q);
	int bsize = b.size();
	if(b.size() > 50){ std::cout << "UNRESTORABLE"; system("pause"); return 0; }
	//std::cout <<"bsize = "<< bsize << std::endl;
	int l1;
	for ( l1 = a.size()-1; l1 >= 0; l1--) {
		if (bsize - 1 - q < 0) break;
		if (a.at(l1) == b.at(bsize - 1 - q) || a.at(l1) == '?') { q++; /*std::cout << "into q=" << q << std::endl;*/ }
		else { if (q_max < q) q_max = q; q = 0; }
	}
	//std::cout << "max" << q_max <<"or" << q << std::endl <<"l1 "<< l1;
	if (q_max < b.size() && q < b.size()) { std::cout << "UNRESTORABLE"; system("pause"); return 0; }
	//std::cout << "ikeru"<<std::endl;
	int l3 = 0;
	for (int l2 = 0; l2 < a.size(); l2++) {
		if (l2 >= l1+1&&l3 < b.size()) { std::cout << b.at(l3); l3++; }
		else if (a.at(l2) == '?')
			std::cout << 'a';
		else std::cout << a.at(l2);
	}
	system("pause");
        return 0;
}


#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x)  cerr<< #x <<" = "<< x<<endl;
using namespace std;

typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int main(){
    //~ freopen("file.in", "r", stdin);
    string s,t,ans;
    cin>>s>>t;
    for(int i=0;i+t.size()<=s.size();i++){
		string tmp=s;
		int res=1;
		for(int j=0;j<(int)t.size();j++){
			if(s[j+i]=='?')
				tmp[j+i]=t[j];
			else{
				if(s[j+i]!=t[j])
					res=0;
			}
		}
		if(res){
			for(int j=0;j<(int)s.size();j++)
				if(tmp[j]=='?')
					tmp[j]='a';
			if(ans.size()==0)
				ans=tmp;
			else
				umin(ans,tmp);
		}
	}
	if(ans.size()==0)
		puts("UNRESTORABLE");
	else
		cout<<ans<<endl;
	return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<int,int>;

int main(){
	int h,w;
	cin >> h >> w;
	vector<vector<char>> s(h, vector<char>(w));
	rep(i,h)rep(j,w) cin >> s[i][j];
	int counts = 0;
	rep(i,h)rep(j,w){
		if(s[i][j] == '.'){
			s[i][j] = '0';
			for(int x = -1; x <=1; x++){
				for(int y = -1; y <= 1; y++){
					int nx = i+x;
					int ny = j+y;
					if(nx >= 0 && nx < h && ny >= 0 && ny < w && s[nx][ny] == '#')	s[i][j]++;
				}
			}
		}
	}
	rep(i,h){
		rep(j,w){
			cout << s[i][j];
		}
		cout << endl;
	}
}

Score : 200 points

Problem Statement

You are given an H × W grid.

The squares in the grid are described by H strings, S_1,...,S_H.

The j-th character in the string S_i corresponds to the square at the i-th row from the top and j-th column from the left (1 \leq i \leq H,1 \leq j \leq W).

. stands for an empty square, and # stands for a square containing a bomb.

Dolphin is interested in how many bomb squares are horizontally, vertically or diagonally adjacent to each empty square.

(Below, we will simply say "adjacent" for this meaning. For each square, there are at most eight adjacent squares.)

He decides to replace each . in our H strings with a digit that represents the number of bomb squares adjacent to the corresponding empty square.

Print the strings after the process.

Constraints

1 \leq H,W \leq 50

S_i is a string of length W consisting of # and ..

Input

Input is given from Standard Input in the following format:

H W
S_1
:
S_H

Output

Print the H strings after the process.

The i-th line should contain a string T_i of length W, where the j-th character in T_i corresponds to the square at the i-th row from the top and j-th row from the left in the grid (1 \leq i \leq H, 1 \leq j \leq W).

Sample Input 1

3 5
.....
.#.#.
.....

Sample Output 1

11211
1#2#1
11211

For example, let us observe the empty square at the first row from the top and first column from the left.

There is one bomb square adjacent to this empty square: the square at the second row and second column.

Thus, the . corresponding to this empty square is replaced with 1.

Sample Input 2

3 5
#####
#####
#####

Sample Output 2

#####
#####
#####

It is possible that there is no empty square.

Sample Input 3

6 6
#####.
#.#.##
####.#
.#..#.
#.##..
#.#...

Sample Output 3

#####3
#8#7##
####5#
4#65#2
#5##21
#4#310


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for(ll i=0; i<n; i++)
#define Rep(i, j, n) for(ll i=j; i<n; i++)
#define all(vec) vec.begin(), vec.end()
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;}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

int dh[8] = {0, 0, 1, 1, 1, -1, -1, -1};
int dw[8] = {1, -1, 0, 1, -1, 0, 1, -1};

int main() {
    int h, w; cin >> h >> w;
    vector<vector<char>> v(h, vector<char>(w));
    rep(i, h) rep(j, w) cin >> v[i][j];
    vector<vector<int>> ans(h, vector<int>(w, 0));
    rep(i, h) {
        rep(j, w) {
            if(v[i][j] == '#') {
                rep(k, 8){
                    int nh = i + dh[k];
                    int nw = j + dw[k];
                    if(nh < 0 || h <= nh || nw < 0 || w <= nw) continue;
                    ans[nh][nw]++;
                }
            }
        }
    }
    rep(i, h) {
        rep(j, w) {
            if(v[i][j] == '#') cout << '#';
            else cout << ans[i][j];
        }
        cout << endl;
    }
}


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

int main(){
  int H,W;
  cin >> H>>W;
  int ni,nj;
  vector <string>s(W);
  for(int i=0;i<W;i++)
    cin >> s[i];
  vector <int>dx={0,1,1,1,0,-1,-1,-1};
  vector <int>dy={1,1,0,-1,-1,-1,0,1};
  for (int i=0;i<H;i++){
    for (int j=0;j<W;j++){
      if(s[i].at(j)=='#')
        continue;
      int num=0;
      for(int k=0;k<8;k++){
          ni=i+dy.at(k);
          nj=j+dx.at(k);
          if(ni<0||ni>H-1)
            continue;
          if(nj<0||nj>W-1)
            continue;
          if(s[ni].at(nj)=='#')
            num++;
      }
      s[i].at(j)=char(num+'0');
    }
  }
  for (int i=0;i<H;i++){
      cout << s[i]<<endl;
  }
  return 0;
}


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

#define lli long long int
#define REP(i,s,n) for(int i=s;i<n;i++)
#define MOD 1000000007
#define NUM 2520
#define INF (1LL<<55)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)

lli dd[60][60];
char d[60][60];

int main(){

    lli h,w;
    cin>>h>>w;

    REP(i,1,h+1)REP(j,1,w+1){
        cin>>d[i][j];
        if(d[i][j]=='#')dd[i][j]=-1;
    }

    REP(i,1,h+1)REP(j,1,w+1){
        lli dx[8]={0,1,1,1,0,-1,-1,-1};
        lli dy[8]={-1,-1,0,1,1,1,0,-1};

        if(dd[i][j]==-1)continue;
        lli cnt=0;
        REP(k,0,8){
            lli nextI = i+dy[k];
            lli nextJ = j+dx[k];
            if(dd[nextI][nextJ]==-1)cnt++;
        }
        dd[i][j]=cnt;
    }

    REP(i,1,h+1){
        REP(j,1,w+1){
            if(dd[i][j]==-1)cout<<"#";
            else cout<<dd[i][j];
        }
        cout<<endl;
    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
  int a,b;
  cin >> a >> b;
  vector<string> A(60);
  vector<int> X = { -1 ,-1 ,-1, 0, 0, 1, 1, 1};
  vector<int> Y = { -1 ,0, 1, -1, 1, -1, 0, 1};
  for(int i = 0;i<a;i++){
    cin >> A.at(i);
  }
  int m = 0;
  for(int i = 0;i<a;i++){
    for(int j = 0;j<b;j++){
      int count = 0;
      if(A.at(i).at(j) == '.'){
        for(int k = 0;k<8;k++){
          int dx = i + X.at(k);
          int dy = j + Y.at(k);
          if(dx >= 0 && dy >= 0 && dx < a && dy < b){
            if(A.at(dx).at(dy) == '#')
              count++;
          }
        }
        A.at(i).at(j) = '0' + count;
      }
    }
  }
  for(int i = 0;i<a;i++){
    cout << A.at(i) << endl;;
  }
}


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

int main() {
  vector<string> grid;
  int h, w, count=0;
  cin >> h >> w;
  string extra(w+2,'.');
  string row, temp;
  grid.push_back(extra);
  for (int i=0; i<h; i++) {
    cin >> temp;
    row = "." + temp + ".";
    grid.push_back(row);
  }
  grid.push_back(extra);
  for (int i=1; i<=h; i++) {
    for (int j=1; j<=w; j++) {
      if (grid[i][j]=='#') cout << "#";
      else {
        count = 0;
        for (int k=-1; k<=1; k++) {
          for (int l=-1; l<=1; l++) {
            if (grid[i+k][j+l]=='#') count++;
          }
        }
        cout << count;
      }
    }
    cout << endl;
  }
  //for (auto i : grid) cout << i << endl;

  return 0;
}


#include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#include<tuple>
#include<utility>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<iomanip>
#include<map>
using namespace std;
using ll=long long int;
typedef pair<int, int> P;
const int INF = 1e9 + 7;
int dx[8] = { 1,0,-1,0,1,1,-1,-1 };
int dy[8] = { 0,1,0,-1,1,-1,1,-1 };

int main() {

	int h, w;
	cin >> h >> w;

	char map[55][55];
	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cin >> map[i][j];
		}
	}

	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {

			if (map[i][j] == '#') {
				continue;
			}

			int ans = 0;
			for (int k = 0; k < 8; k++) {
				int ny = i + dy[k];
				int nx = j + dx[k];

				if (0 <= ny && ny < h && 0 <= nx && nx < w && map[ny][nx] == '#') {
					ans++;
				}
			}

			map[i][j] = ans + '0';

		}
	}

	for (int i = 0; i < h; i++) {
		for (int j = 0; j < w; j++) {
			cout << map[i][j];
		}
		cout << endl;
	}

}


#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstring>
#include <set>
#include <map>
#include <string>
#define pii pair<int,int>
#define fi first
#define se second
#define pcc pair<char,char>

using namespace std;

typedef long long ll;
char s[100][100];

int check(int r, int l){
        int ans = 0;
        for(int i = r-1; i <= r+1; ++i)
                for(int j = l-1; j <= l+1; ++j)
                        if(s[i][j] == '#') ans++;
        return ans;
}

int main(){
        int m, n; cin >> m >> n;
        for(int i = 1; i <= m; ++i) scanf("%s",s[i]+1);
        for(int i = 1; i <= m; ++i){
                for(int j = 1; j <= n; ++j)
                        if(s[i][j] == '#') putchar('#');
                        else printf("%d",check(i,j));
                puts("");
        }
        return 0;
}


#include <iostream>
using namespace std;
using ll = long long int;
int main() {
    ll N;
    cin >> N;
    ll sum = 0;
    for (ll i = 0; i < N; i++) {
        ll li, ri;
        cin >> li >> ri;
        sum += (ri - li + 1);
    }
    cout << sum << endl;
}

Score : 200 points

Problem Statement

Joisino is working as a receptionist at a theater.

The theater has 100000 seats, numbered from 1 to 100000.

According to her memo, N groups of audiences have come so far, and the i-th group occupies the consecutive seats from Seat l_i to Seat r_i (inclusive).

How many people are sitting at the theater now?

Constraints

1≤N≤1000

1≤l_i≤r_i≤100000

No seat is occupied by more than one person.

All input values are integers.

Input

Input is given from Standard Input in the following format:

N
l_1 r_1
:
l_N r_N

Output

Print the number of people sitting at the theater.

Sample Input 1

1
24 30

Sample Output 1

7

There are 7 people, sitting at Seat 24,25,26,27,28,29 and 30.

Sample Input 2

2
6 8
3 3

Sample Output 2

4


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

int main() {
  int N;
  cin >> N;
  int sum = 0;
  cout << sum <<endl;
  for (int i = 0; i < N; i++) {
    int r,l;
    cin >> r >> l;
    sum += l-r + 1;
  }
  cout << sum <<endl;
}


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

int main() {
  int N;
  cin >> N;
  int sum = 0;

  for(int i = 0; i < N; i++) {
    int l, r;
    cin >> l >> r;
    sum += r - l + 1;
  }
  cout << sum << endl;
}


#include <iostream>
using namespace std;

int main(){
    int i, n, l, r, s=0;
    cin >> n;
    for(i=0; i<n; i++){
        cin >> l >> r;
        s += r-l+1;
    }
    cout << s;
    return 0;
}


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

int main() {
  int N;
  int a = 0;
  cin >> N;
  for(int i = 0; i < N; i++){
    int l_i,r_i;
    cin >> l_i >> r_i;
    a += r_i-l_i+1;
  }
  cout << a << endl;
}


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

int main(){
  int N,a,b;
  cin >> N;
  int i = 0;
  int c = 0;
  while (i < N){
    cin >> a >> b;
    c += a - b + 1;
    i++;
  }
  cout << c << endl;
}


// #define _CRT_SECURE_NO_WARNINGS
// #define _USE_MATH_DEFINES	// M_PI=3.1415...
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <locale>
#include <cctype>
#include <sstream>
#include <iomanip>	// 20桁出力 cout << setprecision(20) << double;
#include <queue>
#include <bitset>	// bitset<8> bs1(131uL); // 10000011 bs1[0]は1 01stringからビット集合生成可
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VL;
typedef map<int, int> MAPII;
typedef multimap<int, char, greater<int> > MuMAPIC;
typedef vector<pair<int, int> > VPII;
typedef multimap<int, string, greater<int> > MuMIS;
typedef pair<int, int> P;
typedef pair<int, pair<P, P> > PP;

#define INF 999999999999999997;
#define MP make_pair
#define FAST_IO  cin.tie(0); ios::sync_with_stdio(false);
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORL(i,a,b) for(LL i=(a);i<(b);i++)
#define FOR_REV(i,a,b) for(int i=(a);i>=(b);i--)
//for gcc (未test)
// #define FOREACH_IT(it,c) for(typeof(c)::iterator it=(c).begin(); it!=(c).end(); ++it)
//for Visual Studio
#define foreach_it(type,it,c) for(type::iterator it=c.begin(),c_end=c.end();it!=c_end;++it)
#define FOR_ITR(d) for(auto itr=d.begin(),d_end=d.end();itr!=d_end;++itr)	// C++11
// for Debug.
#define DUMP_VVI(b) FOR(i,0,b.size()){FOR(j,0,b[i].size())printf("%d ",b[i][j]);puts("");}
#define D_OUT(str,value) if(dbgF){cout<<str<<" : "<<value<<endl;}
// 入力をpush_back(d)やarray[d]に使う時に1行で書ける
// int INPUT_INT() {int d;cin>>d;return d;}
template<class T>T IN() { T d; cin >> d; return d; }
// 最大公約数（Greatest Common Divisor）
LL gcd(LL a, LL b) { return (b > 0) ? gcd(b, a%b) : a; }
// 最小公倍数（Least Common Multiple）
LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
// Y年はうるう年か否か
bool uruu(LL Y) { return (((Y % 4 == 0 && Y % 100 != 0) || Y % 400 == 0) ? true : false); }

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

// vector注意
// vec[i][j]の形に入力を入れるとき、vecは初期化してある必要がある.

// ------------------- include, typedef, define END. -------------------

int main() {
	FAST_IO;
	// for D_OUT(str, value)  ... cout<< str <<" : "<< value <<endl;
	bool dbgF = true;

	//コードはここから書く.
	LL n,ans=0;
	cin >> n;
	FOR(i, 0, n) {
		LL a, b;
		cin >> a >> b;
		ans += b - a + 1;
	}
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <map>

int main(void){
    char ans='0';
    std::string S;
    std::cin >> S;
    std::map<char, int> mp;
    for (int i=0; i<26; ++i){
        mp[(char)('a' + i)] = 0;
    }

    for (char c: S) ++mp[c];

    for (int i=0; i<26; ++i){
        if (mp[(char)('a' + i)] == 0){
            ans = (char)('a' + i);
            break;
        }
    }

    if (ans == '0') {
        std::cout << "None" << std::endl;
    } else {
        std::cout << ans << std::endl;
    }

    return 0;
}

Score : 200 points

Problem Statement

You are given a string S consisting of lowercase English letters.
Find the lexicographically (alphabetically) smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Constraints

1 \leq |S| \leq 10^5 (|S| is the length of string S.)

S consists of lowercase English letters.

Input

Input is given from Standard Input in the following format:

S

Output

Print the lexicographically smallest lowercase English letter that does not occur in S.
If every lowercase English letter occurs in S, print None instead.

Sample Input 1

atcoderregularcontest

Sample Output 1

b

The string atcoderregularcontest contains a, but does not contain b.

Sample Input 2

abcdefghijklmnopqrstuvwxyz

Sample Output 2

None

This string contains every lowercase English letter.

Sample Input 3

fajsonlslfepbjtsaayxbymeskptcumtwrmkkinjxnnucagfrg

Sample Output 3

d


#include <bits/stdc++.h>
using namespace std;
string t;
long long k;
char ch;
char a[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u',
'v','w','x','y','z'};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>t;
sort(t.begin(),t.end());
set <char> s;
for(int i=0;i<t.length();i++){
    s.insert(t[i]);
}

for(int i=0;i<t.length();i++){
    if(s.count(a[i])==false){
    ch=a[i];k++;break;

}

}
if(k>0)
cout<<ch;
else cout<<"None";
return 0;}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)

int func(int x)
{
    if(x%2==0)
    {
        return x/2;
    }
    else
    {
        return 3*x+1;
    }
}
int main()
{
    string s;
    cin>>s;
    sort(s.begin(),s.end());
    set <char> alph;
    for(int i=0;i<s.size();i++)
    {
        alph.insert(s[i]);
    }
    vector <pair<bool,char>> check(26);
    char alphabet[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
    for(int i=0;i<26;i++)
    {
        check[i].first=false;
        check[i].second=alphabet[i];
    }
    for(auto itr = alph.begin(); itr != alph.end(); ++itr)
    {
        for(int j=0;j<26;j++)
        {
            if(*itr==alphabet[j])
            {
                check[j].first=true;
            }
        }
    }
    for(int i=0;i<26;i++)
    {
        if(!check[i].first)
        {
            cout<<check[i].second<<endl;;
            return 0;
        }
    }
    cout<<"None"<<endl;
    return 0;
}


#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define Sort(Array) sort(Array.begin(), Array.end())
#define Reverse(a) reverse(a.begin(), a.end())
#define out(ans) cout << ans << endl;
const int MOD = 1000000007;
const int INF = 2147483647;
const ld PI = 3.14159265358979323846;

//------------↓------------- M -------------- E ---------------- M --------------- O ---------------↓--------------//
// コンパイル
// g++ -std=c++1z
//
// -------型変換--------
// int を string に変換
// string s = to_string(n);
// string を int に変換
// int n = stoi(s);
//
// -------二分探索---------
// k以上の値が最初に現れる時のイテレータ
// lower_bound(data.begin(), data.end(), k)
// kより大きい値が最初の現れる時のイテレータ O(logN)
// upper_bound(data.begin(), data.end(), k)
// kがdataに存在するかをtrue or falseで返す O(logN)
// binary_search(data.begin(), data.end(), k)
//
//
//
//
//
//
//
//------------↑------------- M -------------- E ---------------- M --------------- O ---------------↑--------------//

int main(void) {

    string s; cin >> s;
    int l = 'z' - 'a' + 1;
    bool flag[l];
    rep(i,l) flag[i] = false;
    rep(i,s.size()) {
        int num = s[i] - 'a';
        flag[num] = true;
    }
    bool flag_2 = true;
    rep(i,l) {
        if (!flag[i]) {
            char c = i + 'a';
            out(c);
            flag_2 = false;
            break;
        }
    }
    if (flag_2) cout << "None" << endl;

    return 0;
}


//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//int→string
//string→int
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//sqrt
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

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

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

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

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))

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

//#define EPS (1e-10)
//
//struct Point{
//    Point(double _x = 0, double _y = 0){
//        x = _x;
//        y = _y;
//    }
//    double x;
//    double y;
//
//    Point operator + (Point p){
//        return Point(x + p.x, y+ p.y);
//    }
//
//    Point operator - (Point p){
//        return Point(x - p.x, y - p.y);
//    }
//
//    Point operator * (double a){
//        return Point(a * x, a * y);
//    }
//
//    Point operator / (double a){
//        return Point(x / a, y / a);
//    }
//
//    double abs(){
//        return sqrt(norm());
//    }
//
//    double norm(){
//        return x * x + y * y;
//    }
//
//    bool operator < (const Point &p) const{
//        return x!=p.x ? x < p.x : y < p.y;
//    }
//
//    bool operator == (const Point &p) const{
//        return fabs(x - p.x) && fabs(y - p.y) < EPS;
//    }
//
//};
//
//typedef Point Vector;
//
//double dot(Vector a, Vector b){
//    return a.x * b.x + a.y * b.y;
//}

char ch[26];

int main() {

    string S;
    cin >> S;
    REP(i,26){
        ch[i] = 0;
    }
    REP(i, S.size()){
        char c = S[i];
        ch[c-'a'] ++;
    }

    char t;
    bool isOk = false;
    REP(i, 26){
        if(!ch[i]){
            t = i + 'a';
            isOk = true;
            break;
        }
    }

    if(isOk){
        printf("%c", t);
    }else{
        printf("none");
    }

    return 0;

}


#include <iostream>
#define MAX 100001
using namespace std;

int main(){
  char c;

  int a[26]={};

  while(cin>>c){
    a[c-'a']++;
  }

  for(int i=0;i<26;i++){
    if(a[i]==0){
      cout<<(char)('a'+i)<<endl;
      break;
    }
  }
}


#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,popcnt")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
using namespace std;
using ll = long long;
using ld = long double;
const int maxn = 1<<20, mod = 1e9 + 7, i2 = (mod+1)/2;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	int n, t, a = 0, b = 0, c = 0;
	cin >> n;
	for(int i = 0; i < n; i++) {
		cin >> t;
		if(t%4 == 0) a++;
		else if(t%2 == 0) b++;
		else c++;
	}
	int ok = (c <= (a + (b==0)));
	cout << (ok?"Yes\n":"No\n");
	return 0;
}

Score : 400 points

Problem Statement

We have a sequence of length N, a = (a_1, a_2, ..., a_N).
Each a_i is a positive integer.

Snuke's objective is to permute the element in a so that the following condition is satisfied:

For each 1 ≤ i ≤ N - 1, the product of a_i and a_{i + 1} is a multiple of 4.

Determine whether Snuke can achieve his objective.

Constraints

2 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

If Snuke can achieve his objective, print Yes; otherwise, print No.

Sample Input 1

3
1 10 100

Sample Output 1

Yes

One solution is (1, 100, 10).

Sample Input 2

4
1 2 3 4

Sample Output 2

No

It is impossible to permute a so that the condition is satisfied.

Sample Input 3

3
1 4 1

Sample Output 3

Yes

The condition is already satisfied initially.

Sample Input 4

2
1 1

Sample Output 4

No

Sample Input 5

6
2 7 1 8 2 8

Sample Output 5

Yes


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <limits>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif
using ll = long long;
using vll = std::vector<ll>;
using vvll = std::vector<vll>;
using vvvll = std::vector<vvll>;
#define reps(i, S, E) for (ll i = (S); i <= (E); i++)
#define rep(i, N) reps(i, 0, N-1)
#define deps(i, E, S) for (ll i = (E); i >= (S); i--)
#define dep(i, N) deps(i, N-1, 0)
const ll INF = 1LL << 60;
const int INF_INT = 1 << 30;

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; }
struct mll {
	static ll MOD;
	ll val;
	mll(ll v = 0) : val(v % MOD) { if (val < 0) val += MOD; }
	mll operator - () const { return -val; }
	mll operator + (const mll &b) const { return val + b.val; }
	mll operator - (const mll &b) const { return val - b.val; }
	mll operator * (const mll &b) const { return val * b.val; }
	mll operator / (const mll &b) const { return mll(*this) /= b; }
	mll operator + (ll b) const { return *this + mll(b); }
	mll operator - (ll b) const { return *this - mll(b); }
	mll operator * (ll b) const { return *this * mll(b); }
	friend mll operator + (ll a, const mll &b) { return b + a; }
	friend mll operator - (ll a, const mll &b) { return -b + a; }
	friend mll operator * (ll a, const mll &b) { return b * a; }
	mll &operator += (const mll &b) { val = (val + b.val) % MOD; return *this; }
	mll &operator -= (const mll &b) { val = (val + MOD - b.val) % MOD; return *this; }
	mll &operator *= (const mll &b) { val = (val*b.val) % MOD; return *this; }
	mll &operator /= (const mll &b) {
		ll c = b.val, d = MOD, u = 1, v = 0;
		while (d) {
			ll t = c / d;
			c -= t * d; swap(c, d);
			u -= t * v; swap(u, v);
		}
		val = val * u % MOD;
		if (val < 0) val += MOD;
		return *this;
	}
	mll &operator += (ll b) { return *this += mll(b); }
	mll &operator -= (ll b) { return *this -= mll(b); }
	mll &operator *= (ll b) { return *this *= mll(b); }
	mll &operator /= (ll b) { return *this /= mll(b); }
	bool operator == (const mll &b) { return val == b.val; }
	bool operator != (const mll &b) { return val != b.val; }
	bool operator == (ll b) { return *this == mll(b); }
	bool operator != (ll b) { return *this != mll(b); }
	friend bool operator == (ll a, const mll &b) { return mll(a) == b.val; }
	friend bool operator != (ll a, const mll &b) { return mll(a) != b.val; }
	friend ostream &operator << (ostream &os, const mll &a) { return os << a.val; }
	friend istream &operator >> (istream &is, mll &a) { return is >> a.val; }
	static mll Combination(ll a, ll b) {
		chmin(b, a - b);
		if (b < 0) return mll(0);
		mll c = 1;
		rep(i, b) c *= a - i;
		rep(i, b) c /= i + 1;
		return c;
	}
};
using vmll = std::vector<mll>;
using vvmll = std::vector<vmll>;
using vvvmll = std::vector<vvmll>;
using vvvvmll = std::vector<vvvmll>;

struct Fast {
	Fast() {
		cin.tie(0);
		ios::sync_with_stdio(false);
		cout << fixed << setprecision(std::numeric_limits<double>::max_digits10);
	}
} fast; //cin,cout高速化のおまじない＋桁数指定

ll mll::MOD = (ll)(1e9 + 7);// 998244353ll;

//素因数分解
void DecompositPrime(ll n, map<ll, ll> &result) {
	result.clear();
	ll a = 2;
	while (n >= a * a) {
		if (n % a == 0) {
			result[a]++;
			n /= a;
		}
		else {
			a++;
		}
	}
	if (n > 1) {
		result[n]++;
	}
}

int main() {
	ll N;
	cin >> N;
	vector<ll> A(N);
	rep(i, N) {
		cin >> A[i];
	}

	map<ll, ll> mp;
	vll count(3, 0); //2が0個、1個、2個以上のカウント
	rep(i, N) {
		if (A[i] % 4 == 0) {
			count[2]++;
		}
		else if (A[i] % 2 == 0) {
			count[1]++;
		}
		else {
			count[0]++;
		}
	}

	bool result = true;

	//0 2 0 2 ...とする。0が1個につき2が1個なければならない
	if (count[0] > count[2]) {
		//[0 2 0 2 0]の場合は、特例でOK
		if (count[0] + count[2] == N && count[0] - 1 == count[2]) {

		}
		else {
			result = false;
		}
	}

	if (result == true) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int n,x;
int s[6];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++) scanf("%d",&x),s[x%4]++;s[4]=s[0];
	if(s[4]+1==s[1]+s[3]){
		if(s[2]==0) printf("Yes\n");
		else printf("No\n");
	}
	else if(s[4]>=s[1]+s[3]) printf("Yes\n");
	else printf("No\n");
	return 0;
}


#include<iostream>
#include<sstream>
#include<fstream>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<algorithm>
#include<functional>
#include<utility>
#include<bitset>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cstdio>
#include<cstring>
#include<iomanip>
using namespace std;
int n,x,c2=0,c4=0;
bool flag=false;
int read(){
    int ans=0,f=1;
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){ans=ans*10+ch-'0';ch=getchar();}
    return ans*f;
}
int main(){
	std::ios::sync_with_stdio(false);
	n=read();
	for (int i=0;i<n;i++){
		x=read();
		if (x%4==0)	c4++;
		else if (x%2==0) c2++;
	}
	if (c2==n) {cout<<"Yes";return 0;}	else if (!c4) {cout<<"No";return 0;}
	if (c2){n-=c2+1;c4-=1;flag=true;}
	if (c4>=(n/2)||((n<=1)&&flag)) cout<<"Yes";else cout<<"No";
	return 0;
}


#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <bitset>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <utility>
#include <iostream>
#include <algorithm>
using namespace std;
int a[100005],b[5];
int main ( ) {
	int n,i;
	scanf("%d",&n);
	for (i=1;i<=n;i++) scanf("%d",&a[i]),a[i]%=4,b[a[i]]++;
	if (b[1]+b[3]>b[0]+1) {
		puts("No");return 0;
	}
	if (b[1]+b[3]==b[0]+1 && b[2]!=0) {
		puts("No");return 0;
	}
	if (b[2]==1 && b[0]==0) {
		puts("No");return 0;
	}
	puts("Yes");
	return 0;
}
/*
3
1 10 100

Yes

4
1 2 3 4

No

3
1 4 1

Yes

2
1 1

No

6
2 7 1 8 2 8

Yes
*/


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;

const int INF = (int) 1e9 + 123;
const ll LINF = (ll) 1e18 + 123;
const ld EPS = (ld) 1e-12;
const ll MOD = (ll) 1e9 + 7;

#define sz(x) (int) (x).size()
#define mp(x, y) make_pair(x, y)
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define lb(s, t, x) (int) (lower_bound(s, t, x) - s)
#define ub(s, t, x) (int) (upper_bound(s, t, x) - s)
#define rep(i, f, t) for (auto i = f; i < t; i++)
#define per(i, f, t) for (auto i = f; i >= t; i--)

ll power(ll x, ll y, ll mod = MOD) {
    if (y == 0) {
        return 1;
    }
    if (y & 1) {
        return power(x, y - 1, mod) * x % mod;
    } else {
        ll tmp = power(x, y / 2, mod);
        return tmp * tmp % mod;
    }
}

void add(ll &x, ll y, ll mod = MOD) {
    x += y;
    if (x >= mod) x -= mod;
    if (x < 0) x += mod;
}

ll sum(ll x, ll y, ll mod = MOD) {
    add(x, y, mod);
    return x;
}

ll mult(ll x, ll y, ll mod = MOD) {
    return (x * y) % mod;
}

ll div(ll x, ll y, ll mod) {
    return x * power(y, mod - 2, mod);
}

template<typename A, typename B> bool mini(A &x, const B &y) {
    if (y < x) {
        x = y;
        return true;
    }
    return false;
}

template<typename A, typename B> bool maxi(A &x, const B &y) {
    if (y > x) {
        x = y;
        return true;
    }
    return false;
}

template<typename T> void read(T first, T last) {
    while (first != last) {
        cin >> *(first++);
    }
}

template<typename T> void print(T first, T last) {
    while (first != last) {
        cout << *(first++) << " ";
    }
    cout << "\n";
}

void run();

#define TASK ""

int main() {
#ifdef LOCAL
    if (strlen(TASK) > 0) {
        cerr << "Reminder: you are using file i/o, filename: " TASK "!" << endl << endl;
    }
#endif
#ifndef LOCAL
    if (strlen(TASK)) {
        freopen(TASK ".in", "r", stdin);
        freopen(TASK ".out", "w", stdout);
    }
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#endif
    cout << fixed << setprecision(12);
    run();
    return 0;
}

// == SOLUTION == //

const int N = (int) 1e5 + 123;

int n;
int a[N];
int cnt[3];

void run() {
    cin >> n;

    rep(i, 0, n) {
        cin >> a[i];
        if (a[i] % 4 == 0) {
            a[i] = 2;
        } else if (a[i] % 2 == 0) {
            a[i] = 1;
        } else {
            a[i] = 0;
        }
        cnt[a[i]]++;
    }

    if (((cnt[0] <= cnt[2] + 1) && (cnt[1] == 0)) || (cnt[0] <= cnt[2])) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
}


#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <map>
#include <math.h>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

using ll = long long;
using ld = long double;
using Pii = pair<int, int>;
using Pil = pair<int, ll>;
using Pll = pair<ll, ll>;
using Pid = pair<int, ld>;
using Pis = pair<int, string>;
#define rep(i,n) for(int i=0; i<n; i++)
#define repm(i,s,n) for(int i=s; i<n; i++)

const int INF = 1 << 30;
const ll INF_L = 1LL << 60;
const int MOD = 1e9+7;

// ----------------------------------------------------------------
// String Functions
// ----------------------------------------------------------------
int ctoi(char c){
    if(isdigit(c)) return c - '0';
    else if(islower(c)) return c - 'a';
    else if(isupper(c)) return c - 'A';
    else return -1;
}
char itocd(int i){char c = i+'0'; if(isdigit(c)) return c; else return 0x00;}
char itocl(int i){char c = i+'a'; if(islower(c)) return c; else return 0x00;}
char itocu(int i){char c = i+'A'; if(isupper(c)) return c; else return 0x00;}
// ----------------------------------------------------------------

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

// ----------------------------------------------------------------
// Graph Theory
// ----------------------------------------------------------------
vector<vector<int>> adjMat;
vector<set<Pii>> adjList;

void Dijkstra(){}
void BellmanFord(){}
void WarshallFloyd(){}
// ----------------------------------------------------------------

// ----------------------------------------------------------------
// Mathematical Functions
// ----------------------------------------------------------------
ll gcd(ll A, ll B) {if (A%B == 0) {return(B);} else {return(gcd(B, A%B));}}
ll lcm(ll A, ll B) {return A * B / gcd(A, B);}
ll divtimes(ll N, ll D) {ll res = 0; while(N%D == 0) {N/=D; res++;} return res;}

ll powMod(ll B, ll P) {
    if(P == 0) return 1;
    if(P%2 == 0){ll t = powMod(B, P/2); return t*t % MOD;}
    return B * powMod(B, P-1) % MOD;
}

/* ----------------------------------
 Factorial, Permutation, Combination
 ---------------------------------- */
const int FAC_INIT_SIZE = 1e6+9;
vector<ll> fac, finv, inv;

void factModInit() {
    fac.resize(FAC_INIT_SIZE);
    finv.resize(FAC_INIT_SIZE);
    inv.resize(FAC_INIT_SIZE);
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i=2; i < FAC_INIT_SIZE; i++){
        fac[i] = fac[i-1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i-1] * inv[i] % MOD;
    }
}
ll factMod(ll N){return fac[N] % MOD;}
ll factInvMod(ll N){return finv[N] % MOD;}
ll permMod(ll N, ll K){
    if (N < 0 || K < 0 || N < K) return 0;
    else return factMod(N) * factInvMod(N-K) % MOD;
}
ll combMod(ll N, ll K){
    if (N < 0 || K < 0 || N < K) return 0;
    else if (N < FAC_INIT_SIZE){ return factMod(N) * (factInvMod(K) * factInvMod(N-K) % MOD) % MOD;}
    else {
        ll ans = 1; ll Ks = K < N-K ? K : N-K;
        for (ll i=N; i > N-Ks; i--) {ans *= i; ans %= MOD;}
        return ans * factInvMod(Ks) % MOD;
    }
}

/* ----------------------------------
 Sieve of Eratosthenes
---------------------------------- */
vector<bool> isprime;
void sieveInit(int size){
    int sb = size + 9;
    isprime.resize(sb);

    isprime[0] = isprime[1] = false;
    isprime[2] = true;
    for(int i=2; i<sb; i++) {
        if(i%2 == 0) isprime[i] = false;
        else isprime[i] = true;
    }
    for(int i=3; i*i<=sb; i+=2){
        if(isprime[i]){
            int m = 3*i;
            while(m < sb){isprime[m] = false; m += 2*i;}
        } else {
            continue;
        }
    }
}
// ----------------------------------------------------------------

int main() {

    std::ifstream in("input.txt");
    std::cin.rdbuf(in.rdbuf());

    cin.tie(0);
    ios::sync_with_stdio(false);

    int N, A, B; cin >> N >> A >> B;
    ll res;
    if(A > B) res = 0;
    else if(N == 1 && A != B) res = 0;
    else if(N == 1 && A == B) res = 0;
    else res = (B-A) * (N-2) + 1;
    cout << res << endl;
}

Score : 200 points

Problem Statement

Snuke has N integers. Among them, the smallest is A, and the largest is B.
We are interested in the sum of those N integers. How many different possible sums there are?

Constraints

1 ≤ N,A,B ≤ 10^9

A and B are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the number of the different possible sums.

Sample Input 1

4 4 6

Sample Output 1

5

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.

Sample Input 2

5 4 3

Sample Output 2

0

Sample Input 3

1 7 10

Sample Output 3

0

Sample Input 4

1 3 3

Sample Output 4

1


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  ll n,a,b;
  cin>>n>>a>>b;
  if(n==1&&a!=b){
    cout<<0<<endl;
    return 0;
  }
  if(a>b){
    cout<<0<<endl;
    return 0;
  }
  if(n==2){
    cout<<a+b<<endl;
    return 0;
  }
  ll min_s,max_s;
  min_s=a*(n-1)+b;
  max_s=b*(n-1)+a;
  ll ans=max_s-min_s+1;
  cout<<ans<<endl;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main(){
	ll N,A,B,ans;
	cin>>N>>A>>B;
	if(N==1&&A!=B)ans=0;
	else if(A>B)ans=0;
	else ans=(B*(N-1)+A)-(A*(N-1)+B)+1;
	cout<<ans<<endl;
}


#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
  ll n,a,b;
  cin >> n >> a >> b;
  cout << (n-2)*abs(a-b)+1 << endl;
return 0;}


#include<iostream>
#include<string>
#include<algorithm>
#include<bits/stdc++.h>
#include<vector>

using namespace std;

int main() {
  int N,A,B;
  cin >> N >> A >> B;
  if(A>B) cout << 0 << endl;
  else if((N==1 && A==B) || N==2) cout << 1 << endl;
  else if(N==1) cout << 0 << endl;
  else cout << (N-2)*(B-A)+1 << endl;

}


#include <iostream>

using namespace std;

int main()
{
    long long int num,smin,smax;
    cin>>num>>smin>>smax;
    if(num<=0 || smin<=0 || smax<=0)
    {
        cout<<0;
        return 0;
    }
    if(smin>smax)
    {
        cout<<0;
        return 0;
    }
    if(num==1 && smin==smax)
    {
        cout<<1;
        return 0;
    }
    if(num==1 && smin!=smax)
    {
        cout<<0;
        return 0;
    }
    else
    {
        cout<<(num-2)*(smax-smin)+1;
    }

    return 0;
}


#include "bits/stdc++.h"
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <map>
#include <vector>
#include <math.h>
#include <algorithm>
#include <queue>
#include <set>
#include <tuple>
using namespace std;

#define FOR(i,init,a) for(int i=init; i<a; i++)
#define rep(i,a) FOR(i,0,a)
#define rrep(i,a) for(int i=a; i>=0; i--)
#define rep1(i,a) for(int i=1; i<=a; i++)
#define cout1(a) cout << a << endl;
#define cout2(a,b) cout << a << " " << b << endl;
#define cout3(a,b,c) cout << a << " " << b << " " << c << endl;
#define cout4(a,b,c,d) cout << a << " " << b << " " << c << " " << d << endl;
#define mem(a,n) memset( a, n, sizeof(a))
#define all(a) a.begin(),a.end()
#define chmin(a,b) a=min(a,b);
#define chmax(a,b) a=max(a,b);

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;
const int INF = 1e9;
const int MOD = 1e9+7;
const ll LLINF = 1e18;
static const double pi = 3.141592653589793;

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

    ll N,A,B;
    cin>>N>>A>>B;

    ll ans;
    if(N==1){
        ans=(A==B);
    }else{
        if(A>B){
            ans=0;
        }else{
            ans=(B-A)*(N-2)+1;
        }
    }
    cout1(ans)
}


#include <iostream>
using namespace std;
int main()
{
	int N, A, B;
	cin >> N >> A >> B;

	int real_n = N - 2;
	long long patterns = real_n*(B - A) + 1;

	cout << patterns << endl;
}


#include <bits/stdc++.h>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
#define INF 999999999

using namespace std;
typedef pair<int, int> P;
typedef pair<llong, llong> LP;
typedef pair<int, P> PP;
typedef pair<llong, LP> LPP;

int iy[]={0, 0, 1, -1};
int ix[]={1, -1, 0, 0};

long long int ans, n, a, b;

int main(){
	cin >> n >> a >> b;
	if(n == 1 && a != b){
	}else if(b > a){
		ans = (n - 2) * (b - a) + 1;
	}else if(b == a){
		ans = 1;
	}else{
		ans = 0;
	}
	cout << ans << endl;
	return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <tuple>
#include <cmath>
using namespace std;
typedef pair<int,int> P;
typedef tuple<int,int,int> T;

int main(){
    int64_t N,A,B;
    cin>>N>>A>>B;
    int64_t lo = (N-1)*A+B;
    int64_t up = (N-1)*B+A;
    cout<<max(int64_t(0),up-lo+1)<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
const int N = 1e5 + 10;
ll Lft[3*N], Rit[3*N];
int num[3*N];
int main()
{
  //  freopen("in.txt", "r", stdin);
   	int n;
   	while (~scanf("%d", &n))
   	{
   		for (int i = 0; i < 3*n; i++)
   			scanf("%d", num + i);
   		priority_queue<int, vector<int>, greater<int> >qmin;
   		priority_queue<int> qmax;
   		ll sum = 0;
   		qmin.push(num[0]);

   		for (int i = 0; i < n; i++)
   		{
   			sum += num[i];
   			qmin.push(num[i]);
   		}
   		int m = n<<1;
   		Lft[n-1] = sum;
   		for (int i = n; i < m; i++)
   		{
   			if (qmin.top() < num[i])
   			{
   				sum += num[i];
   				sum -= qmin.top();
   				qmin.pop();
   				qmin.push(num[i]);
   			}
   			Lft[i] = sum;
   		}

   		sum = 0;
   		for (int i = 3*n - 1; i >= m; i--)
   		{
   			sum += num[i];
   			qmax.push(num[i]);
   		}
   		Rit[m] = sum;
   		for (int i = m - 1; i >= n; i--)
   		{
   			if (num[i] < qmax.top())
   			{
   				sum += num[i];
   				sum -= qmax.top();
   				qmax.pop();
   				qmax.push(num[i]);
   			}
   			Rit[i] = sum;
   		}
   		ll ans = Lft[n-1] - Rit[n];
   		for (int i = n; i < m; i++)
   		{
   			// if (n == 1)
   			// printf("%d %d\n", Lft[i-1], Rit[i]);
   			ans = max(Lft[i-1] - Rit[i], ans);
   		}
   		printf("%lld\n", ans);
   	}

   return 0;
}

Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

In the test set worth 300 points, N ≤ 1000.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a_1 are removed, a' will be (2, 3), which has a score of 2 - 3 = -1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a_2, a_3 and a_9 are removed, a' will be (8, 7, 4, 6, 5, 3), which has a score of (8 + 7 + 4) - (6 + 5 + 3) = 5.


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

#define inf INT_MAX/3
#define INF INT_MAX/3
#define PB push_back
#define pb push_back
#define MP make_pair
#define mp make_pair
#define ALL(a) (a).begin(),(a).end()
#define all(a) (a).begin(),(a).end()
#define SET(a,c) memset(a,c,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define PII pair<int,int>
#define pii pair<int,int>
#define pcc pair<char,char>
#define pic pair<int,char>
#define pci pair<char,int>
#define VS vector<string>
#define VI vector<int>
#define debug(x) cout<<#x<<": "<<x<<endl
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define MIN(a,b) (a>b?b:a)
#define MAX(a,b) (a>b?a:b)
#define pi 2*acos(0.0)
#define INFILE() freopen("in0.txt","r",stdin)
#define OUTFILE()freopen("out0.txt","w",stdout)
#define in scanf
#define out printf
#define LL long long
#define ll long long
#define ULL unsigned long long
#define ull unsigned long long
#define eps 1e-14
#define FST first
#define SEC second

#define sint(x) int x; scanf("%d",&x);
#define sint2(x,y) int x,y; scanf("%d %d",&x,&y);
#define lint(x) long long x; scanf("%lld",&x);
#define vint(v,n) vector<int> v(n); rep(i,n) scanf("%d", &v[i]);
#define vlint(v,n) vector<lint> v(n); rep(i,n) scanf("%lld", &v[i]);
#define sdouble(x) double x; scanf("%lf",&x);
#define vdouble(v,n) vector<double> v(n); rep(i,n) scanf("%lf", &v[i]);
#define schar(x) char x; scanf(" %c", &x);
#define sstring(x) string x; cin >> x;

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

#include <stdio.h>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <numeric>
#include <climits>
using namespace std;

int main(int argc, char** argv) {
	sint(N);
	vint(a, 3 * N);

	vector<int> left(N), middle(N), right(2 * N);
	copy(a.begin(), a.begin() + N, left.begin());
	copy(a.begin() + N, a.begin() + 2 * N, middle.begin());
	copy(a.begin() + N, a.end(), right.begin());
	sort(all(left));
	sort(all(right));

	ll leftSum = accumulate(all(left), (ll)0);
	ll rightSum = accumulate(right.begin(), right.begin() + N, (ll)0);
	ll min = leftSum - rightSum;
	rep(i, N) {
		int m = middle[i];
		if (m > left[0]) {
			left.erase(left.begin());
			left.insert(lower_bound(all(left), m), m);
			leftSum = accumulate(all(left), (ll)0);
		}
		right.erase(find(all(right), m));
		if (m < right[N - 1]) {
			rightSum = accumulate(right.begin(), right.begin() + N, (ll)0);
		}
		ll score = leftSum - rightSum;
		if (min < score) {
			min = score;
		}
	}
	printf("%lld\n", min);
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
int N;
ll a[300001];
ll ans = -10000000000000;

int main() {
  cin >> N;

  for(int i = 0; i < 3*N; i++) cin >> a[i];

  for(int i = N; i < 2 * N; i++) {
    ll sum_a = 0, sum_b = 0;
    vector<int>g1(i,0);
    vector<int>g2(3*N-i,0);
    for(int j = 0; j < i; j++) g1[j] = a[j];
    for(int j = 0; j < 3*N-i; j++)g2[j] = a[i+j];
    sort(g1.begin(),g1.end(),greater<ll> ());
    sort(g2.begin(),g2.end());
    for(int j = 0; j < N; j++) sum_a += g1[j];
    for(int j = 0; j < N; j++) sum_b += g2[j];
    ans = max(ans,sum_a-sum_b);
  }

  cout << ans <<endl;
}


#include<iostream>
using namespace std;

int main()
{
	string s;
	cin >> s;
	int count_b=0;
	int count_e=0;
	for(int i=1; i<=(int)s.size(); i++){
			if(s[i-1] == 'A' && count_b <= 0)
				count_b = i;
			if(s[i-1] == 'Z')
				count_e=i;
	}
	count_e -= count_b;

	cout << count_e << endl;
}

Score : 200 points

Problem Statement

Snuke has decided to construct a string that starts with A and ends with Z, by taking out a substring of a string s (that is, a consecutive part of s).

Find the greatest length of the string Snuke can construct. Here, the test set guarantees that there always exists a substring of s that starts with A and ends with Z.

Constraints

1 ≦ |s| ≦ 200{,}000

s consists of uppercase English letters.

There exists a substring of s that starts with A and ends with Z.

Input

The input is given from Standard Input in the following format:

s

Output

Print the answer.

Sample Input 1

QWERTYASDFZXCV

Sample Output 1

5

By taking out the seventh through eleventh characters, it is possible to construct ASDFZ, which starts with A and ends with Z.

Sample Input 2

ZABCZ

Sample Output 2

4

Sample Input 3

HASFJGHOGAKZZFEGA

Sample Output 3

12


#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
#define SPEED ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
typedef vector<int>vi;
typedef pair<int,int>pi;
typedef vector<pi>vpi;
typedef vector<vi>vvi;
#define double long double
#define pow powl
#define sqrt sqrtl
#define cbrt cbrtl
#define floor floorl
#define ceil ceill
#define pb push_back
#define ep emplace_back
#define PB pop_back
#define pf push_front
#define ef emplace_front
#define PF pop_front
#define mp make_pair
#define ub(a,b) upper_bound(all(a),b)-a.begin()
#define lb(a,b) lower_bound(all(a),b)-a.begin()
#define bs(a,b) binary_search(all(a),b)
#define mem(a,b) memset(a,b,sizeof(a))
#define in(a,n) FOR(i,0,n-1) cin>>a[i]
#define in1(a,n) FOR(i,1,n) cin>>a[i]
#define ff first
#define ss second
#define out(a,n) FOR(i,0,n-1) cout<<a[i]<<" ";cout<<endl;
#define out1(a,n) FOR(i,1,n) cout<<a[i]<<" ";cout<<endl;
#define clr(a) a.clear()
#define pow2(a) (1LL<<a)
#define all(a) a.begin(),a.end()
#define all1(a) a.begin()+1,a.end()
#define rall(a) a.rbegin(),a.rend()
#define rall1(a) a.rbegin(),a.rend()
#define sz(a) ((int)a.size())
#define FOR(i,a,b) for(auto i=a;i<=b;i++)
#define NFOR(i,a,b) for(auto i=a;i>=b;i--)
const int mod=1e9+7;

inline int add(int a,int b){return (a%mod + b%mod)%mod;}
inline int sub(int a ,int b){return (a%mod - b%mod + mod)%mod;}
inline int mul(int a,int b){return (a%mod * b%mod)%mod;}
inline int power(int a,int b,int MOD){int res=1;while(b){if(b&1){res*=a;res%=MOD;}a=a*a;a%=MOD;b>>=1;}return res;}

#define trace(x) cout<<#x<<" :: "<<x<<endl;
#define trace2(x,y) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<endl;
#define trace3(x,y,z) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<endl;
#define trace4(x,y,z,w) cout<<#x<<" :: "<<x<<"\t"<<#y<<" :: "<<y<<"\t"<<#z<<" :: "<<z<<"\t"<<#w<<" :: "<<w<<endl;
clock_t time_p = clock();
void time()
{
  time_p = clock() - time_p;
  cerr << "Time elapsed : " << (double)(time_p)/CLOCKS_PER_SEC <<endl;
}
signed main()
{
     SPEED;
     string s;
     cin>>s;
     int left=1e9;
     int right=-1;
     int n=s.length();
     for(int i=0;i<n;i++)
     {
     	    if(s[i]=='A')
     	    	left=min(left,i);
     	    if(s[i]=='Z')
     	    	right=max(right,i);
     }
     cout<<right-left+1<<endl;
     return 0;
}


#include <iostream>
using namespace std;
typedef long long ll;
int main(void){
    // Your code here!
    ll K,S,ans=0;
    cin >>K>>S;
    for(int X =0; X <= K; X++){
        for(int Y=0; Y <= K; Y++){
            int Z =S-X-Y;
            if(0<=Z && Z<=K){
                ans++;

            }
        }
    }
    cout << ans << endl;
}

Score : 200 points

Problem Statement

You are given two integers K and S.

Three variable X, Y and Z takes integer values satisfying 0≤X,Y,Z≤K.

How many different assignments of values to X, Y and Z are there such that X + Y + Z = S?

Constraints

2≤K≤2500

0≤S≤3K

K and S are integers.

Input

The input is given from Standard Input in the following format:

K S

Output

Print the number of the triples of X, Y and Z that satisfy the condition.

Sample Input 1

2 2

Sample Output 1

6

There are six triples of X, Y and Z that satisfy the condition:

X = 0, Y = 0, Z = 2

X = 0, Y = 2, Z = 0

X = 2, Y = 0, Z = 0

X = 0, Y = 1, Z = 1

X = 1, Y = 0, Z = 1

X = 1, Y = 1, Z = 0

Sample Input 2

5 15

Sample Output 2

1

The maximum value of X + Y + Z is 15, achieved by one triple of X, Y and Z.


#include <iostream>
#include <string>
#include <functional>
#include <algorithm>

int main(){
    int K,S;
    //入力
    std::cin>>K>>S;
    //計算
    int count = 0;
    for(int x=0;x<=K;x++){
        for(int y=0;y<=K;y++){
            for(int z=0;z<=K;z++){
                if(x+y+z==S)count++;
            }
        }
    }
    std::cout<<count;
    return 0;
}


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

int K,S;

int main()
{
    cin>>K>>S;

    int cnt = 0;
    for (int i = 0; i <= K; i++)
    {
        for (int j = 0; j <= K; j++)
        {
            int c = S-i-j;
            if(c>=0 && c<=K) cnt++;
        }
    }

    cout << cnt << endl;
    return 0;
}


// Sum of Three Integers
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    int K, S;
    int count = 0;
    cin >> K >> S;
    for (int X = 0; X <= K; ++X) {
        for (int Y = 0; Y <= K; ++Y) {
            int Z = S - X - Y;
            if (0<= Z && Z <= K) {
                ++count;
            }
        }
    }
    cout << count << endl;
}


#include <bits/stdc++.h>

using namespace std;

int main() {
  int K, S, ans = 0;

  cin >> K >> S;

  vector<int> lk;

  for (int j = 0; j <= K; j++) {
  for (int k = 0; k <= K; k++) {
    lk.push_back(S - k - j);
  }
  }

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

  for (int i = 0; i <= K; i++) {
    if (*lower_bound(lk.begin(), lk.end(), i) - i == 0) {
      ans++;
    }
  }

  cout << ans << endl;

  return 0;
}


#include <iostream>
#include <math.h>
#include <string>
#include <vector>
#include <algorithm>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define INF 1000000;

using ll = long long;
using namespace std;

int main(){
    int k, s,all_sum, count=0;
    cin >> k >> s;
    rep(i, k+1){
        rep(j, k+1){
            rep(l, k+1){
                all_sum = i+j+l;
                if(all_sum==s) count++;
            }
        }
    }
    cout << count << endl;
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int (i) = 0; (i) < (n); ++(i))
using d = double;

int K, S;
int ans = 0;

int main() {
  cin >> K >> S;
  for(int i = 0; i <= K; ++i) {
    for(int j = 0; j <= K - i; ++j) {
      int X = i, Y = j, Z = K - X - Y;
      if(X + Y + Z == S) ++ans;
    }
  }

  cout << ans << "\n";
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int k,s;
    int x,y,z;
    int cnt=0;
    cin>>k>>s;
    for(x=0;x<=k;x++){
        for(y=0;y<=k;y++){
           z=s-x-y;
           if(z>=0 && z<=k)cnt++;
        }
    }
    cout<<cnt<<endl;
return 0;
}


#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
using namespace std;
using ll = long long;
const int INF = 1<<29;
const int MOD = 1e9+7;
const ll LINF = 1e18;
const double pi = acos(-1);
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) {
    REP(i,v.size()){if(i)os<<" ";os<<v[i];}return os;}
template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v) {
    REP(i,v.size()){if(i)os<<endl;os<<v[i];}return os;}

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

const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};

const int dx8[8] = {1,1,0,-1,-1,-1,0,1};
const int dy8[8] = {0,1,1,1,0,-1,-1,-1};

vector< vector<int> > Graph;
vector< int > value;

int k,s;

int ans = 0;

void input(){
    // remove the bottom 3 lines when you submit this code.
    std::ifstream in("sample.txt");
    std::cin.rdbuf(in.rdbuf());

    cin.tie(0);
    ios::sync_with_stdio(false);

    cin >> k >> s;

}

void solve(){

    for (int x = 0; x <= k; x++) {
        for (int y = 0; y <= k; y++) {
            int t = s-x-y;
            if(0<=t && t<=k) ans++;
        }
    }

    cout << ans << endl;

}

int main(int argc, const char * argv[]) {

    input();
    solve();
    return 0;

}


#include <iostream>
#include <math.h>

using namespace std;

int main() {
    cin.tie(0);
   	ios::sync_with_stdio(false);
    int k;
    int s;
    cin >> k;
    cin >> s;
    int sum = 0;
    int count = 0;
    for (int i = 0;i <= k;i++) {
        for (int j = 0;j <= k;j++) {
            for (int l = 0;l <= k;l++) {
                sum = i + j + l;
                if (sum == s)
                    count++;
            }
        }
    }
    cout << count << endl;
}


#include <iostream>

using namespace std;
int k, s, cnt;
int main()
{
    cin >> k >> s;
    for(int i = 0; i <= k; i++){
        for(int j = 0; j <= k; j++){
            if(s - (i + j) <= k && s - (i + j) >= 0){
                cnt++;
            }
        }
    }
    cout << cnt;
    return 0;
}