Cod sursa(job #3345473)

Utilizator anelissemPopescu Anelisse anelissem Data 9 martie 2026 19:08:52
Problema Subsir crescator maximal Scor 0
Compilator java Status done
Runda Arhiva educationala Marime 2.21 kb
import java.io.*;

public class Main {
    public static void main(String[] args) {
        try {
            InputReader in = new InputReader(new FileInputStream("ssm.in"));
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ssm.out")));

            int n = in.nextInt();
            int[] dp = new int[n + 1];
            int[] st = new int[n + 1];

            int maxSum = Integer.MIN_VALUE;
            int finalStart = 0;
            int finalEnd = 0;

            for (int i = 1; i <= n; i++) {
                int x = in.nextInt();

                if (i == 1 || dp[i - 1] < 0) {
                    dp[i] = x;
                    st[i] = i;
                } else {
                    dp[i] = dp[i - 1] + x;
                    st[i] = st[i - 1];
                }

                if (dp[i] > maxSum) {
                    maxSum = dp[i];
                    finalStart = st[i];
                    finalEnd = i;
                }
            }

            out.println(maxSum + " " + finalStart + " " + finalEnd);
            out.close();
        } catch (IOException e) {
        }
    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024 * 64];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int read() {
            if (numChars == -1) return -1;
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    return -1;
                }
                if (numChars <= 0) return -1;
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (c >= 0 && c <= 32) c = read();
            if (c == -1) return 0;
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                res = res * 10 + (c - '0');
                c = read();
            } while (c > 32);
            return res * sgn;
        }
    }
}