Contest Duration: ~ (local time) (90 minutes) Back to Home

Submission #158980

Source Code Expand

Copy
```import java.io.*;
import java.math.*;
import java.util.*;

import static java.util.Arrays.*;

public class Main {
private static final int mod = (int)1e9+7;

final Random random = new Random(0);
final IOFast io = new IOFast();

/// MAIN CODE
public void run() throws IOException {
if(false){
PriorityQueue<Long> q = new PriorityQueue<>();
q.add(1L);
q.add(0L);
System.err.println(q.poll());
}

//		int TEST_CASE = Integer.parseInt(new String(io.nextLine()).trim());
int TEST_CASE = 1;
if(false)
while(TEST_CASE-- != 0) {
//			int K = io.nextInt();
//			int N = io.nextInt();
int K = 100000000;
int N = 100000;
int[] D = new int[N];
long ans = 0;
PriorityQueue<Long> q = new PriorityQueue<>();
for(int i = 0; i < N; i++) {
//				q.add(io.nextLong()<<17|i);
//				D[i] = io.nextInt();
q.add((random.nextInt(1000)+1L)<<17|i);
D[i] = random.nextInt(1000)+1;
}
for(int i = 0; i < K; i++) {
long v = q.poll();
long x = v>>>17;
int idx = (int)(v&((1<<17)-1));
ans += x;
q.add((x+D[idx])<<17|idx);
}
io.out.println(ans);
}

while(TEST_CASE-- != 0) {
int K = io.nextInt();
int N = io.nextInt();
//			int K = 100000000;
//			int N = 100000;
//			int N = 1;
int[] A = new int[N];
int[] D = new int[N];
for(int i  =0; i < N; i++) {
A[i] = io.nextInt();
D[i] = io.nextInt();
//				A[i] = random.nextInt(1000)+1;
//				D[i] = random.nextInt(1000)+1;
}
// 1000+2000+3000+...+10^3*10^8
long low = 0, high = 1000000L * 10000000L;
while(high - low > 1) {
long mid = (low + high) / 2;
long k = 0;
for(int i = 0; i < N; i++) {
// A[i] + D[i] * k <= mid
if(mid >= A[i]) {
k += (mid - A[i]) / D[i] + 1;
}
}
if(k >= K) high = mid;
else low = mid;
}
//			System.err.println(high);
BigInteger b = BigInteger.valueOf(0);
for(int i = 0; i < N; i++) if(low >= A[i]) {
long t = (low - A[i]) / D[i] + 1;
K -= t;
// D[i] (0 + 1 + 2 + ...)
//				ans += A[i] * t + D[i] * t * (t - 1) / 2;
BigInteger bi = BigInteger.valueOf(A[i]).multiply(BigInteger.valueOf(t)).add(BigInteger.valueOf(D[i]).multiply(BigInteger.valueOf(t)).multiply(BigInteger.valueOf(t-1)).divide(BigInteger.valueOf(2)));
//				b = b.add(BigInteger.valueOf(A[i] * t + D[i] * t * (t - 1) / 2));
b = b.add(bi);
}
//			io.out.println(ans + high * K);
io.out.println(b.add(BigInteger.valueOf(high * K)));
}
}

/// TEMPLATE
static int gcd(int n, int r) { return r == 0 ? n : gcd(r, n%r); }
static long gcd(long n, long r) { return r == 0 ? n : gcd(r, n%r); }

static <T> void swap(T[] x, int i, int j) {
T t = x[i];
x[i] = x[j];
x[j] = t;
}

static void swap(int[] x, int i, int j) {
int t = x[i];
x[i] = x[j];
x[j] = t;
}

static void radixSort(int[] xs) {
int[] cnt = new int[(1<<16)+1];
int[] ys = new int[xs.length];

for(int j = 0; j <= 16; j += 16) {
Arrays.fill(cnt, 0);
for(int x : xs) { cnt[(x>>j&0xFFFF)+1]++; }
for(int i = 1; i < cnt.length; i++) { cnt[i] += cnt[i-1]; }
for(int x : xs) { ys[cnt[x>>j&0xFFFF]++] = x; }
{ final int[] t = xs; xs = ys; ys = t; }
}
}

static void radixSort(long[] xs) {
int[] cnt = new int[(1<<16)+1];
long[] ys = new long[xs.length];

for(int j = 0; j <= 48; j += 16) {
Arrays.fill(cnt, 0);
for(long x : xs) { cnt[(int)(x>>j&0xFFFF)+1]++; }
for(int i = 1; i < cnt.length; i++) { cnt[i] += cnt[i-1]; }
for(long x : xs) { ys[cnt[(int)(x>>j&0xFFFF)]++] = x; }
{ final long[] t = xs; xs = ys; ys = t; }
}
}

static void arrayIntSort(int[][] x, int... keys) {
Arrays.sort(x, new ArrayIntsComparator(keys));
}

static class ArrayIntsComparator implements Comparator<int[]> {
final int[] KEY;

public ArrayIntsComparator(int... key) {
KEY = key;
}

@Override
public int compare(int[] o1, int[] o2) {
for(int k : KEY) if(o1[k] != o2[k]) return o1[k] - o2[k];
return 0;
}
}

static class ArrayIntComparator implements Comparator<int[]> {
final int KEY;

public ArrayIntComparator(int key) {
KEY = key;
}

@Override
public int compare(int[] o1, int[] o2) {
return o1[KEY] - o2[KEY];
}
}

void main() throws IOException {
//		IOFast.setFileIO("rle-size.in", "rle-size.out");
try {
run();
}
catch (EndOfFileRuntimeException e) { }
io.out.flush();
}

public static void main(String[] args) throws IOException {
new Main().main();
}

static class EndOfFileRuntimeException extends RuntimeException {
private static final long serialVersionUID = -8565341110209207657L; }

static
public class IOFast {
private BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
private PrintWriter out = new PrintWriter(System.out);

void setFileIO(String ins, String outs) throws IOException {
out.flush();
out.close();
in.close();
in = new BufferedReader(new FileReader(ins));
out = new PrintWriter(new FileWriter(outs));
System.err.println("reading from " + ins);
}

//		private static final int BUFFER_SIZE = 50 * 200000;
private static int pos, readLen;
private static final char[] buffer = new char[1024 * 8];
private static char[] str = new char[500*8*2];
private static boolean[] isDigit = new boolean[256];
private static boolean[] isSpace = new boolean[256];
private static boolean[] isLineSep = new boolean[256];

static {
for(int i = 0; i < 10; i++) { isDigit['0' + i] = true; }
isDigit['-'] = true;
isSpace[' '] = isSpace['\r'] = isSpace['\n'] = isSpace['\t'] = true;
isLineSep['\r'] = isLineSep['\n'] = true;
}

public int read() throws IOException {
if(pos >= readLen) {
pos = 0;
readLen = in.read(buffer);
if(readLen <= 0) { throw new EndOfFileRuntimeException(); }
}
return buffer[pos++];
}

public int nextInt() throws IOException {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);

int i = 0;
int ret = 0;
if(str[0] == '-') { i = 1; }
for(; i < len; i++) ret = ret * 10 + str[i] - '0';
if(str[0] == '-') { ret = -ret; }
return ret;
//			return Integer.parseInt(nextString());
}

public long nextLong() throws IOException {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);

int i = 0;
long ret = 0;
if(str[0] == '-') { i = 1; }
for(; i < len; i++) ret = ret * 10 + str[i] - '0';
if(str[0] == '-') { ret = -ret; }
return ret;
//			return Long.parseLong(nextString());
}

public char nextChar() throws IOException {
while(true) {
final int c = read();
if(!isSpace[c]) { return (char)c; }
}
}

int reads(int len, boolean[] accept) throws IOException {
try {
while(true) {
final int c = read();
if(accept[c]) { break; }

if(str.length == len) {
char[] rep = new char[str.length * 3 / 2];
System.arraycopy(str, 0, rep, 0, str.length);
str = rep;
}

str[len++] = (char)c;
}
}
catch(EndOfFileRuntimeException e) { ; }

return len;
}

int reads(char[] cs, int len, boolean[] accept) throws IOException {
try {
while(true) {
final int c = read();
if(accept[c]) { break; }
cs[len++] = (char)c;
}
}
catch(EndOfFileRuntimeException e) { ; }

return len;
}

public char[] nextLine() throws IOException {
int len = 0;
str[len++] = nextChar();
len = reads(len, isLineSep);

try {
if(str[len-1] == '\r') { len--; read(); }
}
catch(EndOfFileRuntimeException e) { ; }

return Arrays.copyOf(str, len);
}

public String nextString() throws IOException {
return new String(next());
}

public char[] next() throws IOException {
int len = 0;
str[len++] = nextChar();
len = reads(len, isSpace);
return Arrays.copyOf(str, len);
}

public int next(char[] cs) throws IOException {
int len = 0;
cs[len++] = nextChar();
len = reads(cs, len, isSpace);
return len;
}

public double nextDouble() throws IOException {
return Double.parseDouble(nextString());
}

public long[] nextLongArray(final int n) throws IOException {
final long[] res = new long[n];
for(int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}

public int[] nextIntArray(final int n) throws IOException {
final int[] res = new int[n];
for(int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}

public int[][] nextIntArray2D(final int n, final int k) throws IOException {
final int[][] res = new int[n][];
for(int i = 0; i < n; i++) {
res[i] = nextIntArray(k);
}
return res;
}

public int[][] nextIntArray2DWithIndex(final int n, final int k) throws IOException {
final int[][] res = new int[n][k+1];
for(int i = 0; i < n; i++) {
for(int j = 0; j < k; j++) {
res[i][j] = nextInt();
}
res[i][k] = i;
}
return res;
}

public double[] nextDoubleArray(final int n) throws IOException {
final double[] res = new double[n];
for(int i = 0; i < n; i++) {
res[i] = nextDouble();
}
return res;
}

}

}
```

