#include <bits/stdc++.h>
#define dbg(...) fprintf(stderr,__VA_ARGS__)
#define rs resize
using namespace std;
int main() {
int n,m;
scanf("%d %d",&n,&m);
vector <vector<int>> t(n,vector<int>(2));
for (int i=0; i<n; i++) scanf("%d",&t[i][0]);
for (int i=0; i<n; i++) scanf("%d",&t[i][1]);
vector <bool> p(m,0),o;
p.rs(n,1);
long long w=INT_MAX;
do {
long long s=0,m=0,r=0;
for (int i=0; i<n; i++) {
s+=t[i][p[i]];
r=max(r,s-m);
m=min(m,s);
}
if (r<w) {
w=r;
o=p;
}
w=min(w,r);
} while (next_permutation(p.begin(),p.end()));
printf("%lld\n",max(w,0LL));
for (bool i: o) printf(i?"B":"A");
printf("\n");
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <bits/stdc++.h> #define dbg(...) fprintf(stderr,__VA_ARGS__) #define rs resize using namespace std; int main() { int n,m; scanf("%d %d",&n,&m); vector <vector<int>> t(n,vector<int>(2)); for (int i=0; i<n; i++) scanf("%d",&t[i][0]); for (int i=0; i<n; i++) scanf("%d",&t[i][1]); vector <bool> p(m,0),o; p.rs(n,1); long long w=INT_MAX; do { long long s=0,m=0,r=0; for (int i=0; i<n; i++) { s+=t[i][p[i]]; r=max(r,s-m); m=min(m,s); } if (r<w) { w=r; o=p; } w=min(w,r); } while (next_permutation(p.begin(),p.end())); printf("%lld\n",max(w,0LL)); for (bool i: o) printf(i?"B":"A"); printf("\n"); return 0; } |
English