#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;
const ld eps=1e-7;
const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=20010, K=51;
ll n, m, k, u, v, x, y, t, a, b, ans;
ll A[MAXN];
int dp[K][MAXN];
int seg[MAXN<<2], lazy[MAXN<<2];
string S[K];
int last0[K][MAXN];
void add_lazy(int id, ll val){
lazy[id]+=val;
seg[id]+=val;
}
void shift(int id){
if (!lazy[id]) return ;
add_lazy(id<<1, lazy[id]);
add_lazy(id<<1 | 1, lazy[id]);
lazy[id]=0;
}
void Add(int id, int tl, int tr, int l, int r, ll val){
if (r<=tl || tr<=l) return ;
if (l<=tl && tr<=r){
add_lazy(id, val);
return ;
}
shift(id);
int mid=(tl+tr)>>1;
Add(id<<1, tl, mid, l, r, val);
Add(id<<1 | 1, mid, tr, l, r, val);
seg[id]=min(seg[id<<1], seg[id<<1 | 1]);
}
ll Get(int id, int tl, int tr, int l, int r){
if (r<=tl || tr<=l) return seg[0];
if (l<=tl && tr<=r) return seg[id];
shift(id);
int mid=(tl+tr)>>1;
return min(Get(id<<1, tl, mid, l, r), Get(id<<1 | 1, mid, tr, l, r));
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin>>m>>n>>k;
for (int i=1; i<=n; i++) cin>>A[i];
for (int i=1; i<=m; i++){
cin>>S[i];
S[i]="#"+S[i];
for (int j=1; j<=n; j++){
last0[i][j]=last0[i][j-1];
if (S[i][j]=='0') last0[i][j]=j;
}
}
memset(dp, 31, sizeof(dp));
dp[0][0]=0;
for (int j=1; j<=k; j++){
memset(seg, 0, sizeof(seg));
memset(lazy, 0, sizeof(lazy));
seg[0]=inf;
Add(1, 0, n, 0, 1, dp[j-1][0]);
for (int i=1; i<=n; i++){
for (int x=1; x<=m; x++){
if (S[x][i]=='1') Add(1, 0, n, last0[x][i], i, +A[i]);
else{
for (int y=last0[x][i-1]+1; y<i; y++)
Add(1, 0, n, last0[x][y], y, -A[y]);
}
}
Add(1, 0, n, i, i+1, dp[j-1][i]);
dp[j][i]=Get(1, 0, n, 0, i);
}
cout<<dp[j][n]<<'\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
10 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
356 ms |
5368 KB |
Output is correct |
2 |
Correct |
338 ms |
5368 KB |
Output is correct |
3 |
Correct |
358 ms |
5368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1780 ms |
5756 KB |
Output is correct |
2 |
Execution timed out |
2044 ms |
5888 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
10 ms |
5120 KB |
Output is correct |
3 |
Correct |
356 ms |
5368 KB |
Output is correct |
4 |
Correct |
338 ms |
5368 KB |
Output is correct |
5 |
Correct |
358 ms |
5368 KB |
Output is correct |
6 |
Correct |
1780 ms |
5756 KB |
Output is correct |
7 |
Execution timed out |
2044 ms |
5888 KB |
Time limit exceeded |
8 |
Halted |
0 ms |
0 KB |
- |