Submission Info

Submission Time 2014-04-12 21:30:06+0900 C - 増築王高橋君 tanzaku Java (OpenJDK 1.7.0) 100 9494 Byte AC 751 ms 37728 KB

Test Cases

Set Name Score / Max Score Test Cases
Sample 0 / 0 subtask0_sample-01.txt, subtask0_sample-02.txt
Subtask1 30 / 30 subtask0_sample-01.txt, subtask0_sample-02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt
Subtask2 10 / 10 subtask0_sample-01.txt, subtask0_sample-02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt
Subtask3 15 / 15 subtask0_sample-01.txt, subtask0_sample-02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt, subtask3_01.txt, subtask3_02.txt, subtask3_03.txt, subtask3_04.txt, subtask3_05.txt, subtask3_06.txt, subtask3_07.txt, subtask3_08.txt, subtask3_09.txt, subtask3_10.txt, subtask3_11.txt, subtask3_12.txt
Subtask4 45 / 45 subtask0_sample-01.txt, subtask0_sample-02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt, subtask3_01.txt, subtask3_02.txt, subtask3_03.txt, subtask3_04.txt, subtask3_05.txt, subtask3_06.txt, subtask3_07.txt, subtask3_08.txt, subtask3_09.txt, subtask3_10.txt, subtask3_11.txt, subtask3_12.txt, subtask4_01.txt, subtask4_02.txt, subtask4_03.txt, subtask4_04.txt, subtask4_05.txt, subtask4_06.txt, subtask4_07.txt, subtask4_08.txt, subtask4_09.txt, subtask4_10.txt, subtask4_11.txt, subtask4_12.txt, subtask4_13.txt, subtask4_14.txt
Case Name Status Exec Time Memory
subtask0_sample-01.txt 482 ms 20916 KB
subtask0_sample-02.txt 379 ms 20912 KB
subtask1_01.txt 376 ms 20912 KB
subtask1_02.txt 375 ms 20916 KB
subtask1_03.txt 378 ms 20916 KB
subtask1_04.txt 386 ms 20892 KB
subtask1_05.txt 377 ms 21044 KB
subtask1_06.txt 379 ms 20912 KB
subtask1_07.txt 376 ms 20916 KB
subtask1_08.txt 377 ms 21040 KB
subtask1_09.txt 378 ms 21036 KB
subtask1_10.txt 384 ms 21036 KB
subtask1_11.txt 382 ms 21028 KB
subtask1_12.txt 380 ms 21036 KB
subtask2_01.txt 378 ms 21040 KB
subtask2_02.txt 399 ms 22740 KB
subtask2_03.txt 390 ms 22572 KB
subtask2_04.txt 377 ms 21040 KB
subtask2_05.txt 379 ms 21044 KB
subtask2_06.txt 445 ms 25096 KB
subtask2_07.txt 441 ms 25192 KB
subtask2_08.txt 440 ms 25328 KB
subtask2_09.txt 457 ms 25264 KB
subtask2_10.txt 475 ms 25372 KB
subtask2_11.txt 455 ms 25852 KB
subtask2_12.txt 437 ms 25376 KB
subtask3_01.txt 385 ms 22324 KB
subtask3_02.txt 377 ms 20916 KB
subtask3_03.txt 443 ms 24756 KB
subtask3_04.txt 526 ms 25356 KB
subtask3_05.txt 433 ms 24104 KB
subtask3_06.txt 500 ms 29548 KB
subtask3_07.txt 556 ms 30932 KB
subtask3_08.txt 637 ms 36032 KB
subtask3_09.txt 656 ms 36868 KB
subtask3_10.txt 671 ms 37728 KB
subtask3_11.txt 690 ms 37000 KB
subtask3_12.txt 588 ms 33960 KB
subtask4_01.txt 667 ms 35772 KB
subtask4_02.txt 376 ms 21004 KB
subtask4_03.txt 496 ms 29204 KB
subtask4_04.txt 718 ms 35640 KB
subtask4_05.txt 383 ms 21292 KB
subtask4_06.txt 374 ms 21040 KB
subtask4_07.txt 378 ms 21036 KB
subtask4_08.txt 379 ms 20908 KB
subtask4_09.txt 720 ms 35712 KB
subtask4_10.txt 380 ms 21164 KB
subtask4_11.txt 733 ms 36136 KB
subtask4_12.txt 751 ms 36420 KB
subtask4_13.txt 728 ms 36272 KB
subtask4_14.txt 375 ms 20912 KB