P5194搜索TLE了4个,O2优化后TLE3个
  • 板块灌水区
  • 楼主Creeper250
  • 当前回复3
  • 已保存回复3
  • 发布时间2023/5/15 22:40
  • 上次更新2023/10/23 15:37:57
查看原帖
P5194搜索TLE了4个,O2优化后TLE3个
456812
Creeper250楼主2023/5/15 22:40
#include <bits/stdc++.h>
using namespace std;
int n, c, maxn;
int a[1001];
void dfs(int x, int w)
{
    if (x == n)
    {
        maxn = max(maxn, w);
        return;
    }
    for (int i = x + 1; i <= n; i++)
    {
        if (a[i] + w <= c)
        {
            dfs(i, w + a[i]);
        }
        else
        {
            maxn = max(maxn, w);
            break;
        }
    }
}
int main()
{
    cin >> n >> c;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    dfs(0, 0);
    cout << maxn << endl;
    return 0;
}
2023/5/15 22:40
加载中...