[题解]AT_abc240_f [ABC240F] Sum Sum Max

思路

题目要求的是 maxa=1n{i=1aj=1aAj},所以我们将 i=1aj=1aAj 化简一下,得:

i×A1+(i1)×A2++1×Ax

a 每增加 1 时,这个和 s 将会变为 s+i=1xai+ax

  • 如果在 xi0 时,显然对于答案是有贡献的,全部加上即可。

  • 如果在 xi<0 时,显然当 s 没有被减下 0 时,对于答案都是有贡献的,加上即可。但是在后面需要将剩下的都要加上。

code

#include <bits/stdc++.h>
#define re register
#define int long long

using namespace std;

const int N = 2e5 + 10,inf = 1e18 + 10;
int T,n,m;

inline int read(){
	int r = 0,w = 1;
	char c = getchar();
	while (c < '0' || c > '9'){
		if (c == '-') w = -1;
		c = getchar();
	}
	while (c >= '0' && c <= '9'){
		r = (r << 1) + (r << 3) + (c ^ 48);
		c = getchar();
	}
	return r * w;
}

signed main(){
	T = read();
	while (T--){
		int Max = -inf,sum = 0,res = 0;
		n = read();
		m = read();
		for (re int i = 1;i <= n;i++){
			int x,y;
			x = read();
			y = read();
			if (x >= 0){
				res += sum * y + x * (y + 1) * y / 2;
				Max = max(Max,res);
			}
			else{
				int l = max(1ll,min(sum / (-x),y));//注意这里需要与 1 取 max,避免 sum / (-x) 为负数 
				int del = sum * l + x * (l + 1) * l / 2;
				Max = max(Max,res + del);
				res += sum * y + x * (y + 1) * y / 2;
				Max = max(Max,res);
			}
			sum += x * y;
		}
		printf("%lld\n",Max);
	}
	return 0;
}

作者:WaterSun

出处:https://www.cnblogs.com/BeautifulWish/p/AT_abc240_f.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   BeautifulWish  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
点击右上角即可分享
微信分享提示