Educational Codeforces Round 22

来源:互联网 发布:单机斗牛牛无网络破解 编辑:程序博客网 时间:2024/06/06 02:17
A. The Contest
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Pasha is participating in a contest on one well-known website. This time he wants to win the contest and will do anything to get to the first place!

This contest consists of n problems, and Pasha solves ith problem in ai time units (his solutions are always correct). At any moment of time he can be thinking about a solution to only one of the problems (that is, he cannot be solving two problems at the same time). The time Pasha spends to send his solutions is negligible. Pasha can send any number of solutions at the same moment.

Unfortunately, there are too many participants, and the website is not always working. Pasha received the information that the website will be working only during m time periods, jth period is represented by its starting moment lj and ending moment rj. Of course, Pasha can send his solution only when the website is working. In other words, Pasha can send his solution at some moment T iff there exists a period x such that lx ≤ T ≤ rx.

Pasha wants to know his best possible result. We need to tell him the minimal moment of time by which he is able to have solutions to all problems submitted, if he acts optimally, or say that it's impossible no matter how Pasha solves the problems.

Input

The first line contains one integer n (1 ≤ n ≤ 1000) — the number of problems. The second line contains n integers ai (1 ≤ ai ≤ 105)— the time Pasha needs to solve ith problem.

The third line contains one integer m (0 ≤ m ≤ 1000) — the number of periods of time when the website is working. Next m lines represent these periods. jth line contains two numbers lj and rj (1 ≤ lj < rj ≤ 105) — the starting and the ending moment of jth period.

It is guaranteed that the periods are not intersecting and are given in chronological order, so for every j > 1 the condition lj > rj - 1 is met.

Output

If Pasha can solve and submit all the problems before the end of the contest, print the minimal moment of time by which he can have all the solutions submitted.

Otherwise print "-1" (without brackets).

Examples
input
23 421 47 9
output
7
input
1511 4
output
-1
input
1511 5
output
5
Note

In the first example Pasha can act like this: he solves the second problem in 4 units of time and sends it immediately. Then he spends 3 time units to solve the first problem and sends it 7 time units after the contest starts, because at this moment the website starts working again.

In the second example Pasha invents the solution only after the website stops working for the last time.

In the third example Pasha sends the solution exactly at the end of the first period.

题意:只有m段时间内可以提交代码,问最少需要多少时间可以AK(一次可以提交多份代码)。

解:求总共需要花多少时间解题,然后求m段时间内最接近的,特判sum小于开始比赛的时间

import java.util.Scanner;/** * 作者:张宇翔 创建日期:2017年6月5日 上午13:18:24 描述:ACM */public class Main {private static int Max = (int) (1e3 + 10);private static int n, m;private static int[] a;private static int[] l;private static int[] r;private static int last, sum;public static void main(String[] args) {InitData();GetAns();}private static void InitData() {Scanner cin = new Scanner(System.in);a = new int[Max];l = new int[Max];r = new int[Max];n = cin.nextInt();sum = 0;for (int i = 0; i < n; i++) {a[i] = cin.nextInt();sum += a[i];}m = cin.nextInt();for (int i = 0; i < m; i++) {l[i] = cin.nextInt();r[i] = cin.nextInt();last = r[i];}}private static void GetAns() {if (sum > last) {System.out.println("-1");} else {if(sum<=l[0]){System.out.println(l[0]);return;}int ans = 0;boolean ok = false;for (int i = 0; i < m; i++) {if (l[i] <= sum && r[i] >= sum) {ans = sum;ok = true;break;}}if (!ok) {for (int i = 0; i < m - 1; i++) {if (r[i] < sum && l[i + 1] > sum) {ans = l[i + 1];break;}}}System.out.println(ans);}}}

B. The Golden Age
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Unlucky year in Berland is such a year that its number n can be represented as n = xa + yb, where a and b are non-negative integer numbers.

For example, if x = 2 and y = 3 then the years 4 and 17 are unlucky (4 = 20 + 3117 = 23 + 32 = 24 + 30) and year 18 isn't unluckyas there is no such representation for it.

Such interval of years that there are no unlucky years in it is called The Golden Age.

You should write a program which will find maximum length of The Golden Age which starts no earlier than the year l and ends no later than the year r. If all years in the interval [l, r] are unlucky then the answer is 0.

Input

The first line contains four integer numbers xyl and r (2 ≤ x, y ≤ 10181 ≤ l ≤ r ≤ 1018).

Output

Print the maximum length of The Golden Age within the interval [l, r].

If all years in the interval [l, r] are unlucky then print 0.

Examples
input
2 3 1 10
output
1
input
3 5 10 22
output
8
input
2 3 3 5
output
0
Note

In the first example the unlucky years are 2, 3, 4, 5, 7, 9 and 10. So maximum length of The Golden Age is achived in the intervals [1, 1][6, 6] and [8, 8].

In the second example the longest Golden Age is the interval [15, 22].

题意:就是有一个数叫做不幸运数,他满足题目的那个公式,现在给你一个区间[l,r],让你找一个在这个区间里面一个最长的区间使得这个区间里面的所有数都不是不幸运数,让你输出最长区间的区间长度 
解析:其实可以直接把这个区间里面的所有不幸运数全部都处理出来,然后维护一下最长的区间长度就可以了,不过对于[l,r]两个端点要特殊处理一下
import java.util.Collections;import java.util.Comparator;import java.util.Scanner;import java.util.Vector;/** * 作者:张宇翔 创建日期:2017年6月5日 上午13:18:24 描述:ACM */public class Main {private static long T=(long) 1e18;private static long x,y,l,r;private static Vector<Long> vector;public static void main(String[] args) {InitData();GetAns();}private static void InitData() {Scanner cin = new Scanner(System.in);vector=new Vector<>();x=cin.nextLong();y=cin.nextLong();l=cin.nextLong();r=cin.nextLong();}private static void GetAns() {long q=1,p=1;for(int i=0;i<70;++i){q=1;for(int j=0;j<70;++j){if(p>=(l-q)&&p<=(r-q)){vector.add(p+q);}if(q<=(T/y)){q*=y;}else{break;}}if(p<=(T/x)){p*=x;}else{break;}}vector.add(l-1);vector.add(r+1);Collections.sort(vector,new Comparator<Long>() {@Overridepublic int compare(Long o1, Long o2) {return o1.compareTo(o2);}});long MAX=0;int size=vector.size();for(int i=1;i<size;i++){MAX=Math.max(MAX, vector.get(i)-vector.get(i-1)-1);}System.out.println(MAX);}}//999999999999999999 999999999999999999 1 1000000000000000000



原创粉丝点击