at_abc396_c题解

### 思路

首先,一个很显然的结论,$a_i$ 中大于等于 $0$ 的数一定要选。

然后,对于 $a_i$ 中的负数,我们要根据选择他后,$a_i+b_i$ 是否大于等于 $0$。

所以思路很明显了,可以直接排序后用双指针做。

### 代码

```cpp
#include
using namespace std;
const long long N = 2000010;
long long n,m;
long long a[N],b[N];
bool debug = 0;
string s;
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin >> n >> m;
    for (long long i = 1; i <= n; i ++ ){
        cin >> a[i];
    }
    for (long long i = 1; i <= m; i ++ ){
        cin >> b[i];
    }
    sort(a + 1,a + 1 + n);
    reverse(a + 1,a + 1 + n);
    sort(b + 1,b + 1 + m);
    reverse(b + 1,b + 1 + m);
    long long res = 0,s = 0;
    int j = 1;
    for (long long i = 1; i <= n; i ++ ){
        if (a[i] >= 0){
            s += a[i];
            if (j <= m && b[j] >= 0){
                s += b[j];
                j ++ ;
            }
        }
        else{
            if (j <= m && b[j] + a[i] >= 0){
                s += a[i];
                s += b[j];
                j ++ ;
            }
        }
    }
    cout <     return 0;
}
```

你可能感兴趣的:(c语言,开发语言)