Download More RAM

A.Download More RAM
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Did you know you can download more RAM? There is a shop with 𝑛 different pieces of software that increase your RAM. The 𝑖-th RAM increasing software takes 𝑎𝑖 GB of memory to run (temporarily, once the program is done running, you get the RAM back), and gives you an additional 𝑏𝑖 GB of RAM (permanently). Each software can only be used once. Your PC currently has 𝑘 GB of RAM.

Note that you can’t use a RAM-increasing software if it takes more GB of RAM to use than what you currently have.

Since RAM is the most important thing in the world, you wonder, what is the maximum possible amount of RAM achievable?

Input
The first line of the input contains a single integer 𝑡 (1≤𝑡≤100) — the number of test cases. The description of test cases follows.

The first line of each test case contains the integers 𝑛 and 𝑘 (1≤𝑛≤100, 1≤𝑘≤1000). Then two lines follow, each containing 𝑛 integers describing the arrays 𝑎 and 𝑏 (1≤𝑎𝑖,𝑏𝑖≤1000).

Output
For each test case, output a single line containing the largest amount of RAM you can achieve.

Example
input
4
3 10
20 30 10
9 100 10
5 1
1 1 5 1 1
1 1 1 1 1
5 1
2 2 2 2 2
100 100 100 100 100
5 8
128 64 32 16 8
128 64 32 16 8
output
29
6
1
256

Note
In the first test case, you only have enough RAM to run the third software initially, but that increases your RAM to 20 GB, which allows you to use the first software, increasing your RAM to 29 GB. The only software left needs 30 GB of RAM, so you have to stop here.

In the second test case, you can use the first, second, fourth and fifth software that need only 1 GB of RAM per software to run to increase your RAM to 5 GB, and then use the last remaining one to increase your RAM to 6 GB.

In the third test case, all the software need more than 1 GB of RAM to run, so the amount of RAM you have stays at 1 GB.

解题思路:
贪心
排序后判断当前内存是否可以满足程序的运行

代码:
c++

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    int t = 0;
    cin>> t;
    while(t--)
    {
        int n,k;
        cin>>n>>k;
        int a[1000],b[1000];
        int temp = k;
        for (int i = 0; i < n; ++i) {
            cin>> a[i];
        }
        for (int i = 0; i < n; ++i) {
            cin >> b[i];
        }
        vector<std::pair<int, int>> ttt;
        for (int i = 0; i < n; ++i) {
            ttt.push_back(pair<int,int>(a[i],b[i]));
        }

        sort(ttt.begin(), ttt.end(),
             [=](std::pair<int, int>& a, std::pair<int, int>& b) { return a.first < b.first; });
        for(auto s :ttt)
        {
            if(temp >= s.first)
                temp += s.second;
            else
                break;
        }
        cout<<temp<<endl;
    }
    return 0;
}

总结:

时间复杂度: O(n)
空间复杂度: O(n)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值