题解 | 小苯的最大和

小苯的最大和

https://www.nowcoder.com/practice/c88383720c18407197547c174d902315

#include <iostream>

using namespace std;

typedef long long LL;
const int N = 2e5 + 10;
int a[N];
LL f[N];

int main() {
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i = 1;i <= n;i++){
            cin>>a[i];
        }
        for(int i = 1;i <= n;i++){
            f[i] = f[i - 1] + a[i];
            if(i >= 2) f[i] = max(f[i - 2],f[i]);
            if(i >= 3) f[i] = max(f[i - 3],f[i]);
        }
        cout<<f[n]<<"\n";
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务