Submission #1964140


Source Code Expand

import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

class Main {

	public void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		long[][] a = new long[n][2];
		for (int i = 0; i < n; ++i) {
			a[i][0] = sc.nextLong();
			a[i][1] = sc.nextLong();
		}
		Arrays.sort(a, new Comparator<long[]>() {
			@Override
			public int compare(long[] arg0, long[] arg1) {
				return -Long.compare(arg0[0] + arg0[1], arg1[0] + arg1[1]);
			}
		});
		long ans = Long.MAX_VALUE / 3;
		long[][] f = new long[n + 1][n + 1];
		for (int i = 0; i < f.length; ++i)
			for (int j = 0; j < f[i].length; ++j)
				f[i][j] = Long.MAX_VALUE / 3;
		f[0][0] = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j <= i; ++j) {
				f[i - j][j + 1] = Math.min(f[i - j][j + 1], f[i - j][j] + a[i][0] + j * (a[i][0] + a[i][1]));
				f[i - j + 1][j] = Math.min(f[i - j + 1][j], f[i - j][j] + a[i][1] + (i - j) * (a[i][0] + a[i][1]));
			}
		}
		for (int i = 0; i <= n; ++i) {
			ans = Math.min(ans, f[i][n - i]);
		}

		if (n % 2 == 1) {
			long[][][] g = new long[n + 1][n + 1][2];
			for (int i = 0; i < g.length; ++i)
				for (int j = 0; j < g[i].length; ++j)
					for (int k = 0; k < g[i][j].length; ++k)
						g[i][j][k] = Long.MAX_VALUE / 3;
			g[0][0][0] = 0;
			for (int i = 0; i < n; ++i) {
				for (int j = 0; j <= i; ++j) {
					for (int k = 0; k <= 1; ++k) {
						g[i - j][j + 1][k] = Math.min(g[i - j][j + 1][k],
								g[i - j][j][k] + a[i][0] + j * (a[i][0] + a[i][1]));
						g[i - j + 1][j][k] = Math.min(g[i - j + 1][j][k],
								g[i - j][j][k] + a[i][1] + (i - j) * (a[i][0] + a[i][1]));
						if (k == 0) {
							g[i - j][j][1] = Math.min(g[i - j][j][1], g[i - j][j][0] + (n - 1) * (a[i][0] + a[i][1]));
						}
						if (i + j + k == n)
							ans = Math.min(ans, g[i][j][k]);
					}
				}
			}
		}
		System.out.println(ans);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

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

Submission Info

Submission Time
Task F - Intervals
User fortoobye
Language Java8 (OpenJDK 1.8.0)
Score 0
Code Size 2164 Byte
Status TLE
Exec Time 2118 ms
Memory 732864 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1000
Status
AC × 2
AC × 23
TLE × 3
MLE × 19
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 529 ms 242556 KB
001.txt AC 278 ms 65884 KB
002.txt AC 546 ms 241208 KB
003.txt TLE 2118 ms 732864 KB
004.txt AC 554 ms 244616 KB
005.txt TLE 2109 ms 633920 KB
006.txt AC 541 ms 244632 KB
007.txt AC 424 ms 161936 KB
008.txt AC 536 ms 245740 KB
009.txt AC 242 ms 52460 KB
010.txt MLE 546 ms 245004 KB
011.txt AC 367 ms 124504 KB
012.txt AC 532 ms 244380 KB
013.txt AC 224 ms 49644 KB
014.txt MLE 543 ms 246828 KB
015.txt MLE 1334 ms 293036 KB
016.txt MLE 556 ms 246304 KB
017.txt MLE 517 ms 244732 KB
018.txt MLE 545 ms 245840 KB
019.txt TLE 2113 ms 619860 KB
020.txt AC 96 ms 19412 KB
021.txt AC 96 ms 20560 KB
022.txt MLE 539 ms 247684 KB
023.txt MLE 533 ms 247080 KB
024.txt AC 540 ms 243292 KB
025.txt AC 546 ms 245096 KB
026.txt AC 541 ms 245456 KB
027.txt MLE 542 ms 245692 KB
028.txt MLE 538 ms 248116 KB
029.txt MLE 535 ms 247060 KB
030.txt MLE 528 ms 246688 KB
031.txt MLE 533 ms 247736 KB
032.txt MLE 534 ms 246236 KB
033.txt AC 534 ms 244768 KB
034.txt MLE 553 ms 246244 KB
035.txt MLE 539 ms 245280 KB
036.txt AC 534 ms 244000 KB
037.txt MLE 544 ms 247924 KB
038.txt AC 551 ms 242280 KB
039.txt AC 535 ms 244424 KB
040.txt MLE 546 ms 248008 KB
041.txt MLE 540 ms 246340 KB
042.txt AC 543 ms 238108 KB
example0.txt AC 99 ms 17620 KB
example1.txt AC 101 ms 20948 KB