import java.util.Random;
interface Sum {
long sum(int[] buf);
}
class RangeSum implements Sum {
@Override
public long sum(final int[] buf) {
long sum = 0L;
for (int v: buf) {
sum += v;
}
return sum;
}
}
class IterSum implements Sum {
@Override
public long sum(final int[] buf) {
long sum = 0L;
for (int i = 0; i < buf.length; ++i) {
sum += buf[i];
}
return sum;
}
}
class BackIterSum implements Sum {
@Override
public long sum(final int[] buf) {
long sum = 0L;
for (int i = buf.length; i-- > 0;) {
sum += buf[i];
}
return sum;
}
}
public class Main {
public static void main
(final String[] args
) { Compiler.
compileClass(BackIterSum.
class); int size
= Integer.
parseInt(args
[0]); int[] buf = new int[size];
for (int i = 0; i < size; ++i) {
buf[i] = random.nextInt();
}
Sum sum;
switch (Integer.
parseInt(args
[1])) { case 0:
sum = new RangeSum();
break;
case 1:
sum = new IterSum();
break;
case 2:
sum = new BackIterSum();
break;
default:
}
long start
= System.
currentTimeMillis(); long total = sum.sum(buf);
long end
= System.
currentTimeMillis(); System.
out.
println(total
+ ": " + (end
- start
)); }
}
aW1wb3J0IGphdmEudXRpbC5SYW5kb207CgppbnRlcmZhY2UgU3VtIHsKICAgIGxvbmcgc3VtKGludFtdIGJ1Zik7Cn0KCmNsYXNzIFJhbmdlU3VtIGltcGxlbWVudHMgU3VtIHsKICAgIEBPdmVycmlkZQogICAgcHVibGljIGxvbmcgc3VtKGZpbmFsIGludFtdIGJ1ZikgewogICAgICAgIGxvbmcgc3VtID0gMEw7CiAgICAgICAgZm9yIChpbnQgdjogYnVmKSB7CiAgICAgICAgICAgIHN1bSArPSB2OwogICAgICAgIH0KICAgICAgICByZXR1cm4gc3VtOwogICAgfQp9CgpjbGFzcyBJdGVyU3VtIGltcGxlbWVudHMgU3VtIHsKICAgIEBPdmVycmlkZQogICAgcHVibGljIGxvbmcgc3VtKGZpbmFsIGludFtdIGJ1ZikgewogICAgICAgIGxvbmcgc3VtID0gMEw7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBidWYubGVuZ3RoOyArK2kpIHsKICAgICAgICAgICAgc3VtICs9IGJ1ZltpXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHN1bTsKICAgIH0KfQoKY2xhc3MgQmFja0l0ZXJTdW0gaW1wbGVtZW50cyBTdW0gewogICAgQE92ZXJyaWRlCiAgICBwdWJsaWMgbG9uZyBzdW0oZmluYWwgaW50W10gYnVmKSB7CiAgICAgICAgbG9uZyBzdW0gPSAwTDsKICAgICAgICBmb3IgKGludCBpID0gYnVmLmxlbmd0aDsgaS0tID4gMDspIHsKICAgICAgICAgICAgc3VtICs9IGJ1ZltpXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHN1bTsKICAgIH0KfQoKcHVibGljIGNsYXNzIE1haW4gewogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oZmluYWwgU3RyaW5nW10gYXJncykgewogICAgICAgIENvbXBpbGVyLmNvbXBpbGVDbGFzcyhNYWluLmNsYXNzKTsKICAgICAgICBDb21waWxlci5jb21waWxlQ2xhc3MoUmFuZ2VTdW0uY2xhc3MpOwogICAgICAgIENvbXBpbGVyLmNvbXBpbGVDbGFzcyhJdGVyU3VtLmNsYXNzKTsKICAgICAgICBDb21waWxlci5jb21waWxlQ2xhc3MoQmFja0l0ZXJTdW0uY2xhc3MpOwogICAgICAgIGludCBzaXplID0gSW50ZWdlci5wYXJzZUludChhcmdzWzBdKTsKICAgICAgICBpbnRbXSBidWYgPSBuZXcgaW50W3NpemVdOwogICAgICAgIFJhbmRvbSByYW5kb20gPSBuZXcgUmFuZG9tKDBMKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IHNpemU7ICsraSkgewogICAgICAgICAgICBidWZbaV0gPSByYW5kb20ubmV4dEludCgpOwogICAgICAgIH0KICAgICAgICBTdW0gc3VtOwogICAgICAgIHN3aXRjaCAoSW50ZWdlci5wYXJzZUludChhcmdzWzFdKSkgewogICAgICAgICAgICBjYXNlIDA6CiAgICAgICAgICAgICAgICBzdW0gPSBuZXcgUmFuZ2VTdW0oKTsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICBjYXNlIDE6CiAgICAgICAgICAgICAgICBzdW0gPSBuZXcgSXRlclN1bSgpOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGNhc2UgMjoKICAgICAgICAgICAgICAgIHN1bSA9IG5ldyBCYWNrSXRlclN1bSgpOwogICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgIGRlZmF1bHQ6CiAgICAgICAgICAgICAgICB0aHJvdyBuZXcgUnVudGltZUV4Y2VwdGlvbigpOwogICAgICAgIH0KICAgICAgICBsb25nIHN0YXJ0ID0gU3lzdGVtLmN1cnJlbnRUaW1lTWlsbGlzKCk7CiAgICAgICAgbG9uZyB0b3RhbCA9IHN1bS5zdW0oYnVmKTsKICAgICAgICBsb25nIGVuZCA9IFN5c3RlbS5jdXJyZW50VGltZU1pbGxpcygpOwogICAgICAgIFN5c3RlbS5vdXQucHJpbnRsbih0b3RhbCArICI6ICIgKyAoZW5kIC0gc3RhcnQpKTsKICAgIH0KfQ==