結果

問題 No.967 引き算をして門松列(その2)
ユーザー 37zigen37zigen
提出日時 2020-03-22 04:35:55
言語 Java19
(openjdk 21)
結果
AC  
実行時間 552 ms / 2,000 ms
コード長 1,269 bytes
コンパイル時間 2,385 ms
コンパイル使用メモリ 74,064 KB
実行使用メモリ 61,644 KB
最終ジャッジ日時 2023-08-26 01:10:23
合計ジャッジ時間 7,778 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
55,396 KB
testcase_01 AC 126 ms
55,816 KB
testcase_02 AC 133 ms
55,820 KB
testcase_03 AC 366 ms
61,000 KB
testcase_04 AC 359 ms
61,644 KB
testcase_05 AC 368 ms
61,620 KB
testcase_06 AC 392 ms
60,900 KB
testcase_07 AC 420 ms
61,464 KB
testcase_08 AC 502 ms
60,832 KB
testcase_09 AC 550 ms
61,544 KB
testcase_10 AC 552 ms
61,020 KB
testcase_11 AC 549 ms
61,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main(String[] args) throws FileNotFoundException {
		long t = System.currentTimeMillis();
		new Main().run();
		System.err.println(System.currentTimeMillis() - t);
	}
	
	long solve(long A,long B,long C,long X,long Y,long Z) {
		final long INF=Long.MAX_VALUE/3;
		long ret=INF,base=0;
		if(A==C) {
			--A;
			base+=X;
			if(A==0)return -1;
		}
		
		// A < C
		// B is min
		{
			if(A-1>0)ret=Math.min(ret,base+Y*Math.max(0,(B-(A-1))));
		}
		// A < C		
		// B is max
		if(B>=3){
			long cost=Math.max(0, A-(B-1))*X+Math.max(0, C-(B-1))*Z;
			A=Math.min(A, B-1);
			C=Math.min(C, B-1);
			ret=Math.min(ret, cost+base+(A!=C?0:Math.min(X, Z)));
		}
		return ret==INF?-1:ret;
	}
	
	void run() {
		Scanner sc = new Scanner(System.in);
		int T=sc.nextInt();
		for(int t=0;t<T;++t) {
			long A=sc.nextLong();
			long B=sc.nextLong();
			long C=sc.nextLong();
			long X=sc.nextLong();
			long Y=sc.nextLong();
			long Z=sc.nextLong();
			if(A>C||(A==C&&X>Z)) {
				A^=C;C^=A;A^=C;X^=Z;Z^=X;X^=Z;
			}
			System.out.println(solve(A,B,C,X,Y,Z));
		}
	}


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

}
0