- import java.util.*;
- import java.io.*;
- public class MainClass {
- FastScanner in;
- PrintWriter out;
- Set<Integer> I = new LinkedHashSet<Integer>();
- Set<Integer> J = new LinkedHashSet<Integer>();
- Set<Integer> swap = new LinkedHashSet<Integer>();
- ArrayList<Long> answA;
- ArrayList<Long> answB;
- ArrayList<Long> swAnsw;
- long [] a;
- long [] b;
- long [] sw;
- boolean ch = false;
- int n, ridx;
- long ar;
- long maxEl = 0;
- long currTime, maxTime;
- public void IJ() {
- for (int i = 0; i < n; i++) {
- if (a[i] <= b[i]) {
- I.add(i);
- maxEl = Math.max(maxEl, a[i]);
- } else {
- J.add(i);
- maxEl = Math.max(maxEl, b[i]);
- }
- }
- }
- public void findMax() {
- for (int i = 0; i < n; i++) {
- if (I.contains(i) && maxEl == a[i]) {
- ridx = i;
- case1();
- return;
- }
- }
- for (int i = 0; i < n; i++) {
- if (J.contains(i) && maxEl == b[i]) {
- ridx = i;
- case2();
- return;
- }
- }
- }
- public void case1() {
- I.remove(ridx);
- Iterator<Integer> II = I.iterator();
- Iterator<Integer> JI = J.iterator();
- while (II.hasNext()) {
- int i = II.next();
- answA.set(i, currTime);
- currTime += a[i];
- }
- if (!JI.hasNext()) {
- if (currTime >= b[ridx]) {
- answA.set(ridx, currTime);
- } else {
- answA.set(ridx, b[ridx]);
- }
- }
- while(JI.hasNext()) {
- int i = JI.next();
- answA.set(i, currTime);
- currTime += a[i];
- if (!JI.hasNext()) {
- if (currTime >= b[ridx]) {
- answA.set(ridx, currTime);
- } else {
- answA.set(ridx, b[ridx]);
- }
- }
- }
- maxTime = currTime;
- currTime = 0;
- answB.set(ridx, currTime);
- currTime += b[ridx];
- II = I.iterator();
- JI = J.iterator();
- while (II.hasNext()) {
- int i = II.next();
- answB.set(i, currTime);
- currTime += b[i];
- }
- while(JI.hasNext()) {
- int i = JI.next();
- answB.set(i, currTime);
- currTime += b[i];
- }
- maxTime = Math.max(maxTime, currTime);
- }
- public void case2() {
- J.remove(ridx);
- Iterator<Integer> II = I.iterator();
- Iterator<Integer> JI = J.iterator();
- answA.set(ridx, currTime);
- currTime += a[ridx];
- while (JI.hasNext()) {
- int i = JI.next();
- answA.set(i, currTime);
- currTime += a[i];
- }
- while(II.hasNext()) {
- int i = II.next();
- answA.set(i, currTime);
- currTime += a[i];
- }
- maxTime = currTime;
- currTime = 0;
- II = I.iterator();
- JI = J.iterator();
- while (JI.hasNext()) {
- int i = JI.next();
- answB.set(i, currTime);
- currTime += b[i];
- }
- if (!II.hasNext()) {
- if (currTime >= a[ridx]) {
- answB.set(ridx, currTime);
- } else {
- answB.set(ridx, a[ridx]);
- }
- }
- while(II.hasNext()) {
- int i = II.next();
- answB.set(i, currTime);
- currTime += b[i];
- if (!II.hasNext()) {
- if (currTime >= a[ridx]) {
- answB.set(ridx, currTime);
- } else {
- answA.set(ridx, a[ridx]);
- }
- }
- }
- maxTime = Math.max(maxTime, currTime);
- }
- void answer() {
- out.write(maxTime + "\r\n");
- for (int i = 0; i < n; i++) {
- out.write(answA.get(i) + " ");
- }
- out.write("\r\n");
- for (int i = 0; i < n; i++) {
- out.write(answB.get(i) + " ");
- }
- }
- public void solve() throws IOException {
- n = in.nextInt();
- a = new long [n];
- b = new long [n];
- answA = new ArrayList<Long>();
- answB = new ArrayList<Long>();
- for (int i = 0; i < n; i++) {
- a[i] = in.nextInt();
- answA.add(0L);
- answB.add(0L);
- }
- for (int i = 0; i < n; i++) {
- b[i] = in.nextInt();
- }
- IJ();
- findMax();
- answer();
- }
- public void run() {
- try {
- in = new FastScanner(new File("o2cmax.in"));
- out = new PrintWriter(new File("o2cmax.out"));
- solve();
- out.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- class FastScanner {
- BufferedReader br;
- StringTokenizer st;
- FastScanner(File f) {
- try {
- br = new BufferedReader(new FileReader(f));
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- String next() {
- while (st == null || !st.hasMoreTokens()) {
- try {
- st = new StringTokenizer(br.readLine());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return st.nextToken();
- }
- int nextInt() {
- return Integer.parseInt(next());
- }
- }
- public static void main(String[] arg) {
- new MainClass().run();
- }
- }