首页 > 美团笔试8.15第四题
头像
牛客627813384号
编辑于 2020-08-15 20:56
+ 关注

美团笔试8.15第四题

现在在本地运行都能通过测试,不知道会不会超时和超内存,有问题可以指出来一起探讨
测试样例:5 2 2
4 2
3 3
5 4
5 3
1 5
输出:18

        public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			int n = sc.nextInt();
			int a = sc.nextInt();
			int b = sc.nextInt();
			int[][] arr = new int[n][2];
			for(int i=0; i<n; i++) {
				for(int j=0; j<2; j++) {
					arr[i][j] = sc.nextInt();
				}
			}
			findRes(arr, a, b, 0, 0, 0, 0);
			System.out.println(res);
			
		}
		sc.close();
	}
	static int res = 0;
	public static void findRes(int[][] arr, int a, int b, int ca, int cb, int sum, int index) {
		if(index == arr.length) {
			if(index==arr.length && ca==a && cb==b) {
				res = Math.max(res, sum);
			}
			return;
		}
		findRes(arr, a, b, ca+1, cb, sum+arr[index][0], index+1);
		findRes(arr, a, b, ca, cb+1, sum+arr[index][1], index+1);
		findRes(arr, a, b, ca, cb, sum, index+1);
	}     

全部评论

(6) 回帖
加载中...
话题 回帖

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