#include<bits/stdc++.h>
using namespace std;
// define
#define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
#define ll long long
#define ii pair <int , int>
#define iii pair <int , ii>
#define se second
#define fi first
#define all(v) (v).begin() , (v).end()
#define Unique(v) sort(all(v)) , v.resize(unique(all(v)) - v.begin())
#define bit(x,i) (((x) >> (i)) & 1LL)
#define flip(x,i) ((x) ^ (1LL << (i)))
#define ms(d,x) memset(d , x , sizeof(d))
#define exist __exist
#define ends __ends
#define visit visited
#define left __left
#define right __right
#define prev __prev
#define next __next
#define sitingfake 1
#define orz 1
//constant
const long long mod = 1e9 + 7;
const long long linf = 4557430888798830399LL;
const long long nlinf = -4485090715960753727LL;
const int LOG = 20;
const int inf = 1061109567;
const int ninf = -1044266559;
const int dx[] = {0 , -1 , 0 , 1};
const int dy[] = {-1 , 0 , 1 , 0};
template<typename T> bool maximize(T &a, const T &b)
{
if(a < b) {a = b; return 1;}
return 0;
}
template<typename T> bool minimize(T &a, const T &b)
{
if(a > b) {a = b; return 1;}
return 0;
}
void Plus(ll & a ,ll b)
{
b %= mod;
a += b;
if(a < 0) a += mod;
a %= mod;
return;
}
void Mul(ll & a, ll b)
{
(a *= (b % mod)) %= mod;
return;
}
//code
const int maxn = 15005;
int n , m;
ii a[maxn];
int b[maxn];
namespace sub1
{
int dp[505];
int trace[505];
bool approve()
{
for(int i = 1; i <= n; i++)
{
if(a[i].fi != 1) return 0;
}
return 1;
}
void solve()
{
ms(dp , 0x3f);
dp[0] = 0;
for(int sum = 1; sum <= n; sum++)
{
for(int i = 1; i <= m; i++)
{
if(b[i] <= sum && minimize(dp[sum] , dp[sum - b[i]] + 1))
{
trace[sum] = b[i];
}
}
}
vector <int> sz;
if(dp[n] == inf)
{
cout << "-1";
return;
}
int cur = n;
while(cur != 0)
{
sz.push_back(trace[cur]);
cur -= trace[cur];
}
cur = 1;
cout << dp[n] << "\n";
for(int x : sz)
{
cout << x << " ";
while(x--)
{
cout << cur << " ";
cur++;
}
cout << "\n";
}
}
}
namespace sub2
{
bool approve()
{
if(m != 1) return 0;
return 1;
}
void solve()
{
int sum = 0;
for(int i = 1; i <= n; i++)
{
sum += a[i].fi;
}
if(sum % b[1] != 0)
{
cout << -1;
return;
}
int lim = sum / b[1];
vector <int> ans[lim + 2];
for(int i = 1; i <= n; i++)
{
if(a[i].fi > lim)
{
cout << -1;
return;
}
else
{
for(int j = 1; j <= lim; j++)
{
if(ans[j].size() < b[1])
{
ans[j].push_back(i);
a[i].fi--;
}
if(a[i].fi == 0) break;
}
if(a[i].fi != 0)
{
cout << -1;
return;
}
}
}
cout << lim << "\n";
for(int i = 1; i <= lim; i++)
{
cout << b[1] << " ";
for(int x : ans[i])
{
cout << x << " ";
}
cout << "\n";
}
}
}
namespace sub4
{
const int maxn = 505;
bool dp[505][505][505];
int sum = 0;
vector <int> sz;
bool approve()
{
int sum = 0;
for(int i = 1; i <= n; i++)
{
sum += a[i].fi;
}
return sum <= 500;
}
void Trace(int curBox , int j , int S)
{
if(curBox == 0) return;
if(b[j] <= S && dp[curBox - 1][j][S - b[j]])
{
sz.push_back(b[j]);
Trace(curBox - 1 , j , S - b[j]);
}
else Trace(curBox , j + 1 , S);
}
void PrintAns(int steps)
{
priority_queue <ii> pq;
for(int i = 1; i <= n; i++)
{
pq.push(a[i]);
}
cout << steps << "\n";
sort(all(sz) , greater <int> ());
for(int x : sz)
{
vector <ii> res;
for(int i = 1; i <= x; i++)
{
res.push_back(pq.top());
pq.pop();
}
cout << x << " ";
for(int i = 1; i <= x; i++)
{
cout << res[i - 1].se << " ";
if(res[i - 1].fi != 1)
{
pq.push({res[i - 1].fi - 1 , res[i - 1].se});
}
}
cout << "\n";
}
}
void solve()
{
sort(a + 1 , a + n + 1);
for(int i = 1; i <= n; i++) sum += a[i].fi;
for(int i = 1; i <= m; i++) dp[0][i][0] = 1;
int it = 1 , val = 0;
for(int i = 1; i <= sum; i++)
{
while(it <= n && a[it].fi < i)
{
val += a[it].fi;
it++;
}
int target = val + (n - it + 1) * i;
int pos = upper_bound(b + 1 , b + m + 1 , sum / i) - b - 1;
for(int j = pos; j >= 1; j--)
{
for(int k = target ; k >= 1; k--)
{
dp[i][j][k] |= dp[i][j + 1][k];
if(k - b[j] >= 0) dp[i][j][k] |= dp[i - 1][j][k - b[j]];
}
}
if(dp[i][1][sum])
{
Trace(i , 1 , sum);
PrintAns(i);
exit(0);
}
}
cout << -1;
}
}
void solve(void)
{
cin >> n;
for(int i = 1; i <= n; i++)
{
cin >> a[i].fi;
a[i].se = i;
}
cin >> m;
for(int i = 1; i <= m; i++)
{
cin >> b[i];
}
if(sub1 :: approve())
{
sub1 :: solve();
return;
}
if(sub2 :: approve())
{
sub2 :: solve();
return;
}
if(sub4 :: approve())
{
sub4 :: solve();
return;
}
}
/**
**/
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task ""
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) solve();
// execute;
}
컴파일 시 표준 에러 (stderr) 메시지
cookies.cpp: In function 'int main()':
cookies.cpp:326:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
326 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
cookies.cpp:327:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
327 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |