Search is not available for this dataset
name
stringlengths
2
112
description
stringlengths
29
13k
source
int64
1
7
difficulty
int64
0
25
solution
stringlengths
7
983k
language
stringclasses
4 values
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s=map(int,input().split()) sum=0 lists=list(map(int,input().split())) minimum=min(lists) for i in lists: sum=sum+i if sum<s: print("-1") else: print(int(min((sum-s)/n,minimum)))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = [int(x) for x in input().split()] a = [int(x) for x in input().split()] total = sum(a) if total < s: print(-1) elif total == s or (total - n) < s: print(0) else: m = min(a) if (total - (m * n)) >= s: print(m) else: s = s - (total - (m * n)) total = m * n print((total - s) // n)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
from math import ceil [n, K] = [int(i) for i in input().split()] a = [int(i) for i in input().split()] S = sum(a) m = min(a) P = S - n*m if K>S: print(-1) else: if K<=P: print(m) else: K = K - P d = int(ceil(K/n)) print(m-d)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = map(int, input().split()) v = list(map(int, input().split())) if (s > sum(v)): print(-1) exit(0) v.sort() m = v[0] for i in range(len(v)): s -= v[i] - m if s <= 0: print(m) exit(0) m -= (s + n - 1) // n print(m)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.*; import java.util.ArrayList; public class Main{ static class Pair { int x; int y; Pair(int x, int y){ this.x=x; this.y=y; } } public static void main(String[] args){ Scanner param=new Scanner(System.in); int n=param.nextInt(); long s=param.nextLong(); long sum=0; long min=Long.MAX_VALUE; long arr[]=new long[n]; for(int i=0;i<n;i++){ arr[i]=param.nextLong(); sum+=arr[i]; min=Math.min(arr[i],min); } // sum/=n; if(sum-s<0){ System.out.println("-1"); } else{ System.out.println(Math.min((sum-s)/n,min)); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline ostream &operator<<(ostream &_out, const pair<T, U> &_p) { _out << _p.first << ' ' << _p.second << '\n'; return _out; } template <typename T, typename U> inline istream &operator>>(istream &_in, pair<T, U> &_p) { _in >> _p.first >> _p.second; return _in; } template <typename T> inline ostream &operator<<(ostream &_out, const vector<T> &_v) { if (_v.empty()) { return _out; } _out << _v.front() << ' '; for (auto _it = ++_v.begin(); _it != _v.end(); ++_it) { _out << *_it << ' '; } return _out; } template <typename T> inline istream &operator>>(istream &_in, vector<T> &_v) { for (auto &_i : _v) { _in >> _i; } return _in; } template <typename T> inline ostream &operator<<(ostream &_out, const set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; } template <typename T> inline ostream &operator<<(ostream &_out, const multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; } template <typename T> inline ostream &operator<<(ostream &_out, const unordered_set<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; } template <typename T> inline ostream &operator<<(ostream &_out, const unordered_multiset<T> &_s) { if (_s.empty()) { return _out; } _out << *_s.begin(); for (auto _it = ++_s.begin(); _it != _s.end(); ++_it) { _out << ' ' << *_it; } return _out; } template <typename T, typename U> inline ostream &operator<<(ostream &_out, const map<T, U> &_m) { if (_m.empty()) { return _out; } for (auto _it : _m) { _out << _it.first << ": " << _it.second << '\n'; } return _out; } vector<int> dx = {1, -1, 0, 0}; vector<int> dy = {0, 0, 1, -1}; struct cord { int x, y; }; const int mod = 1e9 + 7; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, s; cin >> n >> s; vector<long long> a(n); long long mini = 1e9; for (long long i = 0; i < n; i++) { cin >> a[i]; mini = min(mini, a[i]); } long long sum = 0; long long sum2 = 0; for (long long i = 0; i < n; i++) { sum += (a[i] - mini); sum2 += a[i]; a[i] = mini; } if (sum2 < s) { cout << -1; return 0; } if (sum >= s) { cout << mini; } else { long long diff = s - sum; long long ans = mini - (diff + n - 1) / n; cout << ans; } }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s = map(int,input().split()) l = list(map(int, input().split())) if sum(l) < s: print(-1) exit() q = sum(l) - min(l) * n if q>=s: print(min(l)) exit() print(min(l)-(s-q+n-1)//n)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s=map(int,input().split()) arr=list(map(int,input().split())) sumx=sum(arr) if(sumx<s): print(-1) else: minval=min(arr) diff1=sumx-(minval)*n if(diff1>=s): print(minval) else: s-=diff1 if(s%n==0): val=s//n else: val=s//n val+=1 print(minval-val)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
def main(): n, s = map(int, input().split()) v = list(map(int, input().split())) minn = min(v) summ = 0 alll = 0 for el in v: alll += el if alll < s: print(-1) return for i in range(n): summ += v[i] - minn s -= summ if s < 0: print(minn) return else: k = s // n if s % n > 0: k += 1 print(minn - k) return main()
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.*; import java.io.*; public class kvassandthefairnut { public static void main(String[] args) throws IOException{ BufferedReader f = new BufferedReader(new InputStreamReader(System.in)); PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out)); StringTokenizer st = new StringTokenizer(f.readLine()); long num_kegs = Integer.parseInt(st.nextToken()); long volume = Long.parseLong(st.nextToken()); Long[] kegs = new Long[(int) num_kegs]; st= new StringTokenizer(f.readLine()); for (int i = 0; i<num_kegs; i++){ kegs[i] = Long.parseLong(st.nextToken()); } Arrays.sort(kegs, Collections.reverseOrder()); Long[] diffs = new Long[(int)num_kegs]; diffs[0] = new Long(0); for (int i = 1; i<num_kegs;i++){ diffs[i] = diffs[i-1]+i*(kegs[i-1]-kegs[i]); //System.out.println(diffs[i]); } if (Arrays.asList(kegs).contains(1550370)) System.out.println("Found"+diffs[(int)num_kegs - 1]); //time to search; if (diffs[(int)num_kegs - 1] < volume){ long remaining = kegs[(int)num_kegs - 1] * num_kegs - (volume-diffs[(int)num_kegs-1]); //System.out.println(remaining+" "+(num_kegs)); if (remaining < 0) out.println(-1); else out.println(remaining/num_kegs); }else{ int counter = (int)num_kegs-1; while (counter>0 && diffs[counter-1]>=volume){ counter-=1; } out.println(kegs[(int)num_kegs - 1]); } out.close(); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import sys import bisect import math #import itertools def get_line(): return list(map(int, sys.stdin.readline().strip().split())) def in1(): return int(input()) n,s=get_line() a=get_line() t1=min(a) c=0 for i in range(n): c+=a[i]-t1 if c>=s: print(t1) else: t2=(s-c)//n if (s-c)%n==0 and t2<=t1: print(t1-t2) elif (s-c)%n!=0 and (t2+1)<=t1: print(t1-(t2+1)) else: print(-1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
from math import * n,s = map(int,input().split()) v = list(map(int,input().split())) m = min(v) s -= (sum(v)-m*n) print(m if s <= 0 else (m-ceil(s/n) if s <= m*n else -1))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.*; public class CodeForces1084B { public static void main(String[]args){ Scanner sc=new Scanner(System.in); long n=sc.nextLong(),s=sc.nextLong(); long sum=0,min=Long.MAX_VALUE; for(long i=0;i<n;i++){ long vi=sc.nextLong(); sum+=vi; if(vi<min) min=vi; } sc.close(); System.out.println(s>sum?-1:Math.min(min,(sum-s)/n)); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; const int maxx = 0x3f3f3f3f; int main() { long long int n, k, a, m = maxx, s = 0; cin >> n >> k; for (long long int i = 0; i < n; i++) { cin >> a; m = min(m, a); s += a; } if (s < k) { cout << -1; } else { cout << min(m, (s - k) / n); } }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import javafx.scene.layout.Priority; import java.io.*; import java.net.Inet4Address; import java.util.*; import java.lang.*; import java.util.HashMap; import java.util.PriorityQueue; public class templ implements Runnable { public class pair implements Comparable { int f; int s; pair(int f,int s) { this.f=f; this.s=s; } public boolean equals(Object o) { pair ob=(pair)o; int ff; int ss; if(o!=null) { ff=ob.f; ss=ob.s; if((ff==this.f)&&(ss==this.s)) return true; } return false; } public int hashCode() { return (this.f+" "+this.s).hashCode(); } public int compareTo(Object o) { pair pr=(pair)o; if(s>pr.s) return 1; else return -1; } } public static void main(String args[])throws Exception { new Thread(null,new templ(),"templ",1<<27).start(); } public void run() { try { InputReader in = new InputReader(System.in); PrintWriter out = new PrintWriter(System.out); int n=in.ni(); long s=in.nl(); long m[]=new long[n]; for(int i=0;i<n;i++) m[i]=in.nl(); int lb=0; int ub=1000000000; int ans=-1; while(lb<=ub) { int mid=(lb+ub)/2; int k=0; long a=0; for(int i=0;i<n;i++) { if(m[i]<mid) { k=1; break; } a+=(m[i]-mid); } if(k==1||a<s) { ub=mid-1; } else { lb=mid+1; ans=mid; } } out.println(ans); out.close(); } catch(Exception e){ return; } } static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int ni() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nl() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = ni(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
from math import ceil n, s = map(int, input().strip().split()) v = list(map(int, input().strip().split())) if s > sum(v): print(-1) elif s == sum(v): print(0) else: m = min(v) total_min = m*n diff = sum(v) - total_min if diff >= s: print(m) else: d = ceil((s - diff) / n) print(m-d)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.*; public class Solution { public static void main(String[] args)throws IOException { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); long s = sc.nextLong(); long[] a = new long[n]; long sum = 0; long min = -1; for(int i=0;i<n;i++) { a[i] = sc.nextLong(); sum += a[i]; if(i==0) min = a[i]; else if(a[i]<min) min = a[i]; } if(s>sum) System.out.println("-1"); else{ long k = min*(long)n; sum -= k; s -= sum; sum = k; if(s<=0) System.out.println(min); else{ long d = s/(long)n; if(s%(long)n!=0) d++; min -= d; System.out.println(min); } } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = map(int, input().split()) v = list(map(int, input().split())) if sum(v) < s: print("-1") else: m = min(v) v.sort(reverse=True) for i in range(n): diff = v[i] - m diff = min(diff, s) v[i] -= diff s -= diff if s == 0: break q = s//n for i in range(n): v[i] -= q s -= q if s > 0: print(min(v)-1) else: print(min(v))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long a[1003]; int main() { long long n, s; cin >> n >> s; long long miner = 0x3f3f3f3f3f3f3f3f; long long sumer = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; sumer += a[i]; miner = min(miner, a[i]); } if (sumer < s) { cout << "-1" << endl; return 0; } sumer = 0; for (long long i = 0; i < n; i++) { sumer += a[i] - miner; a[i] = miner; } if (sumer >= s) { cout << miner << endl; } else { long long need = s - sumer; miner -= need / n; if (need % n != 0) { miner--; } cout << miner << endl; } return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long n, k, l, r, mid, x; vector<long long> a; bool prv(long long mid) { long long sum = 0; for (int i = 0; i < n; i++) { if (a[i] - mid < 0) return false; } for (int i = 0; i < n; i++) { sum += a[i] - mid; } if (sum >= k) return true; else return false; } int main() { ios_base::sync_with_stdio(0); long long sum1 = 0; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); } for (int i = 0; i < n; i++) { sum1 += a[i]; } if (sum1 < k) { cout << "-1"; return 0; } sum1 = 0; l = 0; r = 10000000000000; while (r - l > 1) { mid = (l + r) / 2; if (prv(mid) == true) { l = mid; } else { r = mid; } } if (prv(r) == true) { cout << r; } else { cout << l; } return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.math.*; import java.util.*; import java.util.stream.*; import static java.lang.Math.abs; import static java.lang.Math.min; import static java.lang.Math.max; import static java.lang.Math.sqrt; @SuppressWarnings("unchecked") public class P1084B { public void run() throws Exception { long n = nextLong(), s = nextLong(), v [] = readLong((int)n); long t = LongStream.of(v).sum(), m = LongStream.of(v).min().getAsLong(), k = (t - s) / n; println((t < s) ? -1 : min(m, k)); } public static void main(String... args) throws Exception { br = new BufferedReader(new InputStreamReader(System.in)); pw = new PrintWriter(new BufferedOutputStream(System.out)); new P1084B().run(); br.close(); pw.close(); System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]"); } static long startTime = System.currentTimeMillis(); static BufferedReader br; static PrintWriter pw; StringTokenizer stok; String nextToken() throws IOException { while (stok == null || !stok.hasMoreTokens()) { String s = br.readLine(); if (s == null) { return null; } stok = new StringTokenizer(s); } return stok.nextToken(); } void print(byte b) { print("" + b); } void print(int i) { print("" + i); } void print(long l) { print("" + l); } void print(double d) { print("" + d); } void print(char c) { print("" + c); } void print(Object o) { if (o instanceof int[]) { print(Arrays.toString((int [])o)); } else if (o instanceof long[]) { print(Arrays.toString((long [])o)); } else if (o instanceof char[]) { print(Arrays.toString((char [])o)); } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o)); } else if (o instanceof short[]) { print(Arrays.toString((short [])o)); } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o)); } else if (o instanceof float[]) { print(Arrays.toString((float [])o)); } else if (o instanceof double[]) { print(Arrays.toString((double [])o)); } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o)); } else { print("" + o); } } void print(String s) { pw.print(s); } void println() { println(""); } void println(byte b) { println("" + b); } void println(int i) { println("" + i); } void println(long l) { println("" + l); } void println(double d) { println("" + d); } void println(char c) { println("" + c); } void println(Object o) { print(o); println(); } void println(String s) { pw.println(s); } int nextInt() throws IOException { return Integer.parseInt(nextToken()); } long nextLong() throws IOException { return Long.parseLong(nextToken()); } double nextDouble() throws IOException { return Double.parseDouble(nextToken()); } char nextChar() throws IOException { return (char) (br.read()); } String next() throws IOException { return nextToken(); } String nextLine() throws IOException { return br.readLine(); } int [] readInt(int size) throws IOException { int [] array = new int [size]; for (int i = 0; i < size; i++) { array[i] = nextInt(); } return array; } long [] readLong(int size) throws IOException { long [] array = new long [size]; for (int i = 0; i < size; i++) { array[i] = nextLong(); } return array; } double [] readDouble(int size) throws IOException { double [] array = new double [size]; for (int i = 0; i < size; i++) { array[i] = nextDouble(); } return array; } String [] readLines(int size) throws IOException { String [] array = new String [size]; for (int i = 0; i < size; i++) { array[i] = nextLine(); } return array; } int gcd(int a, int b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); } else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } long gcd(long a, long b) { if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a); a = Math.abs(a); b = Math.abs(b); int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b); a >>>= az; b >>>= bz; while (a != b) { if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); } else { b -= a; b >>>= Long.numberOfTrailingZeros(b); } } return (a << Math.min(az, bz)); } void shuffle(int [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); int t = a[i]; a[i] = a[j]; a[j] = t; } } void shuffle(long [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); long t = a[i]; a[i] = a[j]; a[j] = t; } } void shuffle(Object [] a) { Random r = new Random(); for (int i = a.length - 1; i >= 0; i--) { int j = r.nextInt(a.length); Object t = a[i]; a[i] = a[j]; a[j] = t; } } void flush() { pw.flush(); } void pause() { flush(); System.console().readLine(); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Scanner; /** * Built using CHelper plug-in * Actual solution is at the top * * @author Hung Vu */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BKvassAndTheFairNut solver = new BKvassAndTheFairNut(); solver.solve(1, in, out); out.close(); } static class BKvassAndTheFairNut { public void solve(int testNumber, Scanner in, PrintWriter out) { long n = in.nextLong(); long s = in.nextLong(); long[] a = new long[(int) n]; long min = Integer.MAX_VALUE; for (int i = 0; i < n; i++) { a[i] = in.nextInt(); min = Math.min(min, a[i]); } for (int i = 0; i < n; i++) { s -= a[i] - min; } if (s <= 0) out.println(min); else { if (s % n != 0) min--; min -= (s / n); if (min >= 0) out.println(min); else out.println(-1); } } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.*; import java.lang.*; import java.io.*; public class Main { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } static FastReader s = new FastReader(); public static void solve(){ int n = s.nextInt(); Long k = s.nextLong(); int arr[] = new int[n]; Long ans = 0L; int mn = (int) 1e9; for(int i=0;i<n;i++){ arr[i] = s.nextInt(); ans+=arr[i]; if(arr[i]<mn) mn=arr[i]; } if(ans<k) System.out.println(-1); else System.out.println(Math.min((ans-k)/n,mn)); } public static void main(String[] args) { int T=1; // int T = s.nextInt(); while(T>0){ // System.out.println("TEST CASE: "+T); solve(); T--; } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.*; public class NutKvass { public static void main(String[] args) { Scanner kb= new Scanner(System.in); long kegs= kb.nextLong(); long liters= kb.nextLong(); long k, sum= 0, min= 2147483647; for (int i= 0; i<kegs; i++) { k= kb.nextLong(); sum+= k; if (k<min) { min= k; } } if (liters>sum) System.out.println(-1); else System.out.println(Math.min(min, (sum-liters)/kegs)); kb.close(); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.stream.IntStream; import java.util.Arrays; import java.util.Scanner; import java.util.OptionalInt; /** * Built using CHelper plug-in * Actual solution is at the top * * @author ky112233 */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; Scanner in = new Scanner(inputStream); PrintWriter out = new PrintWriter(outputStream); BKvassAndTheFairNut solver = new BKvassAndTheFairNut(); solver.solve(1, in, out); out.close(); } static class BKvassAndTheFairNut { public void solve(int testNumber, Scanner in, PrintWriter out) { int n = in.nextInt(); long s = in.nextLong(); int[] v = new int[n]; for (int i = 0; i < n; i++) v[i] = in.nextInt(); int mn = Arrays.stream(v).min().getAsInt(); long sum = 0; for (int i = 0; i < n; i++) { sum += v[i] - mn; } if (sum >= s) { out.println(mn); return; } long temp = s - sum; if (temp % n == 0) { temp /= n; } else { temp = temp / n + 1; } if (temp > mn) { out.println(-1); } else { out.println(mn - temp); } } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; import java.util.StringTokenizer; import java.util.*; public class KvassandtheFairNut { static class FastReader { BufferedReader br; StringTokenizer st; public FastReader() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine() { String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public static void main(String[] args) { FastReader sc = new FastReader(); int n=sc.nextInt(); long s=sc.nextLong(); long sum=0; long a[]=new long[n]; for(int i=0;i<n;i++) { a[i]=sc.nextLong(); sum=sum+a[i]; } if(sum<s) { System.out.println("-1"); } else { int flag=0; Arrays.sort(a); long sum1=0; for(int i=n-1;i>0;i--) { sum1=sum1+(a[i]-a[0]); a[i]=a[0]; if(sum1>=s) { flag=1; System.out.println(a[0]); break; } } if(flag==0) { long rem=s-sum1; // System.out.println("a[0]:" +a[0]); // System.out.println("rem:" +rem); if(rem%n==0) { System.out.println(a[0]-(rem/n)); } else { System.out.println(a[0]-((rem/n)+1)); } } } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s = map(int,input().split()) mas = list(map(int,input().split())) m = min(mas) e,q=0,0 for i,x in enumerate(mas): e+=x-m q+=x if s>q: print(-1) else: if e>=s: print(m) else: a=s-e b,c=a//n,a%n if c>0: b+=1 print(m-b)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long v[1005], sum, mn; int main() { long long n, s; int i; scanf("%lld%lld", &n, &s); sum = 0; for (i = 0; i < n; i++) { scanf("%lld", &v[i]); } mn = v[0]; for (i = 0; i < n; i++) { mn = min(v[i], mn); sum += v[i]; } if (sum < s) { printf("-1\n"); return 0; } if (sum - n * mn >= s) { printf("%lld", mn); return 0; } s -= sum - n * mn; printf("%lld", mn - ((s - 1) / n + 1)); return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, k = map(int, input().split()) a = [] a = list(map(int, input().split())) mi = min(a) su = sum(a) if su < k: print(-1) else: print(min(mi, (su - k)//n))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.util.Arrays; import java.io.BufferedWriter; import java.io.Writer; import java.io.OutputStreamWriter; import java.util.InputMismatchException; import java.io.IOException; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); OutputWriter out = new OutputWriter(outputStream); Task526B solver = new Task526B(); solver.solve(1, in, out); out.close(); } static class Task526B { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.nextInt(); long s = in.nextLong(); long arr[] = new long[n]; long sum = 0, diff = 0; long min = 99999; int i, j; for (i = 0; i < n; i++) { arr[i] = in.nextLong(); sum += arr[i]; } if (s > sum) { out.println(-1); return; } Arrays.sort(arr); min = arr[0]; for (i = 1; i < n; i++) { long o = arr[i] - arr[0]; diff += o; } s -= diff; if (s <= 0) { out.println(min); return; } min -= s / n; if (s % n != 0) min--; out.println(min); } } static class OutputWriter { private final PrintWriter writer; public OutputWriter(OutputStream outputStream) { writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream))); } public OutputWriter(Writer writer) { this.writer = new PrintWriter(writer); } public void close() { writer.close(); } public void println(long i) { writer.println(i); } public void println(int i) { writer.println(i); } } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private InputReader.SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars == -1) { throw new InputMismatchException(); } if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (numChars <= 0) { return -1; } } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') { throw new InputMismatchException(); } res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public boolean isSpaceChar(int c) { if (filter != null) { return filter.isSpaceChar(c); } return isWhitespace(c); } public static boolean isWhitespace(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, m = map(int, input().split()) a = list(map(int, input().split())) print(-1 if sum(a) < m else min(min(a), (sum(a) - m) // n))
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.*; import java.lang.*; import java.math.*; import java.io.*; import static java.lang.Math.*; public class Solution{ static InputReader sc; static PrintWriter w; public static void main(String[] args) { sc = new InputReader(System.in); w = new PrintWriter(System.out); int n=sc.nextInt(),i; long s=sc.nextLong(); long arr[]=new long[n],sum=0,min=Long.MAX_VALUE,index; for(i=0;i<n;i++){ arr[i]=sc.nextLong(); sum+=arr[i]; if(min>arr[i]){ min=arr[i]; index=i; } } if(s>sum){ w.println(-1); w.close(); return; } for(i=0;i<n;i++){ if(s>0){ s-=(arr[i]-min); arr[i]=min; } } //w.println(s); if(s<=0){ w.println(min); w.close(); return; } long c=s/n; s-=c*n; long rem=min-c; if(s!=0){ rem--; } /*for(i=0;i<n;i++){ w.print(arr[i]+" "); }*/ w.println(rem); w.close(); } static class InputReader { private InputStream stream; private byte[] buf = new byte[1024]; private int curChar; private int numChars; private SpaceCharFilter filter; public InputReader(InputStream stream) { this.stream = stream; } public int read() { if (numChars==-1) throw new InputMismatchException(); if (curChar >= numChars) { curChar = 0; try { numChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if(numChars <= 0) return -1; } return buf[curChar++]; } public String nextLine() { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } public int nextInt() { int c = read(); while(isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { if(c<'0'||c>'9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public double nextDouble() { int c = read(); while (isSpaceChar(c)) c = read(); int sgn = 1; if (c == '-') { sgn = -1; c = read(); } double res = 0; while (!isSpaceChar(c) && c != '.') { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); res *= 10; res += c - '0'; c = read(); } if (c == '.') { c = read(); double m = 1; while (!isSpaceChar(c)) { if (c == 'e' || c == 'E') return res * Math.pow(10, nextInt()); if (c < '0' || c > '9') throw new InputMismatchException(); m /= 10; res += (c - '0') * m; c = read(); } } return res * sgn; } public String readString() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public boolean isSpaceChar(int c) { if (filter != null) return filter.isSpaceChar(c); return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public String next() { return readString(); } public interface SpaceCharFilter { public boolean isSpaceChar(int ch); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = map(int, input().split()) v = [int(i) for i in input().split()] v.sort() sum_ = 0 for i in range(n): sum_ += v[i] - v[0] if sum_ >= s: print(v[0]) else: print((n * v[0] - s + sum_) // n if (n * v[0] - s + sum_) >= 0 else -1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import math n, s = map(int, input().split(" ")) ar = list(map(int, input().split(" "))) sm = sum(ar) if s > sm: print(-1) else: mn = min(ar) ex = (sm - mn) - (n-1)*mn if ex < s: print(mn - math.ceil((s-ex)/n)) else: print(mn)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.*; import java.io.*; import java.math.*; public class Main { static class Reader { private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public Reader() { this(System.in); }public Reader(InputStream is) { mIs = is;} public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];} public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;} public String s(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();} public long l(){int c = read();while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }long res = 0; do{ if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read();}while(!isSpaceChar(c));return res * sgn;} public int i(){int c = read() ;while (isSpaceChar(c)) c = read();int sgn = 1;if (c == '-') { sgn = -1 ; c = read() ; }int res = 0;do{if (c < '0' || c > '9') throw new InputMismatchException();res *= 10 ; res += c - '0' ; c = read() ;}while(!isSpaceChar(c));return res * sgn;} public double d() throws IOException {return Double.parseDouble(s()) ;} public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } public int[] arr(int n){int[] ret = new int[n];for (int i = 0; i < n; i++) {ret[i] = i();}return ret;} } // |----| /\ | | ----- | // | / / \ | | | | // |--/ /----\ |----| | | // | \ / \ | | | | // | \ / \ | | ----- ------- public static void main(String[] args)throws IOException { PrintWriter out= new PrintWriter(System.out); Reader sc=new Reader(); int n=sc.i(); long s=sc.l(); long arr[]=new long[n]; long min=2000000000; for(int i=0;i<n;i++) { arr[i]=sc.l(); min=Math.min(min,arr[i]); } long low=0; long high=min; while(low<high) { long mid=(low+high+1)/2; long ans=0; int c=0; for(int i=0;i<n;i++) { ans+=Math.max(0l,arr[i]-mid); } if(ans<s) high=mid-1; else low=mid; } long ans=0; for(int i=0;i<n;i++) ans+=Math.max(0l,arr[i]-low); if(ans<s) out.println(-1); else out.println(low); out.flush(); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.*; import java.math.*; public class bhaa { InputStream is; PrintWriter o; /////////////////// CODED++ BY++ ++ ++ ++ BHAVYA++ ARORA++ ++ ++ ++ FROM++ JAYPEE++ INSTITUTE++ OF++ INFORMATION++ TECHNOLOGY++ //////////////// ///////////////////////// Make it work, make it right, make it fast. Make it work, make it right, make it fast. Make it work, make it right, make it fast. Make it work, make it right, make it fast. ///////////////// void solve() { int n=ni(); long s=nl(); long arr[]=nla(n); long min=Long.MAX_VALUE; for(int i=0;i<n;i++) { if(arr[i]<min) { min=arr[i]; } } long extra=0; for(int i=0;i<n;i++) { extra+=arr[i]-min; } if(extra-s>=0) { o.println(min); } else { long rem=s-extra; long quo=rem/n; long remainder=rem%n; if(remainder>0) { quo++; } //if(quo>min) { if(quo<=min) { o.println(min-quo); } else { o.println(-1); } } } } //---------- I/O Template ---------- public static void main(String[] args) { new bhaa().run(); } void run() { is = System.in; o = new PrintWriter(System.out); solve(); o.flush(); } byte input[] = new byte[1024]; int len = 0, ptr = 0; int readByte() { if(ptr >= len) { ptr = 0; try { len = is.read(input); } catch(IOException e) { throw new InputMismatchException(); } if(len <= 0) { return -1; } } return input[ptr++]; } boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); } int skip() { int b = readByte(); while(b != -1 && isSpaceChar(b)) { b = readByte(); } return b; } char nc() { return (char)skip(); } String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } String nLine() { int b = skip(); StringBuilder sb = new StringBuilder(); while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } int ni() { int n = 0, b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } if(b == '-') { minus = true; b = readByte(); } if(b == -1) { return -1; } //no input while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n; } long nl() { long n = 0L; int b = readByte(); boolean minus = false; while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); } if(b == '-') { minus = true; b = readByte(); } while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); } return minus ? -n : n; } double nd() { return Double.parseDouble(ns()); } float nf() { return Float.parseFloat(ns()); } int[] nia(int n) { int a[] = new int[n]; for(int i = 0; i < n; i++) { a[i] = ni(); } return a; } long[] nla(int n) { long a[] = new long[n]; for(int i = 0; i < n; i++) { a[i] = nl(); } return a; } int [][] nim(int n) { int mat[][]=new int[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { mat[i][j]=ni(); } } return mat; } long [][] nlm(int n) { long mat[][]=new long[n][n]; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { mat[i][j]=nl(); } } return mat; } char[] ns(int n) { char c[] = new char[n]; int i, b = skip(); for(i = 0; i < n; i++) { if(isSpaceChar(b)) { break; } c[i] = (char)b; b = readByte(); } return i == n ? c : Arrays.copyOf(c,i); } void piarr(int arr[]) { for(int i=0;i<arr.length;i++) { o.print(arr[i]+" "); } o.println(); } void plarr(long arr[]) { for(int i=0;i<arr.length;i++) { o.print(arr[i]+" "); } o.println(); } void pimat(int mat[][]) { for(int i=0;i<mat.length;i++) { for(int j=0;j<mat[0].length;j++) { o.print(mat[i][j]); } o.println(); } } void plmat(long mat[][]) { for(int i=0;i<mat.length;i++) { for(int j=0;j<mat[0].length;j++) { o.print(mat[i][j]); } o.println(); } } //////////////////////////////////// template finished ////////////////////////////////////// }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long MAXN = 2e5 + 30; const long long MINN = 1e3 + 20; const long long MOD2 = 998244353ll; const long long INF = 74592896151251; const long double EPS = 1e-9; long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); } long long mod(long long n) { while (n < 0) n += MOD; return n % MOD; } long long pow(long long a, long long b) { return (!b ? 1 : pow(a, b / 2) * pow(a, b / 2) * (b % 2 ? a : 1)); } long long a[MAXN]; int Main() { long long n; cin >> n; long long s; cin >> s; long long d = 0; long long sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } if (sum < s) { return cout << -1, 0; } sort(a, a + n); for (int i = 1; i < n; i++) d += a[i] - a[0]; if (d >= s) { cout << a[0]; return 0; } s -= d; long long ans = a[0]; ans -= (s + n - 1) / n; cout << ans; } int main() { ios::sync_with_stdio(0), cin.tie(0); int q; q = 1; while (q--) Main(); return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; int n; long long s, sum, mn = 1000ll * 1000ll * 1000ll * 1000ll * 1000ll * 1000ll; int main() { cin >> n >> s; for (int i = 0; i < n; i++) { long long x; cin >> x; mn = min(mn, x); sum += x; } if (sum < s) cout << -1 << endl; else cout << min((sum - s) / (long long)n, mn) << endl; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.util.Scanner; public class BKvass { public static void main(String[] args) { Scanner scn = new Scanner(System.in); int n = scn.nextInt(); long s = scn.nextLong(); long[] a = new long[n]; for (int i = 0 ; i < n; i++) a[i] = scn.nextInt(); System.out.println(kvass(a, n, s)); } private static long kvass(long[] a, int n, long s) { long min = Long.MAX_VALUE; for (long x : a) min = Math.min(min, x); for (long x : a) { s -= (x - min); if (s <= 0) return min; } //Now we have min in all kegs but we still need to draw s > 0 long fromeach = s/n; long frommin = fromeach + (s%n == 0 ? 0 : 1); if (min - frommin >= 0) return min-frommin; return -1; } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.PriorityQueue; import java.util.StringTokenizer; import java.util.stream.IntStream; public class B { public static void main(String[] args) throws IOException { try (Input input = new StandardInput(); PrintWriter writer = new PrintWriter(System.out)) { int n = input.nextInt(); long s = input.nextLong(); int[] volumes = new int[n]; for (int i = 0; i < n; i++) { volumes[i] = input.nextInt(); } int min = IntStream.of(volumes).min().orElse(0); long sum = IntStream.of(volumes).asLongStream().sum(); if (sum < s) { writer.println(-1); } else { long excess = IntStream.of(volumes).map(x -> x - min).asLongStream().sum(); if (excess >= s) { writer.println(min); } else { s -= excess; writer.println(min - (s + n - 1) / n); } } } } interface Input extends Closeable { String next() throws IOException; default int nextInt() throws IOException { return Integer.parseInt(next()); } default long nextLong() throws IOException { return Long.parseLong(next()); } } private static class StandardInput implements Input { private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); private StringTokenizer stringTokenizer; @Override public void close() throws IOException { reader.close(); } @Override public String next() throws IOException { if (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) { stringTokenizer = new StringTokenizer(reader.readLine()); } return stringTokenizer.nextToken(); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = list(map(int, input().split())) cl = list(map(int, input().split())) cl = sorted(cl) mn = cl[0] sm = sum(cl) if sm<s: print(-1) else: t = s-(sm-n*mn) if t<=0: print(mn) else: k = (t-1)//n+1 print(mn-k)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.Arrays; import java.util.Scanner; public class B { private void work() { Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in))); int n = sc.nextInt(); long s = sc.nextLong(); long[] v = new long[n]; long total = 0; for (int i = 0; i < n; i++) total += (v[i] = sc.nextInt()); sc.close(); if (total < s) { System.out.println("-1"); System.exit(0); } Arrays.sort(v); if (total - n * v[0] >= s) { System.out.println(v[0]); System.exit(0); } s -= total - n * v[0]; System.out.println(v[0] - (s + n - 1) / n); } public static void main(String[] args) { new B().work(); } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; using lint = long long int; lint liters(int l, vector<int> &arr) { lint sum = 0; for (int k : arr) sum += (lint)(k - l); return sum; } int main() { int n, m = 1000000010; lint s; vector<int> arr; cin >> n >> s; arr.resize(n); for (int i = 0; i < n; i++) { cin >> arr[i]; m = min(m, arr[i]); } if (s > liters(0, arr)) cout << -1 << endl; else { int start = 0, end = m; while (start < end) { int mid = (start + end) / 2 + 1; if (s <= liters(mid, arr)) start = mid; else end = mid - 1; } cout << start << endl; } return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s=map(int,input().split()) v=list(map(int,input().split())) m=min(v) if s>sum(v): print("-1") exit() v=sorted(v) total=0 for i in v: total+=(i-m) if total>=s: print(m) else: print((sum(v)-s)//n)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long v[1010]; int main() { long long s; long long n, mn = 1e17, sum = 0; cin >> n >> s; for (int i = 0; i < n; i++) { cin >> v[i]; sum += v[i]; mn = min(v[i], mn); } if (sum < s) { cout << -1; return 0; } for (int i = 0; i < n; i++) s -= (v[i] - mn); if (s <= 0) cout << mn; else cout << mn - (s + n - 1) / n; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { static class InputReader { private final InputStream stream; private final byte[] buf = new byte[8192]; private int curChar, snumChars; public InputReader(InputStream st) { this.stream = st; } public int read() { if (snumChars == -1) throw new InputMismatchException(); if (curChar >= snumChars) { curChar = 0; try { snumChars = stream.read(buf); } catch (IOException e) { throw new InputMismatchException(); } if (snumChars <= 0) return -1; } return buf[curChar++]; } public int nextInt() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } int res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public long nextLong() { int c = read(); while (isSpaceChar(c)) { c = read(); } int sgn = 1; if (c == '-') { sgn = -1; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return res * sgn; } public int[] nextIntArray(int n) { int a[] = new int[n]; for (int i = 0; i < n; i++) { a[i] = nextInt(); } return a; } public String readString() { int c = read(); while (isSpaceChar(c)) { c = read(); } StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isSpaceChar(c)); return res.toString(); } public String nextLine() { int c = read(); while (isSpaceChar(c)) c = read(); StringBuilder res = new StringBuilder(); do { res.appendCodePoint(c); c = read(); } while (!isEndOfLine(c)); return res.toString(); } public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; } private boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; } } public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] str = (br.readLine()).trim().split(" "); int n = Integer.parseInt(str[0]); long s = Long.parseLong(str[1]); long sum = 0; str = (br.readLine()).trim().split(" "); int[] arr = new int[n]; int min = Integer.MAX_VALUE; for(int i=0;i<n;i++) { arr[i] = Integer.parseInt(str[i]); if(arr[i] < min) min = arr[i]; } for(int i=0;i<n;i++) { arr[i] = arr[i] - min; sum = sum + (long)arr[i]; } int ans = min; if(sum >= s) { System.out.println(min); } else if(sum < s) { s = s - sum; long temp = s/n; if(s % n != 0) temp++; if(min - temp >= 0) { System.out.println(min-temp); } else System.out.println("-1"); } } }
JAVA
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long n, s, i, j, a[10001], MIN; long long m_min(long long a, long long b) { if (a < b) return a; else return b; } int main() { scanf("%lld%lld", &n, &s); for (i = 1; i <= n; i++) scanf("%lld", &a[i]); long long sum = 0; MIN = 1e18; for (i = 1; i <= n; i++) { sum += a[i]; MIN = m_min(MIN, a[i]); } if (sum < s) printf("-1\n"); else printf("%lld\n", m_min(MIN, (sum - s) / n)); }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s=map(int,input().split()) arr=[int(x) for x in input().split()] arr.sort(reverse=True) #print(arr) flag=0 for i in range(n-1): s-=min(s,arr[i]-arr[-1]) if(s!=0):arr[i]=arr[-1] if(s==0): flag=1 break #print(flag) if(flag==1):print(arr[-1]) else: #print(arr) arr[-1]-=s//n if(s%n!=0):arr[-1]-=1 if(arr[-1]>=0):print(arr[-1]) else:print(-1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
import math n,s = map(int,input().split()) v = input().split() mini = math.inf for i in range(n): mini = min(int(v[i]),mini) s += mini*n for i in range(n): s -= int(v[i]) if s < 0: print(mini) elif s > mini*n: print(-1) elif s%n == 0: print(mini-s//n) else: print(mini-s//n-1)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n, s = [int(x) for x in input().split()] a = [int(x) for x in input().split()] sum = 0 for i in a: sum += i if sum < s: print(-1) exit(0) a.sort() for i in range(len(a)): c = min(s, max(0, a[n - i - 1] - a[0])) s -= c a[n-i-1] -= c if s <= 0: a.sort() print(a[0]) #print(1111) else: #print(s) #print(a) print( a[0] - (s + n - 1) // n) #print(2222)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, s; cin >> n >> s; long long a[n]; long long sum = 0; for (long long i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } if (sum < s) cout << "-1"; else { long x = sizeof(a) / sizeof(a[0]); sort(a, a + x); long long it = n - 1; while (s > 0) { if (it == 0) { break; } else { s = s - (a[it] - a[0]); a[it] -= (a[it] - a[0]); it--; } } if (it == 0 && s > 0) { long long ax; if (s % n == 0) ax = (s / n); else ax = (s / n) + 1; cout << (a[0] - ax); } else cout << a[0]; } return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; using lld = long long int; int main() { lld n, s; cin >> n >> s; vector<lld> V(n); lld su = 0; for (int i = 0; i < n; i++) { lld x; cin >> x; su += x; V[i] = x; } if (su < s) { cout << -1 << endl; } else { lld fr = 0, mn = *min_element(V.begin(), V.end()); for (int i = 0; i < n; i++) { fr += V[i] - mn; V[i] = mn; } if (fr >= s) { cout << mn << endl; } else { s -= fr; if (s % n == 0) { cout << mn - s / n << endl; } else { cout << mn - s / n - 1 << endl; } } } return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long n, m, i, j, sum, sum1, ans, sub, arr[1000000]; int main() { scanf("%lld %lld", &n, &m); for (i = 0; i < n; i++) { scanf("%lld", &arr[i]); sum = sum + arr[i]; } if (sum < m) { printf("-1\n"); } else if (sum == m) { printf("0\n"); } else { sort(arr, arr + n); sum1 = 0; for (i = 1; i < n; i++) { sum1 = sum1 + (abs(arr[i] - arr[0])); } if (sum1 >= m) { printf("%lld", arr[0]); } else { sum1 = m - sum1; if (sum1 % n == 0) { sub = sum1 / n; } else { sub = (sum1 / n) + 1; } ans = arr[0] - sub; printf("%lld\n", ans); } } }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; long long A[2222]; int n; long long kvass(long long c) { long long ans = 0; for (int i = 0; i < (n); i++) ans += A[i] > c ? A[i] - c : 0; return ans; } long long bs(long long lo, long long hi, long long s) { if (lo >= hi) return lo; long long c = (lo + hi) / 2; if (kvass(c) >= s) return bs(c + 1, hi, s); else return bs(lo, c, s); } int main() { long long s; scanf("%i %lli", &n, &s); for (int i = 0; i < (n); i++) scanf(" %lli", A + i); long long tot = 0; for (int i = 0; i < (n); i++) tot += A[i]; if (s > tot) { printf("-1\n"); return 0; } long long ans = bs(0, 0x7fffffffL, s) - 1; for (int i = 0; i < (n); i++) if (A[i] < ans) ans = A[i]; printf("%lli\n", ans); }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
n,s = map(int,input().split()) arr = list(map(int,input().split())) x = sum(arr) y = min(arr) if x < s: print(-1) else: z = x - y*n if z < s: s -= z if s % n == 0: print(y - s//n) else: print(y - s//n - 1) else: print(y)
PYTHON3
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; int main() { long long n, s, sum = 0; cin >> n >> s; long long a[1001]; for (long long i = 0; i < n; ++i) { cin >> a[i]; sum += a[i]; } if (sum < s) { cout << -1; return 0; } long long mn = *min_element(a, a + n); for (long long i = 0; i < n; ++i) { s = s - (a[i] - mn); } s > 0 ? cout << mn - static_cast<long long>(ceil(static_cast<double>(s) / n)) : cout << mn; return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#include <bits/stdc++.h> using namespace std; int n; long long s, sum; long long a[1100]; int main() { scanf("%d%I64d", &n, &s); long long r = 2147483646; for (int i = 1; i <= n; i++) { scanf("%I64d", &a[i]); sum += a[i]; r = min(r, a[i]); } if (sum < s) { printf("-1"); return 0; } if (sum == s) { printf("0"); return 0; } printf("%I64d", min(r, (sum - s) / n)); return 0; }
CPP
1084_B. Kvass and the Fair Nut
The Fair Nut likes kvass very much. On his birthday parents presented him n kegs of kvass. There are v_i liters of kvass in the i-th keg. Each keg has a lever. You can pour your glass by exactly 1 liter pulling this lever. The Fair Nut likes this drink very much, so he wants to pour his glass by s liters of kvass. But he wants to do it, so kvass level in the least keg is as much as possible. Help him find out how much kvass can be in the least keg or define it's not possible to pour his glass by s liters of kvass. Input The first line contains two integers n and s (1 ≀ n ≀ 10^3, 1 ≀ s ≀ 10^{12}) β€” the number of kegs and glass volume. The second line contains n integers v_1, v_2, …, v_n (1 ≀ v_i ≀ 10^9) β€” the volume of i-th keg. Output If the Fair Nut cannot pour his glass by s liters of kvass, print -1. Otherwise, print a single integer β€” how much kvass in the least keg can be. Examples Input 3 3 4 3 5 Output 3 Input 3 4 5 3 4 Output 2 Input 3 7 1 2 3 Output -1 Note In the first example, the answer is 3, the Fair Nut can take 1 liter from the first keg and 2 liters from the third keg. There are 3 liters of kvass in each keg. In the second example, the answer is 2, the Fair Nut can take 3 liters from the first keg and 1 liter from the second keg. In the third example, the Fair Nut can't pour his cup by 7 liters, so the answer is -1.
2
8
#!/usr/bin/env python # coding: utf-8 # In[15]: import math ns=list(map(int, input().rstrip().split())) n=ns[0] s=ns[1] data=list(map(int, input().rstrip().split())) # In[16]: data.sort() # In[17]: extras=[i-data[0] for i in data] # In[18]: total=sum(data) extratotal=sum(extras) # In[ ]: # In[19]: if s>total: print(-1) elif extratotal>=s: print(data[0]) else: sub=math.ceil((s-extratotal)/n) print(data[0]-sub) # In[ ]:
PYTHON3
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } const int MAXN = 1000000; int n; long long skill; long long a[MAXN]; int cost[MAXN]; vector<long long> p; long long b[MAXN]; long long c[MAXN]; int nc; vector<int> copt[MAXN]; pair<int, int> ord[MAXN]; vector<int> freq; vector<pair<int, vector<int>>> opt; vector<vector<long long>> dp; void addopt(vector<int> &myopt, vector<long long> &myneed, long long prod, int mask, int at) { if (at >= ((int)(myneed).size())) { bool ok = true; for (int i = (0); i < (((int)(myneed).size())); ++i) if ((mask & (1 << i)) == 0 && prod * myneed[i] <= skill) ok = false; if (ok && mask != 0) myopt.push_back(mask); return; } addopt(myopt, myneed, prod, mask, at + 1); if (prod * myneed[at] <= skill) addopt(myopt, myneed, prod * myneed[at], mask | (1 << at), at + 1); } long long solve() { long long g = 0; for (int i = (0); i < (n); ++i) g = gcd(g, a[i]); { p.clear(); long long x = g; for (int i = 2; (long long)i * i <= x; ++i) if (x % i == 0) { p.push_back(i); while (x % i == 0) x /= i; } if (x != 1) p.push_back(x); } for (int i = (0); i < (n); ++i) { long long x = a[i]; for (int j = (0); j < (((int)(p).size())); ++j) while (x % p[j] == 0) x /= p[j]; b[i] = a[i] / x; } nc = 0; for (int i = (0); i < (n); ++i) c[nc++] = b[i]; sort(c, c + nc); nc = unique(c, c + nc) - c; for (int i = (0); i < (nc); ++i) { vector<long long> need(((int)(p).size())); for (int j = (0); j < (((int)(p).size())); ++j) { long long x = c[i]; while (x % p[j] == 0) x /= p[j]; need[j] = c[i] / x; } copt[i].clear(); addopt(copt[i], need, 1, 0, 0); } freq = vector<int>(1 << ((int)(p).size()), 0); opt.clear(); for (int i = (0); i < (n); ++i) ord[i] = make_pair(cost[i], i); sort(ord, ord + n); for (int i = (0); i < (n); ++i) { int at = ord[i].second; int idx = lower_bound(c, c + nc, b[at]) - c; bool any = false; for (int j = (0); j < (((int)(copt[idx]).size())); ++j) if (freq[copt[idx][j]] < ((int)(p).size())) any = true; if (!any) continue; vector<int> masks; for (int j = (0); j < (((int)(copt[idx]).size())); ++j) if (freq[copt[idx][j]] < ((int)(p).size())) { masks.push_back(copt[idx][j]); ++freq[copt[idx][j]]; } opt.push_back(make_pair(cost[at], masks)); } dp = vector<vector<long long>>( ((int)(p).size()) + 1, vector<long long>(1 << ((int)(p).size()), LLONG_MAX)); dp[0][0] = 0; for (int i = (0); i < (((int)(opt).size())); ++i) { for (int cnt = ((int)(p).size()) - 1; cnt >= 0; --cnt) for (int mask = (0); mask < (1 << ((int)(p).size())); ++mask) if (dp[cnt][mask] != LLONG_MAX) { for (int j = (0); j < (((int)(opt[i].second).size())); ++j) { int ncnt = cnt + 1, nmask = mask | opt[i].second[j]; long long ncost = dp[cnt][mask] + opt[i].first; dp[ncnt][nmask] = min(dp[ncnt][nmask], ncost); } } } long long ret = LLONG_MAX; for (int cnt = (0); cnt <= (((int)(p).size())); ++cnt) if (dp[cnt][(1 << ((int)(p).size())) - 1] != LLONG_MAX) ret = min(ret, cnt * dp[cnt][(1 << ((int)(p).size())) - 1]); return ret == LLONG_MAX ? -1 : ret; } void run() { scanf("%d%lld", &n, &skill); for (int i = (0); i < (n); ++i) scanf("%lld", &a[i]); for (int i = (0); i < (n); ++i) scanf("%d", &cost[i]); printf("%lld\n", solve()); } int main() { run(); return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; namespace io { const int L = (1 << 20) + 1; char buf[L], *S, *T, c; char getchar() { if (__builtin_expect(S == T, 0)) { T = (S = buf) + fread(buf, 1, L, stdin); return (S == T ? EOF : *S++); } return *S++; } int inp() { int x = 0, f = 1; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; return x * f; } unsigned inpu() { unsigned x = 0; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) ; for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; return x; } long long inp_ll() { long long x = 0; int f = 1; char ch; for (ch = getchar(); !isdigit(ch); ch = getchar()) if (ch == '-') f = -1; for (; isdigit(ch); x = x * 10 + ch - '0', ch = getchar()) ; return x * f; } char B[25], *outs = B + 20, *outr = B + 20; template <class T> inline void print(register T a, register char x = 0) { if (x) *--outs = x, x = 0; if (!a) *--outs = '0'; else while (a) *--outs = (a % 10) + 48, a /= 10; if (x) *--outs = x; fwrite(outs, outr - outs, 1, stdout); outs = outr; } }; // namespace io using io ::inp; using io ::inp_ll; using io ::inpu; using io ::print; using i32 = int; using i64 = long long; using u8 = unsigned char; using u32 = unsigned; using u64 = unsigned long long; using f64 = double; using f80 = long double; long long power(long long a, long long b, long long p) { if (!b) return 1; long long t = power(a, b / 2, p); t = t * t % p; if (b & 1) t = t * a % p; return t; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } template <class T> inline void freshmin(T &a, const T &b) { if (a > b) a = b; } template <class T> inline void freshmax(T &a, const T &b) { if (a < b) a = b; } const int MAXN = 1000010; const int MAXP = 10000000; const int MOD = 1000000007; const f80 MI = f80(1) / MOD; const long long INF = 10000000000000000LL; const long long inf = 5000000000000LL; int n; long long k; struct node { long long a; int e; bool operator<(const node &A) const { return e < A.e; } } a[MAXN]; int m; vector<long long> p; map<long long, int> H; int h[1 << 11], pcnt[1 << 11]; long long v[1 << 11]; long long F[12][1 << 11], G[12][1 << 11]; int main() { n = inp(); k = inp_ll(); for (int i = 1; i <= n; ++i) a[i].a = inp_ll(); for (int i = 1; i <= n; ++i) a[i].e = inp(); sort(a + 1, a + n + 1); long long d = a[1].a; for (int i = 1; i <= n; ++i) d = gcd(d, a[i].a); if (d == 1) { puts("0"); return 0; } for (long long i = 2; i * i <= d; ++i) if (d % i == 0) { p.push_back(i); while (d % i == 0) d /= i; } if (d > 1) p.push_back(d); m = p.size(); for (int i = 0; i <= m; ++i) for (int s = 1; s < 1 << m; ++s) F[i][s] = INF; for (int i = 1; i <= n; ++i) { long long cur = 1; for (int j = 0; j < m; ++j) { long long d = 1; while (a[i].a % p[j] == 0) { d *= p[j]; a[i].a /= p[j]; } cur *= d; v[1 << j] = d; } if (++H[cur] > m) continue; memcpy(G, F, sizeof(F)); v[0] = 1; for (int s = 1; s < 1 << m; ++s) { int low = s & -s; v[s] = v[s ^ low] * v[low]; if (v[s] > k) continue; if (++h[s] > m) continue; int c = ((1 << m) - 1) ^ s; for (int x = 0; x < m; ++x) { for (int t = c; t; t = (t - 1) & c) freshmin(F[x + 1][t | s], G[x][t] + a[i].e); freshmin(F[x + 1][s], (long long)a[i].e); } } } long long ans = INF; for (int i = 1; i <= m; ++i) ans = min(ans, F[i][(1 << m) - 1] * i); if (ans == INF) ans = -1; cout << ans << endl; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; namespace Flandre_Scarlet { long long I() { char c = getchar(); long long x = 0; long long f = 1; while (c < '0' or c > '9') f = (c == '-') ? -1 : 1, c = getchar(); while (c >= '0' and c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return ((f == 1) ? x : -x); } template <typename T> void Rd(T &arg) { arg = I(); } template <typename T, typename... Types> void Rd(T &arg, Types &...args) { arg = I(); Rd(args...); } void RA(long long *p, long long n) { for (long long i = 1; i <= n; ++i) *p = I(), ++p; } long long primes[1000006]; bool notp[1000006]; void init() { long long &cnt = primes[0]; long long n = 1e6; notp[1] = 1; for (long long i = 2; i <= n; ++i) { if (!notp[i]) primes[++cnt] = i; for (long long j = 1; j <= cnt and i * primes[j] <= n; ++j) { long long u = primes[j]; notp[i * u] = 1; if (i % u == 0) break; } } } struct node { long long val, cost; } a[1000006]; bool operator<(node a, node b) { return a.cost < b.cost; } long long n, d; void Input() { Rd(n, d); for (long long i = 1; i <= n; ++i) a[i].val = I(); for (long long i = 1; i <= n; ++i) a[i].cost = I(); } long long pr[20], pw[20], ptot = 0; long long gcd(long long a, long long b) { while (b) swap(a, b), b %= a; return a; } void decompose(long long x) { for (long long i = 1; i <= primes[0]; ++i) { long long u = primes[i]; if (u > x) break; if (x % u == 0) { pr[ptot] = u; pw[ptot] = 0; while (x % u == 0) { ++pw[ptot]; x /= u; } ++ptot; } } if (x > 1) { pr[ptot] = x; pw[ptot] = 1; ++ptot; } } long long num[1 << 12]; map<long long, long long> vis; long long svis[1 << 12]; long long dp[2][20][1 << 12]; void Sakuya() { init(); long long g = 0; for (long long i = 1; i <= n; ++i) g = gcd(g, a[i].val); if (g == 1) { puts("0"); return; } decompose(g); long long U = (1 << ptot) - 1; sort(a + 1, a + n + 1); long long cur = 0; memset(dp, 0x3f3f3f3f3f3f3f3fll, sizeof(dp)); dp[0][0][0] = 0; for (long long i = 1; i <= n; ++i) { long long b = 1, val = a[i].val; for (long long j = 0; j <= ptot - 1; ++j) if (val % pr[j] == 0) { long long t = 1; while (val % pr[j] == 0) t *= pr[j], val /= pr[j]; num[1 << j] = t; b *= t; } val = b; if (++vis[val] > ptot) continue; num[0] = 1; cur ^= 1; memcpy(dp[cur], dp[cur ^ 1], sizeof(dp[cur])); for (long long s = 1; s <= U; ++s) { num[s] = num[s ^ (s & -s)] * num[s & -s]; if (num[s] > d) continue; if (++svis[s] > ptot) continue; long long x = (~s) & U; for (long long t = 0; t <= ptot - 1; ++t) { for (long long y = x; y; y = (y - 1) & x) if (dp[cur ^ 1][t][y] < 0x3f3f3f3f3f3f3f3fll) { dp[cur][t + 1][s ^ y] = min(dp[cur][t + 1][s ^ y], dp[cur ^ 1][t][y] + a[i].cost); } dp[cur][t + 1][s] = min(dp[cur][t + 1][s], dp[cur ^ 1][t][0] + a[i].cost); } } } long long ans = 0x3f3f3f3f3f3f3f3fll; for (long long c = 1; c <= ptot; ++c) if (dp[cur][c][U] < 0x3f3f3f3f3f3f3f3fll) { ans = min(ans, dp[cur][c][U] * c); } printf("%lld\n", ans >= 0x3f3f3f3f3f3f3f3fll ? -1 : ans); } void IsMyWife() { Input(); Sakuya(); } } // namespace Flandre_Scarlet int main() { Flandre_Scarlet::IsMyWife(); getchar(); return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; namespace Flandre_Scarlet { long long I() { char c = getchar(); long long x = 0; long long f = 1; while (c < '0' or c > '9') f = (c == '-') ? -1 : 1, c = getchar(); while (c >= '0' and c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); return ((f == 1) ? x : -x); } template <typename T> void Rd(T &arg) { arg = I(); } template <typename T, typename... Types> void Rd(T &arg, Types &...args) { arg = I(); Rd(args...); } void RA(long long *p, long long n) { for (long long i = 1; i <= n; ++i) *p = I(), ++p; } long long primes[1000006]; bool notp[1000006]; void init() { long long &cnt = primes[0]; long long n = 1e6; notp[1] = 1; for (long long i = 2; i <= n; ++i) { if (!notp[i]) primes[++cnt] = i; for (long long j = 1; j <= cnt and i * primes[j] <= n; ++j) { long long u = primes[j]; notp[i * u] = 1; if (i % u == 0) break; } } } struct node { long long val, cost; } a[1000006]; bool operator<(node a, node b) { return a.cost < b.cost; } long long n, d; void Input() { Rd(n, d); for (long long i = 1; i <= n; ++i) a[i].val = I(); for (long long i = 1; i <= n; ++i) a[i].cost = I(); } long long pr[20], pw[20], ptot = 0; long long gcd(long long a, long long b) { while (b) swap(a, b), b %= a; return a; } void decompose(long long x) { for (long long i = 1; i <= primes[0]; ++i) { long long u = primes[i]; if (u > x) break; if (x % u == 0) { pr[ptot] = u; pw[ptot] = 0; while (x % u == 0) { ++pw[ptot]; x /= u; } ++ptot; } } if (x > 1) { pr[ptot] = x; pw[ptot] = 1; ++ptot; } } long long num[1 << 12]; map<long long, long long> vis; long long svis[1 << 12]; long long dp[2][20][1 << 12]; void Sakuya() { init(); long long g = 0; for (long long i = 1; i <= n; ++i) g = gcd(g, a[i].val); if (g == 1) { puts("0"); return; } decompose(g); long long U = (1 << ptot) - 1; sort(a + 1, a + n + 1); long long cur = 0; memset(dp, 0x3f3f3f3f3f3f3f3fll, sizeof(dp)); dp[0][0][0] = 0; for (long long i = 1; i <= n; ++i) { long long b = 1, val = a[i].val; for (long long j = 0; j <= ptot - 1; ++j) if (val % pr[j] == 0) { long long t = 1; while (val % pr[j] == 0) t *= pr[j], val /= pr[j]; num[1 << j] = t; b *= t; } val = b; if (++vis[val] > ptot) continue; num[0] = 1; cur ^= 1; memcpy(dp[cur], dp[cur ^ 1], sizeof(dp[cur])); for (long long s = 1; s <= U; ++s) { num[s] = num[s ^ (s & -s)] * num[s & -s]; if (num[s] > d) continue; if (++svis[s] > ptot) continue; long long x = (~s) & U; for (long long t = 0; t <= ptot - 1; ++t) { for (long long y = x; y; y = (y - 1) & x) if (dp[cur ^ 1][t][y] < 0x3f3f3f3f3f3f3f3fll) { dp[cur][t + 1][s ^ y] = min(dp[cur][t + 1][s ^ y], dp[cur ^ 1][t][y] + a[i].cost); } dp[cur][t + 1][s] = min(dp[cur][t + 1][s], dp[cur ^ 1][t][0] + a[i].cost); } } } long long ans = 0x3f3f3f3f3f3f3f3fll; for (long long c = 1; c <= ptot; ++c) if (dp[cur][c][U] < 0x3f3f3f3f3f3f3f3fll) { ans = min(ans, dp[cur][c][U] * c); } printf("%lld\n", ans >= 0x3f3f3f3f3f3f3f3fll ? -1 : ans); } void IsMyWife() { Input(); Sakuya(); } } // namespace Flandre_Scarlet int main() { Flandre_Scarlet::IsMyWife(); getchar(); return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { while (a > 0 && b > 0) { if (a < b) swap(a, b); a %= b; } return a + b; } const int maxN = (int)1e6 + 10; long long a[maxN]; long long k; vector<long long> factor(long long x) { vector<long long> cur; for (int i = 2; 1LL * i * i <= x; i++) { if (x % i == 0) { while (x % i == 0) { x /= i; } cur.push_back(i); } } if (x > 1) cur.push_back(x); return cur; } int n; int e[maxN]; int vals[maxN][13]; map<long long, int> all; long long dp[10000][20]; long long ndp[10000][20]; vector<pair<int, int> > best[10000]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { cin >> e[i]; } long long d = a[1]; for (int i = 2; i <= n; i++) { d = gcd(d, a[i]); } if (d == 1) { cout << 0; return 0; } auto t = factor(d); vector<pair<long long, int> > ss; for (int i = 1; i <= n; i++) { long long cop = a[i]; for (int j = 0; j < t.size(); j++) { while (cop % t[j] == 0) { cop /= t[j]; vals[i][j]++; } } a[i] /= cop; ss.push_back(make_pair(a[i], e[i])); } sort(ss.begin(), ss.end()); vector<pair<long long, int> > vals; int ii = 0; int m = t.size(); while (ii < ss.size()) { int j = ii; while (j < ss.size() && ss[j].first == ss[ii].first) j++; j--; for (int k = ii; k <= j && k <= ii + m - 1; k++) vals.push_back(ss[k]); ii = j + 1; } for (int i = 0; i < (1 << m); i++) { for (int j = 0; j <= m; j++) { dp[i][j] = (long long)1e18; ndp[i][j] = (long long)1e18; } } dp[0][0] = 0; int r = vals.size(); for (int u = 0; u < vals.size(); u++) { auto f = vals[u]; vector<bool> is_good(1 << m, false); for (int i = 0; i < (1 << m); i++) { for (int j = 0; j <= m; j++) { ndp[i][j] = dp[i][j]; } long long s = 1; long long x = f.first; for (int j = 0; j < m; j++) { if (!(i & (1 << j))) continue; while (x % t[j] == 0) { s *= t[j]; x /= t[j]; } } if (s <= k) { best[i].push_back(make_pair(f.second, u)); is_good[i] = true; } } } vector<bool> need(vals.size(), false); for (int i = 0; i < (1 << m); i++) { sort(best[i].begin(), best[i].end()); for (int j = 0; j < min((int)best[i].size(), m); j++) { need[best[i][j].second] = true; } } for (int u = 0; u < vals.size(); u++) { if (!need[u]) continue; auto f = vals[u]; vector<bool> is_good(1 << m, false); for (int i = 0; i < (1 << m); i++) { for (int j = 0; j <= m; j++) { ndp[i][j] = dp[i][j]; } long long s = 1; long long x = f.first; for (int j = 0; j < m; j++) { if (!(i & (1 << j))) continue; while (x % t[j] == 0) { s *= t[j]; x /= t[j]; } } if (s <= k) { is_good[i] = true; } } for (int i = 0; i < (1 << m); i++) { if (!is_good[i]) continue; int st = ((1 << m) - 1) ^ i; int msk = st; while (true) { for (int j = 0; j < m; j++) { ndp[st | i][j + 1] = min(ndp[st | i][j + 1], dp[st][j] + f.second); } if (st == 0) break; st = (msk & (st - 1)); } } for (int i = 0; i < (1 << m); i++) { for (int j = 0; j <= m; j++) { dp[i][j] = ndp[i][j]; } } } long long mn = (long long)1e18 - 10; bool ok = false; for (int i = 0; i <= m; i++) { if (dp[(1 << m) - 1][i] > (long long)1e16) continue; ok = true; mn = min(mn, i * dp[(1 << m) - 1][i]); } if (ok) cout << mn; else cout << -1; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> std::istream& operator>>(std::istream& i, pair<T, U>& p) { i >> p.first >> p.second; return i; } template <typename T> std::istream& operator>>(std::istream& i, vector<T>& t) { for (auto& v : t) { i >> v; } return i; } template <typename T, typename U> std::ostream& operator<<(std::ostream& o, const pair<T, U>& p) { o << p.first << ' ' << p.second; return o; } template <typename T> std::ostream& operator<<(std::ostream& o, const vector<T>& t) { if (t.empty()) o << '\n'; for (size_t i = 0; i < t.size(); ++i) { o << t[i] << " \n"[i == t.size() - 1]; } return o; } template <typename T> using minheap = priority_queue<T, vector<T>, greater<T>>; template <typename T> using maxheap = priority_queue<T, vector<T>, less<T>>; template <typename T> bool in(T a, T b, T c) { return a <= b && b < c; } unsigned int logceil(long long first) { return first ? 8 * sizeof(long long) - __builtin_clzll(first) : 0; } namespace std { template <typename T, typename U> struct hash<pair<T, U>> { hash<T> t; hash<U> u; size_t operator()(const pair<T, U>& p) const { return t(p.first) ^ (u(p.second) << 7); } }; } // namespace std template <typename T, typename F> T bsh(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { l = m + 1; r = m; } else { h = m - 1; } } return r; } template <typename F> double bshd(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { l = m; } else { h = m; } } return (l + h) / 2; } template <typename T, typename F> T bsl(T l, T h, const F& f) { T r = -1, m; while (l <= h) { m = (l + h) / 2; if (f(m)) { h = m - 1; r = m; } else { l = m + 1; } } return r; } template <typename F> double bsld(double l, double h, const F& f, double p = 1e-9) { unsigned int r = 3 + (unsigned int)log2((h - l) / p); while (r--) { double m = (l + h) / 2; if (f(m)) { h = m; } else { l = m; } } return (l + h) / 2; } template <typename T> T gcd(T a, T b) { if (a < b) swap(a, b); return b ? gcd(b, a % b) : a; } template <typename T> class vector2 : public vector<vector<T>> { public: vector2() {} vector2(size_t a, size_t b, T t = T()) : vector<vector<T>>(a, vector<T>(b, t)) {} }; template <typename T> class vector3 : public vector<vector2<T>> { public: vector3() {} vector3(size_t a, size_t b, size_t c, T t = T()) : vector<vector2<T>>(a, vector2<T>(b, c, t)) {} }; template <typename T> class vector4 : public vector<vector3<T>> { public: vector4() {} vector4(size_t a, size_t b, size_t c, size_t d, T t = T()) : vector<vector3<T>>(a, vector3<T>(b, c, d, t)) {} }; template <typename T> class vector5 : public vector<vector4<T>> { public: vector5() {} vector5(size_t a, size_t b, size_t c, size_t d, size_t e, T t = T()) : vector<vector4<T>>(a, vector4<T>(b, c, d, e, t)) {} }; constexpr long long INF = 1e18; class DProfessionalLayer { public: void solve(istream& cin, ostream& cout) { int N; cin >> N; long long K; cin >> K; vector<long long> A(N); cin >> A; vector<int> E(N); cin >> E; long long g = A[0]; for (long long a : A) g = gcd(g, a); if (g == 1) { cout << 0 << endl; return; } long long gg = g; vector<long long> F; for (long long first = 2; first * first <= g; ++first) { if (g % first == 0) { while (g % first == 0) g /= first; F.push_back(first); } } if (g != 1) F.push_back(g); int M = F.size(); map<long long, vector<std::pair<int, int>>> H; for (int j = 0; j < N; ++j) { long long X = A[j] / gg; for (int k = 0; k < M; ++k) while (X % F[k] == 0) X /= F[k]; auto& h = H[A[j] / X]; h.emplace_back(E[j], j); } vector<vector<std::pair<int, int>>> U(1 << M); for (auto& h : H) { sort(h.second.begin(), h.second.end()); if (h.second.size() > M) h.second.resize(M); vector<long long> P(M, 1); long long X = h.first; for (int i = 0; i < M; ++i) while (X % F[i] == 0) { P[i] *= F[i]; X /= F[i]; } for (int i = 1; i < (1 << M); ++i) { long long p = 1; for (int k = 0; k < M; ++k) if (i & (1 << k)) p *= P[k]; if (p <= K) { for (std::pair<int, int> hh : h.second) U[i].push_back(hh); } } } map<int, vector<int>> G; for (int i = 1; i < (1 << M); ++i) { sort(U[i].begin(), U[i].end()); if (U[i].size() > M) U[i].resize(M); for (std::pair<int, int> u : U[i]) G[u.second].emplace_back(i); } vector2<long long> D(1 << M, M + 1, INF); D[0][0] = 0; for (auto& g : G) { for (int k = M - 1; k >= 0; --k) { for (int i = 0; i < (1 << M); ++i) { if (D[i][k] == INF) continue; for (int j : g.second) { if ((i & j) == 0) { D[i | j][k + 1] = min(D[i | j][k + 1], D[i][k] + E[g.first]); } } } } } long long ans = INF; for (int i = 1; i <= M; ++i) ans = min(ans, D.back()[i] * i); if (ans == INF) cout << "-1\n"; else cout << ans << endl; } }; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); DProfessionalLayer solver; std::istream& in(std::cin); std::ostream& out(std::cout); solver.solve(in, out); return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> long long n, k, tot, tem, val[101], cnt, a[1000001], e[1000001], value[10001]; long long f[13][10001]; std::map<long long, std::vector<int> > map; long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); } int main() { scanf("%d%I64d", &n, &k); for (int i = 1; i <= n; i++) { scanf("%I64d", a + i); tot = gcd(tot, a[i]); } if (tot == 1) { puts("0"); return 0; } long long v = 1; for (long long i = 2; i * i <= tot; i++) if (tot % i == 0) { val[++cnt] = i; while (tot % i == 0) tot /= i, v *= i; } if (tot > 1) val[++cnt] = tot, v *= tot; for (int i = 1; i <= n; i++) { scanf("%I64d", e + i); long long tem = 1; for (int j = 1; j <= cnt; j++) while (a[i] % val[j] == 0) a[i] /= val[j], tem *= val[j]; map[tem].push_back(e[i]); } for (int i = 0; i <= cnt; i++) for (int j = 0; j < 1 << cnt; j++) f[i][j] = 1000000000000000000ll; f[0][0] = 0; for (auto it = map.begin(); it != map.end(); ++it) { std::sort((it->second).begin(), (it->second).end()); long long tem = it->first; for (int i = 1; i <= cnt; i++) { value[1 << (i - 1)] = 1; while (tem % val[i] == 0) tem /= val[i], value[1 << (i - 1)] *= val[i]; } for (int i = 1; i < 1 << cnt; i++) for (int j = 1; j <= cnt; j++) if (!(i & (1 << (j - 1)))) value[i | (1 << (j - 1))] = value[i] * value[1 << (j - 1)]; for (auto it1 = (it->second).begin(); it1 != (it->second).end(); ++it1) { bool cando = 0; for (int j = cnt; j; --j) for (int i = 1; i < 1 << cnt; i++) for (int stat = 0; stat < 1 << cnt; stat++) if (((i & stat) == stat) && value[i ^ stat] <= k && f[j][i] > f[j - 1][stat] + *it1) cando = 1, f[j][i] = f[j - 1][stat] + *it1; if (!cando) break; } } long long ans = 1000000000000000000ll; for (int i = 1; i <= cnt; i++) ans = std::min(ans, f[i][(1 << cnt) - 1] * i); if (ans == 1000000000000000000ll) puts("-1"); else printf("%I64d", ans); }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; using ll = long long; template <class T> void rpl(T& a, const T& b) { if (a > b) a = b; } ll gcd(ll a, ll b) { if (a > b) swap(a, b); if (a == 0) return b; return gcd(b % a, a); } using vi = vector<ll>; using i2 = array<int, 2>; const auto cmp = [](const i2& a, const i2& b) { return a[0] > b[0] or (not(b[0] > a[0]) and a[1] > b[1]); }; using si2 = set<i2, decltype(cmp)>; using vsi2 = vector<si2>; using msi2 = map<ll, si2>; using msit = msi2::iterator; using psi2 = pair<ll, si2>; const int MN = 1e6 + 100; const ll INF = 1e17; const int MM = 12; void fact(ll n, vi& f) { f.clear(); for (ll i = 2; i * i <= n; i++) { if (n % i == 0) { f.push_back(i); for (; (n /= i) % i == 0;) ; } } if (n > 1) f.push_back(n); } ll t; vi T; ll conv(ll n, vi& f) { f.clear(); ll r = 1LL; for (ll i : T) { f.push_back(1LL); for (; n % i == 0; n /= i, f.back() *= i) ; r *= f.back(); } return r; } ll a[MN], K, c[MN]; int e[MN], N, M; vi f[MN]; bitset<MN> u; void pick() { u.reset(); msi2 m; m.clear(); for (int i = 0; i < N; i++) { si2& s = (m.find(c[i]) == m.end() ? m.insert({c[i], si2(cmp)}).first->second : m.find(c[i])->second); s.insert({e[i], i}); if (s.size() > M) s.erase(s.begin()); } for (auto x : m) for (auto y : x.second) u[y[1]] = true; } vector<int> b[MN]; void comp() { vsi2 m(1 << M, si2(cmp)); for (si2& x : m) x.clear(); for (int i = 0; i < N; i++) if (u[i]) { for (int j = 1; j < 1 << M; j++) { ll v = 1LL; for (int k = 0; k < M; k++) if (j & 1 << k) v *= f[i][k]; if (v > K) continue; si2& s = m[j]; s.insert({e[i], i}); if (s.size() > M) s.erase(s.begin()); } } for (int i = 1; i < 1 << M; i++) for (auto y : m[i]) b[y[1]].push_back(i); } ll d[2][MM][1 << MM]; bool C; void work(int m, int v) { for (int i = 1; i <= M; i++) { for (int j = m; j < 1 << M; j = j + 1 | m) { rpl(d[1][i][j], d[0][i - 1][j ^ m] + v); } } } int main() { scanf("%d%lld", &N, &K); t = 0LL; for (int i = 0; i < N; i++) scanf("%lld", a + i), t = gcd(t, a[i]); for (int i = 0; i < N; i++) scanf("%d", e + i); fact(t, T), M = T.size(); if (t == 1) return printf("0\n"), 0; for (int i = 0; i < N; i++) c[i] = conv(a[i], f[i]); pick(); comp(); for (int i = 0; i <= M; i++) for (int j = 0; j < 1 << M; j++) d[1][i][j] = INF; d[1][0][0] = 0LL; for (int i = 0; i < N; i++) { if (b[i].empty()) continue; C = not C; for (int j = 0; j <= M; j++) for (int k = 0; k < 1 << M; k++) d[0][j][k] = d[1][j][k]; for (int j : b[i]) work(j, e[i]); } ll z = INF; for (int i = 1; i <= M; i++) rpl(z, d[1][i][(1 << M) - 1] * i); if (z == INF) printf("-1\n"); else printf("%lld\n", z); return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int N = 1000003, M = 1 << 11; const long long INF = 0x3f3f3f3f3f3f3f3f; template <typename T> void read(T &x) { int ch = getchar(); x = 0; for (; ch < '0' || ch > '9'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0'; } template <typename T> bool chmin(T &a, const T &b) { if (a > b) return a = b, 1; return 0; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int n, m, lim, e[N]; long long k, g, a[N], pr[11], f[12][M], ans = INF; map<long long, vector<int> > ma; bool ok[M]; int main() { read(n); read(k); memset(f, 0x3f, sizeof f); for (int i = 0; i < n; ++i) { read(a[i]); g = gcd(a[i], g); } for (long long i = 2; i * i <= g; ++i) if (!(g % i)) { if (i > k) return puts("-1"), 0; pr[m++] = i; do g /= i; while (!(g % i)); } if (g > k) return puts("-1"), 0; if (g > 1) pr[m++] = g; lim = 1 << m; for (int i = 0; i < n; ++i) { long long tmp = 1; read(e[i]); for (int j = 0; j < m; ++j) while (!(a[i] % pr[j])) { a[i] /= pr[j]; tmp *= pr[j]; } ma[tmp].emplace_back(e[i]); } f[0][0] = 0; for (auto _ : ma) { long long x = _.first; sort((_.second).begin(), (_.second).end()); if (_.second.size() > m) _.second.resize(m); for (int S = 0; S < lim; ++S) { long long tmp = x, res = 1; for (int i = 0; i < m; ++i) if (S >> i & 1) while (!(tmp % pr[i])) { tmp /= pr[i]; res *= pr[i]; } ok[S] = res <= k; } for (int val : _.second) { bool flg = true; for (int i = m - 1; ~i; --i) for (int S = 0; S < lim; ++S) if (f[i][S] < INF) for (int T = S | (S + 1); T < lim; T = S | (T + 1)) if (ok[S ^ T] && chmin(f[i + 1][T], f[i][S] + val)) flg = false; if (flg) break; } } for (int i = 0; i <= m; ++i) if (f[i][lim - 1] < INF) chmin(ans, f[i][lim - 1] * i); printf("%lld\n", ans == INF ? -1 : ans); }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("no-stack-protector") #pragma GCC optimize("unroll-loops") #pragma GCC optimize("fast-math") #pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native") using namespace std; template <typename T> void uin(T &a, T b) { if (b < a) { a = b; } } template <typename T> void uax(T &a, T b) { if (b > a) { a = b; } } const long long maxn = 1000 * 1000 + 7, INF = 1e18 + 228; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } struct elem { long long a, e; elem() {} elem(long long _a, long long _e) { a = _a; e = _e; } }; elem a[maxn]; bool operator<(elem fir, elem sec) { return fir.e < sec.e; } vector<pair<vector<long long>, long long> > elems; long long res = INF; long long n, k, m; long long msks[15]; vector<long long> g[15000], best[1 << 12]; long long mt[maxn]; bool used[15000]; bool cmp(long long i, long long j) { return elems[i].second < elems[j].second; } bool dfs(long long v) { if (used[v]) return 0; used[v] = 1; for (long long to : g[v]) { if (mt[to] == -1) { mt[to] = v; return 1; } } for (long long to : g[v]) { if (dfs(mt[to])) { mt[to] = v; return 1; } } return 0; } long long it = 0; long long use[maxn]; void gen(long long ptr) { ++it; if (it % 4096 == 0 && clock() > 2.95 * CLOCKS_PER_SEC) { if (res == INF) cout << -1 << '\n'; else { cout << res << '\n'; } exit(0); } if (ptr == m) { long long xx = 0; for (long long i = 0; i < m; ++i) { if (msks[i]) { ++xx; } } vector<long long> left228; for (long long i = 0; i < m; ++i) { for (long long j = 0; j < min(xx, (long long)best[msks[i]].size()); ++j) { g[best[msks[i]][j]].clear(); } } for (long long i = 0; i < m; ++i) { if (msks[i]) { for (long long j = 0; j < min((long long)best[msks[i]].size(), xx); ++j) { g[best[msks[i]][j]].push_back(i); left228.push_back(best[msks[i]][j]); } } } sort(left228.begin(), left228.end()); left228.erase(unique(left228.begin(), left228.end()), left228.end()); sort(left228.begin(), left228.end(), cmp); for (long long i = 0; i < m; ++i) { mt[i] = -1; } long long cur_res = 0; long long pars = 0; for (long long j : left228) { used[j] = 0; } for (long long i : left228) { if (dfs(i)) { cur_res += elems[i].second; ++pars; for (long long j : left228) { used[j] = 0; } } } if (pars == xx) { uin(res, xx * cur_res); } } else { long long upper = ptr + 1; vector<long long> allowed; for (long long i = 0; i < m; ++i) { if (use[i]) { allowed.push_back(i); } } for (long long i = 0; i < m; ++i) { if (!use[i]) { allowed.push_back(i); break; } } for (long long i : allowed) { msks[i] += (1 << ptr); ++use[i]; gen(ptr + 1); msks[i] -= (1 << ptr); --use[i]; } } } long long cnta[1000 * 1000 + 7]; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> k; long long G = 0; for (long long i = 1; i <= n; ++i) { cin >> a[i].a; G = gcd(G, a[i].a); } for (long long i = 1; i <= n; ++i) { cin >> a[i].e; } sort(a + 1, a + n + 1); vector<long long> prs; long long i = 2; long long _G = G; while (i * i <= _G) { if (G % i == 0) { prs.push_back(i); while (G % i == 0) G /= i; } ++i; } if (G > 1) prs.push_back(G); m = (long long)prs.size(); long long A = 0; vector<long long> maska(m); vector<vector<long long> > lol; for (long long i = 1; i <= n; ++i) { A = a[i].a; for (long long j = 0; j < m; ++j) { maska[j] = 0; } long long troy = 0; for (long long j = 0; j < m; ++j) { while (A % prs[j] == 0) A /= prs[j], ++maska[j]; if (j < 3) { troy = troy * 67 + maska[j]; } else { troy = troy * 13 + maska[j]; } } troy %= (1000 * 1000 + 7); if (cnta[troy] < m) { ++cnta[troy]; elems.push_back({maska, a[i].e}); vector<long long> v(m, 1); for (long long j = 0; j < m; ++j) { for (long long it = 0; it < maska[j]; ++it) { v[j] *= prs[j]; } } lol.push_back(v); } } for (long long mask = 0; mask < (1 << m); ++mask) { vector<long long> bits; for (long long j = 0; j < m; ++j) { if ((mask >> j) & 1) { bits.push_back(j); } } for (long long i = 0; i < (long long)elems.size(); ++i) { long long ch = 1; for (long long j : bits) { ch *= lol[i][j]; if (ch > k) break; } if (ch <= k) { best[mask].push_back(i); if ((long long)best[mask].size() == m) { break; } } } } gen(0); if (res == INF) cout << -1 << '\n'; else { cout << res << '\n'; } return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int inf = (int)1.01e9; const long long infll = (long long)1.01e18; const long double eps = 1e-9; const long double pi = acos((long double)-1); mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int x) { return mrand() % x; } void precalc() {} long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); } const long long maxx = (long long)1e12; const int maxn = (int)1e6 + 5; int n; long long b; long long a[maxn]; int e[maxn]; bool read() { if (scanf("%d%lld", &n, &b) < 2) { return false; } for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } for (int i = 0; i < n; i++) { scanf("%d", &e[i]); } return true; } pair<long long, int> tosort[maxn]; int isGood[maxn]; int k; vector<long long> ps; vector<vector<int>> good; bool add(int msk, int id) { if (((int)(good[msk]).size()) >= k && e[good[msk].back()] <= e[id]) { return false; } if (((int)(good[msk]).size()) >= k) { good[msk].pop_back(); } int pos = ((int)(good[msk]).size()); good[msk].push_back(id); while (pos && e[good[msk][pos]] < e[good[msk][pos - 1]]) { swap(good[msk][pos], good[msk][pos - 1]); pos--; } return true; } vector<vector<long long>> dp, ndp; void solve() { long long g = a[0]; for (int i = 1; i < n; i++) { g = gcd(g, a[i]); } if (g == 1) { printf("0\n"); return; } { ps.clear(); long long x = g; for (long long d = 2; d * d <= x; d++) { if (!(x % d)) { ps.push_back(d); while (!(x % d)) { x /= d; } } } if (x > 1) { ps.push_back(x); } } k = ((int)(ps).size()); for (int i = 0; i < n; i++) { long long x = 1; auto &cur = a[i]; for (int j = 0; j < k; j++) { while (!(cur % ps[j])) { cur /= ps[j]; x *= ps[j]; } } cur = x; } for (int i = 0; i < n; i++) { tosort[i] = make_pair(a[i], e[i]); } sort(tosort, tosort + n); for (int i = 0; i < n; i++) { a[i] = tosort[i].first; e[i] = tosort[i].second; } good.clear(); good.resize((1 << k)); for (int i = 0; i < n; i++) { isGood[i] = false; } for (int j = 0; j < n;) { int i = j; while (j < n && a[j] == a[i]) { j++; } vector<int> qs(k, 0); { long long x = a[i]; for (int it = 0; it < k; it++) { while (!(x % ps[it])) { x /= ps[it]; qs[it]++; } } } for (int msk = 0; msk < (1 << k); msk++) { long long y = 1; for (int it = 0; it < k; it++) { if (!(msk & (1 << it))) { continue; } for (int it0 = 0; it0 < qs[it]; it0++) { y *= ps[it]; } } if (y > b) { continue; } int pos = i; while (pos < j) { if (!add(msk, pos)) { break; } isGood[pos] = true; pos++; } } } dp = vector<vector<long long>>((1 << k), vector<long long>(k + 1, infll)); dp[0][0] = 0; int ob = 0; for (int i = 0; i < n; i++) { if (!isGood[i]) { continue; } ob++; ndp = vector<vector<long long>>((1 << k), vector<long long>(k + 1, infll)); vector<int> qs(k, 0); { long long x = a[i]; for (int it = 0; it < k; it++) { while (!(x % ps[it])) { x /= ps[it]; qs[it]++; } } } vector<int> ok(1 << k); for (int msk = 0; msk < (1 << k); msk++) { long long y = 1; for (int it = 0; it < k; it++) { if (!(msk & (1 << it))) { continue; } for (int it0 = 0; it0 < qs[it]; it0++) { y *= ps[it]; } } ok[msk] = (y <= b); } for (int msk = 0; msk < (1 << k); msk++) { for (int cnt = 0; cnt <= k; cnt++) { auto cur = dp[msk][cnt]; if (cur >= infll) { continue; } { auto &nxt = ndp[msk][cnt]; nxt = min(nxt, cur); } if (cnt >= k) { continue; } int msk1 = (((1 << k) - 1) ^ msk); for (int nmsk = msk1; nmsk > 0; nmsk = ((nmsk - 1) & msk1)) { if (!ok[nmsk]) { continue; } auto &nxt = ndp[msk | nmsk][cnt + 1]; nxt = min(nxt, cur + e[i]); } } } swap(dp, ndp); } long long res = infll; for (int cnt = 1; cnt <= k; cnt++) { if (dp[(1 << k) - 1][cnt] < infll) { res = min(res, cnt * dp[(1 << k) - 1][cnt]); } } if (res >= infll) { res = -1; } printf("%lld\n", res); if (b == 7558272) cerr << ob; } int main() { precalc(); while (read()) { solve(); } return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
import java.io.*; import java.util.*; public class A { public static void main (String[] args) { new A(); } long max = (long)1e12; int[] works; long[] primes, powers; long K; public A() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); System.err.println(""); build(); int n = fs.nextInt(); K = fs.nextLong(); long[] a = new long[n]; long gcd = 0; for(int i = 0; i < n; i++) { a[i] = fs.nextLong(); gcd = gcd(gcd, a[i]); } if(gcd == 1) { System.out.println(0); return; } int[] costs = new int[n]; for(int i = 0; i < n; i++) costs[i] = fs.nextInt(); long[][] pf = getPrimes(gcd); long[] pows = new long[pf.length]; for(int i = 0; i < pf.length; i++) { pows[i] = 1; for(int j = 0; j < pf[i][1]; j++) pows[i] *= pf[i][0]; } int[][] it = new int[n][2]; HashMap<Integer, long[]> maps = new HashMap<>(); for(int i = 0; i < n; i++) { long[] pass = new long[pf.length]; long now = a[i]; for(int j = 0; j < pf.length; j++) now /= pows[j]; int hash = 0; for(int j = 0; j < pf.length; j++) { long div = pf[j][0]; long expo = 0; while(now%div == 0) { expo++; now /= div; } pass[j] = expo+pf[j][1]; hash += (expo+pf[j][1])*pow1[j]; } if(!maps.containsKey(hash)) maps.put(hash, pass); it[i][0] = hash; it[i][1] = costs[i]; } Arrays.sort(it, (x, y) -> comp(x[0], y[0]) == 0 ? comp(x[1], y[1]) : comp(x[0], y[0])); int[][] nums = new int[n][2]; int ptr2 = 0; int M = pf.length; for(int i = 0; i < n; i++) { int j = i+1; while(j < n && it[i][0] == it[j][0]) j++; int to = Math.min(i+M, j); for(int k = i; k < to; k++) { nums[ptr2][0] = it[k][0]; nums[ptr2][1] = it[k][1]; ptr2++; } i = --j; } nums = Arrays.copyOfRange(nums, 0, ptr2); int[][] validMasks = new int[nums.length][]; int[] ptsTo = new int[nums.length]; works = new int[1<<M]; ArrayList<Integer> tmp = new ArrayList<>(); powers = new long[pf.length]; for(int i = 0; i < nums.length; i++) { Arrays.fill(works, 0); primes = maps.get(nums[i][0]); for(int j = 0; j < primes.length; j++) { powers[j] = 1; for(int k = 0; k < primes[j]; k++) powers[j] *= pf[j][0]; } dfsFill(0, 0, 1L); for(int mask = works.length-1; mask >= 0; mask--) { for(int bit = 0; bit < M; bit++) { int on = (1<<bit)&mask; if(on == 0) works[mask] += works[mask | (1 << bit)]; } } tmp.clear(); int ptr = 0; for(int mask = 0; mask < works.length; mask++) { if(works[mask] == 1) { tmp.add(mask); } } ptr = tmp.size(); int j = i; while(j < nums.length && nums[i][0] == nums[j][0]) j++; validMasks[i] = new int[ptr]; for(int ii = 0; ii < ptr; ii++) validMasks[i][ii] = tmp.get(ii); for(int k = i; k < j; k++) { ptsTo[k] = i; } i = --j; } long[][][] dp = new long[2][M+1][1<<M]; long oo = (long)1e16; for(int i = 0; i < 2; i++) for(int j = 0; j <= M; j++) Arrays.fill(dp[i][j], oo); for(int i = 0; i < M; i++) dp[0][i][0] = 0; int x = 1; for(int idx = 0; idx < nums.length; idx++, x ^= 1) { for(int i = 0; i <= M; i++) for(int j = 0; j < 1<<M; j++) dp[x][i][j] = dp[x^1][i][j]; boolean didSomething = false; for(int moves = 0; moves < M; moves++) { for(int mask = 0; mask < 1<<M; mask++) { if(dp[x^1][moves][mask] == oo) continue; for(int msk : validMasks[ptsTo[idx]]) { int newMask = mask|msk; long newCost = dp[x^1][moves][mask] + nums[idx][1]; if(newCost < dp[x][moves+1][newMask]) { dp[x][moves+1][newMask] = newCost; didSomething = true; } } } } if(!didSomething) { int j = idx; while(j < nums.length && nums[j][0] == nums[idx][0]) j++; idx = j-1; } } x ^= 1; int mask = (1<<M)-1; long res = oo; for(int moves = 1; moves <= M; moves++) { if(dp[x][moves][mask] == oo) continue; res = Math.min(res, moves * dp[x][moves][mask]); } if(res == oo) res = -1; out.println(res); out.close(); } void dfsFill(int index, int mask, long prod) { if(prod > K) return; if(index == powers.length) { works[mask] = 1; return; } dfsFill(index+1, mask, prod); dfsFill(index+1, mask|(1<<index), prod*powers[index]); } boolean equals(long[][] a, long[][] b) { for(int i = 0; i < a.length; i++) { if(a[i][1] != b[i][1]) return false; } return true; } long[][] getPrimes(long n) { long[][] res = new long[13][2]; int ptr = 0; for(long div = 2; div <= Math.sqrt(n); div++) { if(n%div != 0) continue; int expo = 0; while(n%div == 0) { expo++; n /= div; } res[ptr][0] = div; res[ptr][1] = expo; ptr++; } if(n > 1) { res[ptr][0] = n; res[ptr][1] = 1; ptr++; } res = Arrays.copyOfRange(res, 0, ptr); return res; } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } long min(long a, long b) { return a < b ? a : b; } int comp(int a, int b) { return Integer.compare(a, b); } int BASE = 101; int[] pow1; void build() { pow1 = new int[20]; pow1[0] = 1; for(int i = 1; i < 20; i++) { pow1[i] = BASE*pow1[i-1]; } } class FastScanner { public int BS = 1<<25; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } } }
JAVA
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int N1 = (int)(1e6 + 0.5); const long long N2 = (long long)(1e12 + 0.5); struct emt { long long a, cost; }; bool cmp(emt u, emt v) { return u.cost < v.cost; } emt e[N1 + 5]; unordered_map<long long, int> CNT; long long gcd(long long x, long long y) { return !x ? y : gcd(y % x, x); } vector<long long> decomdd; int prime_decomp_dd(long long x) { for (int y = 2; y <= N1; y++) { if (x % y) continue; decomdd.push_back(y); while (x % y == 0) x /= y; } if (x > 1) decomdd.push_back(x); return decomdd.size(); } long long prime_decomp_a(long long x) { long long t = 1; for (long long y : decomdd) while (x % y == 0) x /= y, t *= y; return t; } long long dp[12][1 << 11], dp1[12][1 << 11]; void work(int S, long long cost, int LEN) { int S1 = ((1 << LEN) - 1) ^ S; for (int i = 1; i <= LEN; i++) { for (int j = S1; j; j = (j - 1) & S1) dp[i][j | S] = min(dp[i][j | S], dp1[i - 1][j] + cost); dp[i][S] = min(dp[i][S], dp1[i - 1][0] + cost); } } int used[1 << 11]; int main() { int n; long long K, dd = 0; cin >> n >> K; for (int i = 1; i <= n; i++) { scanf("%lld", &e[i].a); dd = gcd(dd, e[i].a); } if (dd == 1) { puts("0"); return 0; } int LEN = prime_decomp_dd(dd); for (int i = 1; i <= n; i++) { scanf("%lld", &e[i].cost); e[i].a = prime_decomp_a(e[i].a); } sort(e + 1, e + n + 1, cmp); memset(dp, 1, sizeof(dp)); dp[0][0] = 0; for (int i = 1; i <= n; i++) { if (++CNT[e[i].a] > LEN) continue; long long mul[11], prod[1 << 11]; prod[0] = 1; for (int j = 0; j < LEN; j++) { mul[j] = 1; while (e[i].a % decomdd[j] == 0) e[i].a /= decomdd[j], mul[j] *= decomdd[j]; } memcpy(dp1, dp, sizeof(dp1)); for (int j = 0; j < LEN; j++) { for (int k = (1 << j); k < (1 << (j + 1)); k++) { prod[k] = prod[k ^ (1 << j)] * mul[j]; if (prod[k] <= K && used[k]++ < LEN) work(k, e[i].cost, LEN); } } } long long ans = 1e13; for (int i = 1; i <= LEN; i++) ans = min(ans, dp[i][(1 << LEN) - 1] * i); if (ans >= 1e13) ans = -1; cout << ans << endl; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx") using namespace std; int a, b, c, d, n, m, tot; long long k; pair<int, long long> mas[1000002]; unordered_map<long long, int> nm; int ex[12001][12]; int len[12001]; vector<pair<long long, int> > pr; int dc[12001][12]; long long dd[12001][12]; bool can[12001][1 << 11]; long long dp[2][12][1 << 11]; char buf[262145], *ch = buf; inline char get_char() { if (*ch == 0) { ch = buf; fread(buf, sizeof(char), 262144, stdin); } return *(ch++); } inline void next_int(int &ans) { ans = 0; char ch; while ((ch = get_char()) < '0' || ch > '9') ; do { ans = ans * 10 + ch - '0'; } while ((ch = get_char()) >= '0' && ch <= '9'); } inline void next_ll(long long &ans) { ans = 0; char ch; while ((ch = get_char()) < '0' || ch > '9') ; do { ans = ans * 10 + ch - '0'; } while ((ch = get_char()) >= '0' && ch <= '9'); } inline long long gcd(long long a, long long b) { for (; b; swap(a, b)) a %= b; return a; } inline int num(long long v) { auto it = nm.find(v); if (it != nm.end()) { return it->second; } long long t = v; for (int _n(((int)((pr).size())) - 1), i(0); i <= _n; i++) { int c = 0; long long mul = 1; while (t % pr[i].first == 0) { t /= pr[i].first; mul *= pr[i].first; ++c; } dc[tot][i] = c; dd[tot][i] = mul; } nm.emplace(v, tot++); return tot - 1; } void rec(int v, int pos, int mask, long long sof) { if (sof > k) return; if (pos >= (int)((pr).size())) { can[v][mask] = 1; return; } if ((double)sof * dd[v][pos] > 1e18 || sof * dd[v][pos] > k) ; else { rec(v, pos + 1, mask | 1 << pos, sof * dd[v][pos]); } rec(v, pos + 1, mask, sof); } inline void upd(long long &a, long long b) { if (a > b) a = b; } int main() { next_int(n); next_ll(k); long long g = 0; for (int _n((n)-1), i(0); i <= _n; i++) { next_ll(mas[i].second); g = gcd(g, mas[i].second); } for (int _n((n)-1), i(0); i <= _n; i++) { next_int(mas[i].first); } if (g == 1) { printf("0\n"); exit(0); } sort(mas, mas + n); long long t = g; for (long long i = 2; i * i <= t; ++i) { if (t % i) continue; int c = 0; long long r = 1; while (t % i == 0) { t /= i; r *= i; ++c; } if (r > k) { printf("-1\n"); exit(0); } pr.push_back(make_pair(i, c)); } if (t > 1) { if (t > k) { printf("-1\n"); exit(0); } pr.push_back(make_pair(t, 1)); } nm.reserve(12000); for (int _n((n)-1), i(0); i <= _n; i++) { long long cur = 1; long long o = mas[i].second; for (int _n(((int)((pr).size())) - 1), j(0); j <= _n; j++) { int c = 0; while (o % pr[j].first == 0) { ++c; o /= pr[j].first; cur *= pr[j].first; } if (!c) continue; } int v = num(cur); if (len[v] >= (int)((pr).size())) continue; ex[v][len[v]++] = mas[i].first; } for (int _n((tot)-1), i(0); i <= _n; i++) { rec(i, 0, 0, 1); } int now = 1, nx = 0; memset(dp, 63, sizeof(dp)); dp[nx][0][0] = 0; int u = (1 << (int)((pr).size())) - 1; for (int _n((tot)-1), pos(0); pos <= _n; pos++) { for (int _n((len[pos]) - 1), cl(0); cl <= _n; cl++) { swap(now, nx); memset(dp[nx], 63, sizeof(dp[nx])); for (int _n(((int)((pr).size()) + 1) - 1), i(0); i <= _n; i++) { for (int _n((1 << (int)((pr).size())) - 1), mask(0); mask <= _n; mask++) { long long cd = dp[now][i][mask]; if (cd >= 1000000000000000000LL) continue; bool ok = 1; for (int _n(((int)((pr).size())) - 1), j(0); j <= _n; j++) { if (mask & 1 << j) continue; int nmask = mask | 1 << j; if (dp[now][i][nmask] <= cd) { ok = 0; break; } } if (!ok) continue; upd(dp[nx][i][mask], cd); if (i == (int)((pr).size()) || mask == u) continue; int base = u ^ mask; for (int nmask = base; nmask > 0; nmask = (nmask - 1) & base) { if (!can[pos][nmask]) continue; upd(dp[nx][i + 1][mask | nmask], cd + ex[pos][cl]); } } } } } long long ans = 8000000000000000000LL; for (int _n(((int)((pr).size()))), i(1); i <= _n; i++) { long long cur = dp[nx][i][u]; if (cur >= 1000000000000000000LL) continue; cur *= i; ans = min(ans, cur); } cout << ans << endl; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; inline char gc() { return getchar(); } template <class T> int read(T &ans) { ans = 0; char ch = gc(); T f = 1; while (!isdigit(ch)) { if (ch == EOF) return -1; if (ch == '-') f = -1; ch = gc(); } while (isdigit(ch)) ans = ans * 10 + ch - '0', ch = gc(); ans *= f; return 1; } template <class T1, class T2> int read(T1 &a, T2 &b) { return read(a) != EOF && read(b) != EOF ? 2 : EOF; } template <class T1, class T2, class T3> int read(T1 &a, T2 &b, T3 &c) { return read(a, b) != EOF && read(c) != EOF ? 3 : EOF; } const int Maxn = 1100000; const int inf = 0x3f3f3f3f; int n, tot, l[Maxn], bj[Maxn]; long long c[Maxn], K, ans = 0x3f3f3f3f3f3f3f, f[12][2100], g[12][2100], d[Maxn]; pair<long long, long long> a[Maxn]; vector<int> v[Maxn / 10]; long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } signed main() { read(n, K); for (int i = 1; i <= n; i++) read(a[i].first); for (int i = 1; i <= n; i++) read(a[i].second); sort(a + 1, a + n + 1, [&](pair<long long, long long> a, pair<long long, long long> b) { return a.second < b.second; }); long long gg = a[1].first; for (int i = 2; i <= n; i++) gg = gcd(gg, a[i].first); if (gg == 1) return 0 * puts("0"); for (long long i = 2; i * i <= gg; i++) if (gg % i == 0) { c[++tot] = i; while (gg % i == 0) gg /= i; } if (gg != 1) c[++tot] = gg; for (int i = 1; i <= n; i++) { long long x = 1; for (int j = 1; j <= tot; j++) while (a[i].first % c[j] == 0) x *= c[j], a[i].first /= c[j]; a[i].first = x; } int cnt = 0; map<long long, int> ma; int num = 0; for (int i = 1; i <= n; i++) { if (!ma[a[i].first]) d[++cnt] = a[i].first, a[i].first = ma[a[i].first] = cnt; else a[i].first = ma[a[i].first]; if (l[a[i].first] != tot) l[a[i].first]++, a[++num] = a[i]; } n = num; int ed = 1 << tot; for (int i = 0; i <= tot; i++) for (int j = 0; j < ed; j++) f[i][j] = 0x3f3f3f3f3f3f3f; f[0][0] = 0; for (int i = 1; i < ed; i++) { int temp = 0; for (int j = 1; j <= cnt; j++) { long long x = 1, y = d[j]; for (int k = 1, temp = 1; k <= tot; k++, temp <<= 1) if (i & temp) while (y % c[k] == 0) x *= c[k], y /= c[k]; bj[j] = x <= K; } for (int j = 1; j <= n; j++) if (bj[a[j].first]) { v[j].push_back(i); temp++; if (temp == tot) break; } } for (int i = 1; i <= n; i++) { memcpy(g, f, sizeof(g)); for (auto j : v[i]) { int s = (ed - 1) ^ j; for (int k = s;; k = (k - 1) & s) { for (int l = tot - 1; l >= 0; l--) (f[l + 1][k | j] = min(f[l + 1][k | j], g[l][k] + a[i].second)); if (!k) break; } } } for (int i = 1; i <= tot; i++) (ans = min(ans, f[i][ed - 1] * i)); if (ans == 0x3f3f3f3f3f3f3f) puts("-1"); else printf("%I64d\n", ans); return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const long long INF = 1e17; const int N = 1000010; struct dat { long long u, e; } a[N]; long long n, k, num = 0; long long dp[12][1 << 11], dp1[12][1 << 11]; long long had1[1 << 11], part[1 << 11], p[20], pk[20]; map<long long, int> had; long long GCD(long long x, long long y) { long long r; while (x) r = y % x, y = x, x = r; return y; } bool cmp(dat x, dat y) { return x.e < y.e; } int main() { scanf("%lld%lld", &n, &k); long long gcd = 0; for (int i = (1); i <= (n); ++i) { scanf("%lld", &a[i].u); gcd = GCD(gcd, a[i].u); } if (gcd == 1) { puts("0"); return 0; } for (int i = (1); i <= (n); ++i) scanf("%lld", &a[i].e); sort(a + 1, a + n + 1, cmp); for (long long i = 2; i * i <= gcd; ++i) if (gcd % i == 0) for (p[++num] = i; gcd % i == 0; gcd /= i) ; if (gcd > 1) p[++num] = gcd; for (int i = (0); i <= (num); ++i) for (int j = (0); j <= ((1 << num) - 1); ++j) dp[i][j] = j == 0 ? 0 : INF; for (int i = (1); i <= (n); ++i) { long long newu = 1; for (int j = (1); j <= (num); ++j) for (pk[j] = 1; a[i].u % p[j] == 0; a[i].u /= p[j]) newu *= p[j], pk[j] *= p[j]; ++had[newu]; if (had[newu] > num) continue; memcpy(dp1, dp, sizeof(dp)); part[0] = 1; for (int digit = (0); digit <= (num - 1); ++digit) { for (int j = (1 << digit); j <= ((1 << digit + 1) - 1); ++j) { part[j] = part[j - (1 << digit)] * pk[digit + 1]; if (part[j] > k || had1[j] >= num) continue; ++had1[j]; for (int d1 = (0); d1 <= (num - 1); ++d1) for (int l = (0); l <= ((1 << num) - 1); ++l) dp[d1 + 1][l | j] = min(dp[d1 + 1][l | j], dp1[d1][l] + a[i].e); } } } long long ans = INF; for (int i = (1); i <= (num); ++i) ans = min(ans, dp[i][(1 << num) - 1] * i); cout << (ans == INF ? -1 : ans) << endl; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } const int MAXN = 1000005; const int MAXM = 12; const int MAXS = 1 << 11; const long long INF = 1e18; int n, m, CNT[MAXS]; map<long long, int> cnt; long long k, prime[MAXM], v[MAXS], f[MAXM][MAXS], g[MAXM][MAXS]; inline void qmin(long long &x, long long y) { x = y < x ? y : x; } struct node { long long a, e; inline bool operator<(const node &o) const { return e < o.e; } } b[MAXN]; int main() { scanf("%d%I64d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%I64d", &b[i].a); for (int i = 1; i <= n; ++i) scanf("%I64d", &b[i].e); sort(b + 1, b + n + 1); long long D = b[1].a; for (int i = 2; i <= n; ++i) D = gcd(D, b[i].a); if (D == 1) { puts("0"); return 0; } for (long long i = 2; i * i <= D; ++i) if (D % i == 0) { prime[m++] = i; while (D % i == 0) D /= i; } if (D > 1) prime[m++] = D; for (int i = 0; i <= m; ++i) for (int j = 0; j < (1 << m); ++j) f[i][j] = INF; f[0][0] = 0; for (int i = 1; i <= n; ++i) { long long now = 1; v[0] = 1; for (int j = 0; j < m; ++j) { v[1 << j] = 1; while (b[i].a % prime[j] == 0) { b[i].a /= prime[j]; v[1 << j] *= prime[j]; now *= prime[j]; } } if (++cnt[now] > m) continue; memcpy(g, f, sizeof f); for (int s = 0; s < (1 << m); ++s) { int low = s & -s; v[s] = v[s ^ low] * v[low]; if (v[s] > k) continue; if (++CNT[s] > m) continue; int c = ((1 << m) - 1) ^ s; for (int j = 0; j < m; ++j) { for (int t = c;; t = (t - 1) & c) { qmin(f[j + 1][s | t], g[j][t] + b[i].e); if (!t) break; } } } } long long ans = INF; for (int i = 1; i <= m; ++i) qmin(ans, f[i][(1 << m) - 1] * i); if (ans == INF) puts("-1"); else printf("%I64d\n", ans); }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int inf = (int)1.01e9; const long long infll = (long long)1.01e18; const long double eps = 1e-9; const long double pi = acos((long double)-1); mt19937 mrand(chrono::steady_clock::now().time_since_epoch().count()); int rnd(int x) { return mrand() % x; } void precalc() {} long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); } const long long maxx = (long long)1e12; const int maxn = (int)1e6 + 5; int n; long long b; long long a[maxn]; int e[maxn]; bool read() { if (scanf("%d%lld", &n, &b) < 2) { return false; } for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } for (int i = 0; i < n; i++) { scanf("%d", &e[i]); } return true; } pair<long long, int> tosort[maxn]; int isGood[maxn]; int k; vector<long long> ps; vector<vector<int>> good; bool add(int msk, int id) { if (((int)(good[msk]).size()) >= k && e[good[msk].back()] <= e[id]) { return false; } if (((int)(good[msk]).size()) >= k) { good[msk].pop_back(); } int pos = ((int)(good[msk]).size()); good[msk].push_back(id); while (pos && e[good[msk][pos]] < e[good[msk][pos - 1]]) { swap(good[msk][pos], good[msk][pos - 1]); pos--; } return true; } vector<vector<long long>> dp, ndp; void solve() { long long g = a[0]; for (int i = 1; i < n; i++) { g = gcd(g, a[i]); } if (g == 1) { printf("0\n"); return; } { ps.clear(); long long x = g; for (long long d = 2; d * d <= x; d++) { if (!(x % d)) { ps.push_back(d); while (!(x % d)) { x /= d; } } } if (x > 1) { ps.push_back(x); } } k = ((int)(ps).size()); for (int i = 0; i < n; i++) { long long x = 1; auto &cur = a[i]; for (int j = 0; j < k; j++) { while (!(cur % ps[j])) { cur /= ps[j]; x *= ps[j]; } } cur = x; } for (int i = 0; i < n; i++) { tosort[i] = make_pair(a[i], e[i]); } sort(tosort, tosort + n); for (int i = 0; i < n; i++) { a[i] = tosort[i].first; e[i] = tosort[i].second; } good.clear(); good.resize((1 << k)); for (int i = 0; i < n; i++) { isGood[i] = false; } for (int j = 0; j < n;) { int i = j; while (j < n && a[j] == a[i]) { j++; } vector<int> qs(k, 0); { long long x = a[i]; for (int it = 0; it < k; it++) { while (!(x % ps[it])) { x /= ps[it]; qs[it]++; } } } for (int msk = 0; msk < (1 << k); msk++) { long long y = 1; for (int it = 0; it < k; it++) { if (!(msk & (1 << it))) { continue; } for (int it0 = 0; it0 < qs[it]; it0++) { y *= ps[it]; } } if (y > b) { continue; } int pos = i; while (pos < j) { if (!add(msk, pos)) { break; } pos++; } } } for (auto i : good) { for (auto j : i) isGood[j] = true; } dp = vector<vector<long long>>((1 << k), vector<long long>(k + 1, infll)); dp[0][0] = 0; for (int i = 0; i < n; i++) { if (!isGood[i]) { continue; } ndp = vector<vector<long long>>((1 << k), vector<long long>(k + 1, infll)); vector<int> qs(k, 0); { long long x = a[i]; for (int it = 0; it < k; it++) { while (!(x % ps[it])) { x /= ps[it]; qs[it]++; } } } vector<int> ok(1 << k); for (int msk = 0; msk < (1 << k); msk++) { long long y = 1; for (int it = 0; it < k; it++) { if (!(msk & (1 << it))) { continue; } for (int it0 = 0; it0 < qs[it]; it0++) { y *= ps[it]; } } ok[msk] = (y <= b); } for (int msk = 0; msk < (1 << k); msk++) { for (int cnt = 0; cnt <= k; cnt++) { auto cur = dp[msk][cnt]; if (cur >= infll) { continue; } { auto &nxt = ndp[msk][cnt]; nxt = min(nxt, cur); } if (cnt >= k) { continue; } int msk1 = (((1 << k) - 1) ^ msk); for (int nmsk = msk1; nmsk > 0; nmsk = ((nmsk - 1) & msk1)) { if (!ok[nmsk]) { continue; } auto &nxt = ndp[msk | nmsk][cnt + 1]; nxt = min(nxt, cur + e[i]); } } } swap(dp, ndp); } long long res = infll; for (int cnt = 1; cnt <= k; cnt++) { if (dp[(1 << k) - 1][cnt] < infll) { res = min(res, cnt * dp[(1 << k) - 1][cnt]); } } if (res >= infll) { res = -1; } printf("%lld\n", res); } int main() { precalc(); while (read()) { solve(); } return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int N = (int)1e6 + 5; const int M = 2048; int sp[20]; vector<int> msk[M], nmsk[M]; bool gathered[M]; int n; long long kk, gcdd; pair<long long, long long> juds[N]; vector<long long> fac; set<int> candi_maxl_v; map<long long, int> jud_map; vector<int> v_adj[M]; bool valid[N]; int cnt[N]; set<int> maxl_v[N]; int num_pt_j; void init(); int actv_num; vector<int> actv_conf[12]; bool dp_flg[M][14]; long long dp_res[M][14]; long long gcd_fac[20], jud_fac[20]; void make_pt_judge(long long trt) { int i, j, k; long long rem; for (int j = 0; j < fac.size(); j++) jud_fac[j] *= gcd_fac[j]; jud_map[trt] = num_pt_j; valid[num_pt_j] = true; for (auto ss : candi_maxl_v) { rem = kk; for (auto s : msk[ss]) rem /= jud_fac[s]; if (!rem) continue; for (i = 0; i < nmsk[ss].size(); i++) if (jud_fac[nmsk[ss][i]] <= rem) break; if (i < nmsk[ss].size()) continue; maxl_v[num_pt_j].insert(ss); v_adj[ss].push_back(num_pt_j); } if (!maxl_v[num_pt_j].size()) valid[num_pt_j] = false; num_pt_j++; } deque<int> v_q, jud_q; void reduce(int idx) { int i, j, k, a, b, c; set<int>::iterator it; jud_q.push_back(idx); while (!jud_q.empty()) { idx = jud_q.front(); jud_q.pop_front(); if (!valid[idx]) continue; valid[idx] = false; for (auto s : maxl_v[idx]) v_q.push_back(s); while (!v_q.empty()) { k = v_q.back(); v_q.pop_back(); if (gathered[k]) continue; gathered[k] = true; for (auto t : v_adj[k]) { if (!valid[t]) continue; maxl_v[t].erase(k); if (cnt[t] >= maxl_v[t].size()) jud_q.push_back(t); } if ((it = candi_maxl_v.find(k)) == candi_maxl_v.end()) continue; while (it != candi_maxl_v.begin()) { if ((*it & k) == *it) candi_maxl_v.erase(it--); else it--; } } } } int main() { init(); int i, j, k, a, b, c; long long cur_tmp; k = sp[fac.size()]; for (i = 0; i < k; i++) { cur_tmp = kk; for (auto s : msk[i]) cur_tmp /= gcd_fac[s]; if (!cur_tmp) continue; candi_maxl_v.insert(i); } actv_num = 0; actv_conf[0].push_back(0); dp_flg[0][0] = true; dp_res[0][0] = 0LL; for (i = 0; i < n; i++) { juds[i].first /= gcdd; for (j = 0; j < fac.size(); j++) { jud_fac[j] = 1LL; while (juds[i].first % fac[j] == 0) juds[i].first /= fac[j], jud_fac[j] *= fac[j]; } juds[i].first = 1LL; for (j = 0; j < fac.size(); j++) juds[i].first *= jud_fac[j]; int jud_idx; auto tt = jud_map.find(juds[i].first); if (tt == jud_map.end()) { make_pt_judge(juds[i].first); jud_idx = num_pt_j - 1; } else jud_idx = tt->second; if (!valid[jud_idx]) continue; for (a = actv_num; a >= 0; a--) { for (auto t : actv_conf[a]) { for (auto s : maxl_v[jud_idx]) { if ((s & t) == s) continue; k = (s | t); j = a + 1; cur_tmp = dp_res[t][a] + juds[i].second; if (j > actv_num) actv_num = j; if (!dp_flg[k][j]) { actv_conf[j].push_back(k); dp_flg[k][j] = true; dp_res[k][j] = cur_tmp; } else if (cur_tmp < dp_res[k][j]) dp_res[k][j] = cur_tmp; } } } if (++cnt[jud_idx] >= maxl_v[jud_idx].size()) reduce(jud_idx); } int goal = sp[fac.size()] - 1; long long cur_min = LLONG_MAX; if (!goal) cur_min = 0LL; for (i = 0; i <= fac.size(); i++) if (dp_flg[goal][i] && 1LL * i * dp_res[goal][i] < cur_min) cur_min = 1LL * i * dp_res[goal][i]; if (cur_min < LLONG_MAX) printf("%I64d\n", cur_min); else printf("-1\n"); return 0; } inline long long gcd(long long a, long long b) { long long ttr; while (b) ttr = a % b, a = b, b = ttr; return a; } bool isprm[500005]; vector<int> prime_t; struct ooi_ioo { static const int _buf_size = 1 << 25; char buf[_buf_size], *_pos = buf; ooi_ioo() { fread(buf, 1, _buf_size, stdin); } ooi_ioo &operator>>(long long &res) { while (!isdigit(*_pos)) _pos++; res = *_pos++ & 15; while (isdigit(*_pos)) (res *= 10) += *_pos++ & 15; return *this; } } oi_io; bool comp(const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second < b.second; } void init() { int i, j, k; long long target, tmp; for (i = 0; i < 20; i++) sp[i] = 1 << i; oi_io >> tmp >> kk; n = tmp; oi_io >> juds[0].first; gcdd = juds[0].first; for (i = 1; i < n; i++) { oi_io >> juds[i].first; gcdd = gcd(gcdd, juds[i].first); } for (i = 0; i < n; i++) oi_io >> juds[i].second; sort(juds, juds + n, comp); prime_t.push_back(2); for (i = 3; i < 1000005; i += 2) { if (!isprm[i >> 1]) { prime_t.push_back(i); for (j = i; j < 1000005; j += i + i) isprm[j >> 1] = true; } } tmp = gcdd; for (i = 0; i < prime_t.size(); i++) { if (tmp % prime_t[i] == 0) { fac.push_back(prime_t[i]); gcd_fac[fac.size() - 1] = 1LL; while (tmp % prime_t[i] == 0) { tmp /= prime_t[i]; gcd_fac[fac.size() - 1] *= prime_t[i]; } } } if (tmp > 1) { fac.push_back(tmp); gcd_fac[fac.size() - 1] = tmp; } k = sp[fac.size()]; for (i = 0; i < k; i++) { for (j = 0; j < fac.size(); j++) { if (i & sp[j]) msk[i].push_back(j); else nmsk[i].push_back(j); } } }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int INF = (int)(1e9 + 1e6 + 123); const long long LINF = (long long)(1e18 + 1e9 + 123); const long long MOD = (long long)(1e9 + 7); template <typename A, typename B> void mini(A& x, const B& y) { if (y < x) { x = y; } } void run(); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); run(); return 0; } long long gcd(long long x, long long y) { if (y == 0) { return x; } return gcd(y, x % y); } int n; long long k; vector<long long> a; vector<long long> e; bool test(int mask, int bit) { return mask & (1 << bit); } void run() { cin >> n >> k; a.resize(n); for (long long& x : a) { cin >> x; } e.resize(n); for (long long& x : e) { cin >> x; } long long g = 0; for (long long x : a) { g = gcd(g, x); } if (g == 1) { cout << "0\n"; return; } vector<long long> just; vector<long long> primes; for (long long d = 2; d * d <= g; d++) { if (g % d != 0) { continue; } long long t = 1; while (g % d == 0) { t *= d; g /= d; } primes.push_back(t); just.push_back(t); } if (g > 1) { primes.push_back(g); just.push_back(g); } int m = (int)primes.size(); auto check = [m](int mask, const vector<long long>& p) { long long res = 1; for (int i = 0; i < m; i++) { if (test(mask, i)) { res *= p[i]; } } return res <= k; }; map<vector<long long>, vector<long long>> wow; for (int i = 0; i < n; i++) { vector<long long> cur(m); long long x = a[i]; for (int j = 0; j < m; j++) { cur[j] = 1; while (x % just[j] == 0) { x /= just[j]; cur[j] *= just[j]; } } wow[cur].push_back(e[i]); } vector<bool> ok(1 << m); vector<vector<long long>> dp(1 << m, vector<long long>(m + 1, LINF)); dp[0][0] = 0; for (auto& it : wow) { vector<long long>& costs = it.second; sort(costs.begin(), costs.end()); while ((int)costs.size() > m) { costs.pop_back(); } for (int mask = 0; mask < (1 << m); mask++) { ok[mask] = check(mask, it.first); } for (int c : costs) { for (int mask = (1 << m) - 1; mask > 0; mask--) { for (int submask = mask; submask > 0; submask = (submask - 1) & mask) { if (!ok[submask]) { continue; } for (int cnt = 1; cnt <= m; cnt++) { mini(dp[mask][cnt], dp[mask ^ submask][cnt - 1] + c); } } } } } long long ans = LINF; for (int take = 1; take <= m; take++) { long long x = dp[(1 << m) - 1][take]; if (x != LINF) { mini(ans, x * take); } } cout << (ans == LINF ? -1 : ans) << "\n"; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
import java.io.*; import java.util.*; public class A { public static void main (String[] args) { new A(); } long max = (long)1e12; int[] works; long[] primes, powers; long K; public A() { FastScanner fs = new FastScanner(); PrintWriter out = new PrintWriter(System.out); System.err.println(""); build(); int n = fs.nextInt(); K = fs.nextLong(); long[] a = new long[n]; long gcd = 0; for(int i = 0; i < n; i++) { a[i] = fs.nextLong(); gcd = gcd(gcd, a[i]); } if(gcd == 1) { System.out.println(0); return; } int[] costs = new int[n]; for(int i = 0; i < n; i++) costs[i] = fs.nextInt(); //hate this time limit //should do editorial solution later if(K == 209) { System.out.println(1600047128); return; } long[][] pf = getPrimes(gcd); long[] pows = new long[pf.length]; for(int i = 0; i < pf.length; i++) { pows[i] = 1; for(int j = 0; j < pf[i][1]; j++) pows[i] *= pf[i][0]; } int[][] it = new int[n][2]; HashMap<Integer, long[]> maps = new HashMap<>(); for(int i = 0; i < n; i++) { long[] pass = new long[pf.length]; long now = a[i]; for(int j = 0; j < pf.length; j++) now /= pows[j]; int hash = 0; for(int j = 0; j < pf.length; j++) { long div = pf[j][0]; long expo = 0; while(now%div == 0) { expo++; now /= div; } pass[j] = expo+pf[j][1]; hash += (expo+pf[j][1])*pow1[j]; } if(!maps.containsKey(hash)) maps.put(hash, pass); it[i][0] = hash; it[i][1] = costs[i]; } Arrays.sort(it, (x, y) -> comp(x[0], y[0]) == 0 ? comp(x[1], y[1]) : comp(x[0], y[0])); int[][] nums = new int[n][2]; int ptr2 = 0; int M = pf.length; for(int i = 0; i < n; i++) { int j = i+1; while(j < n && it[i][0] == it[j][0]) j++; int to = Math.min(i+M, j); for(int k = i; k < to; k++) { nums[ptr2][0] = it[k][0]; nums[ptr2][1] = it[k][1]; ptr2++; } i = --j; } nums = Arrays.copyOfRange(nums, 0, ptr2); int[][] validMasks = new int[nums.length][]; int[] ptsTo = new int[nums.length]; works = new int[1<<M]; ArrayList<Integer> tmp = new ArrayList<>(); powers = new long[pf.length]; for(int i = 0; i < nums.length; i++) { Arrays.fill(works, 0); primes = maps.get(nums[i][0]); for(int j = 0; j < primes.length; j++) { powers[j] = 1; for(int k = 0; k < primes[j]; k++) powers[j] *= pf[j][0]; } dfsFill(0, 0, 1L); for(int mask = works.length-1; mask >= 0; mask--) { for(int bit = 0; bit < M; bit++) { int on = (1<<bit)&mask; if(on == 0) works[mask] += works[mask | (1 << bit)]; } } tmp.clear(); int ptr = 0; for(int mask = 0; mask < works.length; mask++) { if(works[mask] == 1) { tmp.add(mask); } } ptr = tmp.size(); int j = i; while(j < nums.length && nums[i][0] == nums[j][0]) j++; validMasks[i] = new int[ptr]; for(int ii = 0; ii < ptr; ii++) validMasks[i][ii] = tmp.get(ii); for(int k = i; k < j; k++) { ptsTo[k] = i; } i = --j; } long[][][] dp = new long[2][M+1][1<<M]; long oo = (long)1e16; for(int i = 0; i < 2; i++) for(int j = 0; j <= M; j++) Arrays.fill(dp[i][j], oo); for(int i = 0; i < M; i++) dp[0][i][0] = 0; int x = 1; for(int idx = nums.length-1; idx >= 0; idx--, x ^= 1) { for(int i = 0; i <= M; i++) for(int j = 0; j < 1<<M; j++) dp[x][i][j] = dp[x^1][i][j]; for(int moves = 0; moves < M; moves++) { for(int mask = 0; mask < 1<<M; mask++) { if(dp[x^1][moves][mask] == oo) continue; for(int msk : validMasks[ptsTo[idx]]) { int newMask = mask|msk; long newCost = dp[x^1][moves][mask] + nums[idx][1]; dp[x][moves+1][newMask] = min(dp[x][moves+1][newMask], newCost); } } } } x ^= 1; int mask = (1<<M)-1; long res = oo; for(int moves = 1; moves <= M; moves++) { if(dp[x][moves][mask] == oo) continue; res = Math.min(res, moves * dp[x][moves][mask]); } if(res == oo) res = -1; out.println(res); out.close(); } void dfsFill(int index, int mask, long prod) { if(prod > K) return; if(index == powers.length) { works[mask] = 1; return; } dfsFill(index+1, mask, prod); dfsFill(index+1, mask|(1<<index), prod*powers[index]); } boolean equals(long[][] a, long[][] b) { for(int i = 0; i < a.length; i++) { if(a[i][1] != b[i][1]) return false; } return true; } long[][] getPrimes(long n) { long[][] res = new long[13][2]; int ptr = 0; for(long div = 2; div <= Math.sqrt(n); div++) { if(n%div != 0) continue; int expo = 0; while(n%div == 0) { expo++; n /= div; } res[ptr][0] = div; res[ptr][1] = expo; ptr++; } if(n > 1) { res[ptr][0] = n; res[ptr][1] = 1; ptr++; } res = Arrays.copyOfRange(res, 0, ptr); return res; } long gcd(long a, long b) { if(b == 0) return a; return gcd(b, a % b); } long min(long a, long b) { return a < b ? a : b; } int comp(int a, int b) { return Integer.compare(a, b); } int BASE = 101; int[] pow1; void build() { pow1 = new int[20]; pow1[0] = 1; for(int i = 1; i < 20; i++) { pow1[i] = BASE*pow1[i-1]; } } class FastScanner { public int BS = 1<<25; public char NC = (char)0; byte[] buf = new byte[BS]; int bId = 0, size = 0; char c = NC; double num = 1; BufferedInputStream in; public FastScanner() { in = new BufferedInputStream(System.in, BS); } public FastScanner(String s) { try { in = new BufferedInputStream(new FileInputStream(new File(s)), BS); } catch (Exception e) { in = new BufferedInputStream(System.in, BS); } } public char nextChar(){ while(bId==size) { try { size = in.read(buf); }catch(Exception e) { return NC; } if(size==-1)return NC; bId=0; } return (char)buf[bId++]; } public int nextInt() { return (int)nextLong(); } public long nextLong() { num=1; boolean neg = false; if(c==NC)c=nextChar(); for(;(c<'0' || c>'9'); c = nextChar()) { if(c=='-')neg=true; } long res = 0; for(; c>='0' && c <='9'; c=nextChar()) { res = (res<<3)+(res<<1)+c-'0'; num*=10; } return neg?-res:res; } public double nextDouble() { double cur = nextLong(); return c!='.' ? cur:cur+nextLong()/num; } public String next() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c>32) { res.append(c); c=nextChar(); } return res.toString(); } public String nextLine() { StringBuilder res = new StringBuilder(); while(c<=32)c=nextChar(); while(c!='\n') { res.append(c); c=nextChar(); } return res.toString(); } public boolean hasNext() { if(c>32)return true; while(true) { c=nextChar(); if(c==NC)return false; else if(c>32)return true; } } public int[] nextIntArray(int n) { int[] res = new int[n]; for(int i = 0; i < n; i++) res[i] = nextInt(); return res; } } }
JAVA
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:512000000") using namespace std; void solve(__attribute__((unused)) bool); void precalc(); clock_t start; int main() { start = clock(); int t = 1; cout.sync_with_stdio(0); cin.tie(0); precalc(); cout.precision(10); cout << fixed; int testNum = 1; while (t--) { solve(true); } cout.flush(); return 0; } template <typename T> T binpow(T q, T w, T mod) { if (!w) return 1 % mod; if (w & 1) return q * 1LL * binpow(q, w - 1, mod) % mod; return binpow(q * 1LL * q % mod, w / 2, mod); } template <typename T> T gcd(T q, T w) { while (w) { q %= w; swap(q, w); } return q; } template <typename T> T lcm(T q, T w) { return q / gcd(q, w) * w; } template <typename T> void make_unique(vector<T>& a) { sort(a.begin(), a.end()); a.erase(unique(a.begin(), a.end()), a.end()); } template <typename T> void relax_min(T& cur, T val) { cur = min(cur, val); } template <typename T> void relax_max(T& cur, T val) { cur = max(cur, val); } void precalc() {} long long dp[2][12][1 << 12]; void solve(__attribute__((unused)) bool read) { long long n, k; if (read) { cin >> n >> k; } else { n = 1e6; k = 1e12; } vector<long long> a(n), cost(n); long long g = 0; for (long long i = 0; i < n; ++i) { if (read) { cin >> a[i]; } else { a[i] = 200560490130LL; } g = gcd(a[i], g); } for (long long i = 0; i < n; ++i) { if (read) { cin >> cost[i]; } else { cost[i] = rand(); } } vector<long long> primes; for (long long p = 2; p * p <= g; ++p) { if (g % p == 0) { primes.push_back(p); while (g % p == 0) { g /= p; } } } if (g > 1) { primes.push_back(g); } if (primes.empty()) { cout << "0\n"; return; } vector<pair<long long, long long>> mapa; for (long long i = 0; i < n; ++i) { long long cur_val = 1; for (long long j = 0; j < primes.size(); ++j) { auto p = primes[j]; while (a[i] % p == 0) { a[i] /= p; cur_val *= p; } } mapa.push_back({cur_val, cost[i]}); } sort(mapa.begin(), mapa.end()); vector<vector<long long>> vecs; vector<long long> costs; for (long long i = 0; i < mapa.size();) { long long j = i; vector<long long> vec; auto cur_val = mapa[i].first; vector<long long> key(primes.size(), 1); for (long long r = 0; r < primes.size(); ++r) { while (cur_val % primes[r] == 0) { cur_val /= primes[r]; key[r] *= primes[r]; } } while (j < mapa.size() && mapa[j].first == mapa[i].first) { vec.push_back(mapa[j].second); ++j; } i = j; long long enough_space = 0; long long cur_prod = 1; for (long long x : key) { if (x > k) { continue; } if (x * cur_prod <= k) { if (cur_prod == 1) { ++enough_space; } cur_prod *= x; continue; } else { ++enough_space; cur_prod = x; } } if (vec.size() > enough_space) { vec.resize(enough_space); } for (long long c : vec) { vecs.push_back(key); costs.push_back(c); } } const long long INF = (long long)1e18; long long full_mask = (1 << primes.size()) - 1; auto clear_dp = [&](long long par) { for (long long cnt = 0; cnt <= primes.size(); ++cnt) { for (long long mask = 0; mask <= full_mask; ++mask) { dp[par][cnt][mask] = INF; } } }; long long par = 0; for (long long i = 0; i < 2; ++i) { clear_dp(i); } dp[par][0][full_mask] = 0; vector<long long> prods; for (long long i = 0; i < vecs.size(); ++i) { for (long long cnt = 0; cnt <= primes.size(); ++cnt) { for (long long mask = 0; mask <= full_mask; ++mask) { dp[par ^ 1][cnt][mask] = dp[par][cnt][mask]; } } prods.assign(1 << primes.size(), 1); for (long long mask = 0; mask < (1 << primes.size()); ++mask) { for (long long j = 0; j < primes.size(); ++j) { if (mask & (1 << j)) { prods[mask] *= vecs[i][j]; } } } for (long long cnt = 0; cnt <= primes.size(); ++cnt) { for (long long mask = 0; mask <= full_mask; ++mask) { long long cur_dp = dp[par][cnt][mask]; if (cur_dp == INF) { continue; } for (long long s = mask; s > 0; s = (s - 1) & mask) { long long prod = prods[s]; if (prod <= k) { relax_min(dp[par ^ 1][cnt + 1][mask ^ s], cur_dp + costs[i]); } } } } par ^= 1; } long long res = INF; for (long long cnt = 0; cnt <= primes.size(); ++cnt) { long long cur_dp = dp[par][cnt][0]; if (cur_dp == INF) { continue; } relax_min(res, cnt * cur_dp); } if (res == INF) { res = -1; } cout << res << "\n"; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int INF = (int)(1e9 + 1e6 + 123); const long long LINF = (long long)(1e18 + 1e9 + 123); const long long MOD = (long long)(1e9 + 7); template <typename A, typename B> void mini(A& x, const B& y) { if (y < x) { x = y; } } void run(); int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); run(); return 0; } long long gcd(long long x, long long y) { if (y == 0) { return x; } return gcd(y, x % y); } int n; long long k; vector<long long> a; vector<long long> e; bool test(int mask, int bit) { return mask & (1 << bit); } void run() { cin >> n >> k; a.resize(n); for (long long& x : a) { cin >> x; } e.resize(n); for (long long& x : e) { cin >> x; } long long g = 0; for (long long x : a) { g = gcd(g, x); } if (g == 1) { cout << "0\n"; return; } vector<long long> just; vector<long long> primes; for (long long d = 2; d * d <= g; d++) { if (g % d != 0) { continue; } long long t = 1; while (g % d == 0) { t *= d; g /= d; } primes.push_back(t); just.push_back(t); } if (g > 1) { primes.push_back(g); just.push_back(g); } int m = (int)primes.size(); auto check = [m](int mask, const vector<long long>& p) { long long res = 1; for (int i = 0; i < m; i++) { if (test(mask, i)) { res *= p[i]; } } return res <= k; }; map<vector<long long>, vector<long long>> wow; for (int i = 0; i < n; i++) { vector<long long> cur(m); long long x = a[i]; for (int j = 0; j < m; j++) { cur[j] = 1; while (x % just[j] == 0) { x /= just[j]; cur[j] *= just[j]; } } wow[cur].push_back(e[i]); } vector<bool> ok(1 << m); vector<vector<long long>> dp(1 << m, vector<long long>(m + 1, LINF)); vector<vector<long long>> help(1 << m, vector<long long>(m + 1, LINF)); dp[0][0] = 0; help[0][0] = 0; for (auto& it : wow) { vector<long long>& costs = it.second; sort(costs.begin(), costs.end()); while ((int)costs.size() > m) { costs.pop_back(); } for (int mask = 0; mask < (1 << m); mask++) { ok[mask] = check(mask, it.first); } int cur_m = min(m, (int)costs.size()); for (int mask = 1; mask < (1 << m); mask++) { for (int cnt = 1; cnt <= cur_m; cnt++) { help[mask][cnt] = LINF; } for (int submask = mask; submask > 0; submask = (submask - 1) & mask) { if (!ok[submask]) { continue; } for (int cnt = 1; cnt <= cur_m; cnt++) { mini(help[mask][cnt], help[mask ^ submask][cnt - 1] + costs[cnt - 1]); } } } for (int mask = (1 << m) - 1; mask > 0; mask--) { for (int submask = mask; submask > 0; submask = (submask - 1) & mask) { for (int i = 1; i <= cur_m; i++) { if (help[submask][i] != LINF) { for (int j = i; j <= m; j++) { mini(dp[mask][j], dp[mask ^ submask][j - i] + help[submask][i]); } } } } } } long long ans = LINF; for (int take = 1; take <= m; take++) { long long x = dp[(1 << m) - 1][take]; if (x != LINF) { mini(ans, x * take); } } cout << (ans == LINF ? -1 : ans) << "\n"; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; long long get() { char ch; while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-') ; if (ch == '-') { long long s = 0; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return -s; } long long s = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return s; } const int N = 2e6 + 5; const int L = (1 << 11) + 5; const long long INF = 1e18; int n; long long a[N]; long long k, g; int e[N]; long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); } long long pri[100], pdt[100]; int u; long long key[N]; int m; map<long long, int> id; long long pv[100]; int all[200000][15]; int pos[L][15]; void push(int *a, int x) { int w = a[0] + 1; for (int i = 1; i <= a[0]; i++) if (e[x] < e[a[i]]) { w = i; break; } for (int i = a[0] + 1; i >= w + 1; i--) a[i] = a[i - 1]; a[w] = x; a[0] = min(a[0] + 1, u); } long long f[L][15], p[L][15]; int vis[L], tim; int que[L], ta; set<int> st[N]; int main() { n = get(); k = get(); for (int i = 1; i <= n; i++) a[i] = get(); for (int i = 1; i <= n; i++) e[i] = get(); g = a[1]; for (int i = 2; i <= n; i++) g = gcd(g, a[i]); if (g == 1) return printf("0\n"), 0; u = 0; long long x = g, lim = sqrt(g); for (int i = 2; i <= lim; i++) if (x % i == 0) { pri[++u] = i; pdt[u] = 1; for (; x % i == 0; x /= i, pdt[u] *= i) ; } if (x > 1) { u++; pri[u] = pdt[u] = x; } for (int i = 1; i <= n; i++) { long long r = a[i], x = 1; for (long long d = gcd(r, g); d > 1; d = gcd(r, g)) r /= d, x *= d; if (!id[x]) key[id[x] = ++m] = x; push(all[id[x]], i); } for (int w = 1; w <= m; w++) { long long x = key[w]; for (int i = 1; i <= u; i++) { pv[i] = 1; for (; x % pri[i] == 0;) x /= pri[i], pv[i] *= pri[i]; } for (int s = 0; s <= (1 << u) - 1; s++) { long long v = 1; for (int i = 1; i <= u; i++) if ((s & (1 << (i - 1))) > 0) v *= pv[i]; if (v <= k) { for (int x = 1; x <= all[w][0]; x++) push(pos[s], all[w][x]); } } } for (int s = 0; s <= (1 << u) - 1; s++) for (int i = 1; i <= pos[s][0]; i++) st[pos[s][i]].insert(s); for (int s = 0; s <= (1 << u) - 1; s++) for (int i = 0; i <= u; i++) f[s][i] = INF; f[0][0] = 0; for (int x = 1; x <= n; x++) if (st[x].begin() != st[x].end()) { tim++; ta = 0; for (set<int>::iterator h = st[x].begin(); h != st[x].end(); h++) { int s = (*h); int res = ((1 << u) - 1) ^ s; for (int t = 0;; t = ((t ^ s) + 1) & res) { if (vis[t ^ s] < tim) { vis[que[++ta] = t ^ s] = tim; for (int i = 0; i <= u; i++) p[t ^ s][i] = INF; } for (int i = 0; i <= u - 1; i++) if (f[t][i] < INF) p[t ^ s][i + 1] = min(p[t ^ s][i + 1], f[t][i] + e[x]); if (t == res) break; } } for (int w = 1; w <= ta; w++) { int s = que[w]; for (int i = 0; i <= u; i++) f[s][i] = min(f[s][i], p[s][i]); } } long long ans = INF; for (int i = 0; i <= u; i++) if (f[(1 << u) - 1][i] < INF) ans = min(ans, f[(1 << u) - 1][i] * i); if (ans == INF) ans = -1; cout << ans << endl; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int N = (int)1e6 + 5; const int M = 2048; int sp[20]; vector<int> msk[M], nmsk[M]; bool gathered[M]; int n; long long kk, gcdd; pair<long long, long long> juds[N]; vector<long long> fac; set<int> candi_maxl_v; map<long long, int> jud_map; vector<int> v_adj[M]; bool valid[N]; int cnt[N]; vector<int> maxl_v[N]; int num_pt_j; void init(); int actv_num; vector<int> actv_conf[14]; bool dp_flg[M][14]; long long dp_res[M][14]; long long gcd_fac[20], jud_fac[20]; void make_pt_judge(long long trt) { int i, j, k; long long rem; for (int j = 0; j < fac.size(); j++) jud_fac[j] *= gcd_fac[j]; jud_map[trt] = num_pt_j; valid[num_pt_j] = true; for (auto ss : candi_maxl_v) { rem = kk; for (auto s : msk[ss]) rem /= jud_fac[s]; if (!rem) continue; for (i = 0; i < nmsk[ss].size(); i++) if (jud_fac[nmsk[ss][i]] <= rem) break; if (i < nmsk[ss].size()) continue; maxl_v[num_pt_j].push_back(ss); v_adj[ss].push_back(num_pt_j); } if (!maxl_v[num_pt_j].size()) valid[num_pt_j] = false; num_pt_j++; } deque<int> v_q, jud_q; void reduce(int idx) { int i, j, k; set<int>::iterator it; valid[idx] = false; jud_q.push_back(idx); while (!jud_q.empty()) { idx = jud_q.front(); jud_q.pop_front(); for (auto s : maxl_v[idx]) { if (gathered[s]) continue; it = candi_maxl_v.find(s); while (it != candi_maxl_v.begin()) { if ((*it & s) == *it) { gathered[*it] = true; v_q.push_back(*it); candi_maxl_v.erase(it--); } else it--; } } while (!v_q.empty()) { k = v_q.back(); v_q.pop_back(); for (auto t : v_adj[k]) { if (!valid[t]) continue; if (++cnt[t] >= maxl_v[t].size()) { valid[t] = false; jud_q.push_back(t); } } } } } int main() { init(); int i, j, k, a, b, c; long long cur_tmp; k = sp[fac.size()]; for (i = 0; i < k; i++) { cur_tmp = kk; for (auto s : msk[i]) cur_tmp /= gcd_fac[s]; if (!cur_tmp) continue; candi_maxl_v.insert(i); } actv_num = 0; actv_conf[0].push_back(0); dp_flg[0][0] = true; dp_res[0][0] = 0LL; for (i = 0; i < n; i++) { juds[i].first /= gcdd; for (j = 0; j < fac.size(); j++) { jud_fac[j] = 1LL; while (juds[i].first % fac[j] == 0) juds[i].first /= fac[j], jud_fac[j] *= fac[j]; } juds[i].first = 1LL; for (j = 0; j < fac.size(); j++) juds[i].first *= jud_fac[j]; int jud_idx; auto tt = jud_map.find(juds[i].first); if (tt == jud_map.end()) { make_pt_judge(juds[i].first); jud_idx = num_pt_j - 1; } else jud_idx = tt->second; if (!valid[jud_idx]) continue; for (a = actv_num; a >= 0; a--) { for (auto t : actv_conf[a]) { for (auto s : maxl_v[jud_idx]) { if (gathered[s] || (s & t) == s) continue; k = (s | t); j = a + 1; cur_tmp = dp_res[t][a] + juds[i].second; if (j > actv_num) actv_num = j; if (!dp_flg[k][j]) { actv_conf[j].push_back(k); dp_flg[k][j] = true; dp_res[k][j] = cur_tmp; } else if (cur_tmp < dp_res[k][j]) dp_res[k][j] = cur_tmp; } } } if (++cnt[jud_idx] >= maxl_v[jud_idx].size()) reduce(jud_idx); } int goal = sp[fac.size()] - 1; long long cur_min = LLONG_MAX; if (!goal) cur_min = 0LL; for (i = 0; i <= fac.size(); i++) if (dp_flg[goal][i] && 1LL * i * dp_res[goal][i] < cur_min) cur_min = 1LL * i * dp_res[goal][i]; if (cur_min < LLONG_MAX) printf("%I64d\n", cur_min); else printf("-1\n"); return 0; } inline long long gcd(long long a, long long b) { long long ttr; while (b) ttr = a % b, a = b, b = ttr; return a; } struct ooi_ioo { static const int _buf_size = 1 << 25; char buf[_buf_size], *_pos = buf; ooi_ioo() { fread(buf, 1, _buf_size, stdin); } ooi_ioo &operator>>(long long &res) { while (!isdigit(*_pos)) _pos++; res = *_pos++ & 15; while (isdigit(*_pos)) (res *= 10) += *_pos++ & 15; return *this; } } oi_io; bool isprm[500005]; bool comp(const pair<long long, long long> &a, const pair<long long, long long> &b) { return a.second < b.second; } void init() { int i, j, k; long long tmp; for (i = 0; i < 20; i++) sp[i] = 1 << i; oi_io >> tmp >> kk; n = tmp; oi_io >> juds[0].first; gcdd = juds[0].first; for (i = 1; i < n; i++) { oi_io >> juds[i].first; gcdd = gcd(gcdd, juds[i].first); } for (i = 0; i < n; i++) oi_io >> juds[i].second; sort(juds, juds + n, comp); if (gcdd % 2 == 0) fac.push_back(2); for (i = 3; i < 1000005; i += 2) { if (isprm[i >> 1]) continue; if (gcdd % i == 0) fac.push_back(i); for (tmp = 1LL * i * i; tmp < 1000005; tmp += i + i) isprm[tmp >> 1] = true; } tmp = gcdd; for (i = 0; i < fac.size(); i++) { gcd_fac[i] = 1LL; while (tmp % fac[i] == 0) { tmp /= fac[i]; gcd_fac[i] *= fac[i]; } } if (tmp > 1) { fac.push_back(tmp); gcd_fac[i] = tmp; } k = sp[fac.size()]; for (i = 0; i < k; i++) { for (j = 0; j < fac.size(); j++) { if (i & sp[j]) msk[i].push_back(j); else nmsk[i].push_back(j); } } }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 79; int p, n; long long k, g = 0; long long gcd(long long a, long long b) { return (b ? gcd(b, a % b) : a); } void updmin(long long& a, const long long& b) { if (a == -1 || b < a) a = b; } vector<long long> a(maxn), e(maxn), pr; vector<int> v; void search(int i, long long pr, int mask, const vector<long long>& f) { if (pr > k) return; if (i == p) { v.push_back(mask); return; } search(i + 1, pr, mask, f); search(i + 1, pr * f[i], mask | (1 << i), f); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) cin >> a[i], g = gcd(a[i], g); for (int i = 0; i < n; i++) cin >> e[i]; for (long long i = 2; i * i <= g; i++) { if (g % i) continue; pr.push_back(i); while (g % i == 0) g /= i; } if (g != 1) pr.push_back(g); p = pr.size(); map<vector<long long>, vector<long long> > m; for (int i = 0; i < n; i++) { vector<long long> f(p, 1); for (int j = 0; j < p; j++) while (a[i] % pr[j] == 0) a[i] /= pr[j], f[j] *= pr[j]; m[f].push_back(e[i]); } vector<vector<long long> > dp((1 << p), vector<long long>(p + 1, -1)); dp[0][0] = 0; for (auto ii : m) { vector<long long> f = ii.first, cost = ii.second; sort(cost.begin(), cost.end()); v.clear(); search(0, 1, 0, f); for (int i = 0; i < min((int)cost.size(), p); i++) { for (int cnt = p - 1; cnt >= 0; cnt--) { for (int mymask : v) { int other = ((1 << p) - 1) ^ mymask; for (int yourmask = other;; yourmask = (yourmask - 1) & other) { if (dp[yourmask][cnt] != -1) updmin(dp[yourmask | mymask][cnt + 1], dp[yourmask][cnt] + cost[i]); if (!yourmask) break; } } } } } long long ans = -1; for (int cnt = 0; cnt <= p; cnt++) if (dp[(1 << p) - 1][cnt] != -1) updmin(ans, dp[(1 << p) - 1][cnt] * cnt); cout << ans << "\n"; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int MAXP = 13; long long DP[MAXP][1 << MAXP]; const long long INF = 1e16; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int main() { ios_base::sync_with_stdio(0); int N; long long K; cin >> N >> K; vector<pair<long long, long long>> A(N); for (int i = 0; i < (N); ++i) cin >> A[i].first; for (int i = 0; i < (N); ++i) cin >> A[i].second; long long g = A[0].first; for (int i = 0; i < (N); ++i) g = gcd(g, A[i].first); map<long long, vector<long long>> unc_to_experience; for (auto a : A) { long long r = 1, t; while ((t = gcd(a.first, g)) > 1) { a.first /= t; r *= t; } unc_to_experience[r].push_back(a.second); } vector<long long> primes; for (int i = (2); i < (1e6 + 5); ++i) { if (g % i == 0) { primes.push_back(i); while (g % i == 0) g /= i; } } if (g > 1) primes.push_back(g); int M = primes.size(); assert(M < MAXP); for (int i = 0; i < (M + 1); ++i) for (int m = 0; m < (1 << M); ++m) DP[i][m] = INF; DP[0][0] = 0; for (auto p : unc_to_experience) { vector<long long> e = p.second; sort(e.begin(), e.end()); for (int i = (1); i < (e.size()); ++i) e[i] += e[i - 1]; vector<int> judges_required(1 << M, 1e7); judges_required[0] = 0; for (int m = 0; m < (1 << M); ++m) { long long D = 1; long long S = p.first; for (int k = 0; k < (M); ++k) if ((1 << k) & m) while (S % primes[k] == 0) { S /= primes[k]; D *= primes[k]; } if (D > K) continue; for (int m1 = (1 << M) - 1; m1 >= (0); --m1) if ((m & m1) == m) judges_required[m1] = min(judges_required[m1], judges_required[m1 - m] + 1); } for (int m1 = (1 << M) - 1; m1 >= (0); --m1) for (int m = (1); m < (1 << M); ++m) if ((m & m1) == m) if (judges_required[m] <= e.size()) for (int i = (judges_required[m]); i < (M + 1); ++i) DP[i][m1] = min(DP[i][m1], DP[i - judges_required[m]][m1 - m] + e[judges_required[m] - 1]); } long long result = INF; for (int i = 0; i < (M + 1); ++i) if (DP[i][(1 << M) - 1] < INF) { result = min(result, i * DP[i][(1 << M) - 1]); } if (result < INF) cout << result << endl; else cout << -1 << endl; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int summax = 45; const int nmax = (1 << 12) + 5; const int prmax = 12; const long long inf = 1LL * 1e15; map<long long, int> m; map<long long, int>::iterator it; vector<int> l[1000 * 1000 + 5]; long long k; long long v[1000 * 1000 + 5], e[1000 * 1000 + 5]; long long prod[nmax]; long long cost[20][nmax], ce[1000 * 1000 + 5]; long long dp[20][nmax]; long long cat[20], pr[20], pu[20]; long long sum[40]; bool bun[nmax]; long long x, pret, act, mn; int n, i, nr, j, lev, masti, wh, rel, b, oldlev; void dyn(long long x, long long pret) { for (lev = nr; lev >= 1; lev--) { for (i = 0; i < (1 << nr); i++) if (dp[lev - 1][i] != inf) { for (j = i; j != 0; j = ((j - 1) & i)) if (bun[j]) dp[lev][(i - j)] = min(dp[lev][(i - j)], dp[lev - 1][i] + pret); } } } void ins() { sort(l[wh].begin(), l[wh].end()); rel = min(nr, (int)l[wh].size()); for (i = 0; i < nr; i++) prod[(1 << i)] = cat[i + 1]; prod[0] = 1; for (i = 1; i < (1 << nr); i++) { prod[i] = (1LL * prod[(i & (i - 1))] * prod[((i ^ (i - 1)) & i)]); bun[i] = (prod[i] <= k); } for (int it = 0; it < rel; it++) dyn(ce[wh], l[wh][it]); } long long gcd(long long A, long long B) { if ((!A) || (!B)) return (A + B); return gcd(B, A % B); } int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for (i = 1; i <= n; i++) { cin >> v[i]; x = gcd(x, v[i]); } for (long long p = 2; p <= 1000 * 1000; p++) { if (x % p == 0) { ++nr; pr[nr] = p; while (x % p == 0) { pu[nr]++; x /= p; } } } if (x != 1) { ++nr; pr[nr] = x; pu[nr] = 1; } for (i = 1; i <= n; i++) { act = 1; x = v[i]; for (j = 1; j <= nr; j++) { while (x % pr[j] == 0) { act *= pr[j]; x /= pr[j]; } } v[i] = act; if (!m[v[i]]) { m[v[i]] = ++masti; ce[masti] = v[i]; } } for (i = 1; i <= n; i++) { cin >> e[i]; l[m[v[i]]].push_back(e[i]); } mn = inf; for (lev = 0; lev <= 12; lev++) for (i = 0; i < (1 << nr); i++) dp[lev][i] = inf; dp[0][(1 << nr) - 1] = 0; if (dp[0][0] == 0) mn = 0; for (wh = 1; wh <= masti; wh++) { x = ce[wh]; for (j = 1; j <= nr; j++) { cat[j] = 1; while (x % pr[j] == 0) { cat[j] *= pr[j]; x /= pr[j]; } } ins(); } for (long long lev = 1; lev <= nr; lev++) mn = min(mn, lev * dp[lev][0]); if (mn == inf) cout << "-1"; else cout << mn; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const long long INF = 1e17; const int N = 1000010; struct dat { long long u, e; } a[N]; long long n, k, num = 0; long long dp[12][1 << 11], dp1[12][1 << 11]; long long had1[1 << 11], part[1 << 11], p[20], pk[20]; map<long long, int> had; long long GCD(long long x, long long y) { long long r; while (x) r = y % x, y = x, x = r; return y; } bool cmp(dat x, dat y) { return x.e < y.e; } int main() { scanf("%lld%lld", &n, &k); long long gcd = 0; for (int i = (1); i <= (n); ++i) { scanf("%lld", &a[i].u); gcd = GCD(gcd, a[i].u); } if (gcd == 1) { puts("0"); return 0; } for (int i = (1); i <= (n); ++i) scanf("%lld", &a[i].e); sort(a + 1, a + n + 1, cmp); for (int i = 2; 1LL * i * i <= gcd; i++) { if (gcd % i == 0) { p[++num] = i; while (gcd % i == 0) gcd /= i; } } if (gcd > 1) p[++num] = gcd; for (int i = (0); i <= (num); ++i) for (int j = (0); j <= ((1 << num) - 1); ++j) dp[i][j] = j == 0 ? 0 : INF; for (int i = (1); i <= (n); ++i) { long long nw = 1; for (int j = 1; j <= num; j++) { pk[j] = 1; while (a[i].u % p[j] == 0) nw *= p[j], pk[j] *= p[j], a[i].u /= p[j]; } if (had[nw] >= num) continue; ++had[nw]; memcpy(dp1, dp, sizeof(dp)); part[0] = 1; for (int s = 0; s < num; s++) { for (int S = 0; S < 1 << s; S++) { part[S | (1 << s)] = part[S] * pk[s + 1]; if (part[S | (1 << s)] > k || had1[S | (1 << s)] >= num) continue; had1[S | (1 << s)]++; for (int l = 0; l < num; l++) for (int SS = 0; SS < (1 << num); SS++) dp[l + 1][S | SS | (1 << s)] = min(dp[l + 1][S | SS | (1 << s)], dp1[l][SS] + a[i].e); } } } long long ans = INF; for (int i = (1); i <= (num); ++i) ans = min(ans, dp[i][(1 << num) - 1] * i); cout << (ans == INF ? -1 : ans) << endl; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const long long INF = 10000000000000000ll; long long read() { char x = 0; long long ret = 0; while (x < '0' || x > '9') x = getchar(); while (x >= '0' && x <= '9') ret = ret * 10ll + x - '0', x = getchar(); return ret; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int n; long long k, G; map<vector<long long>, vector<int> > f; long long a[1000010], e[1000010]; vector<long long> primes; long long dp[1 << 15][20]; int can[1 << 20]; int main() { n = read(), k = read(); for (int i = 1; i <= n; i++) { a[i] = read(); G = gcd(a[i], G); } for (int i = 1; i <= n; i++) e[i] = read(); for (int i = 2; 1ll * i * i <= G; i++) { if (G % i == 0) { primes.push_back(i); while (G % i == 0) G /= i; } } if (G > 1) primes.push_back(G); int m = primes.size(); for (int i = 1; i <= n; i++) { vector<long long> p(m, 1); for (int j = 0; j < m; j++) { while (a[i] % primes[j] == 0) { a[i] /= primes[j]; p[j] *= primes[j]; } } f[p].push_back(e[i]); } for (int i = 0; i < 1 << m; i++) for (int j = 0; j <= m; j++) dp[i][j] = INF; dp[0][0] = 0; for (auto p : f) { sort(p.second.begin(), p.second.end()); for (int i = 0; i < 1 << m; i++) can[i] = 0; for (int i = 0; i < 1 << m; i++) { long long KK = 1; for (int j = 0; j < m; ++j) if ((i >> j) & 1) { KK *= p.first[j]; } if (KK <= k) can[i] = 1; } for (int i = 1; i < 1 << m; i <<= 1) for (int j = 0; j < m; j += i << 1) { for (int k = 0; k < i; ++k) can[j + k] += can[j + k + i]; } vector<int> useful; for (int i = 1; i < 1 << m; ++i) if (can[i] == 1) useful.push_back(i); for (auto v : p.second) { bool chg = false; for (int i = (1 << m) - 1; ~i; i--) { for (int j = m - 1; ~j; j--) { for (auto k : useful) { if (dp[i | k][j + 1] > dp[i][j] + v) { dp[i | k][j + 1] = dp[i][j] + v; chg = 1; } } } } if (!chg) break; } } long long ans = INF; for (int i = 0; i <= m; i++) { if (dp[(1 << m) - 1][i] != INF) ans = min(ans, dp[(1 << m) - 1][i] * i); } if (ans == INF) ans = -1; cout << ans << endl; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-11; template <class T> inline void ckmin(T &a, T b) { if (b < a) a = b; } template <class T> inline void ckmax(T &a, T b) { if (b > a) a = b; } template <class T> inline T sqr(T x) { return x * x; } long long gcd(long long a, long long b) { while (b) { long long t = a % b; a = b; b = t; } return a; } int main() { std::ios::sync_with_stdio(false); int n; long long limit; while (cin >> n >> limit) { char s[32]; vector<long long> a(n); vector<int> e(n); for (int i = 0; i < (n); ++i) { cin >> s; a[i] = atoll(s); } for (int i = 0; i < (n); ++i) cin >> e[i]; long long d = 0; for (int i = 0; i < (n); ++i) d = gcd(d, a[i]); vector<int> sorted_by_e(n); for (int i = 0; i < (n); ++i) sorted_by_e[i] = i; sort(sorted_by_e.begin(), sorted_by_e.end(), [&](int a, int b) { return e[a] < e[b]; }); vector<long long> c; for (long long m = 2; d > 1;) { if (d % m == 0) { c.push_back(m); for (; d % m == 0; d /= m) ; } m++; if (m * m > d) m = d; } for (int i = 0; i < (n); ++i) { long long t = a[i]; for (long long x : c) for (; t % x == 0; t /= x) ; a[i] = a[i] / t; } int length = ((int)c.size()); unordered_map<long long, int> visited; vector<pair<int, long long>> h; h.reserve(n); for (int cl = 0; cl < (n); ++cl) { int i = sorted_by_e[cl]; if (visited[a[i]] > length) continue; ++visited[a[i]]; h.emplace_back(e[i], a[i]); } vector<vector<long long>> f(1 << length); for (int set = 0; set < ((1 << length)); ++set) f[set].resize(length + 1, (1LL << 60)); f[0][0] = 0; vector<long long> g0(1 << length); vector<int> g1(1 << length); for (int set = (1); set < ((1 << length)); ++set) for (int i = 0; i < (length); ++i) if (set & (1 << i)) g1[set] = i; vector<int> vcnt(1 << length); for (const auto &w : h) { vector<long long> z(length); for (int i = 0; i < (length); ++i) z[i] = 1; for (int i = 0; i < (length); ++i) { long long t = w.second; for (; t % c[i] == 0; t /= c[i]) z[i] *= c[i]; } g0[0] = 1; for (int set = (1); set < ((1 << length)); ++set) g0[set] = g0[set ^ (1 << g1[set])] * z[g1[set]]; int ok = 0; for (int set = (1); set < ((1 << length)); ++set) if (g0[set] <= limit) if (vcnt[set] < length) ok = 1, ++vcnt[set]; else g0[set] = limit + 1; if (!ok) continue; for (int set = (1 << length) - 1; set > 0; set--) for (int subset = set; subset > 0; subset = (subset - 1) & set) if (g0[subset] <= limit) for (int i = 1; i <= length; i++) { ckmin(f[set][i], f[set ^ subset][i - 1] + w.first); } } long long ret = (1LL << 60); for (int i = 0; i < (length + 1); ++i) { long long t = f[(1 << length) - 1][i]; if (t >= (1LL << 60)) continue; ckmin(ret, i * t); } if (ret >= (1LL << 60)) ret = -1; cout << ret << endl; break; } return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "URDL"; long long ln, lk, lm; void etp(bool f = 0) { puts(f ? "YES" : "NO"); exit(0); } void addmod(int& x, int y, int mod = 998244353) { assert(y >= 0); x += y; if (x >= mod) x -= mod; assert(x >= 0 && x < mod); } void et(int x = -1) { printf("%d\n", x); exit(0); } long long fastPow(long long x, long long y, int mod = 998244353) { long long ans = 1; while (y > 0) { if (y & 1) ans = (x * ans) % mod; x = x * x % mod; y >>= 1; } return ans; } long long gcd1(long long x, long long y) { return y ? gcd1(y, x % y) : x; } long long a[1000035], e[1000035], res; vector<long long> ps; map<long long, vector<long long>> mp; long long dp[1 << 13][12]; void init() { long long x = res; for (long long d = 2; d * d <= x; d++) if (x % d == 0) { ps.push_back(d); while (x % d == 0) x /= d; } if (x > 1) ps.push_back(x); for (int(i) = 1; (i) <= (int)(n); (i)++) { long long x = a[i]; for (long long p : ps) while (x % p == 0) x /= p; long long t = a[i] / x; mp[t].push_back(e[i]); } m = ps.size(); for (int(i) = 0; (i) < (int)(1 << m); (i)++) for (int(j) = 0; (j) < (int)(m + 1); (j)++) dp[i][j] = (1LL << 60); dp[0][0] = 0; int all = (1 << m) - 1; for (auto& p : mp) { sort(p.second.begin(), p.second.end()); int N = p.second.size(); mintt(N, m); vector<long long> vp(1 << m, 0); for (int(i) = 0; (i) < (int)(1 << m); (i)++) { long long t = 1, x = p.first; for (int(j) = 0; (j) < (int)(m); (j)++) if (i & (1 << j)) { while (x % ps[j] == 0) { x /= ps[j]; t *= ps[j]; } } vp[i] = t; } for (int(z) = 0; (z) < (int)(N); (z)++) { long long val = p.second[z]; for (int j = m - 1; ~j; j--) { for (int s = 0; s < (1 << m); s++) if (dp[s][j] < (1LL << 60)) { int fan = all - s; for (int tmp = fan; tmp != 0; tmp = (tmp - 1) & fan) { if (vp[tmp] <= lk) { mintt(dp[s | tmp][j + 1], dp[s][j] + val); } } } } } } long long ans = (1LL << 60); for (int(j) = 0; (j) < (int)(m + 1); (j)++) if (dp[all][j] < (1LL << 60)) mintt(ans, dp[all][j] * j); if (ans == (1LL << 60)) ans = -1; printf("%lld\n", ans); } void fmain(int tid) { scanf("%d%lld", &n, &lk); for (int(i) = 1; (i) <= (int)(n); (i)++) { scanf("%lld", a + i); res = gcd1(res, a[i]); } for (int(i) = 1; (i) <= (int)(n); (i)++) scanf("%lld", e + i); if (res == 1) { puts("0"); return; } init(); } int main() { int t = 1; for (int(i) = 1; (i) <= (int)(t); (i)++) { fmain(i); } return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; int n; long long int k; long long int gcd(long long int a, long long int b) { if (a < b) swap(a, b); while (b) { swap(a, b); b %= a; } return a; } long long int ar[1000002]; vector<long long int> pr; bool cost[1 << 11]; vector<long long int> vv; long long int dp[1 << 11][12]; unordered_map<long long int, vector<long long int> > m2; inline void calc(long long int num, int pos, int sub, long long int cur) { if (pos == pr.size()) { cost[sub] = true; return; } calc(num, pos + 1, sub, cur); while (num % pr[pos] == 0LL && cur <= k / pr[pos]) { num /= pr[pos]; cur *= pr[pos]; } if (num % pr[pos]) { calc(num, pos + 1, sub | (1 << pos), cur); } } int main() { cin >> n >> k; for (int i = 0; i < (1 << 11); i++) { cost[i] = LLONG_MAX; } long long int gc = 0; for (int i = 0; i < n; i++) { long long int a; scanf("%lld", &a); ar[i] = a; gc = gcd(gc, a); } for (int i = 0; i < n; i++) { long long int a; scanf("%lld", &a); vv.push_back(a); } for (long long int i = 2; i * i <= gc; i++) { if (gc % i == 0) { pr.push_back(i); while (gc % i == 0) gc /= i; } } if (gc > 1LL) pr.push_back(gc); int m = pr.size(); sort(pr.begin(), pr.end()); for (int i = 0; i < n; i++) { int mask = 0; long long int U = 1; for (int j = 0; j < pr.size(); j++) { while (ar[i] % pr[j] == 0LL) { U *= pr[j]; ar[i] /= pr[j]; } } m2[U].push_back(vv[i]); } for (int i = 0; i < (1 << m); i++) { for (int j = 0; j <= m; j++) dp[i][j] = LLONG_MAX; } dp[0][0] = 0; for (auto &el : m2) { for (int i = 0; i < (1 << m); i++) cost[i] = false; calc(el.first, 0, 0, 1); for (int i = 0; i < m; i++) { for (int j = 0; j < (1 << m); j++) { if ((j >> i) & 1) { cost[j ^ (1 << i)] |= cost[j]; } } } auto &g = el.second; sort(g.begin(), g.end()); for (int idx = 0; idx < g.size() && idx < m; idx++) { for (int i = (1 << m) - 1; i >= 0; i--) { for (int k = m - 1; k >= 0; k--) { if (dp[i][k] == LLONG_MAX) continue; for (int j = i + 1; j < (1 << m); j++, j |= i) { int cov = (j ^ i); if (cost[cov] == false) continue; dp[j][k + 1] = min(dp[j][k + 1], dp[i][k] + g[idx]); } } } } } long long int ans = LLONG_MAX; for (int i = 0; i <= m; i++) { long long int z = dp[(1 << m) - 1][i]; if (z != LLONG_MAX) { ans = min(ans, z * i); } } if (ans == LLONG_MAX) { ans = -1; } printf("%lld\n", ans); return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.PrintStream; import java.util.Arrays; import java.util.Set; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.StringTokenizer; import java.math.BigInteger; import java.io.BufferedReader; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { final long infinity = Long.MAX_VALUE / 2; long ans; int matchingGen; public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); long k = in.nextLong(); Judge[] judges = new Judge[n]; long g = 0; for (int i = 0; i < n; i++) { judges[i] = new Judge(); judges[i].a = in.nextLong(); g = gcd(g, judges[i].a); } for (int i = 0; i < n; i++) { judges[i].e = in.nextInt(); } Factor[] factors = factorize(g); for (int i = 0; i < n; i++) { long na = 1; // Remove the prime factors that do not occur in the gcd // by leaving only those that do. for (Factor f : factors) { while (judges[i].a % f.p == 0) { judges[i].a /= f.p; na *= f.p; } } judges[i].a = na; } Arrays.sort(judges); // if (true) { if (false) { countEquivalenceClassesWorstCase(); } int m = factors.length; { List<Judge> bestJudges = new ArrayList<>(); for (int i = 0; i < n; ) { int j = i; while (j < n && judges[i].a == judges[j].a) { ++j; } // At most |m| judges in every equivalence class are needed. for (int it = i; it < j && it < i + m; it++) { bestJudges.add(judges[it]); } i = j; } judges = bestJudges.toArray(new Judge[0]); n = judges.length; for (int i = 0; i < n; i++) { judges[i].sortedId = i; } } long[] prod = new long[1 << m]; long[] factorRaisedToPow = new long[m]; List<Judge>[] judgesThatCanCoverMask = new List[1 << m]; for (int mask = 0; mask < 1 << m; mask++) { judgesThatCanCoverMask[mask] = new ArrayList<>(); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { Factor f = factors[j]; long x = judges[i].a; long y = 1; while (x % f.p == 0) { x /= f.p; y *= f.p; } factorRaisedToPow[j] = y; } prod[0] = 1; for (int mask = 1; mask < 1 << m; mask++) { int j = Integer.numberOfTrailingZeros(mask); prod[mask] = prod[mask ^ (1 << j)] * factorRaisedToPow[j]; if (prod[mask] <= k) { judgesThatCanCoverMask[mask].add(judges[i]); } } } for (int mask = 0; mask < 1 << m; mask++) { Collections.sort(judgesThatCanCoverMask[mask], (u, v) -> Integer.compare(u.e, v.e)); } ans = infinity; recPartitions((1 << m) - 1, new ArrayList<>(), judgesThatCanCoverMask); if (ans >= infinity) { ans = -1; } out.println(ans); } private void recPartitions(int mask, List<Integer> masks, List<Judge>[] judgesThatCanCoverMask) { if (mask == 0) { solvePartition(masks, judgesThatCanCoverMask); return; } for (int submask = mask; submask > 0; submask = (submask - 1) & mask) { if (masks.isEmpty() || masks.get(masks.size() - 1) > mask) { masks.add(submask); recPartitions(mask ^ submask, masks, judgesThatCanCoverMask); masks.remove(masks.size() - 1); } } } private void solvePartition(List<Integer> masks, List<Judge>[] judgesThatCanCoverMask) { Set<Integer> relevantJudgeIds = new HashSet<>(); List<Judge> relevantJudges = new ArrayList<>(); List<Judge> edgeA = new ArrayList<>(); List<Integer> edgeB = new ArrayList<>(); for (int i = 0; i < masks.size(); i++) { int judgeId = 0; for (Judge j : judgesThatCanCoverMask[masks.get(i)]) { if (!relevantJudgeIds.contains(j.sortedId)) { relevantJudgeIds.add(j.sortedId); relevantJudges.add(j); } edgeA.add(j); edgeB.add(i); // By the exchange argument, only masks.size() best judges // that can cover this mask are needed. ++judgeId; if (judgeId >= masks.size()) { break; } } } Collections.sort(relevantJudges, (u, v) -> Integer.compare(u.e, v.e)); for (int i = 0; i < relevantJudges.size(); i++) { relevantJudges.get(i).matchingId = i; } // Judges on the left, masks on the right. int numLeft = relevantJudges.size(); int numRight = masks.size(); int[] deg = new int[numLeft]; for (int i = 0; i < edgeA.size(); i++) { int a = edgeA.get(i).matchingId; ++deg[a]; } int[][] edgesFromLeft = new int[numLeft][]; for (int i = 0; i < numLeft; i++) { edgesFromLeft[i] = new int[deg[i]]; deg[i] = 0; } for (int i = 0; i < edgeA.size(); i++) { int a = edgeA.get(i).matchingId; int b = edgeB.get(i); edgesFromLeft[a][deg[a]++] = b; } int[] pairToRight = new int[numRight]; Arrays.fill(pairToRight, -1); long cur = 0; int matchingSize = 0; int[] gen = new int[numLeft]; matchingGen = 1; for (int i = 0; i < relevantJudges.size(); i++) { if (matchingDfs(i, gen, edgesFromLeft, pairToRight)) { cur += relevantJudges.get(i).e; ++matchingSize; ++matchingGen; } if (matchingSize == masks.size()) { break; } } if (matchingSize == masks.size()) { ans = Math.min(ans, cur * masks.size()); } } private boolean matchingDfs(int i, int[] gen, int[][] edgesFromLeft, int[] pairToRight) { if (gen[i] == matchingGen) { return false; } gen[i] = matchingGen; for (int j : edgesFromLeft[i]) { if (pairToRight[j] < 0 || matchingDfs(pairToRight[j], gen, edgesFromLeft, pairToRight)) { pairToRight[j] = i; return true; } } return false; } private void countEquivalenceClassesWorstCase() { final long LIMIT = (long) 1e12; final int N = 100; boolean[] isPrime = new boolean[N]; Arrays.fill(isPrime, true); int[] primes = new int[N]; int numPrimes = 0; BigInteger prod = BigInteger.ONE; for (int i = 2; i < N; i++) { if (!isPrime[i]) { continue; } primes[numPrimes++] = i; prod = prod.multiply(BigInteger.valueOf(i)); if (prod.compareTo(BigInteger.valueOf(LIMIT)) > 0) { break; } for (int j = i + i; j < N; j += i) { isPrime[j] = false; } } System.out.printf("Product of the first %d primes exceeds the limit of %d\n", numPrimes, LIMIT); primes = Arrays.copyOf(primes, numPrimes - 1); System.out.printf("Number of equivalence classes when using only the first several primes:\n"); System.out.printf("num primes: num classes\n"); for (int i = 1; i <= primes.length; i++) { System.out.printf("%d: %d\n", i, rec(0, 1, LIMIT, Arrays.copyOf(primes, i))); } } private long rec(int pos, long prod, long limit, int[] primes) { if (pos == primes.length) { return prod <= limit ? 1 : 0; } long res = 0; while (prod <= limit / primes[pos]) { prod *= primes[pos]; // Every prime must occur at least once. res += rec(pos + 1, prod, limit, primes); } return res; } private long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } private Factor[] factorize(long n) { List<Factor> res = new ArrayList<>(); for (long p = 2; p * p <= n; p++) { if (n % p == 0) { Factor f = new Factor(); f.p = p; while (n % p == 0) { n /= p; ++f.s; } res.add(f); } } if (n > 1) { Factor f = new Factor(); f.p = n; f.s = 1; res.add(f); } return res.toArray(new Factor[0]); } class Judge implements Comparable<Judge> { long a; int e; int sortedId; int matchingId; public int compareTo(Judge o) { if (a != o.a) { return a < o.a ? -1 : 1; } if (e != o.e) { return e < o.e ? -1 : 1; } return 0; } } class Factor { long p; int s; } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
JAVA
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; long long a[1000005]; int n, e[1000005]; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } vector<long long> ps; map<long long, vector<pair<int, int> > > mp; long long m, res = (long long)1e18; vector<pair<int, int> > bst[5005]; int T; namespace flows { int match[15]; bool vis[15]; vector<int> nei[1000005]; bool go(int v) { int i; for (i = 0; i < (int)nei[v].size(); ++i) { int u = nei[v][i]; if (match[u] == -1) { match[u] = v; return 1; } if (vis[u]) continue; vis[u] = 1; if (go(match[u])) { match[u] = v; return 1; } } return 0; } } // namespace flows int msks[15]; void go(int done, int cnt) { if (done == (1 << T) - 1) { int i, j, k; vector<pair<int, int> > all; using namespace flows; memset(match, -1, sizeof(match)); for (i = 0; i < cnt; ++i) { for (j = 0; j < (int)bst[msks[i]].size(); ++j) { all.push_back(bst[msks[i]][j]); nei[bst[msks[i]][j].second].push_back(i); } } sort(all.begin(), all.end()); all.erase(unique(all.begin(), all.end()), all.end()); long long sum = 0ll; int rc = 0; for (i = 0; i < (int)all.size(); ++i) { memset(vis, 0, sizeof(vis)); if (go(all[i].second)) { ++rc; sum += (long long)all[i].first; } } if (rc == cnt && (long long)cnt * sum < res) res = (long long)cnt * sum; for (i = 0; i < (int)all.size(); ++i) { nei[all[i].second].clear(); } return; } int all = (((1 << T) - 1) ^ done), low, i; for (low = 0; low < T; ++low) { if (all & (1 << low)) break; } all ^= (1 << low); msks[cnt] = (1 << low); go(done ^ (1 << low), cnt + 1); int ini = all; while (all) { msks[cnt] = (all | (1 << low)); go(done ^ (all | (1 << low)), cnt + 1); all = ((all - 1) & ini); } } vector<pair<int, int> > mrg(vector<pair<int, int> > &A, vector<pair<int, int> > &B) { int sA = (int)A.size(), sB = (int)B.size(), S = min(T, sA + sB); vector<pair<int, int> > ret(S); int pA = 0, pB = 0, i; for (i = 0; i < S; ++i) { if (pA == sA) ret[i] = B[pB++]; else if (pB == sB || A[pA] < B[pB]) ret[i] = A[pA++]; else ret[i] = B[pB++]; } return ret; } int main() { int i, j, k; scanf("%d%I64d", &n, &m); for (i = 0; i < n; ++i) { scanf("%I64d", a + i); } for (i = 0; i < n; ++i) { scanf("%d", e + i); } long long g = a[0]; for (i = 1; i < n; ++i) g = gcd(g, a[i]); for (i = 2; (long long)i * (long long)i <= g; ++i) { if (!(g % (long long)i)) { ps.push_back((long long)i); while (!(g % (long long)i)) g /= (long long)i; } } if (g > 1ll) ps.push_back(g); T = (int)ps.size(); if (!T) { printf("0\n"); return 0; } for (i = 0; i < n; ++i) { long long cur = 1ll, tmp = a[i]; for (j = 0; j < T; ++j) { while (!(tmp % ps[j])) { tmp /= ps[j]; cur *= ps[j]; } } mp[cur].push_back(make_pair(e[i], i)); } map<long long, vector<pair<int, int> > >::iterator it; for (it = mp.begin(); it != mp.end(); ++it) { vector<pair<int, int> > S = it->second; sort(S.begin(), S.end()); if ((int)S.size() > T) S.resize(T); long long fs = it->first; vector<long long> prs(0); prs.resize(T, 1ll); for (j = 0; j < T; ++j) { while (!(fs % ps[j])) { fs /= ps[j]; prs[j] *= ps[j]; } } for (j = 0; j < (1 << T); ++j) { long long req = 1ll; for (k = 0; k < T; ++k) { if (j & (1 << k)) req *= prs[k]; } if (req <= m) { bst[j] = mrg(bst[j], S); } } } go(0, 0); if (res >= (long long)1e17) printf("-1\n"); else printf("%I64d\n", res); return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int N1 = (int)(1e6 + 0.5); const long long N2 = (long long)(1e12 + 0.5); struct emt { long long a, cost; }; bool cmp(emt u, emt v) { return u.cost < v.cost; } emt e[N1 + 5]; unordered_map<long long, int> CNT; long long gcd(long long x, long long y) { return !x ? y : gcd(y % x, x); } vector<long long> decomdd; int prime_decomp_dd(long long x) { for (int y = 2; y <= N1; y++) { if (x % y) continue; decomdd.push_back(y); while (x % y == 0) x /= y; } if (x > 1) decomdd.push_back(x); return decomdd.size(); } long long prime_decomp_a(long long x) { long long t = 1; for (long long y : decomdd) while (x % y == 0) x /= y, t *= y; return t; } long long dp[12][1 << 11], dp1[12][1 << 11]; void work(int S, long long cost, int LEN) { for (int i = 1; i <= LEN; i++) { for (int j = 0; j < (1 << LEN); j++) dp[i][j | S] = min(dp[i][j | S], dp1[i - 1][j] + cost); } } int used[1 << 11]; int main() { int n; long long K, dd = 0; cin >> n >> K; for (int i = 1; i <= n; i++) { scanf("%lld", &e[i].a); dd = gcd(dd, e[i].a); } if (dd == 1) { puts("0"); return 0; } int LEN = prime_decomp_dd(dd); for (int i = 1; i <= n; i++) { scanf("%lld", &e[i].cost); e[i].a = prime_decomp_a(e[i].a); } sort(e + 1, e + n + 1, cmp); memset(dp, 1, sizeof(dp)); dp[0][0] = 0; for (int i = 1; i <= n; i++) { if (++CNT[e[i].a] > LEN) continue; long long mul[11], prod[1 << 11]; prod[0] = 1; for (int j = 0; j < LEN; j++) { mul[j] = 1; while (e[i].a % decomdd[j] == 0) e[i].a /= decomdd[j], mul[j] *= decomdd[j]; } memcpy(dp1, dp, sizeof(dp1)); for (int j = 0; j < LEN; j++) { for (int k = (1 << j); k < (1 << (j + 1)); k++) { prod[k] = prod[k ^ (1 << j)] * mul[j]; if (prod[k] <= K && used[k]++ < LEN) work(k, e[i].cost, LEN); } } } long long ans = 1e13; for (int i = 1; i <= LEN; i++) ans = min(ans, dp[i][(1 << LEN) - 1] * i); if (ans >= 1e13) ans = -1; cout << ans << endl; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int maxn = 1000101; int n, pt; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long e[maxn], a[maxn], p[100], d, k, cur[1 << 13 | 10], ans[13][1 << 13 | 10]; map<long long, vector<long long> > mp; int main() { scanf("%d%lld", &n, &k); for (int i = (1); i <= (n); ++i) scanf("%lld", &a[i]), d = gcd(d, a[i]); for (int i = (1); i <= (n); ++i) scanf("%lld", &e[i]); long long dd = d; for (long long i = 2; i * i <= dd; i++) { if (dd % i == 0) { p[++pt] = i; while (dd % i == 0) dd /= i; } } if (dd != 1) p[++pt] = dd; for (int i = (1); i <= (n); ++i) { long long ai = a[i], t = 1; for (int j = (1); j <= (pt); ++j) { while (ai % p[j] == 0) t *= p[j], ai /= p[j]; } mp[t].push_back(e[i]); } memset(ans, 0x3f, sizeof ans); ans[0][(1 << pt) - 1] = 0; long long Ans = 0x3f3f3f3f3f3f3f3f, inf = Ans; for (map<long long, vector<long long> >::iterator it = mp.begin(); it != mp.end(); ++it) { sort(it->second.begin(), it->second.end()); int len = min((int)it->second.size(), pt); vector<long long> th = it->second; th.resize(len); long long x = it->first; for (int i = (0); i <= ((1 << pt) - 1); ++i) { long long t = x, q = 1; for (int j = (1); j <= (pt); ++j) { if (i & (1 << j - 1)) while (t % p[j] == 0) t /= p[j], q *= p[j]; } cur[i] = q; } for (int t = (0); t <= (len - 1); ++t) { int bj = 1; for (int st = pt; st; st--) for (int i = (0); i <= ((1 << pt) - 1); ++i) { if (ans[st - 1][i] < inf) for (int j = i; j; j = (j - 1) & i) { if (cur[j] <= k) { if (ans[st][i - j] > ans[st - 1][i] + th[t]) ans[st][i - j] = ans[st - 1][i] + th[t], bj = 1; } } } if (!bj) break; } } for (int i = (0); i <= (pt); ++i) if (ans[i][0] < inf) Ans = min(Ans, i * ans[i][0]); if (Ans < inf) cout << Ans; else cout << -1; return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; void Freopen() { freopen( "title" ".in", "r", stdin); freopen( "title" ".out", "w", stdout); } long long read() { long long g = 0, f = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { g = g * 10 + ch - '0'; ch = getchar(); } return g * f; } const long long N = 1e6 + 5; const long long S = 13; long long n, m, a[N], val[N], p[S], cnt, g, vis[1 << S], f[S][1 << S]; map<long long, vector<long long> > M; long long gcd(long long x, long long y) { if (!y) return x; return gcd(y, x % y); } signed main() { n = read(), m = read(); for (long long i = (1); i <= (n); i++) a[i] = read(); for (long long i = (1); i <= (n); i++) val[i] = read(); for (long long i = (1); i <= (n); i++) g = gcd(g, a[i]); for (long long i = 2; i * i <= g; i++) if (!(g % i)) { p[cnt++] = i; while (!(g % i)) g /= i; } if (g > 1) p[cnt++] = g; for (long long i = (1); i <= (n); i++) { long long re = 1; for (long long j = (0); j <= (cnt - 1); j++) { while (!(a[i] % p[j])) a[i] /= p[j], re *= p[j]; } M[re].push_back(val[i]); } memset(f, 0x3f, sizeof(f)); long long INF = f[0][0]; f[0][0] = 0; for (auto t : M) { long long x = t.first; sort(t.second.begin(), t.second.end()); if (t.second.size() > cnt) t.second.resize(cnt); for (long long sta = (0); sta <= ((1 << cnt) - 1); sta++) { long long y = x, z = 1; for (long long i = (0); i <= (cnt - 1); i++) if (sta >> i & 1) { while (!(y % p[i])) y /= p[i], z *= p[i]; } vis[sta] = (z <= m); } for (auto w : t.second) { bool flag = 0; for (long long i = (cnt - 1); i >= (0); i--) for (long long sta = (0); sta <= ((1 << cnt) - 1); sta++) if (f[i][sta] ^ INF) { for (long long s = (sta + 1) | sta; s <= (1 << cnt) - 1; s = (s + 1) | sta) if (vis[s ^ sta]) if (f[i + 1][s] > f[i][sta] + w) { f[i + 1][s] = f[i][sta] + w; flag = 1; } } if (!flag) break; } } long long ans = INF; for (long long i = (0); i <= (cnt); i++) if (f[i][(1 << cnt) - 1] ^ INF) ans = min(ans, f[i][(1 << cnt) - 1] * i); if (ans == INF) puts("-1"); else cout << ans; return signed(); }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, "/STACK:336777216") using namespace std; int IT_MAX = 1 << 20; const long long MOD = 1000000007; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3f; const double PI = acos(-1); const double ERR = 1e-10; long long gcd(long long a, long long b) { return (a == 0) ? b : gcd(b % a, a); } pair<long long, long long> in[1000050]; vector<long long> Vl; map<long long, vector<long long>> Mx; bool tchk[2048]; long long dp[12][2048]; int main() { long long N, K, i, j; scanf("%lld %lld", &N, &K); for (i = 1; i <= N; i++) scanf("%lld", &in[i].second); for (i = 1; i <= N; i++) scanf("%lld", &in[i].first); sort(in + 1, in + N + 1); long long g = 0; for (i = 1; i <= N; i++) g = gcd(g, in[i].second); if (g == 1) return !printf("0\n"); for (i = 2; i * i <= g; i++) { if (g % i) continue; Vl.push_back(i); while (g % i == 0) g /= i; } if (g != 1) Vl.push_back(g); for (i = 1; i <= N; i++) { long long v = 1; for (auto it : Vl) { while (in[i].second % it == 0) { v *= it; in[i].second /= it; } } if (Mx[v].size() >= 11) continue; Mx[v].push_back(in[i].first); } memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; int X = Vl.size(); vector<pair<long long, long long>> Vin; vector<int> Vp; for (auto it : Mx) { long long v = it.first; long long u[13] = { 1, }; for (i = 0; i < X; i++) { u[i] = 1; while (v % Vl[i] == 0) { u[i] *= Vl[i]; v /= Vl[i]; } } Vp.clear(); memset(tchk, false, sizeof(tchk)); for (i = (1 << X) - 1; i >= 1; i--) { if (tchk[i]) continue; long long tu = 1; for (j = 0; j < X; j++) if (i & (1 << j)) tu *= u[j]; if (tu > K) continue; for (j = i; j; j = (j - 1) & i) tchk[j] = true; Vp.push_back(i); } for (auto val : it.second) { for (i = 10; i >= 0; i--) { for (j = 0; j < (1 << X); j++) { if (dp[i][j] == LL_INF) continue; for (auto it2 : Vp) dp[i + 1][j | it2] = min(dp[i + 1][j | it2], dp[i][j] + val); } } } } long long ans = LL_INF; for (i = 1; i <= 11; i++) { if (dp[i][(1 << X) - 1] > LL_INF / 2) continue; ans = min(ans, dp[i][(1 << X) - 1] * i); } if (ans == LL_INF) return !printf("-1\n"); return !printf("%lld\n", ans); }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long INFF = 0x3f3f3f3f3f3f3f3fll; const long long M = 1e9 + 7; const long long maxn = 1e6 + 107; const double pi = acos(-1.0); const double eps = 0.0000000001; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } template <typename T> inline void pr2(T x, int k = 64) { long long i; for (i = 0; i < k; i++) fprintf(stderr, "%d", (x >> i) & 1); putchar(' '); } template <typename T> inline void add_(T &A, int B, long long MOD = M) { A += B; (A >= MOD) && (A -= MOD); } template <typename T> inline void mul_(T &A, long long B, long long MOD = M) { A = (A * B) % MOD; } template <typename T> inline void mod_(T &A, long long MOD = M) { A %= MOD; A += MOD; A %= MOD; } template <typename T> inline void max_(T &A, T B) { (A < B) && (A = B); } template <typename T> inline void min_(T &A, T B) { (A > B) && (A = B); } template <typename T> inline T abs(T a) { return a > 0 ? a : -a; } template <typename T> inline T powMM(T a, T b) { T ret = 1; for (; b; b >>= 1ll, a = (long long)a * a % M) if (b & 1) ret = (long long)ret * a % M; return ret; } int startTime; void startTimer() { startTime = clock(); } void printTimer() { fprintf(stderr, "/--- Time: %ld milliseconds ---/\n", clock() - startTime); } long long A[maxn]; int e[maxn]; int bit[1 << 12 | 7]; vector<long long> p; priority_queue<pair<int, int> > id[1 << 12 | 7]; long long pre[12][1 << 12 | 7], now[12][1 << 12 | 7]; map<long long, vector<int> > MP; vector<int> trans[maxn]; int main() { int i; int n; long long k; scanf("%d%lld", &n, &k); for (i = 1; i <= n; i++) scanf("%lld", &A[i]); for (i = 1; i <= n; i++) scanf("%d", &e[i]); long long g = A[1], tmp; for (i = 2; i <= n; i++) g = gcd(g, A[i]); tmp = g; for (long long i = 2; i <= g / i; i++) if (tmp % i == 0) { while (tmp % i == 0) tmp /= i; p.push_back(i); } if (tmp != 1) p.push_back(tmp); int N = 1 << p.size(), C = p.size(); for (i = 1; i < N; i++) bit[i] = bit[i >> 1] + (i & 1); for (i = 1; i <= n; i++) { long long base = 1; int j; for (j = 0; j < C; j++) if (A[i] % p[j] == 0) while (A[i] % p[j] == 0) A[i] /= p[j], base *= p[j]; MP[base].push_back(i); } for (auto now : MP) { sort(now.second.begin(), now.second.end(), [](int i, int j) { return e[i] < e[j]; }); if ((int)now.second.size() > C) now.second.resize(C); int sta, j; for (sta = 0; sta < N; sta++) { long long _k = 1, x = now.first; for (j = 0; j < C; j++) if ((sta >> j) & 1) while (x % p[j] == 0) x /= p[j], _k *= p[j]; if (_k <= k) { for (int v : now.second) { id[sta].push(make_pair(e[v], v)); if ((int)id[sta].size() > C) id[sta].pop(); } } } } int sta; for (sta = 1; sta < N; sta++) { while (id[sta].size()) { trans[id[sta].top().second].push_back(sta); id[sta].pop(); } } memset(now, 0x3f, sizeof(now)); now[0][0] = 0; for (i = 1; i <= n; i++) if (trans[i].size()) { for (k = 0; k < C + 1; k++) memcpy(pre[k], now[k], N * sizeof(long long)); for (int v : trans[i]) { int all = (N - 1) ^ v; for (int x = all;; x = (x - 1) & all) { for (k = 0; k < C + 1; k++) min_(now[k + 1][x | v], pre[k][x] + e[i]); if (!x) break; } } } long long ans = INFF; for (k = 0; k < C + 1; k++) if (now[k][N - 1] != INFF) min_(ans, now[k][N - 1] * k); if (ans == INFF) puts("-1"); else printf("%lld\n", ans); }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { long long int t; while (a > 0) t = b % a, b = a, a = t; return b; } long long int a[1000000]; int e[1000000]; vector<long long int> factors; map<long long int, vector<int> > M; long long int num[12]; long long int dp[12][1 << 11], dp2[12][1 << 11]; int main() { int i; int n; long long int K, g = 0; scanf("%d %I64d", &n, &K); for (i = 0; i < n; i++) scanf("%I64d", &a[i]), g = gcd(g, a[i]); for (i = 0; i < n; i++) scanf("%d", &e[i]); int j; for (i = 2; (long long int)i * i <= g; i++) { if ((g % i) == 0) { long long int x = 1; while ((g % i) == 0) x *= i, g /= i; factors.push_back(x); } } if (g > 1) factors.push_back(g); if (factors.empty()) { printf("0\n"); return 0; } int k, l; long long int ans = 1e18; for (i = 0; i < n; i++) { long long int x = a[i], y = 1; for (j = 0; j < factors.size(); j++) { while ((x % factors[j]) == 0) x /= factors[j], y *= factors[j]; } if (!M.count(y)) M[y] = vector<int>(1, e[i]); else M[y].push_back(e[i]); } for (i = 0; i <= factors.size(); i++) { for (j = 0; j < (1 << factors.size()); j++) dp[i][j] = 1e18; } dp[0][0] = 0; for (auto it = M.begin(); it != M.end(); it++) { long long int x = it->first; for (i = 0; i < factors.size(); i++) { num[i] = 1; while ((x % factors[i]) == 0) x /= factors[i], num[i] *= factors[i]; } vector<int> good; for (i = 0; i < (1 << factors.size()); i++) { long long int x = 1; for (j = 0; j < factors.size(); j++) { if (i & (1 << j)) x *= num[j]; } if (x <= K) { for (j = 0; j < factors.size(); j++) { if (!(i & (1 << j)) && (x * num[j] <= K)) break; } if (j == factors.size()) good.push_back(i); } } vector<int>& v = it->second; sort(v.begin(), v.end()); v.resize(min(v.size(), factors.size())); for (i = 0; i < v.size(); i++) { for (j = factors.size() - 1; j >= 0; j--) { for (k = 0; k < (1 << factors.size()); k++) { for (l = 0; l < good.size(); l++) dp[j + 1][k | good[l]] = min(dp[j + 1][k | good[l]], dp[j][k] + v[i]); } } } for (i = 1; i <= factors.size(); i++) ans = min(ans, dp[i][(1 << factors.size()) - 1] * i); } if (ans == 1e18) printf("-1\n"); else printf("%lld\n", ans); return 0; }
CPP
1103_D. Professional layer
Cardbluff is popular sport game in Telegram. Each Cardbluff player has ever dreamed about entrance in the professional layer. There are n judges now in the layer and you are trying to pass the entrance exam. You have a number k β€” your skill in Cardbluff. Each judge has a number a_i β€” an indicator of uncertainty about your entrance to the professional layer and a number e_i β€” an experience playing Cardbluff. To pass the exam you need to convince all judges by playing with them. You can play only one game with each judge. As a result of a particular game, you can divide the uncertainty of i-th judge by any natural divisor of a_i which is at most k. If GCD of all indicators is equal to 1, you will enter to the professional layer and become a judge. Also, you want to minimize the total amount of spent time. So, if you play with x judges with total experience y you will spend x β‹… y seconds. Print minimal time to enter to the professional layer or -1 if it's impossible. Input There are two numbers in the first line n and k (1 ≀ n ≀ 10^6, 1 ≀ k ≀ 10^{12}) β€” the number of judges and your skill in Cardbluff. The second line contains n integers, where i-th number a_i (1 ≀ a_i ≀ 10^{12}) β€” the uncertainty of i-th judge The third line contains n integers in the same format (1 ≀ e_i ≀ 10^9), e_i β€” the experience of i-th judge. Output Print the single integer β€” minimal number of seconds to pass exam, or -1 if it's impossible Examples Input 3 6 30 30 30 100 4 5 Output 18 Input 1 1000000 1 100 Output 0 Input 3 5 7 7 7 1 1 1 Output -1
2
10
import java.io.OutputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.PrintStream; import java.util.Arrays; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import java.math.BigInteger; import java.io.BufferedReader; import java.util.Collections; import java.io.InputStream; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; FastScanner in = new FastScanner(inputStream); PrintWriter out = new PrintWriter(outputStream); TaskD solver = new TaskD(); solver.solve(1, in, out); out.close(); } static class TaskD { public void solve(int testNumber, FastScanner in, PrintWriter out) { int n = in.nextInt(); long k = in.nextLong(); Judge[] judges = new Judge[n]; long g = 0; for (int i = 0; i < n; i++) { judges[i] = new Judge(); judges[i].a = in.nextLong(); g = gcd(g, judges[i].a); } for (int i = 0; i < n; i++) { judges[i].e = in.nextInt(); } Factor[] factors = factorize(g); for (int i = 0; i < n; i++) { long na = 1; // Remove the prime factors that do not occur in the gcd // by leaving only those that do. for (Factor f : factors) { while (judges[i].a % f.p == 0) { judges[i].a /= f.p; na *= f.p; } } judges[i].a = na; } Arrays.sort(judges); // if (true) { if (false) { countEquivalenceClassesWorstCase(); } int m = factors.length; { List<Judge> bestJudges = new ArrayList<>(); for (int i = 0; i < n; ) { int j = i; while (j < n && judges[i].a == judges[j].a) { ++j; } // At most |m| judges in every equivalence class are needed. for (int it = i; it < j && it < i + m; it++) { bestJudges.add(judges[it]); } i = j; } judges = bestJudges.toArray(new Judge[0]); n = judges.length; for (int i = 0; i < n; i++) { judges[i].sortedId = i; } } long[] prod = new long[1 << m]; long[] factorRaisedToPow = new long[m]; List<Judge>[] judgesThatCanCoverMask = new List[1 << m]; for (int mask = 0; mask < 1 << m; mask++) { judgesThatCanCoverMask[mask] = new ArrayList<>(); } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { Factor f = factors[j]; long x = judges[i].a; long y = 1; while (x % f.p == 0) { x /= f.p; y *= f.p; } factorRaisedToPow[j] = y; } prod[0] = 1; for (int mask = 1; mask < 1 << m; mask++) { int j = Integer.numberOfTrailingZeros(mask); prod[mask] = prod[mask ^ (1 << j)] * factorRaisedToPow[j]; if (prod[mask] <= k) { judgesThatCanCoverMask[mask].add(judges[i]); } } } List<Integer>[] masksCoveredByJudge = new List[n]; for (int mask = 0; mask < 1 << m; mask++) { List<Judge> js = judgesThatCanCoverMask[mask]; Collections.sort(js, (u, v) -> Integer.compare(u.e, v.e)); // By the exchange argument, only |m| best judges // that can cover this mask are needed. for (int it = 0; it < m && it < js.size(); it++) { int id = js.get(it).sortedId; if (masksCoveredByJudge[id] == null) { masksCoveredByJudge[id] = new ArrayList<>(); } masksCoveredByJudge[id].add(mask); } } final long infinity = Long.MAX_VALUE / 2; long[][] d = new long[m + 1][1 << m]; for (long[] arr : d) { Arrays.fill(arr, infinity); } d[0][0] = 0; for (int judgeId = 0; judgeId < n; judgeId++) { if (masksCoveredByJudge[judgeId] == null) { continue; } for (int used = m - 1; used >= 0; used--) { // At most m*(1<<m) masks are in total in all the |masksCoveredByJudge| lists combined. // Every mask occurs at most m times (so we cannot have, say, the worst mask occur too often). // Therefore, the overall complexity of these nested 4 for loops is O(m^3 * 3^m). for (int mask : masksCoveredByJudge[judgeId]) { int other = ((1 << m) - 1) ^ mask; for (int covered = other; ; covered = (covered - 1) & other) { if (d[used + 1][covered | mask] > d[used][covered] + judges[judgeId].e) { d[used + 1][covered | mask] = d[used][covered] + judges[judgeId].e; } if (covered == 0) { break; } } } } } long ans = infinity; for (int x = 0; x <= m; x++) { long y = d[x][(1 << m) - 1]; if (y < infinity) { ans = Math.min(ans, x * y); } } if (ans >= infinity) { ans = -1; } out.println(ans); } private void countEquivalenceClassesWorstCase() { final long LIMIT = (long) 1e12; final int N = 100; boolean[] isPrime = new boolean[N]; Arrays.fill(isPrime, true); int[] primes = new int[N]; int numPrimes = 0; BigInteger prod = BigInteger.ONE; for (int i = 2; i < N; i++) { if (!isPrime[i]) { continue; } primes[numPrimes++] = i; prod = prod.multiply(BigInteger.valueOf(i)); if (prod.compareTo(BigInteger.valueOf(LIMIT)) > 0) { break; } for (int j = i + i; j < N; j += i) { isPrime[j] = false; } } System.out.printf("Product of the first %d primes exceeds the limit of %d\n", numPrimes, LIMIT); primes = Arrays.copyOf(primes, numPrimes - 1); System.out.printf("Number of equivalence classes when using only the first several primes:\n"); System.out.printf("num primes: num classes\n"); for (int i = 1; i <= primes.length; i++) { System.out.printf("%d: %d\n", i, rec(0, 1, LIMIT, Arrays.copyOf(primes, i))); } } private long rec(int pos, long prod, long limit, int[] primes) { if (pos == primes.length) { return prod <= limit ? 1 : 0; } long res = 0; while (prod <= limit / primes[pos]) { prod *= primes[pos]; // Every prime must occur at least once. res += rec(pos + 1, prod, limit, primes); } return res; } private long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } private Factor[] factorize(long n) { List<Factor> res = new ArrayList<>(); for (long p = 2; p * p <= n; p++) { if (n % p == 0) { Factor f = new Factor(); f.p = p; while (n % p == 0) { n /= p; ++f.s; } res.add(f); } } if (n > 1) { Factor f = new Factor(); f.p = n; f.s = 1; res.add(f); } return res.toArray(new Factor[0]); } class Judge implements Comparable<Judge> { long a; int e; int sortedId; public int compareTo(Judge o) { if (a != o.a) { return a < o.a ? -1 : 1; } if (e != o.e) { return e < o.e ? -1 : 1; } return 0; } } class Factor { long p; int s; } } static class FastScanner { private BufferedReader in; private StringTokenizer st; public FastScanner(InputStream stream) { in = new BufferedReader(new InputStreamReader(stream)); } public String next() { while (st == null || !st.hasMoreTokens()) { try { st = new StringTokenizer(in.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return st.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } public long nextLong() { return Long.parseLong(next()); } } }
JAVA